gpt4 book ai didi

c - 从管道读取时无限循环

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:22 26 4
gpt4 key购买 nike

谁能告诉我为什么从管道读取时会出现无限循环?我不明白我做错了什么。 message 是我在main top 中定义的名字引用它。 MESSAGE 是我的结构名称。它甚至不打印出测试值。

if(manager_pid == 0)
{
printf("Hello? \n");
if(close(pipe1[READING]) != 0)
{
printf("Error in closing pipe1 \n");
}
if(close(pipe2[READING]) != 0)
{
printf("Error in closing pipe2 \n");
}
if(close(pipe3[WRITING]) != 0)
{
printf("Error in closing pipe \n");
}
}
i = 0;
printf("work please \n");
//test_value = read(pipe3[READING], &boo, sizeof(echo));
//printf("test_value is %d \n", test_value);

while(i < 10)
{
printf("In while \n");
//printf("Hello?? \n");
//test_value = read(pipe3[READING], &boo, sizeof(echo));
//printf("test_value is %d \n", test_value);
//printf("Entering infinite loop \n");
//printf("i is %d \n", i);
//nbytes = read(pipe3[0], array, 45);
//printf("nbytes is %d \n", nbytes);
//log_dat_fp = fopen(argv[2], "a");
if(read(pipe3[READING], &message, sizeof(struct MESSAGE)) != -1)
{
printf("Entering if \n");
log_dat_fp = fopen(argv[2], "a");
printf("First if \n");
time(&current_time);
//if(message.owner == 1 && (message.instruction == 'r' || message.instruction == 'R'))
if(message.instruction == 'r' || message.instruction == 'R')
{
if(message.owner == 1)
{
printf("message.owner == 1 with r or R \n");
fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s", strtok(ctime(&current_time), "\n"),
message.owner, getpid(), message.instruction, message.id);
pclose(log_dat_fp);
}
else if(message.owner == 2)
{
printf("message.owner == 2 with r or R \n");
fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s", strtok(ctime(&current_time), "\n"),
message.owner, getpid(), message.instruction, message.id);
pclose(log_dat_fp);
}
else
{
printf("You have junk \n");
}
}
else if(message.instruction == 'u' || message.instruction == 'U')
{
if(message.owner == 1)
{
printf("message.owner == 1 with u or U \n");
fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s %d", strtok(ctime(&current_time), "\n"),
message.owner, getpid(), message.instruction, message.id, message.value);
pclose(log_dat_fp);
}
else if(message.owner == 2)
{
printf("message.owner == 2 with u or U \n");
fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s %d", strtok(ctime(&current_time), "\n"),
message.owner, getpid(), message.instruction, message.id, message.value);
pclose(log_dat_fp);
}
else
{
printf("You have junk \n");
}
}
else
{
printf("manager can't read from pipe\n");
exit(1);
} // read no good
if(message.instruction == 'r' || message.instruction == 'R')
{
if(message.owner == 1)
{
for(i = 0; i < 200; i++)
{
if(strcmp(storage, table[i].id) == 0)
{
match_flag = 1;
value = table[i].value;
}
}
if(match_flag == 1)
{
message.value = value;
message.owner = 0;
if(write(pipe1[WRITING], &message, sizeof(struct MESSAGE)) == sizeof(struct MESSAGE))
{
log_dat_fp = fopen(argv[2], "a");
time(&current_time);
fprintf(log_dat_fp, "Store Manager at time: %s sent message: %c %d %s %d\n", strtok(ctime(&current_time), "\n"),
message.instruction, message.owner, message.id, message.value);
fclose(log_dat_fp);
}
else
{
printf("error returning message to process 1");
exit(1);
}
}
else
message.owner = 1;
if(write(pipe1[WRITING], &message, sizeof(struct MESSAGE)) == sizeof(struct MESSAGE))
{
log_dat_fp = fopen(argv[2], "a");
time(&current_time);
fprintf(log_dat_fp, "Store Manager at time: %s sent message: %c %d %s \n", strtok(ctime(&current_time), "\n"),
message.instruction, message.owner, message.id);
fclose(log_dat_fp);
}

}
else if(message.owner == 2)
{
printf("message.owner == 2 with u or U \n");
fprintf(log_dat_fp, "Store Manager at time: %s received message %d %d %c %s %d", strtok(ctime(&current_time), "\n"),
message.owner, getpid(), message.instruction, message.id, message.value);
pclose(log_dat_fp);
}
else
{
printf("You have junk \n");
}

}
}
else
{
printf("manager had pipe issues.\n");
exit(1);
}// read no good
i++;
//log_dat_fp = fopen(argv[2], "a");
printf("Each pass \n");
}

最佳答案

POSIX read()函数在 EOF 时返回 0,而不是 -1。由于您测试了错误的条件,因此会陷入无限循环。

请注意描述中说:

When attempting to read from an empty pipe or FIFO:

  • If no process has the pipe open for writing, read() shall return 0 to indicate end-of-file.

  • If some process has the pipe open for writing and O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].

  • If some process has the pipe open for writing and O_NONBLOCK is clear, read() shall block the calling thread until some data is written or the pipe is closed by all processes that had the pipe open for writing.

读取零字节因为没有字节可读是成功的。这就是 read() 指示 EOF 的方式,无论它是在文件上还是在任何其他设备类型上。终端是一个特例;它们可能会在您键入(在 Unix 上)control-D 后返回 0 字节,然后重试可能会返回在此之后键入的额外数据。很久以前,磁带驱动器有些相似。但是当 read() 返回 0 时,(暂时)没有更多数据可读。对于管道,这意味着所有写入器都关闭了他们的写入文件描述符。

关于c - 从管道读取时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com