Question

In: Computer Science

You will be writing a C program to test the data sharing ability of a thread...

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;
}

Solutions

Expert Solution

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:-


Related Solutions

This is c++. The goal is to test each method function by writing a simple program...
This is c++. The goal is to test each method function by writing a simple program displaying how they work in int main . #ifndef ARRAY_FUNCTIONS_H #define ARRAY_FUNCTIONS_H #include <iostream> #include <iomanip> using namespace std; template <typename T> class Array_functions { public: int size; int cap; int* ptr; //default ctor Array_functions(); // ctor with one arg Array_functions(int size); //allactes a dynamic array T* allocate(int n); //resizes array onto another T* resize_arr(T *dest,int old_cap, int new_cap); //deletes array void delete_arr(T& a);...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program accepts inputs from the user from the console. The user can provide the following two types of input: add num1 num2 mult num1 num2 Here, num1 and num2 are two integer numbers. E.g., the user may input add 1 2. The threads receive the command along with the number, and performs the appropriate arithmetic operation and returns the results to main program. The main...
Write a C program that creates 5 threads sends the thread index as an argument to...
Write a C program that creates 5 threads sends the thread index as an argument to the thread execution procedure/function. Also, the main process/thread joins the newly created threads sequentially one after the other. From the thread procedure print “I am a thread and my index is “ [print the correct index number]. From the main thread after the join print “I am the main thread and just completed joining thread index “ [print the correct index].
write a report about travel agency in c++ not a program writing just report writing
write a report about travel agency in c++ not a program writing just report writing
)      A thread manufacturer test a sample of eight lengths of a certain type of thread...
)      A thread manufacturer test a sample of eight lengths of a certain type of thread made of blended materials and obtains a mean strength of 8.2 lb with standard deviation 0.06 lb. Assuming tensile strengths are normally distributed, construct a 90% confidence interval for the mean tensile strength of this thread.
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
Suppose we are writing a C++ program for the sales department of a small company. The...
Suppose we are writing a C++ program for the sales department of a small company. The department has 4 sales person and is selling 5 products. The program will allow the input of each sale made (sales person, product, sales amount). The program will also allow a user to see the sales made by a particular sales person or the sales on a particular product. Requirement of the Program: 1. Using a 2-D array as a data structure for storing...
C++, Complete this program as directed // This program will read in a group of test...
C++, Complete this program as directed // This program will read in a group of test scores (positive integers from 1 to 100) // from the keyboard and then calculate and output the average score // as well as the highest and lowest score. There will be a maximum of 100 scores. // PLACE YOUR NAME HERE #include <iostream> using namespace std; typedef int GradeType[100]; // declares a new data type: // an integer array of 100 elements float findAverage...
One of the benefits of Data Analytics is the ability to see and test the full...
One of the benefits of Data Analytics is the ability to see and test the full population. In that case, why is sampling (even monetary sampling) still used, and how is it useful?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT