如何告诉孩子处理C中另一个子进程的pid?

时间:2015-08-17 16:43:42

标签: c parent-child

我正在编写一个程序,我需要创建两个子进程,一个生产者和一个消费者。生产者在文件上写入从stdin读取的内容,消费者在生成者写入该行之后读取相同的文件。我需要同步这两个进程,我想通过使用信号来实现,但我现在遇到的问题是我无法发送(使用kill()函数)来自消费者的信号给生产者。

这是我的计划:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void catcherp(){};
void catcherc(){};

pid_t producer, consumer;

int main () {
int status_consumer, status_producer;
char string[128], reading[128];
FILE * fout, *finn;


producer = fork();
if (producer == 0){
    signal(SIGUSR2, catcherp);
    // producer process, child
    while(1){
        fout = fopen ("test.txt", "w");
        printf ("?: ");
        scanf ("%s", string);
        fputs(string, fout);        
        fclose(fout);

        kill(consumer, SIGUSR1);
        pause();
    }

    exit(0);
}   else {
    // parent process
    consumer = fork ();
    if (consumer == 0) { 
        signal(SIGUSR1, catcherc);
        // consumer process, child
        while(1) {              
            pause();
            finn = fopen ("test.txt", "r");
            fgets(reading, 128, finn);
            printf("%s\n", reading);
            fclose(finn);               

            kill (producer, SIGUSR2);

        }
        exit(0);
    } else {
        printf("This is the parent process\n");
        waitpid(producer, &status_producer, 0);
        waitpid(consumer, &status_consumer, 0);
        printf("The children exited\n");
    }
}
return 0;
}

两个子进程中的exit(0)命令都在那里,因为我仍然需要为循环实现退出条件。我很确定我的问题在于如何在创建生产者流程后创建使用者流程。这意味着制作人会看到&#34;消费者&#34; pid为0,终止程序。

现在,我想了解我应该如何使用fork()函数创建两个并发进程(如果可能的话),有人可以启发我吗?

1 个答案:

答案 0 :(得分:0)

最后我设法解决了这个问题,但是我必须使用临时文件才能在使用者进程内部获取生产者进程的pid。 我希望找到一种更聪明的方法,但课程给出的解决方案基本相同。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

void catcher(){};

int main () {
    int status_consumer, status_producer; // needed for the waitpid functions
    pid_t producer, consumer; // pids of the child processes
    char string[128]; // input string
    FILE * f; // fp

    signal (SIGUSR1, catcher);

    consumer = fork();
    if (consumer == 0) { 
        // child process
        while (1) {     
            pause(); // wait for the ready signal from the sender
            f = fopen ("tmp.txt", "r");
            fscanf (f, "%d %s", &producer, string); // read string and the pid of the sender
            printf("%s\n", string);   
            fclose(f); 

            if (strcmp("end", string) == 0) {
                break; // exit the loop when the word "end" is read
            }
            kill (producer, SIGUSR1);       
        }

        exit(0);
    } else {
        producer = fork (); 
        if (producer == 0) {
            // child process
            while(1) {      
                f = fopen ("tmp.txt", "w");
                printf("?: ");
                scanf("%s", string); // read from stdin the string
                fprintf(f, "%d\t%s\n", getpid(), string); // write on tmp.txt the string
                fclose(f);

                kill(consumer, SIGUSR1);
                if (strcmp("end", string) == 0){
                    break; // exit the loop when the word "end" is read
                }           
                pause();
            }
        } else {
            // parent process
            waitpid(producer, &status_producer, 0);
            waitpid(consumer, &status_consumer, 0);
        }

    }
     return 0;  // end of program
 }

感谢您的评论