Skip to content

Commit 9d166a7

Browse files
optiklaboptiklab
optiklab
authored and
optiklab
committed
Fixed handling child processes kills.
1 parent 5d7997f commit 9d166a7

File tree

5 files changed

+35
-68
lines changed

5 files changed

+35
-68
lines changed

multithreading/03 - Nonamed_pipes_read_write.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <unistd.h>
55
#include <sys/types.h>
66

7-
#define EXIT_FAILURE 1
7+
#include "Common.h"
88

99
// Compilation:
1010
// gcc "03 - Nonamed_pipes_read_write.c" -o nonamedpipesrw
@@ -18,7 +18,10 @@ int main()
1818
pid_t childpid;
1919
char string[] = "Hello, world!\n";
2020
char readbuffer[80];
21-
21+
22+
// Handle child process killing.
23+
handle_child_finishing();
24+
2225
pipe(fd);
2326

2427
printf("Process %d creates child\n", getpid());
@@ -36,8 +39,7 @@ int main()
3639

3740
/* Send "string" through the output side of pipe */
3841
write(fd[1], string, (strlen(string)+1));
39-
printf("Done write in process %d\n", getpid());
40-
exit(0);
42+
printf("Done write in child process %d\n", getpid());
4143
}
4244
else
4345
{
@@ -49,5 +51,7 @@ int main()
4951
printf("Received string: %s in process %d\n", readbuffer, getpid());
5052
}
5153

54+
printf("End process %d\n", getpid());
55+
5256
exit(0);
5357
}

multithreading/03 - Nonamed_pipes_wc_logins.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdlib.h>
33
#include <unistd.h>
44

5+
#include "Common.h"
6+
57
// Compilation:
68
// gcc "03 - Nonamed_pipes_wc_logins.c" -o nonamedpipeslogins
79

@@ -16,7 +18,10 @@ int main()
1618
printf("Result of operation is the same like if you type\n$>who | wc -l\n");
1719

1820
int pfd[2];
19-
21+
22+
// Handle child process killing.
23+
handle_child_finishing();
24+
2025
pipe(pfd);
2126

2227
if (!fork())

multithreading/04 - Named_pipes.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <sys/types.h>
77
#include <sys/stat.h>
88

9-
#define EXIT_FAILURE 1
9+
#include "Common.h"
1010

1111
// Compilation:
1212
// gcc "04 - Named_pipes.c" -o namedpipes
@@ -19,7 +19,9 @@ int main()
1919
char * myfifo = "/tmp/myfifo";
2020
mkfifo(myfifo, 0666);
2121

22-
22+
// Handle child process killing.
23+
handle_child_finishing();
24+
2325
pid_t childpid;
2426
if((childpid = fork()) == -1)
2527
{
@@ -37,23 +39,24 @@ int main()
3739
close(fd);
3840
}
3941

40-
printf("Process %d done write and unlink fifo.\n", getpid());
42+
printf("Child process %d done write and unlink fifo.\n", getpid());
4143
unlink(myfifo);
4244
}
4345
else
4446
{
45-
printf("Process %d creates child %d\n", getpid(), childpid);
47+
printf("Parent process %d creates child %d\n", getpid(), childpid);
4648

4749
char readbuffer[80];
4850
int fd;
4951
if ((fd = open(myfifo, O_RDONLY)))
5052
{
5153
read(fd, readbuffer, 80);
52-
printf("Process %d received string: %s\n", getpid(), readbuffer);
54+
printf("Parent process %d received string: %s\n", getpid(), readbuffer);
5355
close(fd);
5456
}
5557
unlink(myfifo);
5658
}
5759

60+
printf("Exit process %d\n", getpid());
5861
exit(0);
5962
}

multithreading/05 - Socketpair_fd_passing.c

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#include <unistd.h>
44
#include <netinet/in.h>
55
#include <sys/types.h>
6-
#include <sys/wait.h>
76
#include <sys/socket.h>
87

9-
#define EXIT_FAILURE 1
8+
#include "Common.h"
109

1110
// Compilation:
1211
// gcc "05 - Socketpair_fd_passing.c" -o socketpairfdpassing
@@ -16,33 +15,6 @@
1615
// This technology should be helpful in programming multiprocess server programming to handle socket connections
1716
// in child processes instead of single parent process.
1817

19-
void kill_child_handler(int sig)
20-
{
21-
int status;
22-
pid_t done = waitpid(
23-
-1, // Any child
24-
&status,
25-
0); // Blocked mode.
26-
if (done == -1)
27-
{
28-
printf("No more child processes.\n", done);
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-
}
4618

4719
// Send information in a buffer via socket and attached additional information with that buffer i.e. our file descriptor.
4820
// We may send several file descriptors, but this is a simple example.
@@ -142,14 +114,12 @@ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize,
142114
{
143115
if (cmsg->cmsg_level != SOL_SOCKET)
144116
{
145-
fprintf(stderr, "invalid cmsg_level %d\n",
146-
cmsg->cmsg_level);
117+
fprintf(stderr, "invalid cmsg_level %d\n", cmsg->cmsg_level);
147118
exit(EXIT_FAILURE);
148119
}
149120
if (cmsg->cmsg_type != SCM_RIGHTS)
150121
{
151-
fprintf(stderr, "invalid cmsg_type %d\n",
152-
cmsg->cmsg_type);
122+
fprintf(stderr, "invalid cmsg_type %d\n", cmsg->cmsg_type);
153123
exit(EXIT_FAILURE);
154124
}
155125
*fd = *((int *)CMSG_DATA(cmsg));
@@ -178,24 +148,17 @@ void child_read(int sock)
178148
ssize_t size;
179149
sleep(1);
180150

181-
for (;;)
151+
size = sock_fd_read(sock, buf, sizeof(buf), &fd);
152+
if (size > 0)
182153
{
183-
size = sock_fd_read(sock, buf, sizeof(buf), &fd);
184-
if (size <= 0)
185-
break;
186-
printf ("read size: %d, fd: %d\n", size, fd);
187-
if (fd != -1)
188-
{
189-
write(fd, "hello, world\n", 13);
190-
close(fd);
191-
}
154+
printf ("read size: %d, fd: %d, buffer: %s\n", (int)size, fd, buf);
192155
}
193156
}
194157

195158
void parent_writes(int sock)
196159
{
197-
ssize_t size = sock_fd_write(sock, "1", 1, 1);
198-
printf ("wrote size: %d\n", size);
160+
ssize_t size = sock_fd_write(sock, "TEST", 4, 1);
161+
printf ("wrote size: %d\n", (int)size);
199162
}
200163

201164
/*
@@ -248,25 +211,13 @@ void parent_writes(int sock)
248211
int main(int argc, char **argv)
249212
{
250213
int sv[2];
251-
int pid;
214+
pid_t pid;
252215
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) < 0)
253216
{
254217
perror("socketpair");
255218
exit(EXIT_FAILURE);
256219
}
257220

258-
// Handle child process killing.
259-
struct sigaction kill_child_signal;
260-
kill_child_signal.sa_handler = kill_child_handler;
261-
sigemptyset(&kill_child_signal.sa_mask);
262-
kill_child_signal.sa_flags = SA_RESTART; // Permanent handler.
263-
264-
if (sigaction(SIGCHLD, &kill_child_signal, 0) == -1)
265-
{
266-
perror("Error of calling sigaction");
267-
exit(EXIT_FAILURE);
268-
}
269-
270221
switch ((pid = fork()))
271222
{
272223
case 0:
@@ -282,5 +233,8 @@ int main(int argc, char **argv)
282233
break;
283234
}
284235

236+
int status;
237+
waitpid(pid, &status, 0);
238+
285239
return 0;
286240
}

multithreading/Common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <sys/wait.h>
2+
#include <signal.h>
23

34
#define EXIT_FAILURE 1
45

0 commit comments

Comments
 (0)