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 70cb8eb

Browse files
committed
Simple renaming. Minor changes.
1 parent d6196ba commit 70cb8eb

9 files changed

+741
-871
lines changed

‎multithreading/03 - Nonamed_pipes.cpp

Lines changed: 0 additions & 132 deletions
This file was deleted.
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
#include <signal.h>
2-
#include <stdio.h>
3-
#include <stdlib.h>
4-
#include <unistd.h>
5-
6-
#include "Common.h"
7-
8-
// To compile:
9-
// gcc -std=gnu99 "01 - Signals_SIGCHLD.c" -o signals_sigchld
10-
11-
// Task:
12-
// Create a program which creates a child process and handles SIGCHLD signal from its childs.
13-
14-
int main()
15-
{
16-
pid_t main_pid = getpid();
17-
18-
printf("Start of process %d\n", main_pid);
19-
20-
// Handle child process killing.
21-
handle_child_finishing();
22-
23-
// Make child.
24-
pid_t child_pid;
25-
if((child_pid = fork()))
26-
{
27-
printf("PARENT: Started child process %d.\n", child_pid);
28-
29-
printf("PARENT: Parent process works 1 second.\n");
30-
31-
sleep(1);
32-
}
33-
else
34-
{
35-
// Do anything in child.
36-
}
37-
38-
printf("End of process %d\n", getpid());
39-
40-
exit(0);
41-
}
1+
#include <signal.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
6+
#include "Common.h"
7+
8+
// To compile:
9+
// gcc -std=gnu99 "01 - Signals_SIGCHLD.c" -o signals_sigchld
10+
11+
// Task:
12+
// Create a program which creates a child process and handles SIGCHLD signal from its childs.
13+
14+
int main()
15+
{
16+
pid_t main_pid = getpid();
17+
18+
printf("Start of process %d\n", main_pid);
19+
20+
// Handle child process killing.
21+
handle_child_finishing();
22+
23+
// Make child.
24+
pid_t child_pid;
25+
if((child_pid = fork()))
26+
{
27+
printf("PARENT: Started child process %d.\n", child_pid);
28+
29+
printf("PARENT: Parent process works 1 second.\n");
30+
31+
sleep(1);
32+
}
33+
else
34+
{
35+
// Do anything in child.
36+
}
37+
38+
printf("End of process %d\n", getpid());
39+
40+
exit(EXIT_SUCCESS);
41+
}
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,91 @@
1-
#include <signal.h>
2-
#include <stdio.h>
3-
#include <stdlib.h>
4-
#include <unistd.h>
5-
6-
#include "Common.h"
7-
8-
pid_t child_pid;
9-
10-
// Compilation:
11-
// gcc "01 - Signals_SIGINT_SIGTERM_nonkillable_parent.c" -o nonkillable_parent
12-
13-
// Task:
14-
// Create a program which creates a child process and this child process can be killed by SIGTERM or SIGINT.
15-
// However, parent should not be killed by first SIGTERM/SIGINT. It may be killed in further signals.
16-
17-
void cant_stop_me_handler(int sig)
18-
{
19-
if (child_pid)
20-
{
21-
raise(-9); // or kill(-9, child_pid);
22-
printf("SIGCHLD Child: killed %d.\n", child_pid);
23-
}
24-
else
25-
{
26-
printf("SIGCHLD Parent: Ha-ha, can't kill me this time :)! (try again)\n");
27-
}
28-
}
29-
30-
void cant_stop_me_handler_int(int sig)
31-
{
32-
if (child_pid)
33-
{
34-
raise(-9); // or kill(-9, child_pid);
35-
printf("SIGINT Child: killed %d.\n", child_pid);
36-
}
37-
else
38-
{
39-
printf("SIGINT Parent: Ha-ha, can't kill me this time :)! (try again)\n");
40-
}
41-
}
42-
43-
int main()
44-
{
45-
pid_t main_pid = getpid();
46-
47-
printf("Start of process %d forever.\n", main_pid);
48-
49-
// Do no stop by SIGTERM.
50-
struct sigaction cant_stop_me_signal;
51-
cant_stop_me_signal.sa_handler = cant_stop_me_handler;
52-
sigemptyset(&cant_stop_me_signal.sa_mask);
53-
cant_stop_me_signal.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time...
54-
55-
if (sigaction(SIGTERM, &cant_stop_me_signal, 0) == -1)
56-
{
57-
perror("Error of calling sigaction in parent.");
58-
exit(EXIT_FAILURE);
59-
}
60-
61-
// Do no stop by SIGINT.
62-
struct sigaction cant_stop_me_signal1;
63-
cant_stop_me_signal1.sa_handler = cant_stop_me_handler_int;
64-
sigemptyset(&cant_stop_me_signal1.sa_mask);
65-
cant_stop_me_signal1.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time...
66-
67-
if (sigaction(SIGINT, &cant_stop_me_signal1, 0) == -1)
68-
{
69-
perror("Error of calling sigaction in parent.");
70-
exit(EXIT_FAILURE);
71-
}
72-
73-
// Make child.
74-
if((child_pid = fork()))
75-
{
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);
77-
}
78-
else
79-
{
80-
// Do anything in child.
81-
}
82-
83-
while(1)
84-
{
85-
// Forever... until someone press Ctrl+C or write 'kill -9 xxxx'
86-
}
87-
88-
printf("End of process %d\n", getpid());
89-
90-
exit(0);
91-
}
1+
#include <signal.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
6+
#include "Common.h"
7+
8+
pid_t child_pid;
9+
10+
// Compilation:
11+
// gcc "01 - Signals_SIGINT_SIGTERM_nonkillable_parent.c" -o nonkillable_parent
12+
13+
// Task:
14+
// Create a program which creates a child process and this child process can be killed by SIGTERM or SIGINT.
15+
// However, parent should not be killed by first SIGTERM/SIGINT. It may be killed in further signals.
16+
17+
void cant_stop_me_handler(int sig)
18+
{
19+
if (child_pid)
20+
{
21+
raise(-9); // or kill(-9, child_pid);
22+
printf("SIGCHLD Child: killed %d.\n", child_pid);
23+
}
24+
else
25+
{
26+
printf("SIGCHLD Parent: Ha-ha, can't kill me this time :)! (try again)\n");
27+
}
28+
}
29+
30+
void cant_stop_me_handler_int(int sig)
31+
{
32+
if (child_pid)
33+
{
34+
raise(-9); // or kill(-9, child_pid);
35+
printf("SIGINT Child: killed %d.\n", child_pid);
36+
}
37+
else
38+
{
39+
printf("SIGINT Parent: Ha-ha, can't kill me this time :)! (try again)\n");
40+
}
41+
}
42+
43+
int main()
44+
{
45+
pid_t main_pid = getpid();
46+
47+
printf("Start of process %d forever.\n", main_pid);
48+
49+
// Do no stop by SIGTERM.
50+
struct sigaction cant_stop_me_signal;
51+
cant_stop_me_signal.sa_handler = cant_stop_me_handler;
52+
sigemptyset(&cant_stop_me_signal.sa_mask);
53+
cant_stop_me_signal.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time...
54+
55+
if (sigaction(SIGTERM, &cant_stop_me_signal, 0) == -1)
56+
{
57+
perror("Error of calling sigaction in parent.");
58+
exit(EXIT_FAILURE);
59+
}
60+
61+
// Do no stop by SIGINT.
62+
struct sigaction cant_stop_me_signal1;
63+
cant_stop_me_signal1.sa_handler = cant_stop_me_handler_int;
64+
sigemptyset(&cant_stop_me_signal1.sa_mask);
65+
cant_stop_me_signal1.sa_flags = SA_RESETHAND; // Non-permanent handler. Handle only first time...
66+
67+
if (sigaction(SIGINT, &cant_stop_me_signal1, 0) == -1)
68+
{
69+
perror("Error of calling sigaction in parent.");
70+
exit(EXIT_FAILURE);
71+
}
72+
73+
// Make child.
74+
if((child_pid = fork()))
75+
{
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);
77+
}
78+
else
79+
{
80+
// Do anything in child.
81+
}
82+
83+
while(1)
84+
{
85+
// Forever... until someone press Ctrl+C or write 'kill -9 xxxx'
86+
}
87+
88+
printf("End of process %d\n", getpid());
89+
90+
exit(EXIT_SUCCESS);
91+
}

0 commit comments

Comments
(0)

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