System N history correction

Hokey hokey at gang.UUCP
Sat Dec 15 02:46:30 AEST 1984


> > 	mpx(2): file multiplexor, replaced only in V8 by stream I/O
> You miss mpx(2)?  You're the only one I've met who does.
> There are FIFO's, message queues, semaphores and shared memory if you want
> IPC.

Michael, I couldn't pass this up.  Semaphores?  I wouldn't call them that.
What was implemented is a STACK, not a QUEUE.  Can people imagine how useful
this really is?  Just think, the sooner a process wants something the later
it gets it!

Some time ago, Somebody posted a program to test the semaphore system because
their implementation occasionally woke two processes at the same time.  (I
include it at the end of my posting.)  While we never had the bug they found,
I did notice that the first two (out of four) processes in the test NEVER
execute, and the last two simply trade off the resource.  This is not at all
surprising, given the semaphore mechanism implements a stack instead of
something useful.

Anybody care to disagree with me?

Hokey
-----
/* sem_vax.c: test of vax/unix semaphors */

/* 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 semaphors!!!!! 
*
*  This program becomes 4 processes which communicate via one semaphor (simple)
*  Each process does a blocking decrement, a gets(), and an increment
*  in a forever loop.
*  The semaphor starts at 1, so one process (first up) can decrement and begin.
*  After the user enters a line, it increments the semaphor,
*  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 <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

/* 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);

	}
}
-- 
Hokey           ..ihnp4!plus5!hokey
		  314-725-9492



More information about the Comp.unix.wizards mailing list