c - Threads are created, then all exit before critical region -


i need write program class creates 8 threads. 4 producer, , 4 consumer. producers need loop, , randomly send sigusr1 or sigusr2 consumer threads. 2 should register if have received sigusr1 , other 2 register sigusr2.

when try run threads created, "prod ready" printed 4 producer threads,"waiting 1" printed both threads, "waiting 2" printed 3 times threads exit. @ end of debugging says process exits normally.

i need use semaphores control critical regions. great.

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <semaphore.h> #include <signal.h>  #define np  4 #define nc1 2 #define nc2 2 #define cnt 10  void handler1(int signum); void handler2(int signum);  typedef struct {     int sent;     int received;     int buf[1];        int sig1;                   int sig2;                   sem_t con;              sem_t prod;           } sbuf_t;  sbuf_t buff;  void *producer() {     printf("prod ready \n");     int s;     while(1){         sem_wait(&buff.prod);         s=rand()%2;         if(s==1){             buff.sent++;             kill(0,sigusr1);         }         else if(s==2){             buff.sent++;             kill(0,sigusr2);          }sem_post(&buff.prod);     }  }  void *consumer1() {     signal(sigusr1, handler1);     printf("waiting 1\n");     while(1){      } }  void *consumer2() {     signal(sigusr2, handler2);     printf("waiting 2\n");     while(1){      } }  void handler1(int signum){     if(signum==sigusr1){         sem_wait(&buff.con);         printf("caught 1\n");         buff.received++;         buff.sig1++;          sem_post(&buff.con);     } }  void handler2(int signum){     if(signum==sigusr2){         sem_wait(&buff.con);         printf("caught 2 \n");         buff.received++;         buff.sig2++;          sem_post(&buff.con);     } }  void main(){     pthread_t threads[9];     buff.sig1=0;     buff.sig2=0;     buff.sent=0;     buff.received=0;     int index;           sem_init(&buff.con, 0, 0);     sem_init(&buff.prod, 0, 0);     (index = 0; index < np; index++) {             pthread_create(&threads[index], null, producer,null);         }         (index = 0;index < nc1;index++) {             pthread_create(&threads[index+4], null, consumer1,null);         }     (index = 0;index < nc2;index++) {             pthread_create(&threads[index+6], null, consumer2,null);         } } 

in main() function, threads being created,

then main() exits.

when main() exits, threads exited.

suggest reading such functions as: pthread_exit() , pthread_join()

note: following has error in handling of signals , in handling of semaphores, demonstrate use of pthread_join() , pthread_exit()

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <semaphore.h> #include <signal.h>  #define np  4 #define nc1 2 #define nc2 2 #define cnt 10  #define max_threads (9)  void handler1(int signum); void handler2(int signum);  struct sbuf_t  {     int sent;     int received;     int buf[1];        int sig1;                   int sig2;                   sem_t con1;     sem_t con2;              sem_t prod;           };  typedef struct sbuf_t mydata; mydata buff;  void *producer( void *dummy )  {     (void)dummy;      printf("prod ready \n");     int s;      while(1)     {         sem_wait(&buff.prod);         s=rand()%2;          if( !s )         {             buff.sent++;             kill( 0, sigusr1 );         }          else // if( s )         {             buff.sent++;             kill( 0, sigusr2 );         }          //sem_post(&buff.prod);     }      pthread_exit( null ); } // end thread: producer   void *consumer1( void *dummy )  {     (void)dummy;      //signal(sigusr1, handler1);     printf("waiting 1\n");      while(1)     {         sem_wait( &buff.con1 );         //         //sem_post( &buff.prod );     }      pthread_exit( null ); } // end thread: consumer1   void *consumer2( void *dummy )  {     (void)dummy;      //signal(sigusr2, handler2);     printf("waiting 2\n");      while(1)     {         sem_wait( &buff.con2 );         //         //sem_post( &buff.prod );     }      pthread_exit( null ); } // end thread: consumer2   void handler(int signum) {     sem_post(&buff.prod);     if(signum==sigusr1)     {         //sem_wait(&buff.con);         puts("caught 1");         buff.received++;         buff.sig1++;          sem_post(&buff.con1);     }      else if(signum==sigusr2)     {         //sem_wait(&buff.con);         puts("caught 2");         buff.received++;         buff.sig2++;          sem_post(&buff.con2);     } } // end signal handler: handler2   int main( void ) {     pthread_t threads[ max_threads ];     buff.sig1=0;     buff.sig2=0;     buff.sent=0;     buff.received=0;     int index;       sem_init(&buff.con1, 0, 0);     sem_init(&buff.con2, 0, 0);     sem_init(&buff.prod, 0, 0);      signal(sigusr2, handler);     signal(sigusr1, handler);      (index = 0; index < np; index++)      {         pthread_create(&threads[index], null, producer,null);     }      (index = 0;index < nc1;index++)      {         pthread_create(&threads[index+4], null, consumer1,null);     }      (index = 0;index < nc2;index++)      {         pthread_create(&threads[index+6], null, consumer2,null);     }      for( size_t x=0; x<max_threads; x++ )     {         pthread_join( threads[x], null );     } } // end function: main 

however, methods handle signals threads not use kill() processes.

rather code should use functions similar to::

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); int pthread_cond_signal(pthread_cond_t *cond) 

the signaled thread should block on pthread_cond_wait() call until thread sends signal using pthread_cond_signal(), same condition variable.

considering analogy signals delivered processes, bit different because signaled thread has suspended execution waiting signal, unlike process gets interrupted , goes on.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -