Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 6 Sep 2002 10:58:02 -0700
From:      Alfred Perlstein <alfred@freebsd.org>
To:        Michal Mertl <mime@traveller.cz>
Cc:        current@freebsd.org
Subject:   Re: bug in sysv semaphores on -CURRENT
Message-ID:  <20020906175802.GE21806@elvis.mu.org>
In-Reply-To: <Pine.BSF.4.41.0209061411380.77195-200000@prg.traveller.cz>
References:  <Pine.BSF.4.41.0209061411380.77195-200000@prg.traveller.cz>

next in thread | previous in thread | raw e-mail | index | archive | help
* Michal Mertl <mime@traveller.cz> [020906 06:10] wrote:
> There seems to be bug in $SUBJ. When I run attached program on recent
> -CURRENT, it always (after several seconds) triggers the bug. I first
> suspected a problem in the program's logic but on stable in runs just
> fine.
> 
> Esentially I use piece of shm memory to pass some data between several
> processes. I implemented simple locking functions with semaphores and
> noticed it behaves strange on -CURRENT and ok on -STABLE.
> 
> CCing Alfred because he made some changes into the kernel part of $SUBJ. I
> don't expect the bug is new though.
> 
> May I ask someone with older -CURRENT to try running the program for a
> minute?

I found your bug.

In the function ipc_unlock() you do this:

> int
> ipc_unlock(void)
> {
> 	struct sembuf	 sem_buf;
> 	int		 err;
> 
> 	if (ipc_cfg->sem_owner != getpid()) {
> 		fprintf(stderr, "%d: can't unlock (bug), owner: %d\n",
> 			getpid(), ipc_cfg->sem_owner);
> 		return (-1);
> 	}
> 	if (semctl(ipc_cfg->sem_id, 0, GETVAL) != 0) {
> 		fprintf(stderr, "%d: can't unlock (bug), not locked\n",
> 			getpid());
> 		return (-1);
> 	}
> 	printf("%d: ipc_unlock()\n", getpid());
> 	sem_buf.sem_num = 0;
> 	sem_buf.sem_op = 1;
> 	sem_buf.sem_flg = 0;
> 	err = semop(ipc_cfg->sem_id, &sem_buf, 1);
> 	if (err == -1) {
> 		fprintf(stderr, "%d: semop()\n", getpid());
> 		return (-1);
> 	}
> 	ipc_cfg->sem_owner = -1;
> 	return (0);
> }

Problem is that you're messing with lock state after dropping your
semaphore!

If you move the
  ipc_cfg->sem_owner = -1;
to before the semop() call it seems to fix things.

-Alfred

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020906175802.GE21806>