Question: / / Example 7 . 3 #include #include struct char _ print { char character; int count; } ; void * print ( void *

//Example 7.3
#include #include
struct char_print
{
char character; int count;
};
void *print (void *parameters)
{
struct char_print *p;
p =(struct char_print *) parameters; int i;
for (i=0; i< p->count ; ++i) fputc(p->character, stderr);
pthread_exit(NULL);
}
int main()
{
pthread_t t1_id; pthread_t t2_id;
struct char_print t1_args; struct char_print t2_args;
t1_args.character='1'; t1_args.count =60;
pthread_create(&t1_id, NULL, print, (void *) &t1_args);
t2_args.character='0'; t2_args.count =100;
pthread_create(&t2_id, NULL, print, (void *) &t2_args);
pthread_join(t1_id,NULL); pthread_join(t2_id,NULL);
}
1-(1 marks) Compile and run Example 7.3 above and provide a sample output of the program.
2-(1 marks) Remove the two pthread_join() function calls from Example 7.3, re-compile as follows:
$ gcc -o example73 example73.c -l pthread then, run the program as follows:
$ ./example73
What is the output of the program.
3-(1 marks) In question 2, explain why are you getting such an output and how to fix it.
4-(7 marks) Write a program that reports the scheduling behaviour of threads on a multi-CPU system. Use the following function to report on the CPU on which a thread is scheduled:
#include #define _GNU_SOURCE int sched_getcpu()
Your program must create three threads. Each thread will start in a separate function than the other threads. The general structure of the thread-startup function is that it will be in an infinite loop executing two statements:
1- Report the thread number (first, second, third), along with the CPU it was scheduled on
2- Sleeps for 2 seconds (sleep(2))
Note: To get an output that includes scheduling threads on multiple CPUs, the virtual machine running Ubuntu Linux has to be reconfigured.
If you're using VMWare Player, You can increase the number of CPUs available to the virtual machine by going to: Edit Virtual Machine Settings -> Hardware Tab -> Processors
and select the number of CPUs from the drop-box.
If you're using Virtual Box, go to: Machine -> Settings -> System -> Processor, and increase the number of processors in the text box.

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 Programming Questions!