Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Fork和Pipe程序工作不正常_C_Unix_Pipe_Fork_Child Process - Fatal编程技术网

C Fork和Pipe程序工作不正常

C Fork和Pipe程序工作不正常,c,unix,pipe,fork,child-process,C,Unix,Pipe,Fork,Child Process,我试图编写一个简单的fork()和pipe()程序,它从用户那里获取输入,并通过childprocess中的管道输出。但不知何故,我没有得到我想要的结果 第一个输入工作正常,例如,我输入“Hi”并得到结果“Prozessmanager:Hi”,但一旦我尝试输入下一个字符串,它就会崩溃或以某种方式停止工作,直到我输入退出程序的“Q” 我试着调试它,得到的结果是,我试图写入一个破裂的管道 #include <stdio.h> #include <stdlib.h&g

我试图编写一个简单的fork()和pipe()程序,它从用户那里获取输入,并通过childprocess中的管道输出。但不知何故,我没有得到我想要的结果

第一个输入工作正常,例如,我输入“Hi”并得到结果“Prozessmanager:Hi”,但一旦我尝试输入下一个字符串,它就会崩溃或以某种方式停止工作,直到我输入退出程序的“Q”

我试着调试它,得到的结果是,我试图写入一个破裂的管道

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    //#include <signal.h>

    #define BUFSIZE 512
    //int clock = 0;

    //void handler(int signum) {
    //clock++;
    //printf("Clock: 00:00:%d\n", clock);
    //}

    int main(int argc, char** argv) {
        pid_t prozessmanager;
        pid_t reporter;
        int pipefd[2];
        int status;
        char str[BUFSIZE];
        char buf[BUFSIZE];


        if ((pipe(pipefd)) == -1) {
            perror("pipe() error");
            exit(1);
        }

        if ((prozessmanager = fork()) == -1) {
            perror("fork() error");
            exit(1);
        } else if (prozessmanager == 0) { //Prozessmanager prozess
            while (1) {
                //signal(SIGALRM, handler);
                //while(1){
                //alarm(1);
                //}
                close(pipefd[1]);
                read(pipefd[0], buf, sizeof (buf));
                /*if (*buf == 'S') {
                    //handler(clock);
                } else {*/
                if (*buf == 'P') {
                    if ((reporter = fork()) == -1) {
                        perror("fork() error");
                        exit(1);
                    } else if (reporter == 0) { //Reporter prozess
                        printf("Im a Report Prozess, PID: %d\n", getpid());
                        exit(0);
                    } else { //Prozessmanager waits for Reporter
                        wait(&status);
                        printf("Report terminated, PID: %d\n", reporter);
                        break;
                    }
                } else if (*buf == 'Q') {
                    printf("Exit Prozessmanager, PID: %d\n", getpid());
                    exit(0);
                } else {
                    printf("Prozessmanager: %s", buf);
                    break;
                }
                //}
            }
        } else { //Kommandant prozess
            while (1) {
                close(pipefd[0]);
                fgets(str, 80, stdin);
                write(pipefd[1], str, sizeof (str));
                if (*str == 'Q') {
                    break;
                }
            }
            wait(&status);
            printf("Exit Kommandant, PID: %d\n", getpid());
            exit(0);
        }
        return (0);
    }
#包括
#包括
#包括
#包括
#包括
//#包括
#定义BUFSIZE 512
//int时钟=0;
//无效处理程序(int-signum){
//时钟++;
//printf(“时钟:00:00:%d\n”,时钟);
//}
int main(int argc,字符**argv){
项目经理;
pid_t reporter;
int-pipefd[2];
智力状态;
char-str[BUFSIZE];
字符buf[BUFSIZE];
如果((管道(管道FD))=-1){
perror(“管道()错误”);
出口(1);
}
如果((prozessmanager=fork())==-1){
perror(“fork()错误”);
出口(1);
}如果(prozessmanager==0){//prozessmanager prozess
而(1){
//信号机(信号机、处理器);
//而(1){
//警报(1);
//}
关闭(pipefd[1]);
读取(pipefd[0],buf,sizeof(buf));
/*如果(*buf=='S'){
//处理器(时钟);
}否则{*/
如果(*buf=='P'){
如果((reporter=fork())=-1){
perror(“fork()错误”);
出口(1);
}else如果(reporter==0){//reporter prozess
printf(“我是一个报告程序,PID:%d\n”,getpid());
出口(0);
}否则{//Prozessmanager等待记者
等待(&状态);
printf(“报告终止,PID:%d\n”,报告者);
打破
}
}else if(*buf=='Q'){
printf(“退出Prozessmanager,PID:%d\n”,getpid());
出口(0);
}否则{
printf(“项目经理:%s”,buf);
打破
}
//}
}
}else{//Kommandant prozess
而(1){
关闭(pipefd[0]);
fgets(str,80,stdin);
写入(pipefd[1],str,sizeof(str));
如果(*str=='Q'){
打破
}
}
等待(&状态);
printf(“退出Kommandant,PID:%d\n”,getpid());
出口(0);
}
返回(0);
}

您不知道读取了多少字节,因为您放弃了
read()
中的返回值。这总是个坏主意;您必须检查从输入函数中获得的内容。您还需要知道发送给您的字节数。您还需要能够检测管道何时关闭;
read()
的返回值也会告诉您这一点。写作也是如此。即使只有三个字节的输入,也会发送整个缓冲区。请注意,如果您将
非常特别的
写入输入,您的输入循环将终止。Thx对于您的回复,我如何确切地检查read的返回值。我阅读了手册页,并尝试了类似if(read==-1)->error的方法。但不知怎的,它跳过了它,因为read不是-1
intnbytes=read(…);if(nbytes==-1){…错误…}else if(nbytes==0){…EOF…}else{…处理一个或多个字节…}