命名为Piped C问题

时间:2015-12-24 12:55:22

标签: c linux pipe

我有这个C代码:

   #define BUFSIZE 256
    int main ( int argc, char *argv[])
    {
        int fdIn;
        int fdOut;


        if( argc != 3)
        {
            perror("Error argument");
            exit(1);
        }
        printf("Input Pipe\n");
        if( (fdIn = open(argv[1], O_RDONLY ) )<0)
        {
            perror("Error opening input pipe");
            exit(1);
        }
        printf("Output Pipe\n");
        if( (fdOut = open(argv[2], O_WRONLY ) )<0)
        {
            perror("Error opening output pipe");
            exit(1);
        }

        int c = 2;
        printf("Start cicle\n");
        while(c--)
        {
            char var1[BUFSIZE];
            char var2[BUFSIZE];
            char string[100];

            memset(var1, 0, sizeof(var1));
            memset(var2, 0, sizeof(var2));
            memset(string, 0, sizeof(string));


            if( readLine(fdIn, var1, sizeof(var1)) == 0)
            {
                printf("exit \n");
                exit(0);
            }

            if( readLine(fdIn, var2, sizeof(var2)) == 0)
            {
                printf("exit \n");
                exit(0);
            }



            if( atoi(var2) != 0){
                if( atoi(var1) == 0  || (atoi(var1) % atoi(var2)) == 0 )
                    sprintf(string,"ok\n");
                else
                    sprintf(string,"no\n");

            }

            printf("%s", string);
            writeLine(fdOut, string, strlen(string));
        }
        close(fdOut);
        close(fdIn);
        exit(0);
    }

代码中的函数:

int readLine( int fd, char* str, int bufferSize)
{
    return readToDel(fd, '\n', str, bufferSize);
}

int readToDel( int fd, char delimiter, char* str, int bufferSize)
{
    int n;
    int byteLetti =0;
    int index=0;


    do /* Read characters until NULL or end-of-input */
    {

        if( (n = read (fd, str+index, 1)) < 0)
        {
            perror("Error descriptor\n");
            exit(1);
        }
        byteLetti+=n;


    }
    while (n > 0 && *(str+index++) != delimiter && index < bufferSize);


    return byteLetti; /* Return false if end-of-input */
}
int writeLine(int fd, char* buffer, int bufferSize)
{
    if( write (fd, buffer, bufferSize) < 0)
    {
        perror("Error");
        exit(1);
    }
}

我的问题是如何使用这些管道,我将解释:

我输入2个管道(输入程序是2个绝对管道的路径),其中一个管道是输入,另一个是输出。 在输入管道中有一些数据(特别是两个数字),并且在详细说明之后,我必须在输出管道中写入“ok”或“no”。 出于这个原因,通过终端,我做了echo "4\n2\n" > input所以我在输入管道中写了两个数字。 然后我以这种方式执行程序:

./program inputPipeAbsolutePath outputPipeAbsolutePath

但程序在第{if 1}行之前阻塞了第三句if语句,我不知道为什么。 怎么了?

我在Mac上测试了它

UPDATE 没有人可以帮助我?

1 个答案:

答案 0 :(得分:0)

  

程序在打开输出管道时阻塞,我不知道   如何表现。

该程序的行为与记录一致;见man fifo

       Normally, opening the FIFO blocks until the other end is opened also.

因此,您可以选择启动一个从输出管道中读取的程序(例如cat;然后你的程序将继续进行 另一种选择是使用O_RDWR而不是O_WRONLY:

       Under Linux, opening a FIFO for read and write will succeed both in
       blocking and nonblocking mode.  POSIX leaves this behavior undefined.
       This can be used to open a FIFO for writing while there are no
       readers available.