Skip to content

Commit 9ab5c43

Browse files
optiklaboptiklab
optiklab
authored and
optiklab
committed
Small changes from my MacOSX. Improved tracing.
1 parent 0e01b2c commit 9ab5c43

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

multithreading/00 - Fork Processes.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,41 @@
99
#define EXIT_FAILURE 1
1010

1111
// Compilation:
12-
// gcc -std=gnu99 "00 - Processes.c" -o processes
12+
// gcc -std=gnu99 "00 - Fork Processes.c" -o forkProcesses
1313

1414
// Task:
1515
// Use fork to create a child process.
1616

17-
void main(int argc, char *argv[])
17+
int main(int argc, char *argv[])
1818
{
19-
printf("0 Enter to main!\n");
19+
printf("Enter to main!\n");
2020
for (int i = 0; i < NUMBER_OF_PROCESSES; i++)
2121
{
2222
pid_t pid;
2323
int status;
2424
switch(pid=fork())
2525
{
2626
case -1:
27-
perror("1 Error of calling fork");
27+
printf("PROC %d:", i);
28+
perror("1 Error of calling fork");
2829
exit(EXIT_FAILURE);
2930
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);
3334
sleep(10);
34-
printf("4 CHILD: Exit!\n");
35+
printf("PROC %d: 4 CHILD: Exit!\n", i);
3536
exit(EXIT_FAILURE);
3637
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);
4041
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);
4344
}
4445
}
4546

46-
printf("1 Exit main!\n");
47+
printf("Exit main!\n");
4748
exit(0);
4849
}

0 commit comments

Comments
 (0)