Subject: Re: threading example
To: Chris Wareham <chris.wareham@iosystems.co.uk>
From: Pramod Srinivasan <pramodsri@yahoo.com>
List: netbsd-help
Date: 06/07/2002 16:54:42
Hi,
Thanks for the reply...Sorry I was not clear in my
previous email...
I was experimenting in inter process locking, reading
through the pthreads book I see that we can use
pthread_mutex_lock across processes if we have the
mutex in memory shared across processes, it appears to
me that NetBSD 1.5.1_ALPHA does have the pthread
library which supports mutex to be shared across
processes. In the code below, I expected that one of
the processes should have blocked on the mutex, but
the output show that both are running...not able to
understand why?
code
----
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include </usr/pkg/include/pthread.h>
pthread_mutex_t *mutex;
pthread_mutexattr_t mattr;
void process1() {
int i;
printf("process 1\n");
pthread_mutex_lock(mutex);
for (;;) {
printf("Working in process 1\n");
for (i=0; i<10000000; i++);
}
}
void process2() {
int i;
printf("process 2\n");
pthread_mutex_lock(mutex);
for (;;) {
printf("****Working in process 2****\n");
for (i=0; i<10000000; i++);
}
}
main() {
int fd;
fd = open("/dev/zero", O_RDWR);
mutex = (pthread_mutex_t *)mmap(NULL,
sizeof(pthread_mutex_t),
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr,
PTHREAD_PROCESS_SHARED);
pthread_mutex_init(mutex, &mattr);
if (fork() == 0)
process1();
else
process2();
}
output
-----
process 1
process 2
****Working in process 2****
Working in process 1
Working in process 1
****Working in process 2****
****Working in process 2****
Working in process 1
****Working in process 2****
****Working in process 2****
Working in process 1
****Working in process 2****
Working in process 1
****Working in process 2****
Working in process 1
****Working in process 2****
****Working in process 2****
****Working in process 2****
Working in process 1
****Working in process 2****
Working in process 1
****Working in process 2****
^C
--- Chris Wareham <chris.wareham@iosystems.co.uk>
wrote:
> Pramod Srinivasan wrote:
> >
> > I am trying interprocess locking using
> > pthread_mutex_lock but have not been able to get
> it
> > work, here is the test program and the output,
> what is
> > wrong in the program?
> >
>
> There is no need to call fork() when using threads,
> as the whole idea
> is to save the overhead of creating multiple
> 'heavyweight' processes.
> Assuming you want thread 2 to block until it gets a
> lock on the mutex,
> try the following code:
>
> /* Tested on Linux as I don't have access to a
> NetBSD machine from
> my current location. If the pthread emulation
> package you are
> using is any good, then this should work for
> you. */
>
> #include <stdio.h>
> #include </usr/pkg/include/pthread.h>
>
> static pthread_mutex_t global_mutex =
> PTHREAD_MUTEX_INITIALIZER;
>
> static void *thread1(void *);
> static void *thread2(void *);
>
> int main(int argc, char *argv[]) {
> pthread_t p1, p2;
>
> if(pthread_create(&p1, NULL, thread1, NULL)) {
> fputs("Unable to create first thread\n",
> stderr);
> return 1;
> }
>
> if(pthread_create(&p2, NULL, thread2, NULL)) {
> fputs("Unable to create second thread\n",
> stderr);
> return 1;
> }
>
> pthread_join(p1, NULL);
>
> return 0;
> }
>
> void *thread1(void *data) {
> unsigned i;
>
> fputs("Thread 1\n", stderr);
> pthread_mutex_lock(&global_mutex);
> while(1) {
> fputs("Working in thread 1\n", stderr);
> for(i = 0; i < 10000000; i++);
> }
>
> return NULL;
> }
>
> void *thread2(void *data) {
> unsigned i;
>
> fputs("Thread 2\n", stderr);
> pthread_mutex_lock(&global_mutex);
> while(1) {
> fputs("**** Working in thread 2 ****\n",
> stderr);
> for(i = 0; i < 10000000; i++);
> }
>
> return NULL;
> }
>
> Chris
>
> --
> chris.wareham@iosystems.co.uk (work)
> cwareham@btinternet.com (home)
>
__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com