写入的字节数始终为-1

时间:2016-07-01 15:18:12

标签: c arrays pipe byte fwrite

我正在尝试实现一些没有互斥锁,条件和信号量的IPC,只使用管道。 以下代码表示3个孩子中的父进程分叉(p11,p12,p13) 父母用SIGUSR2信号唤醒所有孩子,然后p11开始向管道写50个随机数。 p12和p13竞争性地从管道中读取并将它们得到的数字写入自己的文件(file12和file13)。第50个数字是-1,一旦孩子读数为-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 个答案:

答案 0 :(得分:1)

至少这个 * 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 BTW,编码pa[2]在任何情况下都会引发未定义的行为,因为它会读出pa第3个元素,pa只有两个元素。< / em>的