[英]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;
}
尝试运行此代码时,我得到:
error creating pipe: Invalid argument
为什么?
(根据添加的第一个答案进行修改)
为什么要传递getpid()
作为mkfifo的第二个参数?
第二个参数是模式,例如FIFO文件的权限。 输入man 3 mkfifo
以获取更多信息!
干杯。
mkfifo的第二个参数是mode_t,代表对fifo的权限。
试试: mkfifo("pipe", 0666);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.