Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The program shown below uses the Pthreads API. Run the program and find out what the output are at LINE C and LINE P. Add
The program shown below uses the Pthreads API. Run the program and find out what the output are at LINE C and LINE P.
Add a second thread to the program above replicating the child process part for the new thread. Execute your code three times and record the output and explain why you get those results.
#include #include int value = 0; void *runner (void *param); /* the thread */ int main(int argc, char *argv[]) { pid_t pid; pthread_t tid; pthread_attr_t attr; pid = fork(); if (pid == 0) { /* child process */ pthread_attr_init(&attr); pthread create(&tid, &attr, runner,NULL); } else if (pid gt; 0) { /* parent process */ wait (NULL); printf("PARENT: value = %d", value); /* LINE P */ } pthread_join(tid, NULL); printf("CHILD: value = %d", value); /* LINE C */ } } void *runner (void *param) { value= 5; pthread_exit(0);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Now lets explain the code and predict the output 1 The program creates a shared variab...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started