我一直在嘗試編寫一個程序,在這裏我模擬使用線程和信號量的最短的第一個工作算法。當主線程告訴他這樣做時,每個線程負責打印其ID。主線程(main)將控制何時將這些作業打印在屏幕上。以下是我想出來的,但我似乎做錯了,我無法弄清楚。我設法讓它打印出相應的作業,但它總是失去10個。文件格式是「id(char)arrival(int)burst(int)」。所以我非常需要幫助,因爲我有一直在嘗試無數的事情,並總是在我幾個小時的相同情況下結束。信號燈不能正常工作
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#define MAX 10
sem_t childs[MAX];
sem_t master;
int jobdone = 0;
typedef struct job
{
int burst, arrival, wait_time, turn_around, executed, done;
char id;
} JOB;
JOB jobs[MAX];
void ShortestFirstJob()
{
int i, j;
char id;
int burst, arrival, wait_time, turn_around, done;
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX - 1; j++)
{
if (jobs[j].arrival > jobs[j + 1].arrival)
{
id = jobs[j].id;
done = jobs[j].done;
burst = jobs[j].burst;
arrival = jobs[j].arrival;
wait_time = jobs[j].wait_time;
turn_around = jobs[j].turn_around;
jobs[j].id = jobs[j + 1].id;
jobs[j].burst = jobs[j + 1].burst;
jobs[j].wait_time = jobs[j + 1].wait_time;
jobs[j].arrival = jobs[j + 1].arrival;
jobs[j].turn_around = jobs[j + 1].turn_around;
jobs[j].done = jobs[j + 1].done;
jobs[j + 1].id = id;
jobs[j + 1].burst = burst;
jobs[j + 1].arrival = arrival;
jobs[j + 1].wait_time = wait_time;
jobs[j + 1].turn_around = turn_around;
jobs[j + 1].done = done;
}
}
}
printf("SORT \n");
for (j = 0; j < MAX; j++)
{
printf("%c %d %d\n", jobs[j].id, jobs[j].arrival, jobs[j].burst);
}
}
void* PrintJob(void *params)
{
char id = *((char*) params);
int i, j, k;
for (i = 0; i < MAX; i++)
{
if (id == jobs[i].id)
break;
}
for (k = 0; k < jobs[i].burst; k++)
{
sem_wait(&childs[i]);
printf("%c", id);
fflush(stdout);
jobs[i].executed += 1;
for (j = 0; j < MAX; j++)
{
if ((i != j) && (jobs[j].arrival > 0))
{
jobs[j].arrival -= 1;
jobs[j].wait_time += 1;
}
}
sem_post(&master);
}
jobs[i].done = 1;
jobdone++;
}
int main()
{
pthread_t threadIds[MAX];
char threadsIdx;
int result;
int i = 0;
int j;
int index[MAX];
FILE* fp = fopen("joblist1.txt", "r");
for (j = 0; j < MAX; j++)
{
sem_init(&childs[j], 0, 0);
}
sem_init(&master, 0, 1);
char id;
int arrival, burst;
while (fscanf(fp, "%c %d %d\n", &id, &arrival, &burst) != EOF)
{
jobs[i].id = id;
jobs[i].arrival = arrival;
jobs[i].burst = burst;
jobs[i].wait_time = 0;
jobs[i].turn_around = 0;
jobs[i].executed = 0;
jobs[i].done = 0;
i++;
}
for (j = 0; j < MAX; j++)
{
printf("%c %d %d\n", jobs[j].id, jobs[j].arrival, jobs[j].burst);
}
ShortestFirstJob();
for (i = 0; i < MAX; i++)
{
if (pthread_create(&threadIds[i], NULL, PrintJob, &jobs[i].id) != 0)
{
perror("thread create");
exit(1);
}
}
int k, c = 0;
int min = 0;
int in = 0;
int value, flag = 0;
while (jobdone < MAX)
{
k = 0;
c = 0;
sem_wait(&master);
for (j = 0; j < MAX; j++)
{
if ((jobs[j].arrival == 0) && (jobs[j].done != 1))
{
index[k++] = j;
c++;
}
}
if (c == 1)
{
sem_post(&childs[index[0]]);
}
else if (c > 1)
{
min = jobs[index[0]].burst;
for (j = 1; j < k; j++)
{
if (jobs[index[j]].burst <= min)
{
min = jobs[index[j]].burst;
in = index[j];
}
}
sem_post(&childs[in]);
}
}
for (i = 0; i < MAX; i++)
{
pthread_join(threadIds[i], NULL);
}
return 0;
}