9
9
#define EXIT_FAILURE 1
10
10
11
11
// Compilation:
12
- // gcc -std=gnu99 "00 - Processes.c" -o processes
12
+ // gcc -std=gnu99 "00 - Fork Processes.c" -o forkProcesses
13
13
14
14
// Task:
15
15
// Use fork to create a child process.
16
16
17
- void main (int argc , char * argv [])
17
+ int main (int argc , char * argv [])
18
18
{
19
- printf ("0 Enter to main!\n" );
19
+ printf ("Enter to main!\n" );
20
20
for (int i = 0 ; i < NUMBER_OF_PROCESSES ; i ++ )
21
21
{
22
22
pid_t pid ;
23
23
int status ;
24
24
switch (pid = fork ())
25
25
{
26
26
case -1 :
27
- perror ("1 Error of calling fork" );
27
+ printf ("PROC %d:" , i );
28
+ perror ("1 Error of calling fork" );
28
29
exit (EXIT_FAILURE );
29
30
case 0 :
30
- printf ("1 CHILD: Child process %d!\n" , getpid ());
31
- printf ("2 CHILD: Parent PID %d!\n" , getppid ());
32
- printf ("3 CHILD: Wait 10 seconds...\n" );
31
+ printf ("PROC %d: 1 CHILD: Child process %d!\n" , i , getpid ());
32
+ printf ("PROC %d: 2 CHILD: Parent PID %d!\n" , i , getppid ());
33
+ printf ("PROC %d: 3 CHILD: Wait 10 seconds...\n" , i );
33
34
sleep (10 );
34
- printf ("4 CHILD: Exit!\n" );
35
+ printf ("PROC %d: 4 CHILD: Exit!\n" , i );
35
36
exit (EXIT_FAILURE );
36
37
default :
37
- printf ("1 PARENT: Parent process %d!\n" , getpid ());
38
- printf ("2 PARENT: Child PID %d\n" , pid );
39
- printf ("3 PARENT: Wait until child calls exit()...\n" );
38
+ printf ("PROC %d: 1 PARENT: Parent process %d!\n" , i , getpid ());
39
+ printf ("PROC %d: 2 PARENT: Child PID %d\n" , i , pid );
40
+ printf ("PROC %d: 3 PARENT: Wait until child calls exit()...\n" , i );
40
41
waitpid (pid , & status , 0 );
41
- printf ("4 PARENT: Child exit code: %d\n" , WEXITSTATUS (status ));
42
- printf ("5 PARENT: Exit!\n" );
42
+ printf ("PROC %d: 4 PARENT: Child exit code: %d\n" , i , WEXITSTATUS (status ));
43
+ printf ("PROC %d: 5 PARENT: Exit!\n" , i );
43
44
}
44
45
}
45
46
46
- printf ("1 Exit main!\n" );
47
+ printf ("Exit main!\n" );
47
48
exit (0 );
48
49
}
0 commit comments