c - Wrong Output using Mutex and Pthread Library -


the purpose of below program learn mutex , pthread library. main() creates 3 thread. (thread 1, 2 & 3).
each thread one-by-one sequentially, reads 1 character per file (different file) , stores global constant.
example thread 1 reads character 'a' file1, wait thread 2 & 3 same (i.e. read 'b' , 'c' file2 , file3 respective).
once reading finished, want main print global constant file.out

i have attempted program same getting incorrect output.

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #include <stdbool.h>  char glob_var, strtemp[1024]; pthread_mutex_t lock; int cnt = 0; char inp[3][10] = {"file-1.in", "file-2.in", "file-3.in"};  void * thread_func(void * arg);  void * thread_func(void * arg) {     file *fp;     char * argu = (char *)arg;     fp = fopen(argu, "r");     while (1) {         pthread_mutex_lock(&lock);         glob_var = fgetc(fp);         if(feof(fp))         {              pthread_mutex_unlock(&lock);             break ;         }         if (glob_var != '\n')             strtemp[cnt++] = glob_var;         pthread_mutex_unlock(&lock);     }     fclose(fp);      return (void *)0; }  int main(void) {     file * fpout;     pthread_t threads[3];     int = 0;      fpout = fopen("file.out", "w");      (i = 0; < 3; i++)         pthread_create(&threads[i], null, thread_func, (void *)inp[i]);      (i = 0; < 3; i++)         pthread_join(threads[i], null);      strtemp[cnt] = '\0';     cnt = 0;      while(1) {         glob_var = strtemp[cnt++];         fprintf(fpout, "%c\n", glob_var);         if (strtemp[cnt] == '\0') {             break;         }     }     fclose(fpout);      return 0; } 

input file  file1 file2 file3     u     t o     .     # e     p         r     m e     n     $ 

output file  above code         desired output t                       #                       u r                       t                       o m                       . !                       # $                       e s                       p m                       u                       .                       r p                       m r                       e n                       n 

your mutex here makes sure no more 1 thread accesses global variables @ same time. doesn't give guarantees on order in threads scheduled. want do, mutex wrong synchronization primitive because designed that: exclude other threads accessing same resource @ same time.

you want explicitly allow specific thread after thread. can done using semaphores. need 1 per thread. so, declare second sem_t array globally 3 entries, initialize first 1, others 0. then, pass thread number (0 - 2) threads , like:

int next = num + 1; if (next > 2) next = 0; sem_wait(sems[num]); /* code, accessing inp[num] */ sem_post(sems[next]); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -