简体   繁体   English

C pthread和信令

[英]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. 我需要此代码来创建4个生产者线程,这些线程不断循环并将SIGUSR1或SIGUSR2发送到4个消费者线程。 2 only respond to SIGUSR1, and 2 only respond to SIGUSR2. 2个仅响应SIGUSR1,2个仅响应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. 以下是程序,以及运行时GDB的输出。

  #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

(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也许可以帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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