home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / programm / 4407 < prev    next >
Encoding:
Text File  |  1992-08-21  |  2.3 KB  |  96 lines

  1. Xref: sparky comp.unix.programmer:4407 comp.unix.ultrix:6406 comp.unix.wizards:3672
  2. Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!ames!pacbell.com!UB.com!athertn!wyman
  3. From: wyman@atherton.com (Wyman Chong)
  4. Newsgroups: comp.unix.programmer,comp.unix.ultrix,comp.unix.wizards
  5. Subject: Duplicate pids
  6. Keywords: vfork pid
  7. Message-ID: <BtD5Lp.J9r@atherton.com>
  8. Date: 22 Aug 92 02:29:48 GMT
  9. Followup-To: poster
  10. Organization: Atherton Technology, Inc.
  11. Lines: 83
  12.  
  13.  
  14. We have been having some problem on some process management code. It is 
  15. pretty much similar to what system(3) does. Can the kernel return duplicate
  16. pids. We were able to get a small test program to reproduce the problem.
  17. This happens on Sun OS 4.1.{1,2} on either Sun 3, Sparc 2's and MP's. It also
  18. happens on Ultrix running 4.2. It does not happen on IBM's AIX 3.2. 
  19. Is this a known BSD feature? or are we doing something wrong?
  20.  
  21. Just compile the program and run with 30000 as an argument.
  22.  
  23. pepper:wyman{6}:a.out 30000
  24. duplicate pid found: 3227
  25. do a "ps aux | grep 3227" to see if this is true, then hit '<CR>' to end
  26.  
  27.  
  28. When you get the prompt then go to another window and do the ps command as
  29. instructed...
  30.  
  31. pepper:wyman{13}:ps aux | grep 3227
  32. 4:wyman     3331 15.4  0.9   40  224 q4 S    19:19   0:00 grep -n 3227
  33. 52:wyman     3227  0.0  0.0    0    0 p0 Z    Aug  8  0:00 <defunct>
  34. 82:xrel      3227  0.0  0.0   64    0 p4 IW   Aug 10  0:36 rlogin ash
  35.  
  36. Wyman Chong
  37. wyman@atherton.com
  38.  
  39. #include <stdio.h>
  40. #include <sys/errno.h>
  41. #include <sys/signal.h>
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char **argv;
  46. {
  47.     int pid, loop;
  48.     extern int errno;
  49.     if ( argc == 2 )
  50.         loop = atoi(*(argv+1));
  51.     else
  52.         loop = 10;
  53.  
  54.     for (; loop > 0; loop--)
  55.     {
  56.     pid = vfork();
  57.     switch ( pid )
  58.     {
  59.         case -1:
  60.         perror("Fork failed");
  61.         exit(-1);
  62.         case 0:
  63.         {
  64.         int i;
  65.         sigsetmask(0);
  66.         for ( i = 0; i < 1000; i++)
  67.            ;
  68.         _exit(0);
  69.         }
  70.         default:
  71.         ;
  72.     }
  73.  
  74.     /**
  75.     * To see if the pid is unique.
  76.     */
  77.     errno = 0;
  78.     kill(pid, 0);
  79.         
  80.     if ( errno != 0 && errno != ESRCH )
  81.     {
  82.         char ch;
  83.  
  84.         fprintf(stderr, "duplicate pid found: %d\n", pid);
  85.         fprintf(stderr, "do a \"ps aux | grep %d\" to see if this is true,",
  86.             pid);
  87.         fprintf(stderr, " then hit '<CR>' to end.\n");
  88.         ch = getchar();
  89.         exit(0);
  90.     }
  91.  
  92.     waitpid(pid, 0, 0);
  93.     }
  94.     exit(0);
  95. }
  96.