Question: you will need the template code critical.c and multithread.c. Download this from the page and save it to your computer. Use the following command to

you will need the template code critical.c and multithread.c. Download this from the page and save it to your computer. Use the following command to compile the source code: gcc filename.c -o filename -lrt -lpthread

you will need the template code critical.c and multithread.c. Download this from

****** multithread.c ************* * * Demonstrate use of a multi threading and scheduling using pthreads * * compile with cc multithread.c -o multithread -lrt -lpthread * */ #define _GNU_SOURCE #define _REENTRANT /* macro to ensure system calls are reentrant */ #include /* header file for pthreads */ #include /* header file for POSIX conformance */ #include /* header file for POSIX time management */ #include /* header file for POSIX scheduling */ #include /* header file for standard input/outputlibrary */

void *threadA(void *); /* predefine threadA routine */ void *threadB(void *); /* predefine threadB routine */ void *threadC(void *); /* predefine threadC routine */

pthread_t threadA_id,threadB_id,threadC_id,main_id; /* thread identifiers */ pthread_attr_t attrA,attrB,attrC; /* thread attribute structures */ struct sched_param param; /* scheduling structure for thread attributes */

int policy=SCHED_FIFO; int priority_min,priority_max; /* for range of priority levels */

/* main routine */ int main() { struct timespec start; int status; /* check that system calls return ok */

clock_gettime(CLOCK_REALTIME, &start); /* get the time */ printf("Start time is: %d seconds %d nano_seconds ",start.tv_sec,start.tv_nsec);

/* Set processor affinity */ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0,&mask); /* use only 1 CPU core */ unsigned int len = sizeof(mask); status = sched_setaffinity(0, len, &mask); if (status

/* Find priority limits */ priority_max = sched_get_priority_max(policy); priority_min = sched_get_priority_min(policy); /* Set priority and policy of main thread */

main_id = pthread_self(); param.sched_priority=priority_min+1; status = pthread_setschedparam(main_id, policy, &param); if (status != 0) perror("pthread_setschedparam"); /* error check */ /* Create threadA */ param.sched_priority = priority_min+1; pthread_attr_init(&attrA); status = pthread_attr_setschedpolicy(&attrA,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrA,&param); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadA_id, &attrA, threadA, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadA_id,policy,&param); if (status != 0) perror("pthread_setschedparam");

/* Create threadB */ param.sched_priority = priority_min+1; pthread_attr_init(&attrB); status = pthread_attr_setschedpolicy(&attrB,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrB,&param); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadB_id, &attrB, threadB, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadB_id,policy,&param); if (status != 0) perror("pthread_setschedparam"); /* Create threadC */ param.sched_priority = priority_min+1; pthread_attr_init(&attrC); status = pthread_attr_setschedpolicy(&attrC,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrC,&param); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadC_id, &attrC, threadC, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadC_id,policy,&param); if (status != 0) perror("pthread_setschedparam");

/* Join threads - force main to wait for the thread to terminate */ printf("main() waiting for threads "); status = pthread_join(threadA_id, NULL); if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */ status = pthread_join(threadB_id, NULL); if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */ status = pthread_join(threadC_id, NULL); if (status != 0) perror("pthread_join(threadC_id, NULL)"); /* error check */ printf(" main() reporting that all threads have terminated "); return(0); } /* end of main */

