Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



C How to Program (3rd Edition)    (ISBN: 0130895725)


 

 List Price: $78.67
 Our Price: $78.67
 Used Price: $43.00

 Release Date: 16 August, 2000
 Manufacturer: Prentice Hall (Paperback)
 Sales Rank: 46,122

 Author: Harvey M. Deitel, Paul J. Deitel









More Info

 Forking Example 2001-11-23 02:37:41
 
Category: source:c:runtime
Description: Example of basic forking in c
Platform: unix
Author: detour
Viewed: 4225
Rating: 3.7/5 (18 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
fork.c
/* fork.c written by detour@metalshell.com
 *
 * simple forking example
 * 
 * fork() requires unistd.h
 *
 * http://www.metalshell.com/
 *
 */

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

int main(int argc, char *argv[]) {
  pid_t pid;

  pid = fork();

  if (pid == 0) {
    printf("\nThis is the child process talking.\n");
    printf("Use 'ps -ax | grep %s' to verify child process.\n", argv[0]);
    printf("Sleeping for 20 seconds.\n");
    /* Flush our output before we sleep */
    fflush(stdout);
    sleep(20);
    printf("\nChild exiting\n");
  } else if (pid != -1) {
    /* This is the parent process, so lets exit. */
    printf("Parent exiting.\n");
    exit(0);
  } else {
    printf("There was an error with forking");
    exit(1);
  }

}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?