c - 写入的字节数始终为-1

标签 c arrays pipe byte fwrite

我正在尝试实现一些没有互斥体、条件和信号量的 IPC,仅使用管道。 以下代码表示父进程 fork 3 个子进程 (p11,p12,p13) 父进程用 SIGUSR2 信号唤醒所有子进程,然后 p11 开始向管道写入 50 个随机数。 p12 和 p13 竞争性地从管道中读取数据并将获得的数字写入自己的文件(file12 和 file13)。第 50 个数字是 -1,一旦 children 读到 -1,他们就会退出。 p11子进程中使用的write,只是不写入单个字节,它返回-1 我只能使用 write(2)read(2) 调用进行写入和读取。 我好像无法实现,尝试了好几次都没有结果。希望你能帮助我。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
void handler(int signo)
{
    printf("\nSignal intercepted!\n");
}

void main ()
{
srand(time(NULL));
signal(SIGUSR2,handler);
int pa[2];
pipe(pa[2]); //create pipe
pid_t pid1,pid2,pid3;
if((pid1=fork())==0)  //child 11
    {
        close(pa[0]); //close the read side of the pipe
        pause();
        int i,nwrite,num;
        for(i=0;i<49;i++)
            {

            num=rand()%100+1; //generate a random number
            nwrite=write(pa[1],&num,sizeof(int)); //write it to the  pipe
            if(nwrite==-1) //if there's a write error
                {
                printf("\nWrite error..\n");
                exit(-1);
                }
            }
        num=-1; //generate the last number
        nwrite=(write(pa[1],&num,sizeof(int))); //write the last number to the pipe
        if (nwrite==-1) //if there's a write error
            {
            printf("\nError,now exiting...\n");
            exit(-1);
            }   
        close(pa[1]); //close the pipe in write mode
        exit(1); 
    }

else if ((pid2=fork())==0) //child 12
    {

    close(pa[1]); //close the write side of the pipe
    pause();
    int fd1,nread,num;
    fd1=open("file12.txt",O_CREAT,0777); //create a new file
    while((nread=read(pa[0],&num,sizeof(num)))>0)  //while there are bytes to read
        {
            if(num!=-1) //if this isn't the last number
                {
            write(fd1,&num,sizeof(num)); //write it to the file
                }
            else
                {   
                printf("\n-1 sent!\n"); //notify the last read number
                close(pa[0]); //close the read side of the pipe
                    close(fd1); //close the file descriptor
                    exit(1); //exit
                    }
            }   
        }



    else if ((pid3=fork())==0) //child 13, same as 12
        {
            close(pa[1]);   
            pause();
            int fd2,nread,num;
            fd2=open("file13.txt",O_CREAT,0777);

            while((nread=read(pa[0],&num,sizeof(num)))>0)
                {
                    if(num!=-1)
                    {
                            write(fd2,&num,sizeof(num));
                    }
                    else
                    {
                        printf("\n-1 sent!\n");
                        close(pa[0]);
                        close(fd2);
                        exit(1);
                    }
                }   
        }




else //parent
    {   
        sleep(1);
        kill(pid1,SIGUSR2); //wake up all the childs
        kill(pid2,SIGUSR2);
        kill(pid3,SIGUSR2);
        waitpid(pid1,0,NULL); //wait for the childs to end
        waitpid(pid2,0,NULL);
        waitpid(pid3,0,NULL);
        printf("\nDone, now exiting...\n"); //exit
    }

}

最佳答案

至少这个*1

int pa[2];
pipe(pa[2]);

应该是

int pa[2];
pipe(pa);

背景:

pa[2] 计算结果为 int。另一方面,pipe() 需要一个 int[2],它在定义函数参数的上下文中与 int[] 相同> 与int * 相同。

如果将数组传递给函数,它将衰减为其第一个元素的指针。因此,将 pa 传递给 pipe() 将导致传递 &pa[0],它确实是一个 int * .

<小时/>

*1 顺便说一句,编码 pa[2] 在任何情况下都会引发未定义的行为,因为它读出 pa3rd 元素,其中 pa 只有两个元素。

关于c - 写入的字节数始终为-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38148886/

相关文章:

我可以使用 2 种语言制作 1 个程序吗?

c - 在 BST 中找到添加到 k 的节点对

javascript - 有没有一种方法可以在 jQuery 中克隆数组?

javascript - 禁用无法读取未定义的属性

bash - 傻瓜/awk : piping date to getline *sometimes* won't work

c - 有什么办法可以查看 stdin 缓冲区吗?

c - 来自不相关 block 的指针的比较(>,> =,<,<=)

c - 带浮子的管道

c - fork() 在父进程和子进程之间切换

c - 矩阵加法程序。如果我输入一个换行符,它会跳过一行并进入第三行