sample code to help the first two. basically you just have to create threads for given instruxtions to display output as showed.
Task Read the man pages of the followring functions (you may aeed to run sudo apt-get install glibc-doc command for these) e pthread create e pthread join pthread utex.init 3 partially coenpleted programs have been provided for you to belp you learn theeads. For this lab you are to create an out put similar to that of the pevios lals (r-5, r/5, ete.). This time you are using threads though. Since threads share obal variables, you may make z a global variable, 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 1. You can create your threads inside the loop, and ase pthread join to control the flow. Thus 10 total 2. You can make each thread have its own loop, and use utexes and sleep) to control the Blow. Thus 2 choose either, bat you smast use nnaltithreading threads will be created total threads are created Following is a sample of the output that will be printed to the screen terminal. 19530 TERATION 1 Thread x 19525 Thread 2: = 3905 TERATION2 Thread 1: x=300 Thread 2 x78o TERATION 3 Threadx 77 : Thread 2: -155 TERATION Thread 150 Thread 2:x 30 Thread 1: = 25 Thread2 Listing 1: Screen Outpeat #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 *functionC pthread mutex t mutexl - PTHREAD MUTEX INITIALIZER; int counter = 0; main() 1t int rc1, rc2; pthread t threadl, thread2; /*Create independent threads each of which will execute functionC / functionC, NULL))) { rcl); if((rc 1 pthread_create( &thread 1 , NULL, = printf ("Thread creation failed: %d ", if((re2 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); Read the man pages of the following functions (you may need to run sudo apt-get install glibe-doc command for these): 3 partially completed programs have been provided for you to help you learn threads For this lab you are to create an output similar to that of the previous lab (-, z/5, etc.). This time you are using threads though. Since threads share global variables, you may make z a global variable, thas 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 either, but you must use multithreading 1. You can create your threads inside the loop, and use pthread_join) to control the fow. Thus 10 total threads will be created. 2. You can make each thread have its own loop, and use mutexes and sleep) to control the flow. Thas 2 total threads are created Following is a sample of the output that will be printed to the screen terminal 195330 ITERATION Thread 1: 19525 Thread 2:x 3905 Thread 1:x3900 Thread 2: 780 ITERATION3 Thread :x 77 Thread 2:x155 ITERATION Thread 11 x = 150 Thread 2: x = 30 ITERATION Thread 1: x-25 Thread 2:x Listing 1: Screen Output Task Read the man pages of the followring functions (you may aeed to run sudo apt-get install glibc-doc command for these) e pthread create e pthread join pthread utex.init 3 partially coenpleted programs have been provided for you to belp you learn theeads. For this lab you are to create an out put similar to that of the pevios lals (r-5, r/5, ete.). This time you are using threads though. Since threads share obal variables, you may make z a global variable, 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 1. You can create your threads inside the loop, and ase pthread join to control the flow. Thus 10 total 2. You can make each thread have its own loop, and use utexes and sleep) to control the Blow. Thus 2 choose either, bat you smast use nnaltithreading threads will be created total threads are created Following is a sample of the output that will be printed to the screen terminal. 19530 TERATION 1 Thread x 19525 Thread 2: = 3905 TERATION2 Thread 1: x=300 Thread 2 x78o TERATION 3 Threadx 77 : Thread 2: -155 TERATION Thread 150 Thread 2:x 30 Thread 1: = 25 Thread2 Listing 1: Screen Outpeat #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 *functionC pthread mutex t mutexl - PTHREAD MUTEX INITIALIZER; int counter = 0; main() 1t int rc1, rc2; pthread t threadl, thread2; /*Create independent threads each of which will execute functionC / functionC, NULL))) { rcl); if((rc 1 pthread_create( &thread 1 , NULL, = printf ("Thread creation failed: %d ", if((re2 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); Read the man pages of the following functions (you may need to run sudo apt-get install glibe-doc command for these): 3 partially completed programs have been provided for you to help you learn threads For this lab you are to create an output similar to that of the previous lab (-, z/5, etc.). This time you are using threads though. Since threads share global variables, you may make z a global variable, thas 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 either, but you must use multithreading 1. You can create your threads inside the loop, and use pthread_join) to control the fow. Thus 10 total threads will be created. 2. You can make each thread have its own loop, and use mutexes and sleep) to control the flow. Thas 2 total threads are created Following is a sample of the output that will be printed to the screen terminal 195330 ITERATION Thread 1: 19525 Thread 2:x 3905 Thread 1:x3900 Thread 2: 780 ITERATION3 Thread :x 77 Thread 2:x155 ITERATION Thread 11 x = 150 Thread 2: x = 30 ITERATION Thread 1: x-25 Thread 2:x Listing 1: Screen Output