Permalink
Browse files

fork

  • Loading branch information...
1 parent a5d19d5 commit 9e21bcdc72619812cd0df1dfb03fc0386f1e0039 @dlinyj committed Oct 6, 2016
Showing with 44 additions and 0 deletions.
  1. +7 −0 fork/Makefile
  2. +37 −0 fork/fork.c
View
@@ -0,0 +1,7 @@
+all: fork
+fork: fork.c
+ gcc fork.c -g -o fork
+clean:
+ rm -f *.o
+ rm -f fork
+ rm -f *~
View
@@ -0,0 +1,37 @@
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
+int main(int argc, char * argv[])
+{
+ int pid, status;
+ pid = fork();
+ if (pid == 0) {
+ printf("We are the child pid=%d",getpid());
+ int i = 0;
+ while (i!=1) {
+ printf("********* i=%d *********\n", i);
+ sleep(1);
+ }
+ printf("********* Yippee! We did it! *********\n");
+ return EXIT_FAILURE; // Never get there normally
+ }
+ else {
+ if (wait(&status) == -1) {
+ perror("wait");
+ return EXIT_FAILURE;
+ }
+ if (WIFEXITED(status))
+ printf("Child terminated normally with exit code %i\n", WEXITSTATUS(status));
+ if (WIFSIGNALED(status))
+ printf("Child was terminated by a signal #%i\n", WTERMSIG(status));
+ if (WCOREDUMP(status))
+ printf("Child dumped core\n");
+ if (WIFSTOPPED(status))
+ printf("Child was stopped by a signal #%i\n", WSTOPSIG(status));
+ }
+ return EXIT_SUCCESS;
+}

0 comments on commit 9e21bcd

Please sign in to comment.