Re: Seems weird behaviors of mkfifo
Re: Seems weird behaviors of mkfifo
- Subject: Re: Seems weird behaviors of mkfifo
- From: Paul Forgey <email@hidden>
- Date: Tue, 22 Mar 2005 12:50:24 -0800
The behavior you are seeing is normal for any posix system. As far as
your shell example goes, that's the effects of buffering. The reader
won't read until a buffer is written with an underlying write () call
to the pipe. When that happens is entirely dependent on the shell.
The SIGPIPE's in your C code are normal. It means you tried to read
from the read end of a pipe after the writer closed it. You can set
SIGPIPE to SIG_IGN and have the read() operation fail with EPIPE
instead.
Note what you are trying to do will only work with one client at a
time, and unless you arrange for some sort of handshaking, nothing will
stop more than one client from trying and garbling your input.
Normally two pipes are set up to handshake both ways between the client
and the server.
On Mar 22, 2005, at 10:32 AM, Tian-Jian Barabbas Jiang wrote:
Hi all,
I've just wrote two small programs of client/server mechanism based
on mkfifo, and I've encountered two strange situations.
1. The named pipe was unstable for my programs. I've experienced
"Broken pipe." frequently while sending messages from the client.
2. Reading pipe was not that atomic. For example, If I just test by
commands rather than handcraft programs,
mkfifo -m 0666 /tmp/pipe
tail -f /tmp/pipe
and then
cat > /tmp/pipe
> a
> b
> c
for the first try in another screen, 'tail -f' won't show anything
until I leave 'cat'. But if I give it a try again,
cat > /tmp/pipe
> d
> e
> f
this time the 'tail -f' will show 'd', 'e', 'f' one by one immediately.
For my programs, if I want the server to read and to print the
client's messages immediately, I have to repeat opening the file of
named pipe. For more detail, please refer to comments in my programs
below.
And FYI, I also have tested these programs on FreeBSD 4.10 and FreeBSD
5.3, they have worked fine. I'm wondering if these are Darwin's
problems, so I post this mail here.
Thank you for your patience to read this mail. :)
Best Regards,
Tian-Jian Jiang
* Server.cpp
#include <iostream>
#include <fstream>
#include <string>
// for umask() and mkfifo()
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int main()
{
umask(0);
int returnValue = mkfifo("/tmp/pipe", 0666);
if(returnValue == 0)
{
string previousString("");
while(previousString != "/quit")
{
// It's easy to see the strange behavior in the following codes. I
supposed the normal usage is to open file only once outside of the
while-loop.
// Unfortunately, I have to open the file again and again to make
this program work on Darwin. But I don't need to do this on FreeBSD.
ifstream in("/tmp/pipe");
string outputString("");
in >> outputString;
if(outputString.length() > 0 &&
outputString != previousString)
cout << "> " + outputString << endl;
previousString = outputString;
in.close();
}
}
else
cout << "mkfifo failed." << endl;
return 0;
}
* Client.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string inputString("");
ofstream out("/tmp/pipe");
while(cin >> inputString)
{
out << inputString << endl;
if(inputString == "/quit")
break;
}
out.close();
return 0;
} _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden