Enes Altuncu
Enes Altuncu

Reputation: 439

Signal handling in C with sigaction

I'm trying to implement a simple unix shell in C. However, my background process feature is not working well.

When I run the shell and give a background command, it gives the output like the following:

> sleep 4 &
> [10612]retval: 0 
> [10616]retval: 0 
> [10618]retval: 0 
> [10619]retval: 0 
> [10622]retval: 0 
> [10623]retval: 0

The problem is that it gives an output with a period of sleep time while it should just give the first retval.

My signal handler is

void handler(int s) {
    int pid;
    int status;
    pid = waitpid(-1, NULL, WNOHANG);
    printf("[%d]retval: %d \n", pid, WEXITSTATUS(status));
    fflush(stdout);
}

My sigaction is

struct sigaction act;
memset(&act, 0, sizeof(act));

sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = handler;

So, what is wrong with my code?

Thanks in advance...

EDIT : Also, I have a sigaction()

if (isBackground) {
        sigaction(SIGCHLD, &act, 0);
    }

Upvotes: 0

Views: 523

Answers (0)

Related Questions