Sunday, 15 March 2015

Thread Synchronization in Linux in C -


I am having trouble syncing the thread, so each thread can run a job first, then another thread one Only work can start, and on. Below is my code:

  #include & lt; Unistd.h & gt; # Include & lt; Sys / types.h & gt; # Include & lt; Errno.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Pthread.h & gt; #include & lt; String.h & gt; # Include & lt; Semaphore.h & gt; Zero handler (zero * PTR); Sem_t mutex; Int Work = 2; Number of workers 1/4 work = 4; // Number of jobs for each worker int () {int i = 0; Pthread_t thread_a; Sem_init (and mutes, 0, 1); For (i; i & lt; worker; i ++) {int * n_workers = malloc (sizeof (* n_workers)); * N_workers = i; Pthread_create (& thread_a, NULL, (Zero *) and Handler, N_Workers); } Pthread_join (thread_a, NULL); Sem_destroy (& amp; mutex); Pthread_exit (0); } Zero handler (zero * ptr) {int x = * ((int *) ptr); Int i = 0; For (i; i  

Output is:

  Worker 1: Job 1 employee 1: Job 1 employee 1: Working 2 worker 1: Working 3 Worker 0: employer 0 worker 0: employer 1 worker 0: working 2 worker 0: working 3 in the program, each worker has 4 jobs and 2 employees. The problem is that the worker does all 4 jobs at once and the worker 0 does all the jobs after that. The ideal output will be:  
  worker 0: job 0 employee 1: job 0 Employee 0: Job 1 Worker 1: Job 1 Employee 0: Job 2 Worker 1: Doing Job 2 Worker 0: Job 3 Worker 1: Job 3  

I'm sure Is not that where this issue is, any assistance Ta is highly appreciated. Thank you

If you want to make sure that each thread works [n] before job [n] , You will need to use them either by using SACFASS (which you want to consult), or by using pthread_barrier_t .

If you choose later, then you should be able to get effects with minimal modifications in your code:

  #include & lt; Unistd.h & gt; # Include & lt; Sys / types.h & gt; # Include & lt; Errno.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Pthread.h & gt; #include & lt; String.h & gt; # Include & lt; Semaphore.h & gt; Zero handler (zero * PTR); Pthread_barrier_t constraint; Int Work = 2; Number of workers 1/4 work = 4; // Number of jobs for each worker int () {int i = 0; Pthread_t thread_a; Pthread_barrier_init (and handicap, tap, worker); For (i; i & lt; worker; i ++) {int * n_workers = malloc (sizeof (* n_workers)); * N_workers = i; Pthread_create (& thread_a, NULL, (Zero *) and Handler, N_Workers); } Pthread_join (thread_a, NULL); Pthread_barrier_destroy (& amp; constraint); Pthread_exit (0); } Zero handler (zero * ptr) {int x = * ((int *) ptr); Int i = 0; For (i; i & lt; job; i ++) {printf ("employee% d: working% d \ n", x, i); Pthread_barrier_wait (& amp; constraint); }}  

In this way, before every job [n + 1] starts, every job [N] is executed by the int worker = 2 Workers on the job work on the job [N] does not matter, so you can get different outputs, for example:

  worker 0: job 0 employee 1 : Employ 1 Employee 1: Job Servant 1: 0: Job Servant 1: Job: 2 Worker 1: Working 2 worker 0: Job 3 Worker 1: Job 3  

What is the most important column?


No comments:

Post a Comment