0

I cant even get to print "Main" on the screen. It seems like none of my code runs. When i dont specify any command line arguments in prints out the warning. my 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 is different then 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 a 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 a patient\n", (int) id);
        sem_wait(&semOutputFile);//Critical region 2 starts
        fputs(buff,output);
        sem_post(&semOutputFile);//Critical region 2 ends

        sleep(waitTime);
    }
}
omerfirmak
  • 163
  • 1
  • 9
  • 3
    Output to `stdout` (which happens when you use `printf`) is by default *line buffered*. That means the output is written to the console when either the buffer is full *or* you have a newline. That's why you should always end your output with a newline. – Some programmer dude Mar 28 '15 at 17:17
  • ... or log to `stderr` using `fprintf(stderr, ...` instead of `printf(...`. – alk Mar 28 '15 at 17:18
  • 1
    add `fflush(stdout);` after your `printf` – chqrlie Mar 28 '15 at 17:22
  • Ok that solved not printing anything problem. It seems like my Doctor function is faulty. Thanks – omerfirmak Mar 28 '15 at 17:25
  • Relevant SO question here http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Weather Vane Mar 28 '15 at 18:12

1 Answers1

1

You can add exit(0) to the end of main

What exit(0) does is flush all buffers, (and other good things,) making sure anything that was buffered (like that printf of "Main") gets written out.

All C streams (open with functions in <cstdio>) are closed (and flushed, if buffered), and all files created with tmpfile are removed.

http://www.cplusplus.com/reference/cstdlib/exit/

mrflash818
  • 904
  • 15
  • 21