Sourceware Bugzilla – Attachment 5671 Details for
Bug 12674
sem_post/sem_wait race causing sem_post to return EINVAL
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
the test program, to be run in gdb as described
semtest.c (text/x-csrc), 2.32 KB, created by
Don Hatch
on 2011-04-14 06:33:17 UTC
(
hide
)
Description:
the test program, to be run in gdb as described
Filename:
MIME Type:
Creator:
Don Hatch
Created:
2011-04-14 06:33:17 UTC
Size:
2.32 KB
patch
obsolete
>/* > * semtest.c > * > * test program for sem_post/sem_wait race bug, > * provided by Alberto Bertogli for gentoo bug 93366 > * ( http://bugs.gentoo.org/show_bug.cgi?id=93366 ) > * with later modifications by Don Hatch. > * > * Compile and run with something like: > * gcc -Wall -g semtest.c -lpthread -o semtest > * > * Fails extremely infrequently on AMD64; > * to make it fail reliably requires a debugger > * with thread-specific breakpoints: > * stop thread 2 (waiter) in sem_wait, > * stop thread 3 (poster) at the "cmpq $0x0,0x8(%rdi)" in sem_post, > * let thread 2 (waiter) resume and stop it in free, > * let thread 3 (poster) resume, > * it fails with "sem_post() in poster: Invalid argument". > */ > >#include <stdio.h> >#include <stdlib.h> >#include <sched.h> >#include <pthread.h> >#include <semaphore.h> >#include <string.h> > >/* we show output every N loops to show we're still alive */ >#define N 500000 > >/* sem is used to coordinate the waiter and the poster */ >sem_t sem; > >/* varsem is the semaphore created and destroyed */ >sem_t *varsem; > > >/* thread 2 */ >void *waiter(void *unused) { > unsigned int count = 0; > for (;;) { > /* create a semaphore */ > varsem = malloc(sizeof(sem_t)); > sem_init(varsem, 0, 0); > > /* let the poster know he can post */ > sem_post(&sem); > > /* wait for poster's post */ > while (sem_wait(varsem) != 0) > perror("sem_wait() in waiter"); > > /* destroy the semaphore */ > if (sem_destroy(varsem) != 0) { > perror("destroy failed"); > } > > /* trash the semaphore memory before freeing it */ > memset(varsem, '\1', sizeof(sem_t)); > > free(varsem); > > count++; > if ((count % N) == 0) { > printf("waiter\t%u\n", count); > fflush(stdout); > } > } >} > >/* thread 3 */ >void *poster(void *unused) { > unsigned int count = 0; > for (;;) { > /* wait for waiter's notification that we can post varsem */ > while (sem_wait(&sem) != 0) > perror("sem_wait() in poster"); > > /* post varsem */ > if (sem_post(varsem) != 0) { > perror("sem_post() in poster"); > exit(1); > } > > count++; > if ((count % N) == 0) { > printf("poster\t%u\n", count); > fflush(stdout); > } > > } >} > > >int main(void) { > pthread_t t1, t2; > > sem_init(&sem, 0, 0); > printf("sem: %p\n", &sem); > fflush(stdout); > > pthread_create(&t1, NULL, &waiter, NULL); > pthread_create(&t2, NULL, &poster, NULL); > > pthread_join(t1, NULL); > pthread_join(t2, NULL); > > sem_destroy(&sem); > return 0; >} >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 12674
: 5671 |
6206
|
7196