Question: Operating Systems: C Programs using fork() functions and Threads Part 1 - Threads Write a C program that creates a child thread. Both threads should
Operating Systems: C Programs using fork() functions and Threads





Part 1 - Threads Write a C program that creates a child thread. Both threads should then iterate a number of times. During each iteration each thread should increment a global variable. Each thread should also print something identifying to show it is executing e.g. the parent thread prints character "p" and the child thread prints character "c". To achieve this, implement the program using the POSIX Pthread API The following steps show how your program should work: 1. Your program defines a global variable. 2. It then creates a child thread with the pthread create function. 3. Both parent and child threads then iterate 20 times, each with their own for-loop. During each iteration both threads should increment the same global variable by 5. Both parent and child threads should also print a unique character identifying each other during each iteration. 6. The parent thread then outputs the final value of the global variable Hint: Use the fflush) function to force the output to be written to the screen imme- diately. Also pause for a second after each iteration to give you enough total time to use the "ps" utility in part 3. You will need to use the command line option "-pthread" with the gec compiler to successfully compile your program with the Pthread library
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
