Created
November 28, 2014 06:33
-
-
Save nodakai/8a30e7a58364dd0f6181 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gcc f.c -o f -Wall -Wextra -pedantic -Wno-missing-field-initializers -g3 -O0 && echo OK | |
f.c: 関数 ‘main’ 内: | |
f.c:5:14: 警告: 仮引数 ‘argc’ が未使用です [-Wunused-parameter] | |
OK | |
$ nc -l 33333 & | |
[1] 25653 | |
$ cd /proc/25653 | |
$ killall nc | |
[1] + terminated nc -l 33333 | |
$ /tmp/f 25653 | |
I am also 25653!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
int main(int argc, char *argv[]) { | |
char *p; | |
long checked_pid; | |
int i; | |
if ( ! argv[1]) | |
abort(); | |
checked_pid = strtol(argv[1], &p, 0); | |
if (*p != '\0') | |
abort(); | |
for (i = 0; i < 1000 * 1000; ++i) { | |
pid_t pid = fork(); | |
if (0 == pid) { | |
return 0; | |
} else if (0 < pid) { | |
if (checked_pid == pid) { | |
printf("I am also %d!!\n", (int)pid); | |
return 0; | |
} | |
} else { | |
perror("fork(2)"); | |
return 10; | |
} | |
if (0 == i % 1000) | |
usleep(0); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment