为什么这个带有pipe()的C代码突然停止了?

为什么这个带有pipe()的C代码突然停止了?,c,C,我是一名计算机科学专业的学生,我在附近有一个“操作系统”考试,当我根据老师以前的考试记录做一些练习时,我发现自己被困在这个考试上了 跟踪总结如下: 用两个输入文件(input.txt和out.txt)编写一个C程序,该程序必须创建两个子进程,第一个子进程将从inputfile.txt一次读取两个字符,并且仅当字母相等时才将它们传递给子进程n*2!然后将继续将它们打印到out.txt 我还没有做平等的部分,因为我在撞车的时候被卡在了第一个上面。 我做了几次尝试,但在V:55上的第一个while序列

我是一名计算机科学专业的学生,我在附近有一个“操作系统”考试,当我根据老师以前的考试记录做一些练习时,我发现自己被困在这个考试上了

跟踪总结如下:

用两个输入文件(input.txt和out.txt)编写一个C程序,该程序必须创建两个子进程,第一个子进程将从inputfile.txt一次读取两个字符,并且仅当字母相等时才将它们传递给子进程n*2!然后将继续将它们打印到out.txt

我还没有做平等的部分,因为我在撞车的时候被卡在了第一个上面。 我做了几次尝试,但在V:55上的第一个while序列之后,它们总是“冻结”,我不明白为什么cicle是错误的(?)

以下是我的尝试:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdbool.h>
#include <signal.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

#define MAX 128
#define argDef 3

int checkArgc(int argc);

int main(int argc, char **argv) {
    int pipe1[2], pipe2[2], nbytes, filedesc, pid1, pid2;
    char duelettere[2];
    char soluzione[MAX];
    if (!checkArgc(argc)) {
        printf("Numero di file insufficente!\a\n");
        exit(1);
    }
    printf("Qui tutto bene\n");
    /* Apertura file di input.txt */
    if ((filedesc = open(argv[1], O_RDONLY)) < 0) {
        perror("Errore durante l'apertura dei file.txt \a\n");
        exit(1);
    }
    printf("filedesc : %d\n", filedesc);
    if (pipe(pipe1) < 0) {
        perror("Errore durante la creazione della prima PIPE! \a\n");
        exit(1);
    }
    printf("pipe1 : %p\n", pipe1);
    if (pipe(pipe2) < 0) {
        perror("Errore durante la creazione della seconda PIPE! \a\n");
        exit(1);
    }
    printf("pipe2 : %p\n", pipe2);
    if ((pid1 = fork() < 0)) {
        perror("Errore creazione primo processo figlio \a\n");
        exit(1);
    }
    if (pid1 == 0) { /* Primo figlio */
        printf("pid1 : %d\n", pid1);
        close(pipe1[0]); /* Chiusura canale in entrata, per poter scrivere */
        while ((nbytes=read(filedesc, duelettere, 2)) == 2) {
            write(pipe1[1], duelettere, 2);
        }
        close(pipe1[1]); /* Chiudiamo la PIPE che stava scrivendo */
        close(filedesc);
    } else { /* Siamo nel processo padre, e lanceremo il figlio n^2 */
        close(pipe1[1]);
    }
    if (pid2 = fork() < 0) {
        perror("Errore creazione secondo figlio\a\n");
        exit(1);
    }
    if (pid2 == 0) {
        printf("pid2 : %d", pid2);
        close(pipe1[1]);
        close(pipe2[0]);
        nbytes = read(pipe1[0], soluzione, strlen(soluzione));
        nbytes = write("out.txt", soluzione, strlen(soluzione));
    } else { /* Codice del padre */
        close(pipe2[1]);
        /* Codice finale del padre */
        printf("Siamo arrivati nel padrino parte seconda\n");
    }
    waitpid(pid1, NULL, 0);
    waitpid(pid2, NULL, 0);
    return 0;
}

