Advertisement
ijontichy

forktest.c

May 20th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char** argv)
  5. {
  6.     int forkCount;   // will come from argv[1]
  7.     int pid = 0;     // forked pid
  8.     int mypid = 0;   // forked pid
  9.     int isChild = 0; // duh.
  10.     int i;           // counter
  11.  
  12.     if (argc < 1)
  13.     {
  14.         printf("ERROR: no amount of forks specified\n");
  15.         return 1;
  16.     }
  17.  
  18.     forkCount = atoi(argv[1]);
  19.  
  20.     if (forkCount == 0)
  21.     {
  22.         printf("ERROR: zero (or invalid) amount of forks specified\n");
  23.         return 1;
  24.     }
  25.    
  26.     printf("fork pids: ");
  27.     for (i = 0; i < forkCount; i++)
  28.     {
  29.         pid = fork();
  30.         if (pid == 0)
  31.         {
  32.             isChild = 1;
  33.             break;
  34.         }
  35.         else
  36.         {
  37.             printf("%d:%d  ", i, pid);
  38.         }
  39.     }
  40.  
  41.     if (isChild)
  42.     {
  43.         while (1)
  44.         {
  45.             sleep(1);
  46.         }
  47.     }
  48.     else
  49.     {
  50.         printf("\n");
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement