698 Part VI . Programming in Linux When process calls the fork() system call, it creates an exact copy of itself. After being created by the fork() call, the child process typically calls one of a family of functions collectively known as exec(), providing a program to execute and any options or arguments to that program. Listing 27-1 illustrates the fork()/exec() process. Listing 27-1: Simple fork() and exec() Sequence /* * forkexec.c - illustrate simple fork/exec usage */ #include #include #include #include int main(int argc, char *argv[]) { pid_t child; int status; child = fork(); if (child == 0) { printf( in childn ); execl( /bin/ls , /bin/ls , NULL); } else { printf( in parentn ); waitpid(child, &status, 0); } return 0; } Don t worry about what all the code means. The key points to understand are: . The child - fork() statement creates a new (child) process. . The code between if (child — 0) and the else statements is executed in the child process. In particular, the child uses the execl() function call to execute the /bin/ls program, which creates a directory listing of the current directory. . The waitpid() statement is executed in the parent process, which means that the parent process will wait for the child process to terminate before continuing execution.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.
This entry was posted
on Friday, November 2nd, 2007 at 3:05 pm and is filed under Linux.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.