Named Pipe (FIFO)
Hi,
I'm trying to write a program using named pipes, and I'm having trouble opening it. In another program I call this line:
mkfifo("./fife", 0777);
So my named pipe exists and I see it in my list of files. Then I run the program I included below, but I can not get past the open call. Any help would be appreciated for I can not find a lot of resources on this subject.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#define MSGSIZE 14
void main() {
char *message = "Hello, world!";
int fd;
fd = open("fife", O_WRONLY);
// Can not get past this point!
cout << "fd: " << fd << endl;
if (write(fd, message, MSGSIZE) < 0)
cout << "error writing" << endl;
close(fd);
}
nanodano
Junior Poster in Training
78 posts since Feb 2005
Reputation Points: 36
Solved Threads: 2
Look at the errno which open sets, when it returns with a failure?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
I found out the problem. It waits because it wants another program to handle the other end of the pipe.
I fixed my codes up and I wrote a script like this to fix the problem:
#!/bin/bash
echo "Running clients..."
./client &
./client &
echo "Running server..."
./server
# The & runs the program in the background, so you can run another program, playing client and server in the same console
nanodano
Junior Poster in Training
78 posts since Feb 2005
Reputation Points: 36
Solved Threads: 2
a) you're too late - by at least a year, check the dates.
b) try programming something for yourself.
c) start your own thread when you've got something to show.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953