首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

关于记录锁的struct flock结构中返回的pid有关问题

2012-04-14 
关于记录锁的struct flock结构中返回的pid问题1.cC/C++ code/******************************************

关于记录锁的struct flock结构中返回的pid问题

1.c

C/C++ code
/**************************************************************************************//*        关于记录锁的struct flock结构中返回的pid问题        返回的不是持有锁的那个进程ID吗?  *//*************************************************************************************/#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <errno.h>#include "SynchroProcess.h"int lock_reg(int fd, int cmd, int l_type, off_t l_start,                 short where, off_t l_len){    struct flock lock;        lock.l_type = l_type;    lock.l_whence = where;    lock.l_start = l_start;    lock.l_len = l_len;        return fcntl(fd, cmd, &lock);}#define read_lock(fd, offset, where, len) \            lock_reg(fd, F_SETLK, F_RDLCK, offset, where, len)#define readw_lock(fd, offset, where, len) \            lock_reg(fd, F_SETLKW, F_RDLCK, offset, where, len)            #define write_lock(fd, offset, where, len) \            lock_reg(fd, F_SETLK, F_WRLCK, offset, where, len)#define writew_lock(fd, offset, where, len) \            lock_reg(fd, F_SETLKW, F_WRLCK, offset, where, len)#define un_lock(fd, offset, where, len) \            lock_reg(fd, F_SETLK, F_UNLCK, offset, where, len)pid_t lock_test(int fd, int type, off_t start,                 short where, off_t len){    struct flock lock;            lock.l_type = type;    lock.l_start = start;    lock.l_whence = where;    lock.l_len = len;            if (fcntl(fd, F_GETFL, &lock) < 0)    {        printf("fcntl error!\n");    }        if (lock.l_type == F_UNLCK)    {        return 0;    }        return lock.l_pid;        }static void lockabyte(const char *name, int fd, off_t offset){    if (writew_lock(fd, offset, SEEK_SET, 1) < 0)    {        printf("writew_lock error!\n");    }        printf("%s: got the lock, byte %ld\n", name, offset);}                 int main(){    int fd;    pid_t pid;        if ((fd = creat("temp", O_RDWR )) < 0)    {        printf("creat error!\n");    }        if (write(fd, "ab", 2) != 2)    {        printf("write error!\n");    }        TELL_WAIT();    if ((pid = fork()) < 0)    {        printf("fork error!\n");    }    else if (pid == 0)    {        printf("int child!\n");        lockabyte("temp", fd, 0);        TELL_PARENT(getppid());        WAIT_PARENT();            //返回的不是持有锁的那个进程ID吗?怎么不是父ID啊?    printf("l_pid = %d ppid = %d  pid = %d\n",         lock_test(fd, F_GETFL, 1, SEEK_SET, 1),             getppid(), getpid());                //lockabyte("temp", fd, 1);        //这里产生死锁。。。。        TELL_PARENT(getppid());    }    else    {        WAIT_CHILD();        printf("in parent!\n");        lockabyte("temp", fd, 1);        TELL_CHILD(pid);        WAIT_CHILD();    }                    return 0;}运行结果:[root@localhost work1]# gcc *.c -o 1[root@localhost work1]# ./1int child!temp: got the lock, byte 0in parent!temp: got the lock, byte 1l_pid = 1104221896 ppid = 4788  pid = 4789                 //lock__pid返回的是什么啊?[root@localhost work1]# 


SynchroProcess.h
C/C++ code
//#include "SynchroProcess.h"#include <stdio.h>#include <stdlib.h>#include <unistd.h>#ifndef SYNCHRO_PROCESS#define SYNCHRO_PROCESSvoid TELL_WAIT(void);                    //提示有个等待void TELL_PARENT(pid_t pid);            //告诉父亲等待void TELL_CHILD(pid_t pid);            //告诉孩子等待void WAIT_PARENT( void );                //发信息解除父亲等待void WAIT_CHILD( void );                //发信息解除孩子等待#endif


SynchroProcess.c
C/C++ code
/*********************************************************************************//*    进程同步的函数实现及其应用。*//********************************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <signal.h>#include "SynchroProcess.h"#include <sys/types.h>static volatile int sigflag; static sigset_t newmask, oldmask,zeromask;static void sig_usr(int signo){    sigflag = 1;} void TELL_WAIT(void){    if (signal(SIGUSR1, sig_usr) == SIG_ERR)    {        printf("signal1 error!\n");    }        if (signal(SIGUSR2, sig_usr) == SIG_ERR)    {        printf("signal2 error!\n");    }        sigemptyset(&zeromask);    sigemptyset(&newmask);    sigaddset(&newmask, SIGUSR1);    sigaddset(&newmask, SIGUSR2);        if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)    {        printf("sigprocmask error!");    }}void TELL_PARENT(pid_t pid){    kill(pid, SIGUSR2);}void TELL_CHILD(pid_t pid){    kill(pid, SIGUSR1);}void WAIT_PARENT( void ){    while (sigflag == 0)    {        sigsuspend(&zeromask);    }        sigflag = 0;        if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)    {        printf("SIG_SETMASK error!\n");    }}void WAIT_CHILD( void ){    while (sigflag == 0)    {        sigsuspend(&zeromask);    }        sigflag = 0;        if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)    {        printf("SIG_SETMASK error!\n");    }}


[解决办法]
父进程在wait,肯定是孩子锁住啊,有问题?

热点排行