vonzhou 发表于 2013-07-18 20:35

APUE,死锁发生时内核的策略

这个例子是APUE 程序清单14-4.#include "apue.h"
#include<fcntl.h>

#include "TELL_WAIT.c"
#include "lock_reg.c"

static void lockabyte(const char *name,pid_t pid,int fd,off_t offset){
if(writew_lock(fd,offset,SEEK_SET,1) <0)
        printf("%s :writew_lock error\n",name);

printf("%s-%d :got the lock ,byte %ld\n",name ,pid,offset);
}

int main(void){

int fd;
pid_t pid;

if((fd=creat("templock",FILE_MODE)) <0)
        printf("create error\n");
if(write(fd,"abc",3) !=3)
       
printf("write to file error\n");

TELL_WAIT();

if((pid=fork()) <0){
printf("fork error\n");
}else if(pid==0){
lockabyte("child",getpid(),fd,0);
TELL_PARENT(getppid());
WAIT_PARENT();
lockabyte("child",getpid(),fd,1);
}else{
lockabyte("parent",getpid(),fd,1);
TELL_CHILD(pid);
WAIT_CHILD();
lockabyte("parent",getpid(),fd,0);
}

exit(0);

}


为什么运行结果是:
parent-3269 :got the lock ,byte 1
child-3270 :got the lock ,byte 0
parent :writew_lock error
parent-3269 :got the lock ,byte 0
child-3270 :got the lock ,byte 1
内核把死锁解决了??

页: [1]
查看完整版本: APUE,死锁发生时内核的策略