void *threadA(void *arg) { int j; for(j=1;j

void *threadB(void *arg) { int j; for(j=1;j

void *threadC(void *arg) { int j; for(j=1;j

******critical.c********

* Demonstrate use of mutual exclusion using mutexes * * Upper case output indicates critical output * lower case output indicates non-critical output * * compile with cc critical.c -o critical -lrt -lpthread * */ #define _GNU_SOURCE #define _REENTRANT /* macro to ensure system calls are reentrant */ #include /* header file for pthreads */ #include /* header file for POSIX conformance */ #include /* header file for POSIX time management */ #include /* header file for POSIX scheduling */ #include /* header file for standard input/outputlibrary */

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; /*define mutex */

void *threadA(void *); /* predefine threadA routine */ void *threadB(void *); /* predefine threadB routine */

pthread_t threadA_id,threadB_id,main_id; /* thread identifiers */ pthread_attr_t attrA,attrB; /* thread attribute structures */ struct sched_param param; /* scheduling structure for thread attributes */

int policy=SCHED_FIFO; int priority_min,priority_max; /* for range of priority levels */

/* main routine */ int main() { struct timespec start; int status; /* check that system calls return ok */

clock_gettime(CLOCK_REALTIME, &start); /* get the time */ printf("Start time is: %d seconds %d nano_seconds ",start.tv_sec,start.tv_nsec);

/* Set processor affinity */ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0,&mask); /* use only 1 CPU core */ unsigned int len = sizeof(mask); status = sched_setaffinity(0, len, &mask); if (status

/* Find priority limits */

priority_max = sched_get_priority_max(policy); priority_min = sched_get_priority_min(policy); /* Change priority and policy of main thread */

main_id = pthread_self(); param.sched_priority=priority_min; status = pthread_setschedparam(main_id, policy, &param); if (status != 0) perror("pthread_setschedparam"); /* error check */

/* Create threadA */ param.sched_priority = priority_min; pthread_attr_init(&attrA); status = pthread_attr_setschedpolicy(&attrA,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrA,&param); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadA_id, &attrA, threadA, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadA_id,policy,&param); if (status != 0) perror("pthread_setschedparam");

/* Create threadB */ param.sched_priority = priority_min; pthread_attr_init(&attrB); status = pthread_attr_setschedpolicy(&attrB,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrB,&param); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadB_id, &attrB, threadB, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadB_id,policy,&param); if (status != 0) perror("pthread_setschedparam");

/* Join threads - force main to wait for the thread to terminate */ printf("main() waiting for threads ");

status = pthread_join(threadA_id, NULL); if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */ status = pthread_join(threadB_id, NULL); if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */ printf(" main() reporting that all threads have terminated "); status = pthread_mutex_destroy(&mtx); /* delete mutex */ if (status != 0) perror("pthread_mutex_destroy"); /* error check */ return(0); } /* end of main */

void *threadA(void *arg) { int j; int status; /* check that system calls return ok */ for(j=1;j

void *threadB(void *arg) { int j; int status; /* check that system calls return ok */ for(j=1;j

/* unlock critical region */ /* status = pthread_mutex_unlock(&mtx); if (status != 0) perror("pthread_mutex_unlock");*/ /* error check */ for(j=1;j Assignment:Real-time Programming with pthreads Modify the thread A function so that after printing half the letters it increases the priority of threadB using the instructions: 17th February 2021 For this assignment, you will need the template code critical.c and multithread.c. Download this from the page and save it to your computer. Use the following command to compile the source code: gee filename.c-o filename -Irt -Ipthread where filename is the name of the source code file. The resultant executable should be run using sudo, ie, use the command: sudo /filename Check that the above works for the source code provided, and complete the following exercises. POSIX support for mutexes is illustrated in program critical.c. Examine this code and make sure that you understand what it does 1. Compile and run the program critical.c. To run this program you need to use sudo (l.e., use the command: sudo /critical). Record the output. Briefly explain what this program does and how the scheduling gives rise to the observed behaviour. Include in your report a timing diagram: be sure to show the state of each task (including the main routine) at all times. param.sched priority = priority main+2: pthread setschedpuram(threaded.policy.&param): Make sure that you understand what these changes do 4. Run the modified program (using sudo) and record the output. Drawa timing diagram showing the state of each task (including the main routine) at all times. Explain the scheduling of the threads Modify the code to decrease the priority of threadB after threadA has printed half its letters. Report what happens and comment on this result. Modify the priority of only the executing thread and report what happens when the priorityisincreased and when it is decreased. Comment on and explain these results. 5. Modify the original program so that thread sloops for 1 millisecond after printing half its letters (use the nanosleep command). Run the code (using sudo) and record the output. Draw a timing diagram and explain the scheduling of the threads. Include a print-out of your program code. 2. Modify the code so that the mutexes are no longer commented-out". Run the programagain(using sudo) and record the output. Briefly explain the execution of the modified program. Draw a timing diagram of the modified program. 6. Change the scheduling policy used by multithread.c to be SCHED RR rather than SCHED FR. Perform experiments to determine how these scheduling policies differ. Describe the experiments that you performed and summarise the results. Describe in your report how both scheduling policies determine the scheduling of tasks both when threads have equal priority and when when threads have unequal priorities. Include a print out of your program code. Next, you will need the code for program multithread.c. Examine the code for program multithread.c and make sure that you understand what it does. 3. Run the code and record the output. Torun this program you need to use sudo (ie, use the com- mand: sudo ./multithread). Briefly explain what this program does and how the scheduling gives rise to the observed behaviour. Include in your report a timing diagram, be sure to show the state of each task (including the main routine) at all times. Your report must be no longer than four A4-pages. It should be submitted as a single PDF with a minimum font size of 11pt, and margins of no less than 2cm. Assignment:Real-time Programming with pthreads Modify the thread A function so that after printing half the letters it increases the priority of threadB using the instructions: 17th February 2021 For this assignment, you will need the template code critical.c and multithread.c. Download this from the page and save it to your computer. Use the following command to compile the source code: gee filename.c-o filename -Irt -Ipthread where filename is the name of the source code file. The resultant executable should be run using sudo, ie, use the command: sudo /filename Check that the above works for the source code provided, and complete the following exercises. POSIX support for mutexes is illustrated in program critical.c. Examine this code and make sure that you understand what it does 1. Compile and run the program critical.c. To run this program you need to use sudo (l.e., use the command: sudo /critical). Record the output. Briefly explain what this program does and how the scheduling gives rise to the observed behaviour. Include in your report a timing diagram: be sure to show the state of each task (including the main routine) at all times. param.sched priority = priority main+2: pthread setschedpuram(threaded.policy.&param): Make sure that you understand what these changes do 4. Run the modified program (using sudo) and record the output. Drawa timing diagram showing the state of each task (including the main routine) at all times. Explain the scheduling of the threads Modify the code to decrease the priority of threadB after threadA has printed half its letters. Report what happens and comment on this result. Modify the priority of only the executing thread and report what happens when the priorityisincreased and when it is decreased. Comment on and explain these results. 5. Modify the original program so that thread sloops for 1 millisecond after printing half its letters (use the nanosleep command). Run the code (using sudo) and record the output. Draw a timing diagram and explain the scheduling of the threads. Include a print-out of your program code. 2. Modify the code so that the mutexes are no longer commented-out". Run the programagain(using sudo) and record the output. Briefly explain the execution of the modified program. Draw a timing diagram of the modified program. 6. Change the scheduling policy used by multithread.c to be SCHED RR rather than SCHED FR. Perform experiments to determine how these scheduling policies differ. Describe the experiments that you performed and summarise the results. Describe in your report how both scheduling policies determine the scheduling of tasks both when threads have equal priority and when when threads have unequal priorities. Include a print out of your program code. Next, you will need the code for program multithread.c. Examine the code for program multithread.c and make sure that you understand what it does. 3. Run the code and record the output. Torun this program you need to use sudo (ie, use the com- mand: sudo ./multithread). Briefly explain what this program does and how the scheduling gives rise to the observed behaviour. Include in your report a timing diagram, be sure to show the state of each task (including the main routine) at all times. Your report must be no longer than four A4-pages. It should be submitted as a single PDF with a minimum font size of 11pt, and margins of no less than 2cm

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!