2010-11-29 45 views
0
#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; 
} 

当尝试运行这段代码,我得到:这个管道有什么问题?

error creating pipe: Invalid argument 

为什么?

(修改基础上添加的第一个答案)

+0

你为什么把一个PID传递给`mkfifo`?您还应该针对不同的错误情况使用不同的消息,以便您可以确定发生了哪种情况。 – 2010-11-29 01:02:32

+0

您的代码格式非常糟糕。我改进了一点,它仍然很难看。你希望人们通过你的代码?你至少可以做的就是格式化它。 – abelenky 2010-11-29 01:16:08

回答

1

mkfifo的第二个参数是代表fifo权限的mode_t。

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

2

你为什么要传递getpid()为mkfifo第二个说法?

第二个参数是FIFO文件的权限模式。请输入man 3 mkfifo了解更多信息!

干杯。