diff options
author | punk <punk@libernaut> | 2021-04-21 15:26:19 +0100 |
---|---|---|
committer | punk <punk@libernaut> | 2021-04-21 15:26:19 +0100 |
commit | eac48b5a8d709135a95abcc2243b369095f074f4 (patch) | |
tree | a2e34d995cef5ac8068ec7047e93b1125c80d175 /dev/c/src/fork/fork.c | |
parent | 3bd43803fc8cb7a39a87394cb7c491ddc151e06b (diff) | |
parent | 452477a2635d85ecf772a5242ce97d9479503bb3 (diff) | |
download | doc-eac48b5a8d709135a95abcc2243b369095f074f4.tar.gz |
release 0.7.0
Diffstat (limited to 'dev/c/src/fork/fork.c')
-rw-r--r-- | dev/c/src/fork/fork.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/dev/c/src/fork/fork.c b/dev/c/src/fork/fork.c new file mode 100644 index 0000000..25e4909 --- /dev/null +++ b/dev/c/src/fork/fork.c @@ -0,0 +1,38 @@ + +#include <sys/types.h> +#include <sys/wait.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +int main(){ + + pid_t cpid,ppid; + char errbuf[1024]; + + cpid = fork(); + if (cpid == -1) { + (void) snprintf(errbuf, sizeof(errbuf), + "fork: %s", strerror(errno)); + printf("%s\n", errbuf); + exit(1); + } + + if (cpid == 0) { + //child + ppid = getppid(); + if(ppid == 1){ + printf("parent died ?\n"); + _exit(1); + } + printf("I'm child with %i, parent %i\n", getpid(), ppid); + _exit(0); + } +/* parent */ + wait(NULL); + printf("Child id: %i\n", cpid); + return 0; +} + |