关于mmap的一个问题
有个疑问,文件lock.c有以下三个函数:
pthread_mutex_t* mptr;
int lock_init()
{
int fd;
pthread_mutexattr_t mattr;
fd = open("/dev/zero", O_RDWR, 0);
if (fd == -1)
{
ibslog(1, 5, __FILE__, __LINE__, "open err");
return -1;
}
mptr = (pthread_mutex_t*)mmap(0, sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (mptr == (void *)-1)
{
ibslog(1, 5, __FILE__, __LINE__, "mmap err");
return -1;
}
close(fd);
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(mptr, &mattr);
return 0;
}
/*上锁*/
void lock_wait()
{
pthread_mutex_lock(mptr);
}
/*解锁*/
void lock_release()
{
pthread_mutex_unlock(mptr);
}