使用 fork 的 C 程序在用户输入后不会退出 'exit'
C- program using fork won't exit after user inputs 'exit'
我们的任务是用一个 C 代码创建双向通信模拟。这是我第一次涉足这种代码,所以我创建了这个简单的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include<wait.h>
int main(void)
{
pid_t pid;
char buf[1024];
char cp[50];
char ex[100]="exit";
int readpipe[2];
int writepipe[2];
long int a;
int b;
a=pipe(readpipe);
b=pipe(writepipe);
int test=1;
int length;
if (a == -1) { perror("pipe"); exit(EXIT_FAILURE); }
if (b == -1) { perror("pipe"); exit(EXIT_FAILURE); }
fflush(stdin);
pid=fork();
if(pid==-1)
{
printf("pid:main");
exit(1);
}
while(test==1)
{
if(pid==0)
{
close(readpipe[1]);
close(writepipe[0]);
if(read(readpipe[0],buf,sizeof(buf)) < 0)
{
exit(1);
}
printf("\nSEND TO USER 1:");
fflush(stdin);
fgets(cp, 50, stdin);
length = strlen(cp);
if(cp[length-1] == '\n') {
--length;
cp[length] = '[=11=]';
}
if(strcmp(cp,ex)==0) {
test=0;
break;
}
if(write(writepipe[1],cp,strlen(cp)+1) < 0)
{
exit(1);
}
}
else
{
close(readpipe[0]);
close(writepipe[1]);
printf("\nSEND TO USER 2:");
fflush(stdin);
fgets(cp, 50, stdin);
length = strlen(cp);
if(cp[length-1] == '\n') {
--length;
cp[length] = '[=11=]';
}
if(strcmp(cp,ex)==0) {
test=0;
break;
}
if(write(readpipe[1],cp,strlen(cp)+1) < 0)
{
exit(1);
}
if(read(writepipe[0],buf,sizeof(buf)) < 0)
{
exit(1);
}
}
}
close(readpipe[1]);
close(writepipe[0]);
close(readpipe[0]);
close(writepipe[1]);
return 0;
}
程序在用户 1 或用户 2 输入退出时终止。然而....
错误是每当我按退出时,它都会先打印 "SEND TO USER x" 然后继续退出。我怎样才能解决这个问题?有什么帮助吗?谢谢
当您在发送过程中键入 exit
时,它会跳出循环,关闭其管道末端并退出。
当其他进程尝试从管道读取时,它将得到一个 EOF,由 read()
返回 0
表示。但是您的代码从不检查这一点,它仅在 read()
或 write()
returns 为负值以指示错误时退出(EOF 不是错误)。所以它回到循环的顶部并要求输入发送到另一个进程。当它试图写入关闭的管道时,它会收到一个 EPIPE
错误或一个 SIGPIPE
信号,然后它会退出。
将您的阅读代码更改为:
int n = read(readpipe[0],buf,sizeof(buf));
if (n == 0) {
break;
} else if (n < 1) {
exit(1);
}
我们的任务是用一个 C 代码创建双向通信模拟。这是我第一次涉足这种代码,所以我创建了这个简单的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include<wait.h>
int main(void)
{
pid_t pid;
char buf[1024];
char cp[50];
char ex[100]="exit";
int readpipe[2];
int writepipe[2];
long int a;
int b;
a=pipe(readpipe);
b=pipe(writepipe);
int test=1;
int length;
if (a == -1) { perror("pipe"); exit(EXIT_FAILURE); }
if (b == -1) { perror("pipe"); exit(EXIT_FAILURE); }
fflush(stdin);
pid=fork();
if(pid==-1)
{
printf("pid:main");
exit(1);
}
while(test==1)
{
if(pid==0)
{
close(readpipe[1]);
close(writepipe[0]);
if(read(readpipe[0],buf,sizeof(buf)) < 0)
{
exit(1);
}
printf("\nSEND TO USER 1:");
fflush(stdin);
fgets(cp, 50, stdin);
length = strlen(cp);
if(cp[length-1] == '\n') {
--length;
cp[length] = '[=11=]';
}
if(strcmp(cp,ex)==0) {
test=0;
break;
}
if(write(writepipe[1],cp,strlen(cp)+1) < 0)
{
exit(1);
}
}
else
{
close(readpipe[0]);
close(writepipe[1]);
printf("\nSEND TO USER 2:");
fflush(stdin);
fgets(cp, 50, stdin);
length = strlen(cp);
if(cp[length-1] == '\n') {
--length;
cp[length] = '[=11=]';
}
if(strcmp(cp,ex)==0) {
test=0;
break;
}
if(write(readpipe[1],cp,strlen(cp)+1) < 0)
{
exit(1);
}
if(read(writepipe[0],buf,sizeof(buf)) < 0)
{
exit(1);
}
}
}
close(readpipe[1]);
close(writepipe[0]);
close(readpipe[0]);
close(writepipe[1]);
return 0;
}
程序在用户 1 或用户 2 输入退出时终止。然而....
错误是每当我按退出时,它都会先打印 "SEND TO USER x" 然后继续退出。我怎样才能解决这个问题?有什么帮助吗?谢谢
当您在发送过程中键入 exit
时,它会跳出循环,关闭其管道末端并退出。
当其他进程尝试从管道读取时,它将得到一个 EOF,由 read()
返回 0
表示。但是您的代码从不检查这一点,它仅在 read()
或 write()
returns 为负值以指示错误时退出(EOF 不是错误)。所以它回到循环的顶部并要求输入发送到另一个进程。当它试图写入关闭的管道时,它会收到一个 EPIPE
错误或一个 SIGPIPE
信号,然后它会退出。
将您的阅读代码更改为:
int n = read(readpipe[0],buf,sizeof(buf));
if (n == 0) {
break;
} else if (n < 1) {
exit(1);
}