Skip to content

Commit d6196ba

Browse files
committed
Read and show some attributes of child and parent process.
1 parent 9ec8748 commit d6196ba

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

multithreading/00 - Fork Processes.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
#include <unistd.h>
55
#include <sys/wait.h> // For wait, waitpid
66

7+
#include "Common.h"
8+
79
#define PORTNUM 1500 // Port > 1024 because program will not work not as root.
810
#define NUMBER_OF_PROCESSES 2
9-
#define EXIT_FAILURE 1
1011

1112
// Compilation:
1213
// gcc -std=gnu99 "00 - Fork Processes.c" -o forkProcesses
@@ -17,33 +18,34 @@
1718
int main(int argc, char *argv[])
1819
{
1920
printf("Enter to main!\n");
21+
printf("Creating %d of child processes...\n", NUMBER_OF_PROCESSES);
2022
for (int i = 0; i < NUMBER_OF_PROCESSES; i++)
2123
{
2224
pid_t pid;
2325
int status;
2426
switch(pid=fork())
2527
{
2628
case -1:
27-
printf("PROC %d:", i);
28-
perror("1 Error of calling fork");
29-
exit(EXIT_FAILURE);
29+
printf("PROC %d:", i);
30+
perror("1 Error of calling fork");
31+
exit(EXIT_FAILURE);
3032
case 0:
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);
34-
sleep(10);
35-
printf("PROC %d: 4 CHILD: Exit!\n", i);
36-
exit(EXIT_FAILURE);
33+
printf("PROC %d: 1 CHILD: Child process %d!\n", i, getpid());
34+
printf("PROC %d: 2 CHILD: Parent PID %d!\n", i, getppid());
35+
printf("PROC %d: 3 CHILD: Wait 4 seconds...\n", i);
36+
sleep(4);
37+
printf("PROC %d: 4 CHILD: Exit!\n", i);
38+
exit(EXIT_SUCCESS);
3739
default:
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);
41-
waitpid(pid, &status, 0);
42-
printf("PROC %d: 4 PARENT: Child exit code: %d\n", i, WEXITSTATUS(status));
43-
printf("PROC %d: 5 PARENT: Exit!\n", i);
40+
printf("PROC %d: 1 PARENT: Parent process %d!\n", i, getpid());
41+
printf("PROC %d: 2 PARENT: My child PID %d\n", i, pid);
42+
printf("PROC %d: 3 PARENT: Wait until child calls exit()...\n", i);
43+
waitpid(pid, &status, 0);
44+
printf("PROC %d: 4 PARENT: Child exit code: %d\n", i, WEXITSTATUS(status));
45+
printf("PROC %d: 5 PARENT: Exit!\n", i);
4446
}
4547
}
4648

4749
printf("Exit main!\n");
48-
exit(0);
50+
exit(EXIT_SUCCESS);
4951
}

multithreading/Common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <sys/wait.h>
22
#include <signal.h>
33

4+
#define EXIT_SUCCESS 0
45
#define EXIT_FAILURE 1
56

7+
#define FAILURE -1
8+
69
void kill_child_handler(int sig)
710
{
811
int status;
@@ -36,7 +39,7 @@ void handle_child_finishing()
3639
struct sigaction kill_child_signal;
3740
kill_child_signal.sa_handler = kill_child_handler;
3841
sigemptyset(&kill_child_signal.sa_mask);
39-
kill_child_signal.sa_flags = SA_RESTART; // Permanent handler.
42+
kill_child_signal.sa_flags = SA_RESTART; // Mark handler as Permanent.
4043

4144
if (sigaction(SIGCHLD, &kill_child_signal, 0) == -1)
4245
{

0 commit comments

Comments
 (0)