Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

One of your colleagues is thinking of using signals to allow a parent process to count events that occur in a child process. The idea

One of your colleagues is thinking of using signals to allow a parent process to count events that occur in a child process. The idea is to notify the parent each time an event occurs by sending it a signal, and letting the parents signal handler increment a global counter variable, which the parent can then inspect after the child has terminated. However, when he runs the test program in Figure 8.45 on his system, he discovers that when the parent calls printf, counter always has a value of 2, even though the child has sent five signals to the parent. Perplexed, he comes to you for help. Can you explain the bug?
/* Figure 8.45*/
#include "csapp.h"
int counter =0;
void handler(int sig)
{
counter++;
sleep(1);
/* Do some work in the handler */
return;
}
int main()
{
int i;
Signal(SIGUSR2, handler);
if (Fork()==0)
{
/* Child */
for (i =0; i <5; i++)
{
Kill(getppid(), SIGUSR2);
printf("sent SIGUSR2 to parent
");
}
exit(0);
}
Wait(NULL);
printf("counter=%d
", counter);
exit(0);
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Google Analytics 4 The Data Driven Marketing Revolution

Authors: Galen Poll

2024th Edition

B0CRK92F5F, 979-8873956234

More Books

Students also viewed these Databases questions

Question

Show that if Q 2 k (), then E[Q] k .

Answered: 1 week ago