Unix
fork-1.c
/* This program creates a new process and allows both child and parent to report their idenfication numbers. */ #include <sys/types.h> /* file of data types needed for many compilers */ #include <unistd.h> /* needed for fork, getpid procedures */ #include <stdio.h> #include <stdlib.h> int main (void) { pid_t pid; /* variable to record process id of child */ pid = fork(); /* create new process */ if (pid == -1) /* check for error in spawning child process */ { perror ("error in fork"); exit (1); } if (pid == 0) /* check if this is the new child process */ { /* processing for child */ printf ("This output comes from the child process\n"); printf ("Child report: my pid = %d\n", getpid()); } else { /* processing for parent */ printf ("This output comes from the parent process.\n"); printf ("Parent report: my pid = %d child's pid = %d\n", getpid(), pid); } exit (0); /* quit by reporting no error */ }
fork-2-2.c
/* This program creates two new children processes. */ #include <sys/types.h> /* file of data types needed for many compilers */ #include <unistd.h> /* needed for fork, getpid procedures */ #include <stdio.h> #include <stdlib.h> int main (void) { pid_t pid1; /* variable to record process id of child 1 */ pid_t pid2; /* variable to record process id of child 2 */ pid1 = fork(); /* create child 1 */ if (pid1 == -1) /* check for error in spawning child process */ { perror ("error in fork"); exit (1); } if (pid1 == 0) /* check if this is the new child process */ { /* processing for child 1 */ sleep(2); printf ("This output comes from the 1st child process\n"); } else { pid2 = fork(); /* create child 2 */ if (pid2 == -1) /* check for error in spawning child process */ { perror ("error in fork"); exit (1); } if (pid2 == 0) { /* check if this is the new child process */ sleep(2); printf ("This output comes from the 2nd child process\n"); } else { /* processing for parent */ printf ("This output comes from the parent process.\n"); printf ("Parent report: my pid = %d child1's pid = %d, child2's pid = %d\n", getpid(), pid1, pid2); } } exit (0); /* quit by reporting no error */ }
fork-2.c
/* This program creates two new children processes. */ #include <sys/types.h> /* file of data types needed for many compilers */ #include <unistd.h> /* needed for fork, getpid procedures */ #include <stdio.h> #include <stdlib.h> int main (void) { pid_t pid1; /* variable to record process id of child 1 */ pid_t pid2; /* variable to record process id of child 2 */ pid1 = fork(); /* create child 1 */ if (pid1 == -1) /* check for error in spawning child process */ { perror ("error in fork"); exit (1); } if (pid1 == 0) /* check if this is the new child process */ { /* processing for child 1 */ sleep(2); printf ("This output comes from the 1st child process\n"); } else { pid2 = fork(); /* create child 2 */ if (pid2 == -1) /* check for error in spawning child process */ { perror ("error in fork"); exit (1); } if (pid2 == 0) { /* check if this is the new child process */ sleep(2); printf ("This output comes from the 2nd child process\n"); } else { /* processing for parent */ printf ("This output comes from the parent process.\n"); printf ("Parent report: my pid = %d child1's pid = %d, child2's pid = %d\n", getpid(), pid1, pid2); } } exit (0); /* quit by reporting no error */ }