[英]How can I tell a child process the pid of another child process in C?
I am writing a program in which I need to create two child processes, a producer and a consumer. 我正在编写一个程序,其中需要创建两个子进程,一个生产者和一个消费者。 The producer writes on a file what is read from stdin, the consumer reads the same file after the producer has written the line. 生产者在文件上写入从stdin读取的内容,生产者写完该行后,消费者读取相同的文件。 I need to synchronize the two processes and I wanted to do so by using signals, but I have now a problem in that I cannot send (using the kill() function) the signals from the consumer to the producer. 我需要同步两个进程,我想通过使用信号来实现同步,但是现在我有一个问题,就是我无法(使用kill()函数)将信号从消费者发送到生产者。
This is my program: 这是我的程序:
#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;
}
The exit(0) commands in both child processes are there because I still have to implement the exit condition for the loop. 两个子进程中的exit(0)命令都存在,因为我仍然必须实现循环的退出条件。 I am pretty sure that my problem lies in how I create the consumer process after creating the producer process. 我很确定我的问题在于创建生产者流程之后如何创建消费者流程。 That means that the producer sees the "consumer" pid to be 0, which terminates the program. 这意味着生产者看到“消费者” pid为0,这将终止程序。
Now, I would like to understand how I'm supposed to create two concurrent processes using the fork() function (if it's possible), can someone enlighten me? 现在,我想了解如何使用fork()函数创建两个并发进程(如果可能),有人可以启发我吗?
In the end I managed to solve the problem, but I had to use the temporary file in order to get the pid of the producer process when inside the consumer process. 最后,我设法解决了这个问题,但是我必须使用临时文件才能在使用方进程内部获取生成方进程的pid。 I was hoping to find a smarter way but the solution given by the course was basically the same. 我希望找到一种更聪明的方法,但是该课程给出的解决方案基本相同。
#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
}
Thanks for the comments 感谢您的评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.