Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Named pipes for interprocess comm.

0 views
Skip to first unread message

Julio Kuplinsky

unread,
Oct 11, 1999, 3:00:00 AM10/11/99
to
I want to create a process (client) that
1. Fires up another process (server)
2. Receives a piece of data generated by the server.
The code, stripped to the bare bones, is this:
----------------------------
/* client */
pid = fork();
if(pid==0)
execl( ......., "srvr", ......);
waitpid(pid,NULL,0);
fd=open("fifo", O_RDONLY);
read(fd, ....);
close(fd);
------------------------------
/*server */
unlink("fifo");
mkfifo("fifo" ....);
fd = open("fifo" , O_WRONLY | O_NONBLOCK);
sprintf(buf, ....);
write(fd, buf, ....);
close(fd);

This, as the experts among you must know by now, doesn't work. The
server is created, but the flow seems to freeze at the open() on the server
side. BTW, I understand that I have to use the NONBLOCK option here.
Can anyone help? Thanks.


Rainer Temme

unread,
Oct 12, 1999, 3:00:00 AM10/12/99
to
Julio Kuplinsky <jkup...@rcc.com> schrieb im Beitrag <C%oM3.63$aK6.2808@client>...

> I want to create a process (client) that
> 1. Fires up another process (server)
> 2. Receives a piece of data generated by the server.
> The code, stripped to the bare bones, is this:
> ----------------------------
> /* client */
> pid = fork();
> if(pid==0)
> execl( ......., "srvr", ......);
> waitpid(pid,NULL,0);
Move this waitpid()...................................<<<<<<<<<<<<<<<<<<<<<<<<<<<<

> fd=open("fifo", O_RDONLY);
> read(fd, ....);
> close(fd);
There....................................................<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

> ------------------------------
> /*server */
> unlink("fifo");
> mkfifo("fifo" ....);
> fd = open("fifo" , O_WRONLY | O_NONBLOCK);
> sprintf(buf, ....);
> write(fd, buf, ....);
> close(fd);
>
> This, as the experts among you must know by now, doesn't work. The
> server is created, but the flow seems to freeze at the open() on the server
> side. BTW, I understand that I have to use the NONBLOCK option here.
> Can anyone help? Thanks.
>

Hi Julio,

If you fork()/exec() and wait() in the parent-process, the parent is blocked until
the child terminates. But this is not what you want.

I've the an unlink() mkfifo()...this will be difficult to handle, because if the parent might have already
opened the fifo you're just deleting.

Better (more reliable) will be a construction like this:

parent:
int loop,rc;
for (loop=0,rc=-1; loop<10 && rc==-1; loop++)
{
rc=open("fifo",rdonly);
if (rc<0) sleep(1);
}

son:
mkfifo("fifo",...); /* don't check returncode, coz if it already exists, this call will fail *?
fd=open("fifo",wronly); /* you dont need nonblock...this way the child will syncronize with the father */

regards Rainer


Ken Pizzini

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
On Mon, 11 Oct 1999 13:20:11 -0400, Julio Kuplinsky <jkup...@rcc.com> wrote:
>pid = fork();
>if(pid==0)
> execl( ......., "srvr", ......);
>waitpid(pid,NULL,0);
>fd=open("fifo", O_RDONLY);

>server is created, but the flow seems to freeze at the open() on the server
>side.

Because the waitpid() is waiting for the child to exit. You
either need to delay this waitpid() call until you are expecting
the child to die (such as if you use the double-fork technique
for starting the child), or do a non-blocking form of waitpid()
(i.e., set the third argument to WNOHANG).

--Ken Pizzini

0 new messages