Go to:
Gentoo Home
Documentation
Forums
Lists
Bugs
Planet
Store
Wiki
Get Gentoo!
Gentoo's Bugzilla – Attachment 269761 Details for
Bug 93366
sys-libs/glibc: sem_wait() and sem_post() race
Home
|
New
–
[Ex]
|
Browse
|
Search
|
Privacy Policy
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
modified semtest.c
semtest.c (text/plain), 1.76 KB, created by
Don Hatch
on 2011-04-13 07:05:08 UTC
(
hide
)
Description:
modified semtest.c
Filename:
MIME Type:
Creator:
Don Hatch
Created:
2011-04-13 07:05:08 UTC
Size:
1.76 KB
patch
obsolete
>/* Compile and run with something like: > * gcc -Wall -g semtest.c -lpthread -o semtest > * valgrind --tool=memcheck ./semtest > */ > >#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 93366
:
59404
|
59405
| 269761