Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

perferable using C language or C++ other 3 pictures attached are sample programs. Task Read the man pages of the following fanctions (you may need

perferable using C language or C++
other 3 pictures attached are sample programs. image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Task Read the man pages of the following fanctions (you may need to run sado apt-get inetall glibe-doc conamand for these): e pthread create *pthread nutex unlock 3 partially completed prograns have been provided for you to belp you learn threads For this lab you are to create an outpat sinnilar to that of the previous lab (r-5, a/, ete.). This time you are using threals though. Since tlreads share global variabks, you may make r a glohal varialle, thus you don't have to use a file or shared memory to modify it. There are two ways to do this lab, and you may choose eit her, but yo must use altithreadig I. You can create your threads inside the loop, and use pthread_join) to coatrol the flow. Thus 10 total threads will be ereated. 2. You can make each thread have its own oop, and use mistexes and sleep)0 to coutrol the fow. Thas 2 total threads are created. Following is a sample of the output that will be painted to the screen terminal. x = 19530 IEATION Thread 1:x19525 Thread 2: 3905 Thread 1: 31-3900 Thread 2780 Thread 1: = 775 Thread 2:x135 Thread 1: x- 150 Thread 2: 30 Thread 25 Thread 2:x Listing 1: Screen Outpat #include #include #de fine NUM THREADS 5 void *PrintHello (void *threadid) long tid; tid(long)threadid; printf("Hello World! It's me, thread #81d! ", tid); pthread exit (NULL) int main (int argc, char *argv]) // create an array of thread struct instances with appropriate length pthread t threads [ NUM_THREADS; long t; for (t-0; t void *print message_function( void *ptr main) pthread t threadl, thread2; char *messagel-"Thread 1"; char *message2"Thread 2" int iretl, iret2; which will execute function print message function, (void*) messagel) print_message_function, (void*) message2) /*Create independent threads each of iretl - pthread create (&threadl, NULL, iret2pthread create (&thread2, NULL, // use thread join function to wait for the thread thread2 to terminate pthread join(thread2, NULL); pthread join(thread1, NULL); // do the same for thread1 // print the return value of each thread return 0; void print message function void *ptr char *message; message(char *) pr; printf ("%s ", message); #include #include #include #include void *functionC pthread mutex t mutexl - PTHREAD MUTEX INITIALIZER; int counter = 0; main() -t int rc1, rc2; pthread t threadl, thread2; /*Create independent threads each of which will execute functionC / functionC, NULL))) { rcl) if((rc1 = pthread_create( &thread 1 , NULL, printf ("Thread creation failed: %d ", if((rc2 pthread. Create ( &thre ad2 , NULL, functionC, NULL)) printf ("Thread creation failed: %d ", rc2); pthread join(threadl, NULL) pthread join(thread2, NULL) return 0; void *functionc) { pthread mutex lock (&mutexl) counter++ sleep (1); printf ("Counter value : %d " , counter); pthread mutex unlock( &mutexl); Task Read the man pages of the following fanctions (you may need to run sado apt-get inetall glibe-doc conamand for these): e pthread create *pthread nutex unlock 3 partially completed prograns have been provided for you to belp you learn threads For this lab you are to create an outpat sinnilar to that of the previous lab (r-5, a/, ete.). This time you are using threals though. Since tlreads share global variabks, you may make r a glohal varialle, thus you don't have to use a file or shared memory to modify it. There are two ways to do this lab, and you may choose eit her, but yo must use altithreadig I. You can create your threads inside the loop, and use pthread_join) to coatrol the flow. Thus 10 total threads will be ereated. 2. You can make each thread have its own oop, and use mistexes and sleep)0 to coutrol the fow. Thas 2 total threads are created. Following is a sample of the output that will be painted to the screen terminal. x = 19530 IEATION Thread 1:x19525 Thread 2: 3905 Thread 1: 31-3900 Thread 2780 Thread 1: = 775 Thread 2:x135 Thread 1: x- 150 Thread 2: 30 Thread 25 Thread 2:x Listing 1: Screen Outpat #include #include #de fine NUM THREADS 5 void *PrintHello (void *threadid) long tid; tid(long)threadid; printf("Hello World! It's me, thread #81d! ", tid); pthread exit (NULL) int main (int argc, char *argv]) // create an array of thread struct instances with appropriate length pthread t threads [ NUM_THREADS; long t; for (t-0; t void *print message_function( void *ptr main) pthread t threadl, thread2; char *messagel-"Thread 1"; char *message2"Thread 2" int iretl, iret2; which will execute function print message function, (void*) messagel) print_message_function, (void*) message2) /*Create independent threads each of iretl - pthread create (&threadl, NULL, iret2pthread create (&thread2, NULL, // use thread join function to wait for the thread thread2 to terminate pthread join(thread2, NULL); pthread join(thread1, NULL); // do the same for thread1 // print the return value of each thread return 0; void print message function void *ptr char *message; message(char *) pr; printf ("%s ", message); #include #include #include #include void *functionC pthread mutex t mutexl - PTHREAD MUTEX INITIALIZER; int counter = 0; main() -t int rc1, rc2; pthread t threadl, thread2; /*Create independent threads each of which will execute functionC / functionC, NULL))) { rcl) if((rc1 = pthread_create( &thread 1 , NULL, printf ("Thread creation failed: %d ", if((rc2 pthread. Create ( &thre ad2 , NULL, functionC, NULL)) printf ("Thread creation failed: %d ", rc2); pthread join(threadl, NULL) pthread join(thread2, NULL) return 0; void *functionc) { pthread mutex lock (&mutexl) counter++ sleep (1); printf ("Counter value : %d " , counter); pthread mutex unlock( &mutexl)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions