对于这个C项目,我试图使用管道来使父进程与子进程通信。该子级应该从txt文件(包含实数)中读取行(每秒一次),并使用管道来提供父级,而父级又应从管道中读取并将这些数字写入日志文件。但是,父进程仅读取0.000000的序列。这是我的代码:
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <wait.h>
#define MAX_I 5
#define delay 1
void read_input(FILE *fp);
FILE *file_log;
int status;
pid_t pid;
int pipeFD[2];
int main(int argc, char **argv) {
//initialize
FILE *fp;
if (argc != 2){
if ((fp = fopen("input.txt", "r")) == NULL) {
printf("Error! opening file");
exit(1);}
}
else{
fp = fopen(argv[1], "r");
}
if (pipe(pipeFD) == -1){ /* creates a pipe */
fprintf(stderr, "\nERROR: pipe() failed\n");
exit(1);}
file_log=fopen("file.log","w+"); /*open the log file*/
pid=fork();
if(pid==0) {//child process
printf ("%d starts \n", getpid());
close(pipeFD[0]);//close read end for child
read_input(fp);
return 0;
exit(status);
}else{//parent
double speed_read;
close(pipeFD[1]);//close write end for parent
while(1){
if (read(pipeFD[0], &speed_read, sizeof(speed_read)) >0){
if (speed_read<0)
break;
fprintf(file_log, "%f \n", speed_read);
printf("process %d received %f from child \n",getpid(),speed_read);
}else
printf("Nothing there to read \n");
}
printf("parent ended \n");
wait(&status);
fclose(fp);
fclose(file_log);
}
return 0;
}
void read_input(FILE *fp){
char *line = NULL;
double speed;
int i=0; size_t len = 0; double exit_sign =-10.0;
while(getline(&line, &len, fp) != -1) {
speed=atof(line);
i++;
if(i>MAX_I){//reads up to MAX_I rows of input
printf("I'll send the exit sign to parent now\n");
write(pipeFD[1], &exit_sign, sizeof(double));
free(line);
break;
}
if(write(pipeFD[1], &speed, sizeof(double)>0)){
printf("%d at %d wrote that speed is %f\n",getpid(), i,speed);
}else{printf("Write on pipe failed\n");}
sleep(delay);
}
free(line);
}
这里是打印内容:
15032开始
15032 at 1写道速度为0.345670
处理15031从孩子那里收到0.000000
15032 at 2写道速度为12.678890
处理15031从孩子那里收到0.000000
15032 at 3写道速度为34.789870
处理15031从孩子那里收到0.000000
15032 at 4写道速度为0.000000
处理15031从孩子那里收到0.000000
15032在5写速度为12.009288
处理15031从孩子那里收到0.000000
我现在将出口标志发送给父母
父级结束
同样,日志文件包含以下内容:
0.000000
0.000000
0.000000
0.000000
0.000000
在read_input行中:
if(write(pipeFD[1], &speed, sizeof(double)>0)){
必须
if(write(pipeFD[1], &speed, sizeof(double))>0){
在您的情况下,您不写sizeof(double)
个字节,只写1个]
注意read_input
两次执行free(line);
,但行为未定义,当if(i>MAX_I){
时必须删除一次。>更正,编译和执行后:
。如果您不这样做,那么如果输入文件少于MAX_I行],则父级永远不会停止写Nothing to readpi@raspberrypi:/tmp $ gcc -g -Wall p.c pi@raspberrypi:/tmp $ cat input.txt 1.2 2.3 3.45 7.8 9.12 12.345 pi@raspberrypi:/tmp $ ./a.out 15121 starts 15121 at 1 wrote that speed is 1.200000 process 15120 received 1.200000 from child 15121 at 2 wrote that speed is 2.300000 process 15120 received 2.300000 from child 15121 at 3 wrote that speed is 3.450000 process 15120 received 3.450000 from child 15121 at 4 wrote that speed is 7.800000 process 15120 received 7.800000 from child 15121 at 5 wrote that speed is 9.120000 process 15120 received 9.120000 from child I'll send the exit sign to parent now parent ended pi@raspberrypi:/tmp $ cat file.log 1.200000 2.300000 3.450000 7.800000 9.120000 pi@raspberrypi:/tmp $
此外,当您检测到EOF并执行
中输入文件的末尾时,也可以发送exit_signprintf("Nothing there to read \n");
也执行break;
以完成while
时,效果似乎更好。请注意,当您到达read_input