Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Thread1.c code: #include #include #include /* Thread's task */ void *mythread(void *arg) { printf( --> Thread %s, done , (char *) arg); pthread_exit(NULL); } int
Thread1.c code:
#include#include #include /* Thread's task */ void *mythread(void *arg) { printf(" --> Thread %s, done ", (char *) arg); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t tid1, tid2; /* Thread IDs */ printf("Main thread: begin "); pthread_create(&tid1, NULL, mythread, "A"); /* Create a thread */ pthread_create(&tid2, NULL, mythread, "B"); /* Create another thread */ /* Parent thread waits for the threads to finish */ pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("Main thread: done "); return 0; }
1. In this exercise, we will add more threads into a process. Revise the C program Threadl.c in Example 2 in Lecture Note 3 by adding two more worker threads (called C and D). Now, save it as A3q 1.c. The output of this program is also similar to that in Example 2 except we have additional Thread C and Thread D shown on the screen, too. For example, note that you might have a different order of threads showing up because of the race condition, running this program show the following messages on the screen: Note that the -lpthread option in the gcc command is needed. 1.1) Take a screenshot of your program and put here. 1.2) Compile and run your program. Take a screenshot of your program's output and put here. Example 2: Thread Creation This C program is saved as Thread1.c. - To show how we can create threads. \#include - To show their behaviors. \#include - This program follows the synchronous threading \#include strategy. /* Thread's task */ void * mythread(void *arg) \{ printf(" > Thread \%s, done , (char *) arg); \} pthread_exit(NULL); int main(int arge, char *argv[]) \{ pthread_t tid1, tid2; / * Thread IDs */ printf("Main thread: begin ); pthread_create(\&tid1, NULL, mythread, "A"); /* Create a thread */ pthread_create(\&tid2, NULL, mythread, "B"); /* Create another thread */ /* Parent thread waits for the threads to finish */ pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("Main thread: done ); return 0 ; Example 2: Thread Creation To see the result, type the following commands (note that -lpthread is needed in the gcc command): - Note that the processor will randomly select a thread to execute. The main memory layout of this process will be similar to that shown in Page 9 , a multithreaded process. This program can be explained as follows. This program will create two new (worker) threads (in addition to the main thread - that is created when we run this program). - The main program creates two worker threads by calling pthread_create(). Let's call them tid1 and tid2. Each of which will run the function mythread(), though with different arguments (the string A or B ). - Once a thread is created, it may start running right away (depending on the process scheduler); alternately, it may be put in a "ready queue" but not "running state" and thus not run yet. (Of course, on a multiprocessor, the threads could even be running at the same time.) - After creating the two threads, the main thread calls pthread_join(), which waits for a particular thread to complete. It does so twice, thus ensuring tid1 and tid2 will run and complete before finally allowing the main thread to run again; when it does, it will print "Main thread: done" and exit. Overall, three threads were employed during this run: the main thread, tid1, and tid2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started