multithreading - C pthreads and signaling -
i'm having alot of trouble on assignment class, , great. need code create 4 producer threads continuously loop , send sigusr1 or sigusr2 4 consumer threads. 2 respond sigusr1, , 2 respond sigusr2. signals being sent producers, , received consumers, nothing happens after , program crashes. below program, , output gdb when ran.
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <semaphore.h> #include <signal.h> #include <time.h> #define np 4 #define nc1 2 #define nc2 2 #define cnt 10 void handler1(int); void handler2(int); typedef struct { int sent; int received; int buf[1]; int sig1; int sig2; sem_t con; sem_t prod; } sbuf_t; sbuf_t buff; pthread_t threads[9]; void *producer() { srand(time(0)); int s,i; while(1){ sem_wait(&buff.prod); s=(rand()%2)+1; //printf("prod ready \n"); if(s==1){ buff.sent++; for(i=0;i<4;i++){ pthread_kill(threads[i],sigusr1);} } else if(s==2){ buff.sent++; for(i=0;i<4;i++){ pthread_kill(threads[i],sigusr1);} } usleep(rand()%100000); 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){ printf("caught 1\n"); if(signum==sigusr1){ sem_wait(&buff.con); buff.received++; buff.sig1++; sem_post(&buff.con); } } void handler2(int signum){ printf("caught 2 \n"); if(signum==sigusr2){ sem_wait(&buff.con); buff.received++; buff.sig2++; sem_post(&buff.con); } } void main(){ buff.sig1=0; buff.sig2=0; buff.sent=0; buff.received=0; int index; sem_init(&buff.con, 0, 1); sem_init(&buff.prod, 0, 1); (index = 0;index < nc1;index++) { pthread_create(&threads[index], null, consumer1,null); } (index = 0;index < nc2;index++) { pthread_create(&threads[index+2], null, consumer2,null); } (index = 0; index < np; index++) { pthread_create(&threads[index+4], null, producer,null); }
and gdb
(gdb) run starting program: /home/eric/a.out [thread debugging using libthread_db enabled] using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [new thread 0x7ffff77f6700 (lwp 4944)] [new thread 0x7ffff6ff5700 (lwp 4945)] [new thread 0x7ffff67f4700 (lwp 4946)] [new thread 0x7ffff5ff3700 (lwp 4947)] [new thread 0x7ffff57f2700 (lwp 4948)] program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff5ff3700 (lwp 4947)] consumer2 () @ lab3.c:68 68 } (gdb) continue continuing. program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff67f4700 (lwp 4946)] consumer2 () @ lab3.c:68 68 } (gdb) continuing. program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff6ff5700 (lwp 4945)] consumer1 () @ lab3.c:60 60 } (gdb) continuing. [new thread 0x7ffff4ff1700 (lwp 4949)] program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff77f6700 (lwp 4944)] consumer1 () @ lab3.c:60 60 } (gdb) continuing. caught 1 caught 1 caught 1 program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff5ff3700 (lwp 4947)] consumer2 () @ lab3.c:68 68 } (gdb) continuing. caught 1 [new thread 0x7ffff47f0700 (lwp 4950)] caught 1 program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff67f4700 (lwp 4946)] consumer2 () @ lab3.c:68 68 } (gdb) continuing. program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff6ff5700 (lwp 4945)] consumer1 () @ lab3.c:60 60 } (gdb) continuing. caught 1 program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff77f6700 (lwp 4944)] consumer1 () @ lab3.c:60 60 } (gdb) continuing. caught 1 [new thread 0x7ffff3fef700 (lwp 4951)] caught 1 program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff5ff3700 (lwp 4947)] consumer2 () @ lab3.c:68 68 } (gdb) continuing. program received signal sigusr1, user defined signal 1. [switching thread 0x7ffff67f4700 (lwp 4946)] consumer2 () @ lab3.c:68 68 } (gdb) continuing. caught 1 [thread 0x7ffff3fef700 (lwp 4951) exited] [thread 0x7ffff47f0700 (lwp 4950) exited] [thread 0x7ffff4ff1700 (lwp 4949) exited] [thread 0x7ffff57f2700 (lwp 4948) exited] [thread 0x7ffff5ff3700 (lwp 4947) exited] [thread 0x7ffff67f4700 (lwp 4946) exited] [thread 0x7ffff7fda740 (lwp 4940) exited] cannot find user-level thread lwp 4945: generic error (gdb) continuing. cannot execute command without live selected thread. (gdb)
take at: sigemptyset,sigaddset maybe helps you
Comments
Post a Comment