os161-1.99
 All Data Structures
onefork.c
00001 /*
00002  * onefork - simple test of fork
00003  *
00004  *  relies only on fork, console write, and _exit
00005  *
00006  *  parent prints "P", child prints "C", both exit
00007  *
00008  */
00009 
00010 #include <unistd.h>
00011 #include <string.h>
00012 #include <stdlib.h>
00013 #include <stdio.h>
00014 #include <err.h>
00015 
00016 int
00017 main(int argc, char *argv[])
00018 {
00019   (void)argc;
00020   (void)argv;
00021   pid_t pid;
00022   pid = fork();
00023   if (pid < 0) {
00024     warn("fork");
00025   }
00026   else if (pid == 0) {
00027     /* child */
00028     putchar('C');
00029     putchar('\n');
00030   }
00031   else {
00032     /* parent */
00033     putchar('P');
00034     putchar('\n');
00035   }
00036   return(0);
00037 }
 All Data Structures