1

I'm coding a shell in C that should support background and foreground processes.

Constraints:

  • Background processes that terminate should be caught by signal handler
  • No global variables can be used for communicating from signal handler
  • No list of processes/pids allowed

My solution:

  • Waitpid until foreground process terminates
  • For background processes, immediately return to prompt
  • Handler catches SIGCHLD where waitpid is used to clear process table

Problem:

  • Foreground processes also trig handler causing one of two waitpids to error
  • Can't solve by ignoring SIGCHLD while running foreground process, since background process might terminate during that time
  • Can't find a way to make handler ignore specific pid (the foreground process started)

Thanks!

1 Answers1

0

Problem:

  • Foreground processes also trig handler causing one of two waitpids to error

This is not a problem - just leave the handler then.

void handler(int signum)
{
    pid_t pid;
    while (pid = waitpid(-1, NULL, WNOHANG), pid > 0)
        fprintf(stderr, "%d terminated\n", pid);
}
Armali
  • 18,255
  • 14
  • 57
  • 171