Question: Programming Assignment. Assignment requires that you install the UNIX/Linux to develop your programs to examine the performance of different pthread contention scope and scheduling algorithms.
Programming Assignment. Assignment requires that you install the UNIX/Linux to develop your programs to examine the performance of different pthread contention scope and scheduling algorithms. Try to adopt two different CPU scheduling algorithms, First-Come-First-Serve, and the Round-Robin models, under two different pthread contention scope, process-contention scope, and system-contention scope, execute and compute the average turnaround time. Problem Statement: According to the code attached in the document, rewrite the function philosophers that l The value of max is the last 8 number of your student ID l Display the turnaround time of each pthread of each scheduling algorithm l Please discuss the results of these scheduling algorithms. References: You need to use the library of pthread. You may use the command "man" to read the system manual.
Bonus
The fairness of the attached program is not good. Suppose you can solve it and show it, you can get the bonus grade.
#include
#include
#include
#include
#define NUM_THREADS 10
int state[NUM_THREADS];// 0:thinking 1:hungery 2:eating
pthread_mutex_t test_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t chopstick[NUM_THREADS];
long counter[11];
int check_state(int i)
{
pthread_mutex_lock(&test_mutex);
if (state[(i+9)%NUM_THREADS]!=2 && state[(i+1)%NUM_THREADS]!=2 && state[i]==1)
{
state[i]=2;
pthread_cond_signal( &(chopstick[i]));
}
pthread_mutex_unlock(&test_mutex);
return;
}
int get_chopstick(int i)
{
state[i]=1;
check_state(i);
if (state[i]!=2)
{
pthread_cond_wait( &(chopstick[i]), &test_mutex );
}
}
int release_chopstick(int i)
{
state[i]=0;
check_state((i+9)%NUM_THREADS);
check_state((i+1)%NUM_THREADS);
}
void *philosophers(int *param)
{
int i=0,j=0,uid=*param,max=10000000;
long x;
struct timeval tp_s1, tp_e1;
struct timezone tzp_s1, tzp_e1;
pthread_t tid= pthread_self();
printf("TID[%d] uid[%d] created ",tid,uid);
gettimeofday(&tp_s1,&tzp_s1);
for (i=0;i<100;i++)
{
get_chopstick(uid);
counter[uid]++;
counter[NUM_THREADS]++;
for (j=0;j x=(i+1)*j; release_chopstick(uid); } gettimeofday(&tp_e1,&tzp_e1); printf("TID[%d] uid[%d] dead[%d] counter[%d] ",tid,uid,counter[uid],counter[NUM_THREADS]); printf("TID[%d] Execution time is %d ",tid,tp_e1.tv_sec-tp_s1.tv_sec); pthread_exit(0); } int main(int argc, char *argv[]) { int i, scope,uid[NUM_THREADS],j=0; int policy; pthread_t tid[NUM_THREADS]; pthread_attr_t attr; struct timeval tp_s, tp_e; struct timezone tzp_s, tzp_e; pthread_attr_init(&attr); pthread_attr_setscope(&attr,PTHREAD_SCOPE_PROCESS);//PTHREAD_SCOPE_SYSTEM); if (pthread_attr_getscope(&attr, &scope )!= 0) fprintf(stderr, "Unable to get scheduling scope "); else { if (scope== PTHREAD_SCOPE_PROCESS) { printf("PTHREAD_SCOPE_PROCESS "); } else { if (scope== PTHREAD_SCOPE_SYSTEM) { printf("PTHREAD_SCOPE_SYSTEM "); } else { fprintf(stderr, "Illegal scope value. "); } } } pthread_attr_setschedpolicy(&attr, SCHED_FIFO); if (pthread_attr_getschedpolicy(&attr, &policy) != 0) fprintf(stderr, "Unable to get policy. "); else { if (policy == SCHED_OTHER) printf("SCHED OTHER "); else if (policy == SCHED_RR) printf("SCHED RR "); else if (policy == SCHED_FIFO) printf("SCHED FIFO "); } gettimeofday(&tp_s,&tzp_s); for(j=0;j<100;j++) { for (i=0;i { uid[i]=i; state[i]=counter[i]=0; chopstick[i]=PTHREAD_COND_INITIALIZER; pthread_cond_wait( &(chopstick[i]), &test_mutex ); } counter[NUM_THREADS]=0; for (i=0;i { if (pthread_create(&tid[i],NULL,&philosophers,&(uid[i]))!=0) fprintf(stderr, "Unable to create a thread "); } for (i=0;i pthread_join(tid[i],NULL); } gettimeofday(&tp_e,&tzp_e); printf("Total execution time =%d ",tp_e.tv_sec-tp_s.tv_sec); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
