Skip to content

ex17-advisory-locking should not work #1

@b1ns4oi

Description

@b1ns4oi
if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid > 0) {           /* parent */
        for (i = 0; i < NLOOPS; i += 2) {
            sleep(1);
            printf("parent: before lock\n");
            writew_lock(fd, 0, SEEK_SET, 1);
            printf("parent: got lock\n");

            if ((counter = update((long *)area)) != i)
                err_quit("parent: expected %d, got %d", i, counter);

            un_lock(fd, 0, SEEK_SET, 1);
            printf("parent: unlock\n");
        }
    } else {                        /* child */
        for (i = 1; i < NLOOPS + 1; i += 2) {
            sleep(1);
            printf("child: before lock\n");
            writew_lock(fd, 0, SEEK_SET, 1);
            printf("child: got lock\n");

            if ((counter = update((long *)area)) != i)
                err_quit("child: expected %d, got %d", i, counter);

            un_lock(fd, 0, SEEK_SET, 1);
            printf("child: unlock\n");
        }
    }

After parent un_lock(fd, 0, SEEK_SET, 1); , parent itself can still have a change to acquire the lock again (althrough there's a sleep(1) here, but it just can't make sure child always go next).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions