C - Распечатать расписание заданий, используя pthreads, но qPointer имеет значение null вместо указания на начало очереди
Я пытаюсь использовать C, чтобы напечатать расписание работы, используя pthreads. Но в nextTaskInQueue() qPointer не вызывает ошибку сегментации, но имеет значение NULL, а не указывает на начало очереди узла. Когда вы запустите исполняемый файл, обязательно добавьте 2 в конце в качестве аргумента командной строки. Поскольку мой код требует ввода, введите следующее для ввода: Джим (вкладка) A (вкладка) 2 (вкладка) 5 (введите) Мэри (вкладка) B (вкладка) 2 (вкладка) 3 (введите) Сью (вкладка) A (вкладка) 5 (вкладка) 5 (ввод) Мэри (вкладка) C (вкладка) 6 (вкладка) 4 (ввод). Вот мой исходный код ниже.
#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char user[5];
char process;
int arrival;
int duration;
struct node *next;
}node;
typedef struct Q
{
node *R,*F;
}Q;
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;
Q q;
void initialise(Q *);
int empty(Q *);
void enqueue(Q *, char[5], char, int, int);
void* jobDisplay();
struct node* nextTaskInQueue();
void addToSummary(char[], int);
void summaryDisplay();
int main( int argc, char *argv[] )
{
//sysconf(_SC_NPROCESSORS_ONLN);
int n;
if( argc == 2 )
{
if ( strcmp(argv[1],"2") == 0 )
{
n = atoi(argv[1]);
}
else
{
printf("The argument supplied is not 2.\n");
return 0;
}
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
return 0;
}
else
{
printf("One argument expected.\n");
return 0;
}
char user[5], process;
int arrivalInput, durationInput;
initialise(&q);
printf("\n\tUser\tProcess\tArrival\tDuration\n");
int i = 1;
while ( i < 5 )
{
printf("\t");
if ( scanf("%s\t%c\t%d\t%d", user, &process, &arrivalInput, &durationInput) < 4 )
{
printf("\nThe arguments supplied are incorrect. Try again.\n");
return 0;
}
enqueue(&q, user, process, arrivalInput, durationInput);
i++;
}
printf("\nThis would result in:\n\tTime\tJob\n\t");
pthread_t threadID[n];
i = 0;
for ( i = 0; i < n; i++ )
{
pthread_create(&(threadID[i]),NULL,&jobDisplay,NULL);
}
pthread_join(threadID[0],NULL);
pthread_join(threadID[1],NULL);
pthread_mutex_destroy(&m1);
pthread_mutex_destroy(&m2);
summaryDisplay();
return 0;
}
void initialise(Q *qPointer)
{
qPointer->R=NULL;
qPointer->F=NULL;
rearDisplay=NULL;
tempDisplay=NULL;
frontDisplay=NULL;
}
void enqueue(Q *qPointer, char user[5], char process, int arrivalE, int durationE)
{
node *eP;
eP=(node*)malloc(sizeof(node));
strcpy(eP->user, user);
eP->process=process;
eP->arrival=arrivalE;
eP->duration=durationE;
eP->next=NULL;
if ( empty(qPointer) )
{
qPointer->R=eP;
qPointer->F=eP;
}
else
{
(qPointer->R)->next=eP;
qPointer->R=eP;
}
}
void* jobDisplay()
{
int myTime = 0;
struct node* placeholder = NULL;
placeholder = nextTaskInQueue();
while ( placeholder != NULL )
{
if ( myTime < placeholder->arrival )
{
sleep( placeholder->arrival - myTime );
myTime = placeholder->arrival;
}
printf("%d\t%c\n", myTime, placeholder->process);
sleep(placeholder->duration);
myTime += placeholder->duration;
addToSummary(placeholder->user, myTime);
placeholder = nextTaskInQueue();
}
printf("%d\tCPU %d IDLE\n", myTime, pthread_self());
return NULL;
}
struct node* nextTaskInQueue()
{
struct node* placeholder = NULL;
pthread_mutex_lock(&m1);
struct Q* qPointer=(struct Q *)malloc(1*sizeof(struct Q));
if ( qPointer->F != NULL )
{
placeholder = qPointer->F;
qPointer->F = placeholder->next;
}
pthread_mutex_unlock(&m1);
return placeholder;
}
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("\nSummary\n");
while ( frontDisplay != NULL )
{
printf("%s\t%d\n", frontDisplay->user, frontDisplay->timeLastCalculated);
tempDisplay = frontDisplay->next;
free(frontDisplay);
frontDisplay = tempDisplay;
}
}
int empty(Q *qPointer)
{
if ( qPointer->R==NULL )
return 1;
return 0;
}