Question: C Systems Programming Having just added code to block signals before forking a child process and unblock them after adding the process to the job
C Systems Programming

Having just added code to block signals before forking a child process and unblock them after adding the process to the job queue, a student is surprised to discover a mysterious race condition in his shell implementation that causes it to occasionally segfault when running foreground processes Below are relevant function declarations and bodies /* add a job to the job list / int addi ob (struct job-t *Jobs, pid-t pid, int state, char *dline); /* find a job (by PID) on the job list; return NULL if not found / struct job-t *getjobpid (struct job_t *jobs, pid_t pid) /* delete a job whose PID-pid from the job list/ int deletejob (struct job_t *jobs, pid.t pid); void sigchld_handler (int sig) f pid t pid; while ((pidwaitpid(-1, &status, WNOHANG IWUNTRACED))> 0) if (WIFEXITED (status)) deletejob(jobs, pid); void eval (char *cmdline) /*var declarations &cmdline parsing not shown / sigemptyset (&mask); sigaddset (&mask, SIGCHLD); sigprocmask (SIG_BLOCK, &mask, NULL); if ((pid-fork)) -0) sigprocmask (SIG_UNBLOCK, &mask, NULL); execvp(argv[o], argv); addjob (jobs, pid, (bg - 1? BG: FG), cmdline); sigprocmask (SIG_UNBLOCK, &mask, NULL); if (!bg) waitfg(pid); void waitfg(pid_t pid) struct job.t *j -getjobpid(jobs, pid); while (j->pid- pid &j-stateFG) sleep(1); Explain how, precisely, the race condition plays out in order to cause the segfault, and explain how you would go about fixing it Having just added code to block signals before forking a child process and unblock them after adding the process to the job queue, a student is surprised to discover a mysterious race condition in his shell implementation that causes it to occasionally segfault when running foreground processes Below are relevant function declarations and bodies /* add a job to the job list / int addi ob (struct job-t *Jobs, pid-t pid, int state, char *dline); /* find a job (by PID) on the job list; return NULL if not found / struct job-t *getjobpid (struct job_t *jobs, pid_t pid) /* delete a job whose PID-pid from the job list/ int deletejob (struct job_t *jobs, pid.t pid); void sigchld_handler (int sig) f pid t pid; while ((pidwaitpid(-1, &status, WNOHANG IWUNTRACED))> 0) if (WIFEXITED (status)) deletejob(jobs, pid); void eval (char *cmdline) /*var declarations &cmdline parsing not shown / sigemptyset (&mask); sigaddset (&mask, SIGCHLD); sigprocmask (SIG_BLOCK, &mask, NULL); if ((pid-fork)) -0) sigprocmask (SIG_UNBLOCK, &mask, NULL); execvp(argv[o], argv); addjob (jobs, pid, (bg - 1? BG: FG), cmdline); sigprocmask (SIG_UNBLOCK, &mask, NULL); if (!bg) waitfg(pid); void waitfg(pid_t pid) struct job.t *j -getjobpid(jobs, pid); while (j->pid- pid &j-stateFG) sleep(1); Explain how, precisely, the race condition plays out in order to cause the segfault, and explain how you would go about fixing it
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
