Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ddce52c

Browse files
optiklaboptiklab
optiklab
authored and
optiklab
committed
Move common zombie handling function to commons.
1 parent 60b9c13 commit ddce52c

File tree

5 files changed

+26
-44
lines changed

5 files changed

+26
-44
lines changed

‎multithreading/01 - Signals_SIGCHLD.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,15 @@ int main()
1818
printf("Start of process %d\n", main_pid);
1919

2020
// Handle child process killing.
21-
struct sigaction kill_child_signal;
22-
kill_child_signal.sa_handler = kill_child_handler;
23-
sigemptyset(&kill_child_signal.sa_mask);
24-
kill_child_signal.sa_flags = SA_RESTART; // Permanent handler.
25-
26-
if (sigaction(SIGCHLD, &kill_child_signal, 0) == -1)
27-
{
28-
perror("Error of calling sigaction");
29-
exit(EXIT_FAILURE);
30-
}
21+
handle_child_finishing();
3122

3223
// Make child.
3324
pid_t child_pid;
3425
if((child_pid = fork()))
3526
{
36-
printf("Started child process %d.\n", child_pid);
27+
printf("PARENT: Started child process %d.\n", child_pid);
3728

38-
printf("Parent process works 1 second.\n");
29+
printf("PARENT: Parent process works 1 second.\n");
3930

4031
sleep(1);
4132
}

‎multithreading/01 - Signals_SIGINT_SIGTERM_nonkillable_parent.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int main()
7373
// Make child.
7474
if((child_pid = fork()))
7575
{
76-
printf("Start of child process %d forever. Press Ctrl+C OR write 'kill -9 %d' to terminate child.\n", child_pid, child_pid);
76+
printf("PARENT: Start of child process %d forever. Press Ctrl+C OR write 'kill -9 %d' to terminate child.\n", child_pid, child_pid);
7777
}
7878
else
7979
{

‎multithreading/02 - Flock.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,7 @@ int main()
6767
printf("Start of process %d\n", main_pid);
6868

6969
// Handle child process killing.
70-
struct sigaction kill_child_signal;
71-
kill_child_signal.sa_handler = kill_child_handler;
72-
sigemptyset(&kill_child_signal.sa_mask);
73-
kill_child_signal.sa_flags = SA_RESTART; // Permanent handler.
74-
75-
if (sigaction(SIGCHLD, &kill_child_signal, 0) == -1)
76-
{
77-
perror("Error of calling sigaction");
78-
exit(EXIT_FAILURE);
79-
}
70+
handle_child_finishing();
8071

8172
// Lock file for writing.
8273
if (lock_write())
@@ -93,14 +84,14 @@ int main()
9384
pid_t child_pid;
9485
if ((child_pid = fork()) > 0)
9586
{
96-
printf("Start of child process %d.\n", child_pid);
87+
printf("PARENT: Start of child process %d.\n", child_pid);
9788

9889
// Parent makes child 2.
9990
pid_t next_child_pid;
10091
if ((next_child_pid = fork()) > 0)
10192
{
102-
printf("Start of child process %d.\n", next_child_pid);
103-
printf("Parent process %d sleeps 5 seconds.\n", getpid());
93+
printf("PARENT: Start of child process %d.\n", next_child_pid);
94+
printf("PARENT: Parent process %d sleeps 5 seconds.\n", getpid());
10495
sleep(5);
10596
}
10697
}

‎multithreading/02 - Lock_via_file.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@
1616
// To access to resource both processes checks existance of a file, which means resource is still locked.
1717
// If file is not exists anymore, then this means resource is free.
1818

19-
void handle_child_finishing()
20-
{
21-
// Handle child process killing.
22-
struct sigaction kill_child_signal;
23-
kill_child_signal.sa_handler = kill_child_handler;
24-
sigemptyset(&kill_child_signal.sa_mask);
25-
kill_child_signal.sa_flags = SA_RESTART; // Permanent handler.
26-
27-
if (sigaction(SIGCHLD, &kill_child_signal, 0) == -1)
28-
{
29-
perror("Error of calling sigaction");
30-
exit(EXIT_FAILURE);
31-
}
32-
}
33-
3419
int unlock()
3520
{
3621
unlink("/tmp/file.txt");
@@ -53,7 +38,7 @@ int lock()
5338
return 1;
5439
}
5540

56-
void main()
41+
int main()
5742
{
5843
pid_t main_pid = getpid();
5944

@@ -64,9 +49,9 @@ void main()
6449

6550
// Make childs.
6651
pid_t child_pid;
67-
if(child_pid = fork())
52+
if((child_pid = fork()))
6853
{
69-
printf("Start of child process %d.\n", child_pid);
54+
printf("PARENT: Started child process %d.\n", child_pid);
7055

7156
sleep(1);
7257
}

‎multithreading/Common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,19 @@ void kill_child_handler(int sig)
2727
printf("Zombie for PID -- %d successfully removed.\n", done);
2828
}
2929
}
30+
}
31+
32+
void handle_child_finishing()
33+
{
34+
// Handle child process killing.
35+
struct sigaction kill_child_signal;
36+
kill_child_signal.sa_handler = kill_child_handler;
37+
sigemptyset(&kill_child_signal.sa_mask);
38+
kill_child_signal.sa_flags = SA_RESTART; // Permanent handler.
39+
40+
if (sigaction(SIGCHLD, &kill_child_signal, 0) == -1)
41+
{
42+
perror("Error of calling sigaction");
43+
exit(EXIT_FAILURE);
44+
}
3045
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /