logo
down
shadow

Why don't I see deadlock (EDEADLK) when several processes lock the same fd with F_SETLKW?


Why don't I see deadlock (EDEADLK) when several processes lock the same fd with F_SETLKW?

By : Daoui Meriem
Date : November 17 2020, 04:28 AM
I wish did fix the issue. For deadlock you need at least two locks. I always imagine myself locked in room A, with the key of room B, and someone else in room B, with the key of room A.
In your example there is only one lock: both children try to lock the same big "door" (the whole file). The second who gets there will block until the first releases the lock again, and then sing the same litle song Lock, sleep,... unlock. No deadlock in sight.
code :
#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)


Share : facebook icon twitter icon
How upgrade lock helps avoid deadlock in reader/writer lock

How upgrade lock helps avoid deadlock in reader/writer lock


By : Atikan Klamchai
Date : March 29 2020, 07:55 AM
like below fixes the issue It's funny that I have just answered the same question for SQL Server and UPDATE statements: How do UPDATE locks prevent a common form of deadlock?
The key aspect to the answer is that upgradable transactions never take an S-lock. They immediately U-lock. Therefore, there is never a conflict between an S-lock and an X-lock.
code :
lock.AcquireSharedLock()
SQL Server is allowing X lock on shared lock which is cause of deadlock

SQL Server is allowing X lock on shared lock which is cause of deadlock


By : Adarshdeep Singh
Date : March 29 2020, 07:55 AM
Does that help The table has departmentid, name, groupname,modifieddate.
Departmentid has clustered index and name has nonclustered index and other columns - groupname and modifieddate are included columns. This what happened.
code :
------Session 1----------
set transaction isolation level repeatable read
begin transaction
select * from HumanResources.Department with (index = Clusteredindexname)
what is effect of deadlock on other processes which are not involve in deadlock?

what is effect of deadlock on other processes which are not involve in deadlock?


By : Sagar
Date : March 29 2020, 07:55 AM
hope this fix your issue Only that processes which are in deadlock will.
And it's one of the reasons that most of modern personal computers ignore it.
A deadlock was detected while trying to lock variables. A lock cannot be acquired after 16 attempts. The locks timed out

A deadlock was detected while trying to lock variables. A lock cannot be acquired after 16 attempts. The locks timed out


By : Safwani Lahak
Date : March 29 2020, 07:55 AM
This might help you Unless you require the locking functionality for some reason, you should be able to write the Main method simply as this:
code :
public void Main()
{
    string header = string.Empty;
    string message = string.Empty;

    if (Dts.Variables["User::ValidationEmailMessage"].Value == string.Empty)
    {
        header = "Below are the list of Invalid ProjecRefID and Accountnumbers that are not matching with our existing data:\n\n";
        header += string.Format("{0}\t{1}\t\t\t{2}\n", "ProjectRefID", "Accountnumber");
        Dts.Variables["User::ValidationEmailMessage"].Value = header;
    }


    //Format the query result with tab delimiters
    message =
        string.Format("{0}\t{1}\t{2}",
            Dts.Variables["User::ProjectRefID"].Value,
            Dts.Variables["User::Accountnumber"].Value);

    Dts.Variables["User::ValidationEmailMessage"].Value = Dts.Variables["User::ValidationEmailMessage"].Value + message;

    Dts.TaskResult = (int)ScriptResults.Success;
}
Lock on dedicated lock-object causes deadlock

Lock on dedicated lock-object causes deadlock


By : Igors Losevs
Date : March 29 2020, 07:55 AM
hope this fix your issue Let's assume it gets the lock in the second method first. So it is holding the lock, and waiting for data. It is unclear whether this is directly receiving the data sent by the first method, or whether this is looking for a reply from an unrelated server - a reply to the message sent in the first method. But either way, I'm assuming that there won't be data incoming until the outbound message is sent.
Now consider: the outbound message can't be sent, because you are holding an exclusive lock.
Related Posts Related Posts :
  • Having malloced some memory,I could't calculator the proper size of the memories I malloced.I don't know why
  • What is the main difference between integer pointer and character pointer?
  • Why are some functions declared extern and header file not included in source in Git source code?
  • what is the use of fflush(stdin) in c programming
  • Is it safe to return file File descriptor locally allocated from another function In C
  • Changing undefined values of an array
  • What does an empty parameter list mean?
  • using strtol on a string literal causing segmentation fault
  • Same structure objects memory overlap?
  • C-Linux-Any way to pass command "history" to Linux shell?
  • Using #define in defining string size C
  • How to use thread pool and message queues in Multithreaded Matrix Multiplication?
  • Can't find how to select path to run a C program
  • Automatic variable in C not initialized but given fixed value within loop
  • main() function defined without return type gives warning
  • Output of following code with integer, float, char variable
  • why buffer memory allocation error in opencl
  • Why am I getting this error during run-time?
  • Strange behaviour of the pow function
  • task in increment , decrement , printf() , why these are evaluated in this manner in C
  • 28 extra bytes in bss
  • Waiting for multiple events without polling
  • Why are my variables reporting as "undeclared identifier" when compiling?
  • Correct AddNode function but somehing happens when I printf
  • When I traverse in the splay tree, then now which one is root?
  • Data type conversion in Postfix evaluation
  • No output in terminal (Head First C)
  • Data writes over after realloc
  • function: findnextchar() doesn't work
  • Getting file-size property from Nautilus
  • Forward declare entities in C standard library?
  • Static array in C
  • Function signature indicates return type void*, but returns pointer of a different type
  • How do I measure time per thread in C?
  • Online judgeProblem (Wrong Answer)
  • Use function pointer initialized in other source file
  • last line of file is duplicated using fscanf
  • How to print files with same month of modification using directory in c?
  • ADT Circular Queue enqueue and dequeue
  • What events can cause ferror to return non-zero?
  • Accessing dynamically allocated structure received through socket
  • Vim with C Conditional Parsing
  • C can pointers be aligned with any other data type?
  • sdcc inline asm() not working
  • I/O - Manipulation of C Strings
  • sscanf 1 byte hex data without overflow
  • Avoid division by zero in C when taking log with respect to a random number
  • C - memory corruption with threads
  • Why does my program sometimes returns 0xc0000005 and sometimes runs well?
  • C - Dynamic array handling advice
  • Does Standard define null pointer constant to have all bits set to zero?
  • valgrind report strange memory usage
  • abs without branching, why this code works
  • Heapsort Algorithm by Cormen Implementation in C , not working correctly
  • C database program can't find 1st input. Why?
  • Generate random numbers with rand() except number zero
  • How can I know how many free bits are there in a pointer?
  • Program gets 101 numbers instead of 100
  • Returning array pointer produces "warning: return from incompatible pointer type"
  • Gauss Seidel (Specific equation solver) in C
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org