NOTE: PROGRAMMING C PROBLEM 3. The downside of Priority is that you are at the m
ID: 3875171 • Letter: N
Question
NOTE: PROGRAMMING C PROBLEM
3. The downside of Priority is that you are at the mercy of the order in which users specify importance of jobs and their arrival times. A large job specified to be important at the beginning has everyone waiting and becoming irritated. A solution to this is to try and help people with small jobs get in and out quickly. To do this, we need pre-emption the concept of periodically stopping a job to take away the CPU and decide who is the next eligible process to run. This is needed to prevent large jobs that acquire the CPU and start computing to only hold the CPU while new jobs arrive and have to wait. To implementExplanation / Answer
#include #include #include #include #include #include struct node { char user[5]; char process; int arrival; int duration; int priority; struct node *next; }*head; struct display { char user[5]; int timeLastCalculated; struct display *next; }*frontDisplay, *tempDisplay, *rearDisplay; pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; void initialise(); void insert(char[5], char, int, int, int); void add(char[5], char, int, int, int); int count(); void addafter(char[5], char, int, int, int, int); void* jobDisplay(); void addToSummary(char[], int); void summaryDisplay(); int main( int argc, char *argv[] ) { //sysconf(_SC_NPROCESSORS_ONLN); int n; if( argc == 2 ) { n = atoi(argv[1]); } else if( argc > 2 ) { printf("Too many arguments supplied. "); return 0; } else { printf("One argument expected. "); return 0; } char user[5], process; int arrivalInput, durationInput, priorityInput; initialise(); printf(" User Process Arrival Runtime Priority "); int i = 1; while ( i < 5 ) { printf(" "); if ( scanf("%s %c %d %d %d", user, &process, &arrivalInput, &durationInput, &priorityInput) < 5 ) { printf(" The arguments supplied are incorrect. Try again. "); return 0; } insert(user, process, arrivalInput, durationInput, priorityInput); i++; } printf(" This would result in: Time Job "); pthread_t threadID[n]; i = 0; for ( i = 0; i arrival + temp->duration ) < ( arrival + duration ) ) { c++; } temp = temp->next; } if ( c == 0 ) { add(user, process, arrival, duration, priority); } else if ( c user, user); temp->process = process; temp->arrival = arrival; temp->duration = duration; temp->priority = priority; right = (struct node *)head; while ( right->next != NULL ) { right = right->next; } right->next = temp; right = temp; right->next = NULL; } } } void add(char user[5], char process, int arrival, int duration, int priority) { struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); strcpy(temp->user, user); temp->process = process; temp->arrival = arrival; temp->duration = duration; temp->priority = priority; if ( head == NULL ) { head = temp; head->next = NULL; } else { temp->next = head; head = temp; } } int count() { struct node *n; int c = 0; n = head; while ( n != NULL ) { n=n->next; c++; } return c; } void addafter(char user[5], char process, int arrival, int duration, int priority, int loc) { int i; struct node *temp,*left,*right; right = head; for(i = 1; i next; } temp = (struct node *)malloc(sizeof(struct node)); strcpy(temp->user, user); temp->process = process; temp->arrival = arrival; temp->duration = duration; temp->priority = priority; left->next = temp; left = temp; left->next = right; return; } void* jobDisplay() { char tempUser[5]; char myJob = ' '; int myTime = 0; int myCounter = 0; int tempTime = 0; pthread_mutex_lock(&m1); struct node* placeholder = head; pthread_mutex_unlock(&m1); if ( placeholder == NULL ) { return; } while ( placeholder != NULL ) { if ( myTime arrival ) { sleep( placeholder->arrival - myTime ); myTime = placeholder->arrival; } pthread_mutex_lock(&m1); if ( placeholder->next != NULL ) { if ( placeholder->next->duration duration ) { strcpy(tempUser, placeholder->user); myJob = placeholder->process; tempTime = placeholder->duration - 1; printf(" %d %c ", myTime, myJob); myTime++; sleep(1); placeholder = placeholder->next; } } pthread_mutex_unlock(&m1); myCounter = 1; while ( myCounter duration ) { printf(" %d %c ", myTime, placeholder->process); myTime++; myCounter++; sleep(1); } addToSummary(placeholder->user, myTime); if ( myJob != ' ' ) { myCounter = 1; while ( myCounter next; pthread_mutex_unlock(&m1); } printf(" %d IDLE ", myTime); return NULL; } void addToSummary(char name[], int timeLeft) { pthread_mutex_lock(&m2); if ( rearDisplay == NULL ) { rearDisplay = (struct display *)malloc(1*sizeof(struct display)); rearDisplay->next = NULL; strcpy(rearDisplay->user, name); rearDisplay->timeLastCalculated = timeLeft; frontDisplay = rearDisplay; } else { tempDisplay = frontDisplay; while ( tempDisplay != NULL ) { if ( strcmp(tempDisplay->user, name) == 0 ) { tempDisplay->timeLastCalculated = timeLeft; break; } tempDisplay = tempDisplay->next; } if ( tempDisplay == NULL ) { tempDisplay = (struct display *)malloc(1*sizeof(struct display)); rearDisplay->next = tempDisplay; strcpy(tempDisplay->user, name); tempDisplay->timeLastCalculated = timeLeft; tempDisplay->next = NULL; rearDisplay = tempDisplay; } } pthread_mutex_unlock(&m2); } void summaryDisplay() { printf(" Summary "); while ( frontDisplay != NULL ) { printf(" %s %d ", frontDisplay->user, frontDisplay->timeLastCalculated); tempDisplay = frontDisplay->next; free(frontDisplay); frontDisplay = tempDisplay; } printf(" "); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.