In: Computer Science
You will be writing a C program to test the data sharing ability of a thread and process.
Your C program will do the following:
1. Your parent program will have three variables: int x,y,z; which to be initialized as 10,
20, and 0, respectively.
2. parent creating child: parent will create a child by fork() and the child will perform
z = x+y (i.e., add x and y and store the results in z). parent will wait for child to complete
before parent proceeds. Upon completion of child, parent will print out the value of z.
3. parent creating thread: After (2) above is completed, parent process will now create a
thread by pthread_create() which will do the exact same task done by child above (i.e., z
= x+y). parent will wait for its thread to complete before parent proceeds. Upon
completion of the thread, parent will print out the value of z.
Write using the Provided code below !!!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
int x, y, z;
void *sum(){
z=y+x;
return NULL;
}
int main(){
pid_t child;
pthread_t thread;
// Initializing the global variables
x=10, y=20, z=0;
/* MOVE THE FOLLOWING LINES INTO THEIR
PROPER PLACES...
printf("main function: errno number
is %d\n", errno);
printf("Using a fork(), the value
of z in the child process is: %d\n", z);
printf("Using a fork(), the value
of z in the parent process is: %d\n", z); //value of z after the
fork process won't change
printf("Using a thread, the value
of z is: %d\n", z); //value of z after passing to the thread will
change
*/
/* Add code under every comment to demonstrate differences between child and thread */
// create child process
// check for child creation error
// in parent:
// wait for child, and print value of z
// create thread, wait for it to complete, then print
value of z
// in child:
// sum x and y and store it in z
// print value of z
return 0;
}
Complete code in C:-
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
int x, y, z;
void *sum(){
z=y+x;
return NULL;
}
int main(){
pid_t child;
pthread_t thread;
// Initializing the global variables
x=10, y=20, z=0;
// create child process
child = fork();
// check for child creation error
if(child < 0) {
printf("main function: errno number is %d\n", errno);
}
// in parent:
else if(child > 0){
// wait for child, and print value of z
wait(NULL);
//value of z after the fork process won't change
printf("Using a fork(), the value of z in the parent process is:
%d\n", z);
// create thread, wait for it to complete, then print value of
z
pthread_create(&thread, NULL, &sum, NULL);
pthread_join(thread, NULL);
//value of z after passing to the thread will change
printf("Using a thread, the value of z is: %d\n", z);
}
// in child:
else {
// sum x and y and store it in z
z = x+y;
// print value of z
printf("Using a fork(), the value of z in the child process is:
%d\n", z);
}
return 0;
}
Screenshot of output:-