简体   繁体   中英

C pthreads and signaling

I'm having alot of trouble on an assignment for class, and any help would be great. I need this code to create 4 producer threads that continuously loop and send SIGUSR1 or SIGUSR2 to 4 consumer threads. 2 only respond to SIGUSR1, and 2 only respond to SIGUSR2. The signals are being sent by the producers, and received by consumers, but nothing happens after and the program crashes. Below are the program, and output from 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);

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

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

看看:sigemptyset,sigaddset也许可以帮助您

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM