简体   繁体   English

这根管怎么了?

[英]What's wrong with this pipe?

#include <unistd.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>  
#include <pthread.h>

int main (int argc, char * argv[])
{
    int enteroParaElPipe;  
    int IDPROGRAMACLIENTE=getpid(); 
    printf("%d",IDPROGRAMACLIENTE);

    if((mkfifo("pipe",0666))==-1) 
    {
        perror("error creating pipe, type 1");
        exit(1);
    }

    if((enteroParaElPipe=open("pipe",O_WRONLY))==-1)
    {
       perror("error creating pipe, type 2");
       exit(1);
    }

    char comando[200];

    if(scanf("%199s", comando) == 1)
         puts(comando);

    int written;
    escritos=write(enteroParaElPipe,"HOLA\n",5);
    printf("Written: %d\n",written);
    close(enteroParaElPipe);

    return 0;
}

When trying to run this code I get: 尝试运行此代码时,我得到:

error creating pipe: Invalid argument

Why? 为什么?

(Modifications based on the first answers added) (根据添加的第一个答案进行修改)

Why are you passing getpid() as the 2nd argument for mkfifo? 为什么要传递getpid()作为mkfifo的第二个参数?

The 2nd argument is the mode, as in, the FIFO file's permissions. 第二个参数是模式,例如FIFO文件的权限。 Type man 3 mkfifo for more information! 输入man 3 mkfifo以获取更多信息!

Cheers. 干杯。

The second argument to mkfifo is a mode_t representing the permissions on the fifo. mkfifo的第二个参数是mode_t,代表对fifo的权限。

Try: mkfifo("pipe", 0666); 试试: mkfifo("pipe", 0666);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM