Opened 3 years ago

Last modified 2 years ago

#13795 closed bug

flock may be used multiple times from the same process — at Initial Version

Reported by: pulkomandy Owned by: nobody
Priority: normal Milestone: Unscheduled
Component: System/ Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
Platform: All


This is supposed to work (ie. the second lock should fail because the first one is held):

#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>

int main(void)
        int fd = open("flock", O_TRUNC | O_CREAT | O_WRONLY, 0666);
        int fd2 = open("flock", O_TRUNC | O_CREAT | O_WRONLY, 0666);

        if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
                puts("Failed to grab first lock");
                return -1;

        if (flock(fd2, LOCK_EX | LOCK_NB) == 0) {
                puts("Managed to grab second lock");
                return -1;

        return 0;

Basically, flock is not owned by the team, but by the file descriptor. This also explains the behavior with regard to fork().

We are currently using a session_id (pid_t) as an identifier for locks. Could we switch to using the file descriptor number instead, or would that break something?

Change History (0)

Note: See TracTickets for help on using tickets.