Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 exptools 1/6/84; site ihnet.UUCP Path: utzoo!watmath!clyde!burl!mgnetp!ihnp4!ihnet!eklhad From: eklhad@ihnet.UUCP (K. A. Dahlke) Newsgroups: net.unix,net.unix-wizards Subject: need help with unix semifors, possible OS bug Message-ID: <140@ihnet.UUCP> Date: Wed, 25-Jul-84 09:35:51 EDT Article-I.D.: ihnet.140 Posted: Wed Jul 25 09:35:51 1984 Date-Received: Fri, 27-Jul-84 04:30:14 EDT Organization: AT&T Bell Labs, Naperville, IL Lines: 81 /* sem_vax.c: test of vax/unix semifors */ /* This program seems to run on some machines (e.g. ibm370/unix5.0.3, * 3b-20s/unix5.0.5, etc), * but not on our development machine (i.e. vax11780/unix5.0) * Actually, it does work sometimes, especially under light loads. * Unfortunately, we NEED semifors!!!!! * * This program becomes 4 processes which communicate via one semifor (simple) * Each process does a blocking decrement, a gets(), and an increment * in a forever loop. * The semifor starts at 1, so one process (first up) can decrement and begin. * After the user enters a line, it increments the semifor, * allowing the next-scheduled process to decrement it, and ask for input. * At no time should two processes be waiting on the tty. * It is a simple exclussive-access program. * Sometimes, I enter return, and get two (or more) "waiting input" lines. * Sometimes it is broke from the start, and i get multiple lines * without entering anything. * The problem is not deterministically reproducable. * Try entering input about every 15 seconds, when the Vax is busy. * * Is it just a 5.0 bug that has been fixed? * Should i try to get a later release? * Is it a vax/unix bug? */ #include #include #include #include /* semop structures */ static struct sembuf decr[]= { {0,-1,SEM_UNDO} }; static struct sembuf incr[]= { {0,1,SEM_UNDO} }; main(){ key_t mkey; int id2; mkey = (getgid()<<16); id2 = semget(mkey,1,IPC_CREAT|0660); if(id2<0){ puts("bad semget"); exit(0); } if(semctl(id2,0,SETVAL,1)){ puts("cannot set value to 0 (initially)"); exit(0); } printf("val %d\n",semctl(id2,0,GETVAL,0)); /* one process becomes 4 */ fork(); fork(); while(1){ char s[30]; if(semop(id2,decr,1)<0) printf("bad call to semop(), errno %d\n",errno); /* mutual-exclussive begin */ printf("process %d, val %d: awaiting input\n" ,getpid(),semctl(id2,0,GETVAL,0)); gets(s); /* Using sleep(15) instead of gets(s) does not help */ /* mutual-exclussive end */ if(semop(id2,incr,1)<0) printf("bad call to semop(), errno %d\n",errno); } } -- Karl Dahlke ihnp4!ihnet!eklhad