![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 4
| Fork Example Code: #include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int status;
int pid[2];
int pipe_fd[2];
char *prog1_argv[4];
char *prog2_argv[2];
/*
* Build argument list
*/
prog1_argv[0] = "/usr/local/bin/ls";
prog1_argv[1] = "-l";
prog1_argv[2] = "/";
prog1_argv[3] = NULL;
prog2_argv[0] = "/usr/ucb/more";
prog2_argv[1] = NULL;
/*
* Create the pipe
*/
if (pipe(pipe_fd) < 0)
{
perror ("pipe failed");
exit (errno);
}
/*
* Create a process space for the ls
*/
if ((pid[0]=fork()) < 0)
{
perror ("Fork failed");
exit(errno);
}
if (!pid[0])
{
/*
* Set stdout to pipe
*/
close (pipe_fd[0]);
dup2 (pipe_fd[1], 1);
close (pipe_fd[1]);
/* Execute the ls */
execvp (prog1_argv[0], prog1_argv);
}
if (pid[0])
{
/*
* We're in the parent
*/
/*
* Create a process space for the more
*/
if ((pid[1]=fork()) < 0)
{
perror ("Fork failed");
exit(errno);
}
if (!pid[1])
{
/*
* We're in the child
*/
/*
* Set stdin to pipe
*/
close (pipe_fd[1]);
dup2 (pipe_fd[0], 0);
close (pipe_fd[0]);
/* Execute the more */
execvp (prog2_argv[0], prog2_argv);
}
/* This is the parent */
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid (pid[1], &status, 0);
printf ("Done waiting for more.\n");
}
}
|
| right2001 is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,636
| > if (!pid[0]) Well writing if ( pid[0] == 0 ) would have been more meaningful. |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Understanding fork() | NuNn | C Programming | 8 | 02-27-2009 12:09 PM |
| Fork(), pause(), and storing PID's in a linked list | vital101 | C Programming | 10 | 09-28-2007 02:16 AM |
| Fork - unpredictable? | fredkwok | Linux Programming | 4 | 03-26-2006 02:49 PM |
| fork(), exit() - few questions! | s3t3c | C Programming | 10 | 11-30-2004 06:58 AM |
| Daemon programming: allocated memory vs. fork() | twisgabak | Linux Programming | 2 | 09-25-2003 02:53 PM |