Fixing Posix semaphores

Garrett Wollman wollman at khavrinen.lcs.mit.edu
Tue Dec 21 19:41:48 PST 2004


In article <1103681460.30309.799.camel at zircon.zircon.seattle.wa.us> you write:

>So, there are words there that can be interpreted many different ways.
>The most restricted way to view them is as 14-character names optionally
>beginning with a slash.  That also seems to me to be the stupidest way
>to view them.  Robert's idea of semfs seems brilliant, allowing multiple
>name spaces for jailed processes.  I plan to start thinking and working
>on that idea shortly.

Here is an even easier way that avoids creating a special file type...

sem_open() can be implemented as:

	fd = shm_open(name, oflag, omode);
	if (oflag & O_CREAT) {
		ftruncate(fd, sizeof(struct sem_private));
	}
	sem_private = mmap(fd, ...);
	sem_private->fd = fd;
	sem = malloc(sizeof *sem);
	_sem_init(sem, sem_private, value);
	return (sem);

The underlying semaphore can then be implemented in the standard way
with a mutex, a condition variable (or simulation thereof), and a
counter in the shared-memory region -- no (additional) kernel code
required.  (This avoids the overhead of a system call if there is no
need to wait.)

Alternatively, given the current kernel implementation, you can very
simply convert any vnode into a 20-byte flat name for the file using
VFS_VPTOFH() -- this would require only minimal changes to the
existing code.

-GAWollman



More information about the freebsd-arch mailing list