3

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;

}
4

1 に答える 1

1

デッドロックの場合、少なくとも2 つのロックが必要です。私はいつも自分が部屋 B の鍵で部屋 A に閉じ込められ、部屋 A の鍵で部屋 B に他の誰かが閉じ込められていると想像します。

あなたの例では、ロックは1 つだけです。両方の子が同じ大きな「ドア」(ファイル全体) をロックしようとします。そこに着いた2番目の人は、最初の人が再びロックを解除するまでブロックし、同じ小さな歌「ロック、スリープ、... ロック解除」を歌います。デッドロックは見えません。

さて、以下の例では、親は2 つの「ドア」をロックします。ファイルの 1 番目と 2 番目のバイトはfd(この例ではこれが本当に必要なのでしょうか?) を指し、2 つの子を生成します。両方の子が両方のバイトをロックしようとしますが、それぞれが異なるバイトで始まります。親が両方のバイトを解放するとすぐに、子はロックを取得します。全部で 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)
于 2014-12-29T22:42:10.880 に答える