author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Wed, 02 May 2012 20:49:41 +0200 | |
changeset 164 | e1f4bba1097a |
parent 139 | cb1d3f4cf18e |
permissions | -rw-r--r-- |
56 | 1 |
/** |
77 | 2 |
* fork.c |
3 |
* Copyright (C) 2008 Markus Broeker |
|
56 | 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"); |
|
139
cb1d3f4cf18e
execve returns a value on error and this catches it
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
138
diff
changeset
|
21 |
execve (cmd, argv, argp); |
cb1d3f4cf18e
execve returns a value on error and this catches it
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
138
diff
changeset
|
22 |
exit (EXIT_FAILURE); |
56 | 23 |
case -1: |
24 |
perror ("FORK"); |
|
25 |
return errno; |
|
26 |
default: |
|
27 |
printf ("Pid started as %d\n", pid); |
|
28 |
wait (&pid); |
|
29 |
printf ("Exit-Status = %d\n", pid); |
|
138
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
77
diff
changeset
|
30 |
|
56 | 31 |
return pid; |
32 |
} |
|
138
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
77
diff
changeset
|
33 |
|
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
77
diff
changeset
|
34 |
// should never happen |
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
77
diff
changeset
|
35 |
return -1; |
56 | 36 |
} |
37 |
||
38 |
int main (int argc, char **argv) |
|
39 |
{ |
|
40 |
char *args[] = { |
|
41 |
"/bin/ls", |
|
42 |
"-al", |
|
43 |
NULL |
|
44 |
}; |
|
45 |
||
46 |
if (fork_proc (args, NULL) == 0) { |
|
47 |
printf ("It just works :P\n"); |
|
48 |
} |
|
49 |
||
50 |
return EXIT_SUCCESS; |
|
51 |
} |