home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.programmer:4407 comp.unix.ultrix:6406 comp.unix.wizards:3672
- Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!ames!pacbell.com!UB.com!athertn!wyman
- From: wyman@atherton.com (Wyman Chong)
- Newsgroups: comp.unix.programmer,comp.unix.ultrix,comp.unix.wizards
- Subject: Duplicate pids
- Keywords: vfork pid
- Message-ID: <BtD5Lp.J9r@atherton.com>
- Date: 22 Aug 92 02:29:48 GMT
- Followup-To: poster
- Organization: Atherton Technology, Inc.
- Lines: 83
-
-
- We have been having some problem on some process management code. It is
- pretty much similar to what system(3) does. Can the kernel return duplicate
- pids. We were able to get a small test program to reproduce the problem.
- This happens on Sun OS 4.1.{1,2} on either Sun 3, Sparc 2's and MP's. It also
- happens on Ultrix running 4.2. It does not happen on IBM's AIX 3.2.
- Is this a known BSD feature? or are we doing something wrong?
-
- Just compile the program and run with 30000 as an argument.
-
- pepper:wyman{6}:a.out 30000
- duplicate pid found: 3227
- do a "ps aux | grep 3227" to see if this is true, then hit '<CR>' to end
-
-
- When you get the prompt then go to another window and do the ps command as
- instructed...
-
- pepper:wyman{13}:ps aux | grep 3227
- 4:wyman 3331 15.4 0.9 40 224 q4 S 19:19 0:00 grep -n 3227
- 52:wyman 3227 0.0 0.0 0 0 p0 Z Aug 8 0:00 <defunct>
- 82:xrel 3227 0.0 0.0 64 0 p4 IW Aug 10 0:36 rlogin ash
-
- Wyman Chong
- wyman@atherton.com
-
- #include <stdio.h>
- #include <sys/errno.h>
- #include <sys/signal.h>
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int pid, loop;
- extern int errno;
- if ( argc == 2 )
- loop = atoi(*(argv+1));
- else
- loop = 10;
-
- for (; loop > 0; loop--)
- {
- pid = vfork();
- switch ( pid )
- {
- case -1:
- perror("Fork failed");
- exit(-1);
- case 0:
- {
- int i;
- sigsetmask(0);
- for ( i = 0; i < 1000; i++)
- ;
- _exit(0);
- }
- default:
- ;
- }
-
- /**
- * To see if the pid is unique.
- */
- errno = 0;
- kill(pid, 0);
-
- if ( errno != 0 && errno != ESRCH )
- {
- char ch;
-
- fprintf(stderr, "duplicate pid found: %d\n", pid);
- fprintf(stderr, "do a \"ps aux | grep %d\" to see if this is true,",
- pid);
- fprintf(stderr, " then hit '<CR>' to end.\n");
- ch = getchar();
- exit(0);
- }
-
- waitpid(pid, 0, 0);
- }
- exit(0);
- }
-