This is the code that I have been worked on it .. #include <stdio.h> #include <s
ID: 3677175 • Letter: T
Question
This is the code that I have been worked on it ..
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int times[5];
char process[] = {'A', 'B', 'C', 'D', 'E'};
int turn = 0;
void StartNext(int tid) //choose the next thread to run
{
int i;
for(i = (tid + 1) % 5; times[i] == 0; i = (i + 1) % 5)
if(i == tid) //if every thread has finished
return;
turn = i;
}
void *Run(void * par){ //the thread function
int i = atoi(par);
while(times[i] != 0)
{
while(turn != i); //busy waiting till it is its turn
if(times[i] > 2)
{
printf("%c 2 ", process[i]);
times[i] -= 2;
//turn=(i+1)% 5;
sleep(2); //sleep is to simulate the actual running time
}
else if(times[i] > 0 && times[i] <= 2) //this thread will have finished after this turn
{
printf("%c %d", process[i], times[i]);
printf("==> Existing ");
//turn=(i+1)% 5;
sleep(times[i]);
times[i] = 0;
}
StartNext(i); //choose the next thread to run
}// end while
pthread_exit(0);
}
int main(int argc, char* argv[])
{
pthread_t threads[5];
int i;
if(argc == 7)
{
for(i = 0; i < 5; i++){
times[i] = atoi(argv[i + 2]); //input the burst time of each thread
printf("%d ",times[i]);}
for(i = 0; i < 5; i++)
{
pthread_create(&threads[i], NULL, Run, &i); //Create threads
}
for(i = 0; i < 5; i++)
{
pthread_join(threads[i], NULL); //Join threads
}
}// end if
else {
printf("Should run with 7 arguments..");
return -1;
}
return 0;
}
----------------------------
the problem is the output that I got is just :
1
2
3
4
5
A 1==> Existing
A 1==> Existing
A 1==> Existing
A 1==> Existing
A 1==> Existing
I want the output to be as a picture above.. please fix the code to give me a correct output please ???
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int times[5];
char process[] = {'A', 'B', 'C', 'D', 'E'};
int turn = 0;
void StartNext(int tid) //choose the next thread to run
{
int i;
for(i = (tid + 1) % 5; times[i] == 0; i = (i + 1) % 5)
if(i == tid) //if every thread has finished
return;
turn = i;
}
void *Run(void * par){ //the thread function
int i = atoi(par);
while(times[i] != 0)
{
while(turn != i); //busy waiting till it is its turn
if(times[i] > 2)
{
printf("%c 2 ", process[i]);
times[i] -= 2;
//turn=(i+1)% 5;
sleep(2); //sleep is to simulate the actual running time
}
else if(times[i] > 0 && times[i] <= 2) //this thread will have finished after this turn
{
printf("%c %d /n /n /n ==> Existing ", process[i], times[i]);
//turn=(i+1)% 5;
sleep(times[i]);
times[i] = 0;
}
StartNext(i); //choose the next thread to run
}// end while
pthread_exit(0);
}
int main(int argc, char* argv[])
{
pthread_t threads[5];
int i;
if(argc == 7)
{
for(i = 0; i < 5; i++){
times[i] = atoi(argv[i + 2]); //input the burst time of each thread
printf("%d ",times[i]);}
for(i = 0; i < 5; i++)
{
pthread_create(&threads[i], NULL, Run, &i); //Create threads
}
for(i = 0; i < 5; i++)
{
pthread_join(threads[i], NULL); //Join threads
}
}// end if
else {
printf("Should run with 7 arguments..");
return -1;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.