What happens when a process has exclusive lock on a file and another process tries to write on it?

174 Views Asked by At

I'm trying to check what happens if a process has an exclusive lock on a file and another process try to write on this file.

Will it fail or just block until the lock is lifted ?

To get an answer, I tried write something but it doesn't seem to be working I also tried to lock the same file from another program but it worked ?? I don't get any error when it should be impossible.

Am I doing something wrong ?

lock.c

    int main()
{
    int fd = open("okas", O_RDWR);
    int n = flock(fd, LOCK_EX);
    if (n == -1 )
    {
        perror("ok");
    }
    while (1)
    {
    }
}

write.c

    int main()
{
    int fd = open("okas", O_RDWR);

    int n = write(fd, "1", 1);
    printf("%d", n);
}
0

There are 0 best solutions below