root/user/uw-testbin/pidcheck/pidcheck.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  * pidcheck - simple test of fork and getpid
   3  *
   4  *  relies only on fork, console write, getpid and _exit
   5  *
   6  *  child prints its pid, parent prints childs pid and its own
   7  *
   8  */
   9 
  10 #include <unistd.h>
  11 #include <string.h>
  12 #include <stdlib.h>
  13 #include <stdio.h>
  14 #include <err.h>
  15 
  16 /* declare this volatile to discourage the compiler from
  17    optimizing away the parent's delay loop */
  18 volatile int tot;
  19 
  20 int
  21 main(int argc, char *argv[])
  22 {
  23   (void)argc;
  24   (void)argv;
  25   pid_t pid,pid2;
  26   int i;
  27   pid = fork();
  28   if (pid < 0) {
  29     warn("fork");
  30   }
  31   else if (pid == 0) {
  32     /* child */
  33     pid2 = getpid();
  34     /* print the child's PID, as seen by the child */
  35     printf("C: %d\n",pid2);
  36   }
  37   else {
  38     /* parent */
  39     /* try to delay long enough for the child to finish printing */
  40     tot = 0;
  41     for(i=0;i<1000000;i++) {
  42       tot++;
  43     }
  44     /* print the child's PID, as seen by the parent */
  45     printf("PC: %d\n",pid);
  46     /* print the parent's PID */
  47     pid2 = getpid();
  48     printf("PP: %d\n",pid2);
  49   }
  50   return(0);
  51 }

/* [<][>][^][v][top][bottom][index][help] */