开发者

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?

The 2nd argument is the mode, as in, the FIFO file's permissions. Type man 3 mkfifo for more information!

Cheers.


The second argument to mkfifo is a mode_t representing the permissions on the fifo.

Try: mkfifo("pipe", 0666);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