Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pthread rwlock example

2,760 views
Skip to first unread message

wy

unread,
Jul 20, 2012, 4:21:46 AM7/20/12
to
Hello everyone!
I'm reading Richard Stevens' UNP22E.
And this is an example(named rwlock.c) from the source code, checking
whether rwlock works as "readers shared, writers exclusive".
It works when the No. of readers and writers are both not big.
( ./a.out -n 1000 -r 10 -w 10 )
But turns out "Segmentation fault" when they increase.
( ./a.out -n 1000 -r 1000 -w 1000 )
I've modified the code so that you can compile it directly.
( gcc rw_lock.c -lpthread )

Can someone explain it for me? I'm using Linux 2.6.32-5-amd64
Thanks for your help.


/* this is the start */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define MAXNTHREADS 100

void *reader(void *), *writer(void *);

int nloop = 1000, nreaders = 6, nwriters = 4;

struct {
pthread_rwlock_t rwlock;
pthread_mutex_t rcountlock;
int nreaders;
int nwriters;
} shared = { PTHREAD_RWLOCK_INITIALIZER, PTHREAD_MUTEX_INITIALIZER };

int
main(int argc, char **argv)
{
int c, i;
pthread_t tid_readers[MAXNTHREADS], tid_writers[MAXNTHREADS];

while ( (c = getopt(argc, argv, "n:r:w:")) != -1) {
switch (c) {
case 'n':
nloop = atoi(optarg);
break;

case 'r':
nreaders = atoi(optarg);
break;

case 'w':
nwriters = atoi(optarg);
break;
}
}
if (optind != argc)
fprintf(stderr, "usage: test1 [-n #loops ] [ -r #readers ] [ -w
#writers ]\n"), exit(1);

/* 4create all the reader and writer threads */
for (i = 0; i < nreaders; i++)
pthread_create(&tid_readers[i], NULL, reader, NULL);
printf("readers created\n");
for (i = 0; i < nwriters; i++)
pthread_create(&tid_writers[i], NULL, writer, NULL);
printf("writers created\n");
/* wait for all the threads to complete */
for (i = 0; i < nreaders; i++)
pthread_join(tid_readers[i], NULL);
printf("readers done\n");
for (i = 0; i < nwriters; i++)
pthread_join(tid_writers[i], NULL);
printf("readers done\n");
exit(0);
}

void *
reader(void *arg)
{
int i;

sleep(1);
for (i = 0; i < nloop; i++) {
if (pthread_rwlock_rdlock(&shared.rwlock) < 0)
perror("rdlock");

if (pthread_mutex_lock(&shared.rcountlock) < 0)
perror("mutex lock");
shared.nreaders++; /* shared by all readers; must protect */
if (pthread_mutex_unlock(&shared.rcountlock) < 0)
perror("mutex unlock");

if (shared.nwriters > 0)
fprintf(stderr, "reader: %d writers found\n", shared.nwriters), exit(1);

if (pthread_mutex_lock(&shared.rcountlock) < 0)
perror("mutex lock");
shared.nreaders--; /* shared by all readers; must protect */
if (pthread_mutex_unlock(&shared.rcountlock) < 0)
perror("mutex unlock");

if (pthread_rwlock_unlock(&shared.rwlock) < 0)
perror("unlock");
}
return(NULL);
}

void *
writer(void *arg)
{
int i;

sleep(1);
for (i = 0; i < nloop; i++) {
if (pthread_rwlock_wrlock(&shared.rwlock) < 0)
perror("wrlock");
shared.nwriters++; /* only one writer; need not protect */

if (shared.nwriters > 1)
fprintf(stderr, "writer: %d writers found\n", shared.nwriters), exit(1);
if (shared.nreaders > 0)
fprintf(stderr, "writer: %d readers found\n", shared.nreaders), exit(1);

shared.nwriters--; /* only one writer; need not protect */
if (pthread_rwlock_unlock(&shared.rwlock) < 0)
perror("unlock");
}
return(NULL);
}
/* this is the end */

Rainer Weikusat

unread,
Jul 20, 2012, 8:22:08 AM7/20/12
to
wy <warm...@gmail.com> writes:
> I'm reading Richard Stevens' UNP22E.
> And this is an example(named rwlock.c) from the source code, checking
> whether rwlock works as "readers shared, writers exclusive".
> It works when the No. of readers and writers are both not big.
> ( ./a.out -n 1000 -r 10 -w 10 )
> But turns out "Segmentation fault" when they increase.
> ( ./a.out -n 1000 -r 1000 -w 1000 )

[...]

> #define MAXNTHREADS 100

[...]

> int
> main(int argc, char **argv)
> {
> int c, i;
> pthread_t tid_readers[MAXNTHREADS], tid_writers[MAXNTHREADS];

I suggest that you have a closer look at this.

wy

unread,
Jul 20, 2012, 4:23:16 PM7/20/12
to
On 2012/7/20 20:22, Rainer Weikusat wrote:
> #define MAXNTHREADS 100
>
> I suggest that you have a closer look at this.

Oh my god!
I've spent hours on it. Sometime worked, sometime crashed. It drove me
crazy.
Thanks for your help.

Nicolas George

unread,
Jul 20, 2012, 5:01:03 PM7/20/12
to
wy , dans le message <jucenn$264$1...@speranza.aioe.org>, a �crit�:
> I've spent hours on it. Sometime worked, sometime crashed.

valgrind should be a reflex.

James K. Lowden

unread,
Jul 22, 2012, 8:22:08 PM7/22/12
to
On 20 Jul 2012 21:01:03 GMT
Nicolas George <nicolas$geo...@salle-s.org> wrote:

> wy , dans le message <jucenn$264$1...@speranza.aioe.org>, a écrit :
> > I've spent hours on it. Sometime worked, sometime crashed.
>
> valgrind should be a reflex.

Well. Without injury to Stevens, I think we can say boundary checking
should be a reflex. assert(3) at least.

--jkl

0 new messages