当多个进程使用F_SETLKW锁定相同的fd时,为什么我看不到死锁(EDEADLK)?

时间:2014-12-29 19:21:53

标签: c unix deadlock fcntl

我需要妥善处理EDEADLK。在我的程序中,我看到两个孩子都等到父母睡觉,然后他们应用锁并立即离开它。对不起我的错误,我是一名西班牙学生。

int main(){

    pid_t childpid, childpid2;

    struct flock cerrojo;
    int fd, status;

    if ((fd=open("prueba", O_RDWR)) == -1) 
        perror("apertura fallo");

    cerrojo.l_type =F_WRLCK;
    cerrojo.l_whence =SEEK_SET;
    cerrojo.l_start =0;
    cerrojo.l_len =0;

    if (fcntl(fd, F_SETLK,&cerrojo) == -1) 
        perror("cerrojo fallo");

    if ((childpid= fork()) == -1) {
        printf("Could not create child");
        exit(-1);
    }

    if(childpid){

        if ((childpid2= fork()) == -1) {
            printf("Could not create child");
            exit(-1);
        }

        if(childpid2){

            cerrojo.l_type = F_UNLCK;
            cerrojo.l_whence =SEEK_SET;
            cerrojo.l_start =0;
            cerrojo.l_len =0;

            sleep(2);

            fcntl(fd, F_SETLKW, &cerrojo);

            waitpid(childpid,&status,0);
            waitpid(childpid2,&status,0);

        }


    }

    if(!childpid||!childpid2){

        printf("Soy el hijo %d\n",getpid());

        if(fcntl(fd, F_SETLKW,&cerrojo) == -1){ 
            printf("FCNTL FALLA EN PID: %d\n",getpid());
            sleep(1);
        }

        printf("PID %d va a quitar el cerrojo.\n",getpid());

        cerrojo.l_type = F_UNLCK;
        cerrojo.l_whence =SEEK_SET;
        cerrojo.l_start =0;
        cerrojo.l_len =0;

        fcntl(fd, F_SETLKW, &cerrojo);

        printf("HIJO ACABADO\n");

    }

    return 0;

}

1 个答案:

答案 0 :(得分:0)

对于死锁,您至少需要两个锁。我总是想象自己被锁在房间A,房间B的钥匙和房间B的其他人,房间A的钥匙。

在您的示例中,只有一个锁:两个孩子都试图锁定相同的大“门”(整个文件)。到达那里的第二个人将阻止,直到第一个再次释放锁定,然后唱出同样的litle歌曲锁定,睡眠,...解锁。看不到僵局。

现在,在下面的示例中,父锁定两个“门” - 文件fd的第一个和第二个字节指向(顺便说一句,这对您的示例来说真的是必要的?)然后产生两个孩子。两个孩子都试图锁定两个字节,但每个字节都以不同的字节开头。一旦父母释放两个字节,孩子们就会获得他们的锁,总共有4次锁定尝试,但是最后一次会导致死锁,并且EDEADLK适时失败,所以每个人都过着快乐的生活 - 感谢我们明智的而且只是内核。

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main(){

    pid_t childpid, childpid2;

    struct flock cerrojo;
    int fd, status;

    if ((fd=open("prueba", O_RDWR)) == -1) 
        perror("apertura fallo");

    cerrojo.l_type   = F_WRLCK;
    cerrojo.l_whence = SEEK_SET;
    cerrojo.l_start  = 0;
    cerrojo.l_len    = 2; /* lock "doors" (i.e. bytes)  0 and 1*/

    if (fcntl(fd, F_SETLK,&cerrojo) == -1) 
        perror("cerrojo fallo");


    if((childpid= fork())){ /* parent */
      if ((childpid2= fork())) { /* still parent */

            cerrojo.l_type = F_UNLCK;
            cerrojo.l_len  = 2; /* unlock both doors: let the fun begin :-) */

            sleep(2);
            printf("Tata va a quitar el cerrojo....\n",getpid());
            fcntl(fd, F_SETLKW, &cerrojo); 

            waitpid(childpid,&status,0);
            waitpid(childpid2,&status,0);
        }
    }

    if(!childpid||!childpid2){ /* in child 1 or child 2 */

        printf("Soy el hijo %d\n",getpid());

        int offset0 = (childpid ? 0 : 1);  /* child1 gets 0 and 1, child 2 gets 1 and 0 */
        int offset1 = (childpid ? 1 : 0);
        cerrojo.l_len = 1;


        cerrojo.l_start =  offset0; /* lock door 0 (1) as soon as parent lets us*/
        printf("PID %d locking byte %d\n", getpid(), offset0);
        if(fcntl(fd, F_SETLKW,&cerrojo) == -1){ 
          printf("CERROJO byte %d FALLA EN PID %d (%s)\n",offset0, getpid(),  strerror(errno));
        }

        sleep(1); /* guarantee that the other child has our next door locked ... */
        printf("PID %d locking byte %d\n", getpid(), offset1);
        cerrojo.l_start =  offset1; /* lock door 1 (0). The second of both children who gets here closes the circle and faces deadlock */
        if(fcntl(fd, F_SETLKW,&cerrojo) == -1){
          printf("CERROJO byte %d FALLA EN PID: %d (%s)\n", offset1, getpid(), strerror(errno));
        }

        printf("HIJO %d ACABADO (releasing its lock)\n", getpid()); /* Falling off the end of main() will release the lock anyway */

    }
}

输出:

[hlub@karpaten] ~ > ./test                 
Soy el hijo 29711
PID 29711 locking byte 1
Soy el hijo 29712
PID 29712 locking byte 0
Tata va a quitar el cerrojo....
PID 29711 locking byte 0
PID 29712 locking byte 1
CERROJO byte 1 FALLA EN PID: 29712 (Resource deadlock avoided)
HIJO 29712 ACABADO (releasing its lock)
HIJO 29711 ACABADO (releasing its lock)