Last active
June 14, 2016 22:02
-
-
Save phemmer/f7ddea620f9b9ceb56a2e70900f6f83e to your computer and use it in GitHub Desktop.
Process synchronization shell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| exec 3<"$0" | |
| flock -s 3 | |
| for (( i=0; i<5; i++ )); do | |
| ( | |
| sleeptime=$(( RANDOM % 6 )) | |
| echo $(date) - $BASHPID sleeping $sleeptime | |
| sleep $sleeptime | |
| echo $(date) - $BASHPID dropping shared lock | |
| exec 3>&- | |
| echo $(date) - $BASHPID getting exclusive lock | |
| exec 3<"$0" | |
| flock -x 3 | |
| echo $(date) - $BASHPID dropping exclusive lock | |
| exec 3>&- | |
| echo $(date) - $BASHPID ready to continue | |
| ) & | |
| done | |
| exec 3>&- | |
| wait |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Tue Jun 14 18:01:57 EDT 2016 - 17786 sleeping 1 | |
| Tue Jun 14 18:01:57 EDT 2016 - 17788 sleeping 0 | |
| Tue Jun 14 18:01:57 EDT 2016 - 17785 sleeping 1 | |
| Tue Jun 14 18:01:57 EDT 2016 - 17784 sleeping 3 | |
| Tue Jun 14 18:01:57 EDT 2016 - 17787 sleeping 2 | |
| Tue Jun 14 18:01:57 EDT 2016 - 17788 dropping shared lock | |
| Tue Jun 14 18:01:57 EDT 2016 - 17788 getting exclusive lock | |
| Tue Jun 14 18:01:58 EDT 2016 - 17786 dropping shared lock | |
| Tue Jun 14 18:01:58 EDT 2016 - 17785 dropping shared lock | |
| Tue Jun 14 18:01:58 EDT 2016 - 17786 getting exclusive lock | |
| Tue Jun 14 18:01:58 EDT 2016 - 17785 getting exclusive lock | |
| Tue Jun 14 18:01:59 EDT 2016 - 17787 dropping shared lock | |
| Tue Jun 14 18:01:59 EDT 2016 - 17787 getting exclusive lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17784 dropping shared lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17784 getting exclusive lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17788 dropping exclusive lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17788 ready to continue | |
| Tue Jun 14 18:02:00 EDT 2016 - 17787 dropping exclusive lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17787 ready to continue | |
| Tue Jun 14 18:02:00 EDT 2016 - 17785 dropping exclusive lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17785 ready to continue | |
| Tue Jun 14 18:02:00 EDT 2016 - 17786 dropping exclusive lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17786 ready to continue | |
| Tue Jun 14 18:02:00 EDT 2016 - 17784 dropping exclusive lock | |
| Tue Jun 14 18:02:00 EDT 2016 - 17784 ready to continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment