Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Seems weird behaviors of mkfifo



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:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.