简体   繁体   中英

My C++ program cannot open named pipe

I am trying to run C++ application that works perfectly on Mac OS under Ubuntu. The problem is due to failure in opening a named pipe.

I used mkfifo as follows:

  pipe_name_ = std::string("/tmp/myfifo");
  if (mkfifo(pipe_name_.c_str(), 0666) < 0) {
     error_print("Cannot create a named pipe\n");
     return -1;
  }

  if ((fd_ = open(pipe_name_.c_str(), O_RDONLY | O_CREAT, 0666)) < 0) {
     error_print("Cannot open file description named %s %s\n",
                 pipe_name_.c_str(), strerror(errno));
     return -1;
  }

However, this prints into screen the bellow message that is for open() :

Cannot open file description named /tmp/myfifo Invalid argument

My permissions status is as bellow:

$ls -la /tmp/myfifo
prw-r----- 1 hamidb nonconf 0 Jun 20 13:35 /tmp/myfifo
$umask
0027

I am wondering why it was working fine on Mac OS and not on Linux.

I believe that you have the wrong flags for open as you are not creating the file.

It should be

open(pipe_name_.c_str(), O_RDONLY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM