Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a concurrent system problem. I don\'t understand why counter =- 1 ,2, 2

ID: 3600360 • Letter: T

Question

This is a concurrent system problem. I don't understand why counter =- 1 ,2, 2 And also I don't understand the code and Could you account the ANSWER and CODE in detail? thank you so much

pidt pidi int counter 0i void handlerl (int sig) counter- printf("counter= %d ", counter); fflush (stdout)/* Flushes the printed string to stdout */ kill (pid, SIGUSR1); void handler2 (int sig) counter += 2; print f ( "counter exit (0) %d ", counter); = main() signal (SIGUSR1, handler1); if ( (pid = fork ( ) ) 0) { signal (SIGUSR1, handler2) kill (getppid), SIGUSR1); while (1) else pid_t pi int status; if ((p= wait ( & status)) > 0) { counter + 3; printf ( "counter- %d ", counter) ;

Explanation / Answer

Here the counter is a global variable and initialised to 0

Now in the function handler the counter is decremented by 1 changing the value of counter to -1 and then it is flushed out .

Now when the second function is compiled the counter is incremented by 2 to the original counter=0 so the value becomes 2.

Now when the program is executed first main () is executed and there first handler() function is called and counter=-1 is printed out and as the value is flushed the counter becomes 0 while executing the handler2() so it prints out counter =2 and then the value of PID and signal are killed and becomes 1 so this makes the program to run else loop where counter is -1 Because the handler 2() is killed so the when the counter is incremented by 3 then the value of counter becomes -1+3=2 and prints out output as 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote