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 and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
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
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user to enter a part into the inventory, take a part from the inventory, or quit. You are provided with the InvItem class (InvItem.h) and a partial Driver.cpp. You will be creating a DynamicStack class that should implement a Stack data structure. The DynamicClass should be implemented as a template class to allow any data type be added/removed from the stack. You will submit three...
)      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.
C++ (data structure using c++). please send me copyable file. Write a program and test a...
C++ (data structure using c++). please send me copyable file. Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT