Void handlerint sig int stat waitstat if

Info icon This preview shows pages 7–8. Sign up to view the full content.

void handler(int sig) { int stat; wait(&stat); if (WIFEXITED(stat)) printf("%d", WEXITSTATUS(stat)); } int main() { int i; pid_t pids[5]; signal(SIGCHLD, handler); for (i=0; i<5; i++) if ((pids[i] = fork()) == 0) { printf("1"); while (1) ; /* inf loop */ } for (i=0; i<5; i++) kill(pids[i], SIGKILL); sleep(1); return 2; } a. 1212121212 b. 1111122222 c. 1 d. 12 e. 11111 f. no output Page 7 of 8
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Problem 5. (8 points): 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 addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline); /* 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) { pid_t pid; while ((pid = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 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[0], 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->state == FG) 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. Page 8 of 8
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '09
  • Saelee
  • Pointer, printf, Exit, Parent process, SIGCHLD

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern