2016-11-06 143 views
0

我在課堂上有很多麻煩,任何幫助都很棒。我需要這段代碼來創建4個生成器線程,這些線程可以持續循環並將SIGUSR1或SIGUSR2發送到4個消費者線程。 2只響應SIGUSR1,2只響應SIGUSR2。這些信號由製作者發送,並由消費者接收,但之後沒有任何反應,程序崩潰。下面是該程序,並在運行時從GDB輸出。C pthreads和信號發送

#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); 

     for (index = 0;index < NC1;index++) { 
      pthread_create(&threads[index], NULL, consumer1,NULL); 
     } 
    for (index = 0;index < NC2;index++) { 
      pthread_create(&threads[index+2], NULL, consumer2,NULL); 
     } 
    for (index = 0; index < NP; index++) { 
      pthread_create(&threads[index+4], NULL, producer,NULL); 
     } 

和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 to Thread 0x7ffff5ff3700 (LWP 4947)] 
consumer2() at lab3.c:68 
68  } 
(gdb) continue 
Continuing. 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff67f4700 (LWP 4946)] 
consumer2() at lab3.c:68 
68  } 
(gdb) 
Continuing. 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff6ff5700 (LWP 4945)] 
consumer1() at lab3.c:60 
60  } 
(gdb) 
Continuing. 
[New Thread 0x7ffff4ff1700 (LWP 4949)] 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff77f6700 (LWP 4944)] 
consumer1() at lab3.c:60 
60  } 
(gdb) 
Continuing. 
Caught 1 
Caught 1 
Caught 1 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff5ff3700 (LWP 4947)] 
consumer2() at lab3.c:68 
68  } 
(gdb) 
Continuing. 
Caught 1 
[New Thread 0x7ffff47f0700 (LWP 4950)] 
Caught 1 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff67f4700 (LWP 4946)] 
consumer2() at lab3.c:68 
68  } 
(gdb) 
Continuing. 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff6ff5700 (LWP 4945)] 
consumer1() at lab3.c:60 
60  } 
(gdb) 
Continuing. 
Caught 1 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff77f6700 (LWP 4944)] 
consumer1() at lab3.c:60 
60  } 
(gdb) 
Continuing. 
Caught 1 
[New Thread 0x7ffff3fef700 (LWP 4951)] 
Caught 1 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff5ff3700 (LWP 4947)] 
consumer2() at lab3.c:68 
68  } 
(gdb) 
Continuing. 

Program received signal SIGUSR1, User defined signal 1. 
[Switching to Thread 0x7ffff67f4700 (LWP 4946)] 
consumer2() at 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 for LWP 4945: generic error 
(gdb) 
Continuing. 
Cannot execute this command without a live selected thread. 
(gdb) 

回答

0

看看:sigemptyset,sigaddset也許它可以幫助你