int checkArgc(int argc) {
    if (argc != argDef) {
        return 0;
    } else {
        return 1;
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大值128
#定义argdef3
int checkArgc(int argc);
int main(int argc,字符**argv){
int pipe1[2],pipe2[2],n字节,filedesc,pid1,pid2;
char Duelette[2];
char soluzione[MAX];
如果(!checkArgc(argc)){
printf(“不完整的数字di文件!\a\n”);
出口(1);
}
printf(“Qui tutto bene\n”);
/*Apertura文件di input.txt*/
如果((filedesc=open(argv[1],ordonly))<0){
perror(“Errore durante l'apertura dei file.txt\a\n”);
出口(1);
}
printf(“filedesc:%d\n”,filedesc);
如果(管道(管道1)<0){
perror(“主管道错误!\a\n”);
出口(1);
}
printf(“管道1:%p\n”,管道1);
如果(管道(管道2)<0){
perror(“第二管道错误!\a\n”);
出口(1);
}
printf(“pipe2:%p\n”,pipe2);
if((pid1=fork()<0)){
perror(“错误处理程序错误”);
出口(1);
}
如果(pid1==0){/*Primo-figlio*/
printf(“pid1:%d\n”,pid1);
关闭(管道1[0]);/*根据poter scrivere,位于恩特拉的Chiusura运河*/
而((nbytes=read(filedesc,duelette,2))==2){
书写(管道1[1],双字母,2);
}
关闭(管道1[1]);/*Chiudiamo la PIPE che stava scrivendo*/
关闭(filedesc);
}除此之外{/*Siamo nel processo padre,e LANCERMO il figlio n^2*/
关闭(管道1[1]);
}
if(pid2=fork()<0){
perror(“第二个figlio错误”);
出口(1);
}
如果(pid2==0){
printf(“pid2:%d”,pid2);
关闭(管道1[1]);
关闭(管道2[0]);
nbytes=读取(pipe1[0],索鲁津酮,斯特伦(索鲁津酮));
nbytes=写入(“out.txt”,soluzione,strlen(soluzione));
}else{/*Codice del padre*/
关闭(管道2[1]);
/*帕德雷大结局*/
printf(“第二部分的到达时间”);
}
waitpid(pid1,NULL,0);
waitpid(pid2,NULL,0);
返回0;
}
int checkArgc(int argc){
如果(argc!=argDef){
返回0;
}否则{
返回1;
}
}
我道歉,
有些变量上有意大利名字,因为我是意大利人。

可能还有其他问题,但这两行绝对不正确:

    if ((pid1 = fork() < 0))
    ...
    if (pid2 = fork() < 0) {
if((pid1=fork()<0))
...
if(pid2=fork()<0){
您应该将其更改为:

    if ((pid1 = fork()) < 0)
    ...
    if ((pid2 = fork()) < 0) {
if((pid1=fork())<0)
...
如果((pid2=fork())<0){

可能还有其他问题,但这两行绝对不正确:

    if ((pid1 = fork() < 0))
    ...
    if (pid2 = fork() < 0) {
if((pid1=fork()<0))
...
if(pid2=fork()<0){
您应该将其更改为:

    if ((pid1 = fork()) < 0)
    ...
    if ((pid2 = fork()) < 0) {
if((pid1=fork())<0)
...
如果((pid2=fork())<0){

欢迎使用堆栈溢出。
pipe1
pipe2
数组需要包含两个元素,而不是一个,它应该是
int-pipe1[2],pipe2[2]
read(filedesc,duelette,2)
:您正在将两个字符读入大小为1的数组中。您的示例未编译,缺少一些大括号
{
。尝试正确缩进所有内容,并始终将右大括号放在新行上。
nbytes=read(pipe1[0],soluzione,strlen(duelette))
看起来也是错误的。此时,
duelette
似乎没有包含以nul结尾的字符串,即使它包含,我也不明白为什么该字符串的长度应该与您要读取的字符数相对应。
if(filedesc=open(argv[1],O_RDONLY)<0)
是错误的。它应该是
if((filedesc=open(argv[1],ordonly))<0)
if(pid1=fork()<0)
if(pid2=fork()<0)
。欢迎使用堆栈溢出。
pipe1
pipe2
数组需要包含两个元素,而不是一个,它应该是
int-pipep1[2],pipep2[2]
read(filedesc,duelettere,2)
:您正在将2个字符读入大小为1的数组。您的示例未编译,缺少一些大括号
{}
。请尝试正确缩进所有内容,并始终将结束大括号放在新行。
nbytes=read(pipe1[0],soluzione,strlen(duelette))
看起来也是错误的。此时,
duelette
似乎没有包含以nul结尾的字符串,即使它包含,我也不明白为什么该字符串的长度应该与您要读取的字符数相对应。
if(filedesc=open(argv[1],O_RDONLY)<0)
是错误的。它应该是
if((filedesc=open(argv[1],ordonly))<0)
。对于
if(pid1=fork()<0)
if(pid2=fork()<0)也一样
。感谢您让我注意到这一点,我在if案例中做了很长时间的大括号错误…至少以后会更好!感谢您让我注意到这一点,我在if案例中做了很长时间的大括号错误…至少是bette