fork.c
changeset 56 966ad681f25d
child 77 49e0babccb23
equal deleted inserted replaced
55:2a20d0184041 56:966ad681f25d
       
     1 /**
       
     2  * test/demos/fork.c
       
     3  * Copyright (C) Markus Broeker
       
     4  */
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stdlib.h>
       
     8 #include <unistd.h>
       
     9 #include <sys/wait.h>
       
    10 #include <errno.h>
       
    11 
       
    12 int fork_proc (char **argv, char **argp)
       
    13 {
       
    14     int pid;
       
    15     char *cmd = argv[0];
       
    16 
       
    17     pid = fork ();
       
    18     switch (pid) {
       
    19     case 0:
       
    20         printf ("Starting new Process\n");
       
    21         execve (cmd, argv, argp);
       
    22     case -1:
       
    23         perror ("FORK");
       
    24         return errno;
       
    25     default:
       
    26         printf ("Pid started as %d\n", pid);
       
    27         wait (&pid);
       
    28         printf ("Exit-Status = %d\n", pid);
       
    29         return pid;
       
    30     }
       
    31 }
       
    32 
       
    33 int main (int argc, char **argv)
       
    34 {
       
    35     char *args[] = {
       
    36         "/bin/ls",
       
    37         "-al",
       
    38         NULL
       
    39     };
       
    40 
       
    41     if (fork_proc (args, NULL) == 0) {
       
    42         printf ("It just works :P\n");
       
    43     }
       
    44 
       
    45     return EXIT_SUCCESS;
       
    46 }