Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//thread.c #include #include #include int var1 = 0; //global variable int child_behavior() { printf(Child: Inside child_behavior, my pid: %d , getpid()); var1++; printf(Child: var1 =

//thread.c

#include

#include

#include

int var1 = 0; //global variable

int child_behavior() {

printf("Child: Inside child_behavior, my pid: %d ", getpid());

var1++;

printf("Child: var1 = %d ", var1);

sleep(2);//sleep 1 second

printf("Child: Terminating child_behavior... ");

return 0;

}

int main() {

int state;

int child_pid;

printf("\tParent: my pid: %d ", getpid());

printf("\tParent: Creating new child and waiting... ");

//fork system call

child_pid = fork(); //duplicate

if( child_pid == 0 ){

//child: The return of fork() is zero

child_behavior();

} else {

wait(&state);//wait for the child to terminate

printf("\tParent: Done waiting! Child pid: %d ", child_pid);

printf("\tParent: var1 = %d ", var1);

}

return 0;

}

What is the output of this program? Explain the value(s) obtained for var1.

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

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

Derive Eq. (13.79).

Answered: 1 week ago