Subject: None
To: None <netbsd-help@netbsd.org>
From: Pramod Srinivasan <pramodsri@yahoo.com>
List: netbsd-help
Date: 06/06/2002 15:37:57
Hi,

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?

I am using  NetBSD 1.5.1_ALPHA

Any help is greatly appreciated.

Thanks,
--Pramod

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

process 2 should have got block here right?



__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com