wy
unread,Jul 20, 2012, 4:21:46 AM7/20/12You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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 */