Study on deadlocks in Linux operating systems-general Linux technology-Linux programming and kernel information. The following is a detailed description. Related functions:
Fcntl (), signal series (this is not the focus of the discussion)
Related Data Structure
Struct flock
{
...
Short l_type;/* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
Short l_whence;/* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
Off_t l_start;/* Starting offset for lock */
Off_t l_len;/* Number of bytes to lock */
Pid_t l_pid;/* PID of process blocking our lock
(F_GETLK only )*/
...
};
Code:
# Include
# Include
# Include
# Include
# Include
# Include
// ------- Setting the signal set
Sigfillset (& set );
Sigdelset (& set, SIGINT );
If (sigprocmask (SIG_SETMASK, & set, NULL) =-1)
{
Perror ("setting mask fail ");
Exit (1 );
}
If (pid = fork () = 0)
{
Lock. l_start = FORCHD;
If (fcntl (fd, F_SETLKW, & lock) =-1)
{
Perror ("setting lock fail ...");
Exit (1 );
}
Else
// Puts ("Child: I have lock my own part! ");
Signal (PARSNT, wake); // handle parent's signal
Kill (getppid (), CHDSNT); // telling parent that his has done his job
Sigdelset (& set, PARSNT );
Sigsuspend (& set); // waiting for parent's information;
Lock. l_start = FORPAR; // to lock the parent's part
If (fcntl (fd, F_SETLKW, & lock) =-1)
{
Perror ("child: setting lock fail ...");
# Ifndef DEBUG
Exit (1 );
# Endif
}
Else
Puts ("Suceed to lock parent's part ...");
_ Exit (0 );
}
// Parent ----------------------------
Lock. l_start = FORPAR;
If (fcntl (fd, F_SETLKW, & lock) =-1)
{
Perror ("setting lock fail ...");
Exit (1 );
}
Else
// Puts ("Parent: I have lock my own part! ");
Signal (CHDSNT, wake); // handle child's signal
Kill (pid, PARSNT); // telling child that his has done his job
Sigdelset (& set, CHDSNT );
Sigsuspend (& set); // waiting for child's information;
Running result:
[Sody @ longtem exercise] $./a. out
Child: setting lock fail...: Resource deadlock avoided
Suceed to lock child's part...
[Sody @ longtem exercise] $./a. out
Child: setting lock fail...: Resource deadlock avoided
Suceed to lock child's part...
[Sody @ longtem exercise] $./a. out
Child: setting lock fail...: Resource deadlock avoided
Suceed to lock child's part...
[Sody @ longtem exercise] $./a. out
Child: setting lock fail...: Resource deadlock avoided
Suceed to lock child's part...
The test results are a bit strange. Because I once ran 1000 or more times, half of the Parent and Child processes had the chance to get the lock. However, after running the same number of times, the parent process always gets it. This is not the same as in advanced programming environment.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service