NOTE: PROGRAMMING C PROBLEM Solutions to the problems should be submitted via D2
ID: 3875169 • Letter: N
Question
NOTE: PROGRAMMING C PROBLEM
Solutions to the problems should be submitted via D2L. Your solutions are to be written in C. 1. CPUs today have multiple CPUs. Dual-core has 2 processors and Quad-core has 4 processors. Modify your program so that the output from Assignment #1 now reflects n processors operating at once. To make your program flexible, provide n as a command line argument to the program. The input file is the same, but the output is different. For the example (for n = 2) Process Arrival ue 10 This would result in: Time 10 12 CPU 2 IDLE CPU 1 IDLE Summary ue 10 12 Note: Be careful! You cannot start a job until it has arrived.Explanation / 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.