Unix реализация конвейера на C

oleg1331

все бы хорошо, только вот на ls|wc работает очень криво
код:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main
{
int fd[2];
pipe(fd);
if (fork
{
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
execlp("ls", "ls", (char*)0);
}
else
{
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
execlp("wc", "wc", (char*)0);
}
return 0;
}

при таком исходнике программа просто зависает. Как я понимаю - из-затого, что wc пытается читать из канала, которого уже не существует (psкак раз выдает, что ls уже завершился, а wc еще работает)
Как исправить этот баг?

oleg1331

прикол в том, что такой баг только под Knoppix. Залогинился под FreeBSD - все работает... вот ведь, блин...

vall

- if (fork
+ if (!fork

oleg1331

почему?

vall

Use the force, Luk!

Olenenok

FORK(2)                    Linux Programmer’s Manual                   FORK(2)

NAME
fork - create a child process

SYNOPSIS
#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);

DESCRIPTION
fork creates a child process that differs from the parent process
only in its PID and PPID, and in the fact that resource utilizations
are set to 0. File locks and pending signals are not inherited.

Under Linux, fork is implemented using copy-on-write pages, so the
only penalty that it incurs is the time and memory required to dupli&#8208;
cate the parent’s page tables, and to create a unique task structure
for the child.

RETURN VALUE
On success, the PID of the child process is returned in the parent’s
thread of execution, and a 0 is returned in the child’s thread of exe&#8208;
cution. On failure, a -1 will be returned in the parent’s context, no
child process will be created, and errno will be set appropriately.

ERRORS
EAGAIN fork cannot allocate sufficient memory to copy the parent’s
page tables and allocate a task structure for the child.

EAGAIN It was not possible to create a new process because the caller’s
RLIMIT_NPROC resource limit was encountered. To exceed this
limit, the process must have either the CAP_SYS_ADMIN or the
CAP_SYS_RESOURCE capability.

ENOMEM fork failed to allocate the necessary kernel structures
because memory is tight.

CONFORMING TO
The fork call conforms to SVr4, SVID, POSIX, X/OPEN, 4.3BSD.

EXAMPLE
See pipe(2) and wait(2).

SEE ALSO
clone(2 execve(2 setrlimit(2 unshare(2 vfork(2 wait(2 capa&#8208;
bilities(7)

Linux 2.6.6 2004-05-27 FORK(2)
Оставить комментарий
Имя или ник:
Комментарий: