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 e7eb052

Browse files
optiklaboptiklab
optiklab
authored and
optiklab
committed
Use common library header file for zombie killing method.
1 parent 7685599 commit e7eb052

File tree

3 files changed

+3
-61
lines changed

3 files changed

+3
-61
lines changed

‎multithreading/01 - Signals_SIGCHLD.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,15 @@
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <unistd.h>
5-
#include <sys/wait.h>
65

7-
#defineEXIT_FAILURE 1
6+
#include"Common.h"
87

98
// To compile:
109
// gcc -std=gnu99 "01 - Signals_SIGCHLD.c" -o signals_sigchld
1110

1211
// Task:
1312
// Create a program which creates a child process and handles SIGCHLD signal from its childs.
1413

15-
void kill_child_handler(int sig)
16-
{
17-
int status;
18-
pid_t done = waitpid(
19-
-1, // Any child
20-
&status,
21-
0); // Blocked mode.
22-
if (done == -1)
23-
{
24-
printf("No more child processes.\n");
25-
}
26-
else
27-
{
28-
short isNormalTermination = WIFEXITED(status);
29-
if (!isNormalTermination ||
30-
// WEXITSTATUS should be used only if normal termination = true.
31-
(isNormalTermination && WEXITSTATUS(status) != 0))
32-
{
33-
printf("Zombie for PID -- %d failed.\n", done);
34-
exit(EXIT_FAILURE);
35-
}
36-
else
37-
{
38-
printf("Zombie for PID -- %d successfully removed.\n", done);
39-
}
40-
}
41-
}
42-
4314
int main()
4415
{
4516
pid_t main_pid = getpid();

‎multithreading/01 - Signals_SIGINT_SIGTERM_nonkillable_parent.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <unistd.h>
5-
#include <sys/wait.h>
65

7-
#defineEXIT_FAILURE 1
6+
#include"Common.h"
87

98
pid_t child_pid;
109

‎multithreading/02 - Flock.c

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,17 @@
55
#include <string.h>
66
#include <unistd.h>
77
#include <sys/file.h>
8-
#include <sys/wait.h>
98
#include <sys/types.h>
109
#include <sys/stat.h>
1110

12-
#defineEXIT_FAILURE 1
11+
#include"Common.h"
1312

1413
// Compilation:
1514
// gcc "02 - Flock.c" -o flockc
1615

1716
// This program imitates work of 3 processes (parent and 2 childs) with some file with flock-blocking, which means
1817
// that 2 processes cannot both write into file, but can both read file in the same time. However it cannot write and read in the same time.
1918

20-
void kill_child_handler(int sig)
21-
{
22-
int status;
23-
pid_t done = waitpid(-1, // Any child
24-
&status,
25-
0); // Blocked mode.
26-
if (done == -1)
27-
{
28-
printf("No more child processes.\n");
29-
}
30-
else
31-
{
32-
short isNormalTermination = WIFEXITED(status);
33-
if (!isNormalTermination ||
34-
// WEXITSTATUS should be used only if normal termination = true.
35-
(isNormalTermination && WEXITSTATUS(status) != 0))
36-
{
37-
printf("Zombie for PID -- %d failed.\n", done);
38-
exit(EXIT_FAILURE);
39-
}
40-
else
41-
{
42-
printf("Zombie for PID -- %d successfully removed.\n", done);
43-
}
44-
}
45-
}
46-
4719
int lock_write()
4820
{
4921
int fd;

0 commit comments

Comments
(0)

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