2

我有一个任务,我被卡住了,我不知道问题出在哪里,但是我必须使用一个线程从文件中获取两个数字,在屏幕上打印数字,然后另一个线程将它们两个数字相加,然后将它们打印在屏幕上。这是我到目前为止所拥有的,但我似乎无法让它正常工作,任何帮助将不胜感激。 我应该指出,我必须在不使用互斥锁和信号量的情况下做到这一点。

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

typedef struct
{
     int a;
     int b;
} pair_t;

FILE *f;

static void * reader(void *data){
    pair_t *d = (pair_t *)data;
    sigset_t catch;
    int flag;

    sigemptyset(&catch);

    sigaddset(&catch, SIGUSR1);

    while(!feof(f)){
        sigwait(&catch, &flag);
        fscanf(f,"%d", &d->a);
        fscanf(f,"%d", &d->b);
        printf("Thread 1 submitting : %d %d", d->a, d->b);
        sleep(5);
    }
    return 0;
}

static void * calculator(void *data){
    pair_t *pa = (pair_t *)data;
    sigset_t catch;
    sigemptyset(&catch);
    sigaddset(&catch, SIGUSR1);
    int flag;

    while(1){
        sigwait(&catch, &flag);
        printf("Thread 2 calculated = %d\n",(pa->a + pa->b));
        sleep(5);
    }
    return 0;
}

int main(int argc, char *argv[]){
    f = fopen(argv[1],"r");
    if(f ==NULL){
        perror; 
    }
    if(f !=NULL){
        pair_t * p;

        sigset_t set;
        sigemptyset(&set);
        sigaddset(&set, SIGUSR1);

        pthread_t t1;
        pthread_t t2;

        pthread_sigmask(SIG_BLOCK, &set, NULL);
        pthread_create(&t1, NULL, reader, (void *)&p);
        pthread_create(&t2, NULL, calculator, (void *)&p);

        pthread_join(t1, NULL);
        printf("Goodbye from Thread One");
        pthread_join(t2, NULL);
        printf("Goodbye from Thread Two");

        pthread_exit(NULL); 
    }
    return 0;
}
4

1 回答 1

1

如果我正确理解您的想法,您希望 reader() 读取值,然后将它们提交给计算器。如果是这样,那么首先您必须使用 pthread_kill() 将信号从阅读器发送到计算器。为此,您需要使 t2 对读者可见。
如果您不需要来自计算器的关于完成计算的返回信号,那么您可以从阅读器中删除 sigwait() 。否则你必须从计算器向 t1 发送信号,让读者知道计算已经结束,读者可以继续从文件中读取值。
因此,这是生产者和消费者的常见情况。
下面是带有一些注释的代码:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

typedef struct
{
     int a;
     int b;
} pair_t;

FILE *f;

pthread_t t1; // must be global for pthread_kill()
pthread_t t2; // must be global for pthread_kill()

static void * reader(void *data){

    pair_t *d = (pair_t *)data;
    sigset_t catch;
    int flag;

    sigemptyset(&catch);        
    sigaddset(&catch, SIGUSR1); 

    while(!feof(f)){
        fscanf(f,"%d", &d->a);
        fscanf(f,"%d", &d->b);
        printf("Thread 1 submitting : %d %d", d->a, d->b);
        pthread_kill(t2, SIGUSR1); // tell calculator() to start computations
        sleep(5);               // this is not necessary if confirmation signal from
                                // calculator() is used
        //sigwait(&catch, &flag); // if you need confirmation from calculator()
                                // then this can be enabled
    }
    return 0;
}

static void * calculator(void *data){


    pair_t *pa = (pair_t *)data;
    sigset_t catch;
    sigemptyset(&catch);
    sigaddset(&catch, SIGUSR1);
    int flag;

    while(1){
        sigwait(&catch, &flag);
        printf("Thread 2 calculated = %d\n",(pa->a + pa->b));
        sleep(5); // don't need if confirmation below is used
        //pthread_kill(t1, SIGUSR1); // enable this if confirmation 
                                     // from calculator is necessary
    }
    return 0;
}

int main(int argc, char *argv[]){ 
    f = fopen(argv[1],"r");
    if(f ==NULL){
        perror;
        exit(1); // better to return here, because without file nothing to process
    }
    // no need to check, f != NULL already
    //if(f !=NULL){                     
        pair_t * p;

        sigset_t set;
        sigemptyset(&set);
        sigaddset(&set, SIGUSR1);

        //pthread_t t1;
        //pthread_t t2;

        pthread_sigmask(SIG_BLOCK, &set, NULL);
        pthread_create(&t1, NULL, reader, (void *)&p);
        pthread_create(&t2, NULL, calculator, (void *)&p);


        pthread_join(t1, NULL);
        printf("Goodbye from Thread One");
        pthread_join(t2, NULL);
        printf("Goodbye from Thread Two");

        pthread_exit(NULL); 

    //}
    return 0;
}

此外,如果您使用的几个线程都不会阻塞某些信号,那么将此信号发送到进程会导致不可预测的行为 - 它们中的任何一个都可以捕获信号并唤醒,而其余的仍在等待信号。

于 2013-11-05T13:37:39.947 回答