walnutcy

管道 编程 :virtualbox的共享目录中不可运行

0
阅读(3265)

管道通信编程,这里写下日志主要是因为在virtualbox的共享目录下是不可运行的,其提示错误如下:

Failed to Create fifo <read-fifo.pipe> : Operation not permitted

一定要将运行文件拷贝到linux别的目录下执行,切记。

主要用到了一个:mkfifo函数来创建管道。

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

int main(void)
{
 char write_fifo_name[] = "read-fifo.pipe";
 int  write_fd;
 int  len;
 char ch;
 struct stat stat_buf;

 int ret = mkfifo(write_fifo_name, S_IRUSR | S_IWUSR);
 if (ret == (-1))
 {
  printf("Failed to Create fifo <%s> : %s\n", write_fifo_name, strerror(errno));
  exit(-1);
 }
 
 write_fd = open(write_fifo_name, O_WRONLY);
 
 if ((-1)== write_fd)
 {
  printf("Failed to Open fifo <%s> : %s\n", write_fifo_name, strerror(errno));
  exit(-1);
 }
 
 
 printf("==Start While(1)--Peter\n");
 while(1)
 {
  ch = getchar();
 
  write(write_fd, &ch, 1);
 
  usleep(1);
 }
 
 close(write_fd);
 unlink(write_fifo_name);
 
 return 0;
}