Assignment requires that you should create a virtual machine in AWS (Amazon Web Services) to develop your programs to examine the performance of different pthread contention scope. Try to adopt 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 runner 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. Code MUST BE WRITTEN IN C/C++ #include #include #include #define NUM_THREADS 5 struct timeval tp_s, tp_e; struct timezone tzp_s, tzp_e; void *runner(void *param) { int i=0,j=0; long x; pthread_t tid= pthread_self(); struct timeval tp_s1, tp_e1; struct timezone tzp_s1, tzp_e1; printf(" %d created\n",tid); gettimeofday(&tp_s1,&tzp_s1); for (i=0;i<1000;i++) { for (j=0;j
Assignment requires that you should create a virtual machine in AWS (Amazon Web Services) to develop your
Problem Statement
According to the code attached in the document, rewrite the function runner 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
l Please discuss the results of these scheduling algorithms.
Code MUST BE WRITTEN IN C/C++
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#define NUM_THREADS 5
struct timeval tp_s, tp_e;
struct timezone tzp_s, tzp_e;
void *runner(void *param)
{
int i=0,j=0;
long x;
pthread_t tid= pthread_self();
struct timeval tp_s1, tp_e1;
struct timezone tzp_s1, tzp_e1;
printf(" %d created\n",tid);
gettimeofday(&tp_s1,&tzp_s1);
for (i=0;i<1000;i++)
{
for (j=0;j<MAX;j++)
x=(i+1)*j;
printf(" ");
}
gettimeofday(&tp_e1,&tzp_e1);
printf("Execution time %d diff=%d\n",tid,tp_e1.tv_sec-tp_s1.tv_sec);
printf("Total time %d diff=%d\n",tid,tp_e1.tv_sec-tp_s.tv_sec);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
int i, scope;
pthread_t tid[NUM_THREADS];
struct sched_param p;
pthread_attr_t attr;
int sched_policy=0;
pthread_attr_init(&attr);
if (pthread_attr_getscope(&attr, &scope )!= 0)
fprintf(stderr, "Unable to get scheduling scope\n");
else
{
if (scope== PTHREAD_SCOPE_PROCESS)
{
printf("PTHREAD_SCOPE_PROCESS\n");
else
if (scope== PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM\n");
else
fprintf(stderr, "Illegal scope value.\n");
}
pthread_attr_setscope(&attr,PTHREAD_SCOPE_PROCESS);//PTHREAD_SCOPE_SYSTEM); you must change the contention scope.
gettimeofday(&tp_s,&tzp_s);
for (i=0;i<NUM_THREADS;i++)
{
if (pthread_create(&tid[i],&attr,runner,NULL)!=0)
fprintf(stderr, "Unable to create a thread\n");
}
for (i=0;i<NUM_THREADS;i++)
pthread_join(tid[i],NULL);
gettimeofday(&tp_e,&tzp_e);
printf("Total time =%d\n",tp_e.tv_sec-tp_s.tv_sec);
}
Step by step
Solved in 2 steps with 3 images