On Thu, 24 Jan 2002, guy keren wrote:
>
> On Thu, 24 Jan 2002, Shlomi Fish wrote:
>
> > Actually, I checked it and now it is working. The problem I encountered
> > was that several readers seem to starve the writers. Especially thesecond
> > writer out of two.
>
> show us the code, and state the architecture it's running on (kernel
> version, glibc version? 32 bit files or 64 bit files?) - perhaps its a
> kernel misshap, and perhaps its something in your code. don't be too quick
> to judge that the problem is not yours.
>
The code is attached to this message. Compile it with gcc. Platform:
Linux Kernel 2.4.13-12mdk ( I think it is based on the old AC tree)
glibc 2.2.4-6mdk
Linux Mandrake 8.1
I'm running it on ReiserFS. Don't know about 64-bit files.
> > Should I port my FCFS RWLock to the Linux kernel?
>
> why? did you read the code of the locks that are currently there? your
> lock will have hard time competing with them, if you don't re-write it in
> assembly ;)
>
I'm talking so it will be available for the flock() system call and
friends... That code is in /usr/src/linux/fs/locks.c.
> > > please clear this up again and more.
> >
> > I hope I did. It seems flock() is thread-enabled, so one may choose to use
> > it. But like I said, there seems to be a starvation problem. I suppose one
> > can write a more elaborate lock by putting the arbitration in a dedicated
> > process, and letting it inform the other processes and threads of the time
> > their priviliges were granted by some IPC mechanism.
>
> this is quite an overkill, unless you wish to slow down your system
> because its running too fast. or unless the resource processing itself
> takes a rather long time, in which this complicated locking would not ad
> much to it.
>
Can you think of another solution in case:
1. flock() and friends are not FCFS.
2. You have several processes and several threads in the situation that I
described.
I'd be happy to hear of a better one.
Regards,
Shlomi Fish
> --
> guy
>
> "For world domination - press 1,
> or dial 0, and please hold, for the creator." -- nob o. dy
>
----------------------------------------------------------------------
Shlomi Fish [EMAIL PROTECTED]
Home Page: http://t2.technion.ac.il/~shlomif/
Home E-mail: [EMAIL PROTECTED]
"Let's suppose you have a table with 2^n cups..."
"Wait a second - is n a natural number?"
-- Attached file included as plaintext by Listar --
-- File: flock.c
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define filename "mylock"
#define NUM_READERS 5
#define NUM_WRITERS 2
struct context_struct
{
int writer;
int index;
};
typedef struct context_struct context_t;
void * reader_thread(void * void_context)
{
context_t * context;
int fd;
context = (context_t *)void_context;
fd = open(filename, O_RDONLY);
while (1)
{
flock(fd, LOCK_SH);
printf("Reader %i - Lock!\n", context->index);
usleep(rand()%1000000);
printf("Reader %i - Unlock!\n", context->index);
flock(fd, LOCK_UN);
usleep(rand()%1000000);
}
close(fd);
return NULL;
}
void * writer_thread(void * void_context)
{
context_t * context;
int fd;
context = (context_t *)void_context;
fd = open(filename, O_RDONLY);
while (1)
{
flock(fd, LOCK_EX);
printf("Writer %i - Lock!\n", context->index);
usleep(rand()%1000000);
printf("Writer %i - Unlock!\n", context->index);
flock(fd, LOCK_UN);
usleep(rand()%1000000);
}
close(fd);
return NULL;
}
int main(int argc, char * argv[])
{
context_t * context;
pthread_t readers[NUM_READERS];
pthread_t writers[NUM_WRITERS];
int check;
int a;
for(a=0;a<NUM_READERS;a++)
{
context = malloc(sizeof(context));
context->index = a;
context->writer = 0;
check = pthread_create(
&readers[a],
NULL,
reader_thread,
context
);
if (check != 0)
{
fprintf(stderr, "Could not create Reader #%i!\n", a);
exit(-1);
}
}
for(a=0;a<NUM_WRITERS;a++)
{
context = malloc(sizeof(context));
context->index = a;
context->writer = 0;
check = pthread_create(
&writers[a],
NULL,
writer_thread,
context
);
if (check != 0)
{
fprintf(stderr, "Could not create Reader #%i!\n", a);
exit(-1);
}
}
while (1)
{
sleep(1);
}
}
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]