When running the following code with the text file, it just shows a blank screen and nothing happens. Where did I go wro
Posted: Sun Jul 03, 2022 11:59 am
When running the following code with the text file, it justshows a blank screen and nothing happens. Where did I go wrong?
It is supposed to simulate the shortest job first algorithm tocalculate the total completion time, average wait time, as well assort the sequence into sjf scheduling sequence. Itshould spawn childeren processes with only two cores and the twocore should not run at the same time.
Main Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int pid [50], cpu [50], pcount = 0, nextp = 0;
pthread_mutex_t lock;
int totalTime [2] = {0};
int waitTime = 0;
void *executeJob(void *param) {
long procID = pthread_self();
int myId = *((int *) param);
while (1) {
// mutex required since 2 threads will access the jobs queueand race condition can happen
pthread_mutex_lock(&lock);
if (nextp >= pcount) {
pthread_mutex_unlock(&lock);
return NULL;
}
int process = pid [nextp];
int compute = cpu [nextp];
nextp++;
waitTime += totalTime [myId];
pthread_mutex_unlock(&lock);
totalTime [myId] += compute;
printf ("ThreadID: %ld :: Executing process %d for time %d\n",procID, process, compute);
sleep (compute);
printf ("ThreadID: %ld :: Process %d done.\n", procID,process);
}
}
void sort (int arr1 [], intarr2 [], int n) {
// Order by cpu burst time ascending
int swapped = 1;
while (swapped) {
swapped = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (arr2 > arr2 [j]) {
int tmp = arr2 ; arr2 = arr2 [j]; arr2 [j] =tmp;
tmp = arr1 ; arr1 = arr1 [j]; arr1 [j] = tmp;
swapped = 1;
}
}
}
int main() {
pthread_t t1, t2;
FILE *f = fopen ("input.txt", "r");
while (!feof (f)) {
fscanf (f, "%d %d", &pid [pcount], &cpu [pcount]);
pcount++;
}
fclose (f);
sort (pid, cpu, pcount);
pthread_mutex_init(&lock, NULL);
int id1 = 0, id2 = 1;
pthread_create(&t1, NULL, executeJob, &id1);
pthread_create(&t2, NULL, executeJob, &id2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock);
int t = totalTime [0] > totalTime [1] ? totalTime[0] : totalTime [1];
printf ("Total time taken: %d\n", t);
printf ("Average wait time: %.2f\n", waitTime * 1.0 /pcount);
exit(0);
}
input.txt file:
Pid cpuP0 8P1 8P2 3P3 17P4 13P5 5P6 10
It is supposed to simulate the shortest job first algorithm tocalculate the total completion time, average wait time, as well assort the sequence into sjf scheduling sequence. Itshould spawn childeren processes with only two cores and the twocore should not run at the same time.
Main Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int pid [50], cpu [50], pcount = 0, nextp = 0;
pthread_mutex_t lock;
int totalTime [2] = {0};
int waitTime = 0;
void *executeJob(void *param) {
long procID = pthread_self();
int myId = *((int *) param);
while (1) {
// mutex required since 2 threads will access the jobs queueand race condition can happen
pthread_mutex_lock(&lock);
if (nextp >= pcount) {
pthread_mutex_unlock(&lock);
return NULL;
}
int process = pid [nextp];
int compute = cpu [nextp];
nextp++;
waitTime += totalTime [myId];
pthread_mutex_unlock(&lock);
totalTime [myId] += compute;
printf ("ThreadID: %ld :: Executing process %d for time %d\n",procID, process, compute);
sleep (compute);
printf ("ThreadID: %ld :: Process %d done.\n", procID,process);
}
}
void sort (int arr1 [], intarr2 [], int n) {
// Order by cpu burst time ascending
int swapped = 1;
while (swapped) {
swapped = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (arr2 > arr2 [j]) {
int tmp = arr2 ; arr2 = arr2 [j]; arr2 [j] =tmp;
tmp = arr1 ; arr1 = arr1 [j]; arr1 [j] = tmp;
swapped = 1;
}
}
}
int main() {
pthread_t t1, t2;
FILE *f = fopen ("input.txt", "r");
while (!feof (f)) {
fscanf (f, "%d %d", &pid [pcount], &cpu [pcount]);
pcount++;
}
fclose (f);
sort (pid, cpu, pcount);
pthread_mutex_init(&lock, NULL);
int id1 = 0, id2 = 1;
pthread_create(&t1, NULL, executeJob, &id1);
pthread_create(&t2, NULL, executeJob, &id2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock);
int t = totalTime [0] > totalTime [1] ? totalTime[0] : totalTime [1];
printf ("Total time taken: %d\n", t);
printf ("Average wait time: %.2f\n", waitTime * 1.0 /pcount);
exit(0);
}
input.txt file:
Pid cpuP0 8P1 8P2 3P3 17P4 13P5 5P6 10