Created
March 25, 2014 03:31
-
-
Save mmitou/9754736 to your computer and use it in GitHub Desktop.
forkの確認
This file contains hidden or 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 <sys/types.h> | |
| #include <unistd.h> | |
| /* forkの動作を確認する。 */ | |
| /* 親プロセスが死ぬと、子プロセスはinitの子になる事を確認する。 */ | |
| int main() { | |
| int parent_stack = 0; | |
| int *parent_heap = malloc(sizeof(int)); | |
| pid_t pid; | |
| *parent_heap = 0; | |
| pid = fork(); | |
| if(pid == 0) { | |
| /* child process */ | |
| while(1) { | |
| printf("child:pid = %d\n", getpid()); | |
| printf("child:parent_stack = %d\n", parent_stack); | |
| printf("child:parent_heap = %p, %d\n", parent_heap, *parent_heap); | |
| printf("child:parent_pid = %d\n", getppid()); | |
| printf("\n"); | |
| sleep(2); | |
| } | |
| } else { | |
| /* parent process */ | |
| while(1) { | |
| parent_stack ++; | |
| (*parent_heap)++; | |
| printf("parent:pid = %d\n", getpid()); | |
| printf("parent:parent_stack = %d\n", parent_stack); | |
| printf("parent:parent_heap = %p, %d\n", parent_heap, *parent_heap); | |
| printf("parent:parent_pid = %d\n", getppid()); | |
| printf("\n"); | |
| sleep(3); | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment