LINUX 管道通信问题
本人刚刚接触这些,不太懂,话说LINUX 的库函数 应该有 SDK吧? 在哪。。类似 MSDN 样的。。。
话不多说 直接上代码
- C/C++ code
#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define FIFO "/myfifo"int main(int argc, char** argv){ int fd; char w_buf[100]; int nwrite; if(fd == -1) { if(errno == ENXIO) { printf("open error; no reading process\n"); } } fd = open(FIFO,O_WRONLY|O_NONBLOCK,0); strcpy(w_buf,"1111111111111111111111111111111111"); if((nwrite = write(fd,w_buf,100)) == -1) { printf("write wrong: %d \n",nwrite); if(errno == EAGAIN) { printf("The FIFO has not been read yet,Please try later\n"); } }else { printf("write %s to the FIFO \n",w_buf); } return 1;}write(fd,w_buf,100)) 返回值总是 -1 坐等高手解答
[解决办法]
if(fd == -1)
{
if(errno == ENXIO)
{
printf("open error; no reading process\n");
}
}
fd = open(FIFO,O_WRONLY|O_NONBLOCK,0);
这两段颠倒了吧,先open,再判断
[解决办法]
fd = open(FIFO,O_WRONLY|O_NONBLOCK,0);
if(fd == -1)
{
perror("open");
printf("open wrong: %d \n",fd);
if(errno == ENXIO)
{
printf("open error; no reading process\n");
}
}
[解决办法]
查看open失败原因