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_2

Step: 3

blur-text-image_3

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

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

How can we visually describe our goals?

Answered: 1 week ago