c - Not even my main fuction runs -
i cant print "main" on screen. seems none of code runs. when dont specify command line arguments in prints out warning. input file contains integers on each line.
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <semaphore.h> #include <sys/types.h> int patientcount,treatedpatientcount,maxpatient,allregistered; int *list; file *input,*output; sem_t semoutputfile,semglobalvars; void* nurse(); void* doctor(void *); int main( int argc, char *argv[] ){ printf("main"); if(argc != 3){ printf("command line argument count different expected. aborting!"); return -1; } input = fopen(argv[1],"r"); output = fopen(argv[2],"w"); allregistered = 0; maxpatient = 5; treatedpatientcount = 0; patientcount = 0; list = malloc(sizeof(int)*maxpatient); pthread_t nurse,doc1,doc2; sem_init(&semglobalvars, 0, 1); sem_init(&semoutputfile, 0, 1); pthread_create(&nurse, null, &nurse, null); pthread_create(&doc1, null, &doctor, (void*) 1); pthread_create(&doc2, null, &doctor, (void*) 2); pthread_exit(null); } void* nurse(){ char buff[255],*eof; while(1){ eof = fgets(buff, 255, input); if (eof == null) { allregistered = 1; pthread_exit(null); } int k = atoi(buff); sem_wait(&semglobalvars);//critical region 1 starts if(patientcount == maxpatient){ maxpatient *=2; list = realloc(list,sizeof(int)*maxpatient); } list[patientcount++] = k; sem_post(&semglobalvars);//critical region 1 ends sem_wait(&semoutputfile);//critical region 2 starts fputs("nurse registered patient!\n",output); sem_post(&semoutputfile);//critical region 2 ends sleep(2); } } void* doctor(void * id){ printf("doctor"); char buff[255]; int waittime = 0; while(1){ printf("%d %d %d",allregistered,treatedpatientcount,patientcount); if(allregistered == 1 && treatedpatientcount==patientcount) pthread_exit(null); sem_wait(&semglobalvars);//critical region 1 starts waittime = list[treatedpatientcount++]; sem_post(&semglobalvars);//critical region 1 ends sprintf (buff, "doctor %d treated patient\n", (int) id); sem_wait(&semoutputfile);//critical region 2 starts fputs(buff,output); sem_post(&semoutputfile);//critical region 2 ends sleep(waittime); } }
you can add exit(0)
end of main
what exit(0)
flush buffers, (and other things,) making sure buffered (like printf of "main") gets written out.
all c streams (open functions in <cstdio>) closed (and flushed, if buffered), , files created tmpfile removed.
Comments
Post a Comment