From 44bcd7e827c344fe4374209c1ca7388ea78cd48f Mon Sep 17 00:00:00 2001 From: chyyuu Date: Thu, 14 Apr 2016 22:25:42 +0800 Subject: [PATCH] add fork example with error --- related_info/lab5/fork-related/fork-ex1-with-some-error.c | 14 ++++++++++++++ related_info/lab5/fork-related/fork-ex1-with-some-error.md | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 related_info/lab5/fork-related/fork-ex1-with-some-error.c create mode 100644 related_info/lab5/fork-related/fork-ex1-with-some-error.md diff --git a/related_info/lab5/fork-related/fork-ex1-with-some-error.c b/related_info/lab5/fork-related/fork-ex1-with-some-error.c new file mode 100644 index 0000000..d5c1f03 --- /dev/null +++ b/related_info/lab5/fork-related/fork-ex1-with-some-error.c @@ -0,0 +1,14 @@ +#include +#include + +int main(void) { + pid_t child = fork(); + if (child) { // in parent + sleep(5); + kill(child, SIGKILL); + } else { // in child + for (;;); // loop until killed + } + + return 0; +} diff --git a/related_info/lab5/fork-related/fork-ex1-with-some-error.md b/related_info/lab5/fork-related/fork-ex1-with-some-error.md new file mode 100644 index 0000000..e7fae9c --- /dev/null +++ b/related_info/lab5/fork-related/fork-ex1-with-some-error.md @@ -0,0 +1,11 @@ +This program compiles with no errors or warnings, not even with -Wall -Wextra -Werror. I recommend you don’t run it though, and here’s why. From the POSIX specification for fork(2): + +Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error. +And from the specification for kill(2): + +If pid is -1, sig shall be sent to all processes (excluding an unspecified set of system processes) for which the process has permission to send that signal. +Putting the two together, that program could really ruin our day. If the fork() call fails for some reason2, we store -1 in child. Later, we call kill(-1, SIGKILL), which tries to kill all our processes, and most likely hose our login. Not even screen or tmux will save us!3 + +It’s a pretty scary failure mode, and neither the library nor the language do anything at all to prevent us from having a terrible day. + +