Преглед изворни кода

add fork example with error

main
chyyuu пре 8 година
родитељ
комит
44bcd7e827
2 измењених фајлова са 25 додато и 0 уклоњено
  1. +14
    -0
      related_info/lab5/fork-related/fork-ex1-with-some-error.c
  2. +11
    -0
      related_info/lab5/fork-related/fork-ex1-with-some-error.md

+ 14
- 0
related_info/lab5/fork-related/fork-ex1-with-some-error.c Прегледај датотеку

@ -0,0 +1,14 @@
#include <signal.h>
#include <unistd.h>
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;
}

+ 11
- 0
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.

Loading…
Откажи
Сачувај