0

The Idea here is to create a file to be written to. I'm trying to create ten threads and have them print to the file 10 times each. Using a semaphore to stop multiple threads from writing to the file at once. Everything compiles and I don't get an error exit, however I can't understand why running the program numerous times: 1)It doesn't print 100 lines to the file, infact it's far less 2) The number of lines printed to the file vary each time.

    #include <stdio.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <semaphore.h>

    #define READ    "r"
    #define NEW     "w"
    #define ADD     "a"
    #define NL      "\n"
    #define TAB     "\t"

    #define FNAME   "PROCTAB.txt"
    #define MAX_STRING_LEN  80
    #define NUMBER_OF_THREADS       10
    FILE *fp;
    sem_t mutex;
    int counter;

    FILE *makeTextFile(char *fname, char mode){
       FILE *localFP;
       localFP = fopen(fname, &mode);
       return (localFP);
}

    void *print_message(void *tid){
        int i;
        for (i = 0; i < 10; i++){
            sem_wait(&mutex);
            fp = fopen(FNAME, ADD);
            fprintf(fp, "Thread %d is running.\n", tid);
            fclose(fp);
        sem_post(&mutex);
     }
    }

    int threads(){
     const char *fName = "PROCTAB.txt";
         int status;
         pthread_t threads[NUMBER_OF_THREADS];
      fp = makeTextFile(FNAME, 'w');
         fprintf(fp, "Process ID: %ld\n", (long)getpid());
         fclose(fp);
     int i;
         for (i =0; i < NUMBER_OF_THREADS; i++){
            status = pthread_create(&threads[i], NULL, &print_message, (void *)i);
            if (status != 0){
                printf("pthread_create returned error code %d\n", status);
                exit(-1);
                 }
            }
      return 0;
   }

My main function is contained in a separate file.

1
  • Test fp in fp = fopen(FNAME, ADD); to insure you did open the file successfully all the times you think you did. Also make fp local to print_message(). Commented Jun 5, 2013 at 22:50

1 Answer 1

2

You need to wait for all thread to finish before you exit the program.

if you add trace you will see which thread is finish.

void *print_message(void *tid){
    int i;
    for (i = 0; i < 10; i++){
        sem_wait(&mutex);
        fp = fopen(FNAME, ADD);
        fprintf(fp, "Thread %d is running.\n", tid);
        fclose(fp);
    sem_post(&mutex);
    printf ( "Thread %d has finished.\n", tid);
 }
}

This is how you wait for all threads to finish

   /* Wait for Threads to Finish */
    for (i=0; i<NUMTHREADS; i++) {
        pthread_join(thread[i], NULL);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Ah hah, I think that was a big issue was that it wasn't waiting for each thread to finish. Thanks
So, it answer your question ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.