initial import
[staff/goc4/2018bti7061.git] / c-6-additional / examples / c-paralelism / fork1.c
1 /* Source
2 http://www.thegeekstuff.com/2012/05/c-fork-function/
3 */
4
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <sys/wait.h>
10 #include <stdlib.h>
11
12
13 int var_glb; /* A global variable*/
14
15 int main(void)
16 {
17 pid_t childPID;
18 int var_lcl = 0;
19
20 int i,j,k;
21
22 childPID = fork();
23
24 if(childPID >= 0) // fork was successful
25 {
26 if(childPID == 0) // child process
27 {
28 var_lcl++;
29 var_glb++;
30 printf("\n Child Process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb);
31 }
32 else //Parent process
33 {
34 var_lcl = 10;
35 var_glb = 20;
36 printf("\n Parent process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb);
37
38 }
39 }
40 else // fork failed
41 {
42 printf("\n Fork failed, quitting!!!!!!\n");
43 return 1;
44 }
45
46 return 0;
47 }
48 /* Output:
49 bie1@bie1-VirtualBox:~/svn/examples/cAdditional/parallelism$ ./fork1
50
51 Parent process :: var_lcl = [10], var_glb[20]
52
53 Child Process :: var_lcl = [1], var_glb[1]
54 */