[ProgClub programming] top: st : time stolen from this vm by the hypervisor

Roland Turner roland at rolandturner.com
Thu Jun 6 15:49:47 AEST 2019


No.

Locking requires some sort of exclusivity during the test-and-set 
operation pair and - as there's no built-in way to establish Java-style 
synchronised blocks within shell scripts (indeed, that's what we're 
creating) - we depend upon an exclusivity enforced by the kernel. Mkdir 
provides this exclusivity, touch does not. Touch could have been coded 
to provide the option to fail if the target exists by using O_EXCL at 
the kernel interface, but this hasn't happened.

The problem with what you're proposing is that there's a race:

  * Process 1: [ -f lock ] && exit
  * Process 2: [ -f lock ] && exit
  * Process 2: touch lock
  * Process 1: touch lock

Both successfully verified that the lock didn't exist and then 
successfully created it. This is obviously not OK.

To avoid this requires something that performs a test-and-set 
atomically. When I first wanted to do this in a shell script decades 
ago, the only atomic operation that I could find readily available for 
use in a shell script was mkdir. In the interim, flock has appeared. 
Flock is a better option because cleanup is automatic: all open file 
handles close automatically upon process termination and all locks 
release automatically upon file handle closure, meaning that all locks 
release automatically upon process termination.

- Raz

------------------------------------------------------------------------

On 6/6/19 1:20 pm, Jevan Pipitone wrote:
>
> Maybe this could be used?
>
> https://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html
>
> |[ -f ||$ephemeralDataDir/lock ] && echo "File exist" || echo "File does not 
> exist" There's probably a way of doing it with a file instead of a 
> directory. "[[ works only in Bash, Zsh and the Korn shell" Jevan. |
> On Thu. 06-Jun.-19 3:15 pm, John Elliot V | ProgClub wrote:
>> Hey Jevan.
>>
>> I note that it's your birthday today, so happy birthday!
>>
>> So thanks for your ideas but you can't use a file and 'touch', because
>> Roland's code relies on 'mkdir' failing if the directory already exists;
>> 'touch' doesn't fail in the same way.
>>
>> But as we've discovered we're probably best off finding a solution with
>> 'flock'.
>>
>> Or you could just do what I do to avoid concurrency issues, and run your
>> script manually in screen... ;)
>>
>> May the Force be with you,
>> John Elliot V
>>
>> On 6/6/19 3:10 pm, Jevan Pipitone wrote:
>>> I notice your lock is a folder (directory) which either exists or is
>>> removed.
>>>
>>> A lock could alternatively be a blank (0 byte) file, like what you are
>>> doing for your "alarm".
>>>
>>> https://www.progclub.org/wiki/John%27s_Linux_page#Stopping_a_script_from_running_if_it_previously_exited_due_to_error
>>>
>>> Would the following code work for lock?:
>>>
>>> ephemeralDataDir=/var/run/something
>>> unlock() {
>>>    rm $ephemeralDataDir/lock
>>> }
>>> touch $ephemeralDataDir/lock || exit 1;
>>> trap unlock EXIT
>>>
>>> See:
>>>
>>> https://www.progclub.org/wiki/John%27s_Linux_page#Make_sure_only_one_instance_of_a_script_is_running_at_a_time
>>>
>>> Original Code:
>>>
>>> ephemeralDataDir=/var/run/something
>>> unlock() {
>>>    rmdir $ephemeralDataDir/lock
>>> }
>>> mkdir $ephemeralDataDir/lock || exit 1;
>>> trap unlock EXIT
>>>
>>> I don't know whether there are any benefits of using a folder or using a
>>> 0 byte file, for a lock status?
>>>
>>> Jevan.
>>>
>>>
>>> On Thu. 06-Jun.-19 7:20 am, John Elliot V | ProgClub wrote:
>>>> On 5/6/19 12:23 pm, Roland Turner via programming wrote:
>>>>> Assuming that you're not ready to embrace systemd (which, despite the
>>>>> absurd documentation, is actually pretty simple for common cases):
>>>>>
>>>>>      #! /bin/bash -e
>>>>>
>>>>>      ...
>>>>>
>>>>>      persistentDataDir=/var/lib/something
>>>>>      alarm() {
>>>>>       touch $persistentDataDir/alarm
>>>>>      }
>>>>>      trap alarm ERR
>>>>>      [ -f $persistentDataDir/alarm ] && exit 1
>>>>>
>>>>>> 4. I want to make sure only one copy of my process runs at a time
>>>>> Under the same assumption:
>>>>>
>>>>>      #! /bin/bash -e
>>>>>
>>>>>      ...
>>>>>
>>>>>      ephemeralDataDir=/var/run/something
>>>>>      unlock() {
>>>>>       rmdir $ephemeralDataDir/lock
>>>>>      }
>>>>>      trap unlock EXIT
>>>>>      mkdir $ephemeralDataDir/lock
>>>> Thanks for those examples, very handy! I've written them up for John's
>>>> Linux Page:
>>>>
>>>> https://www.progclub.org/wiki/John%27s_Linux_page#Stopping_a_script_from_running_if_it_previously_exited_due_to_error
>>>>
>>>>
>>>> _______________________________________________
>>>> ProgClub programming
>>>> programming at progclub.org
>>>> https://www.progclub.org/cgi-bin/mailman/listinfo/programming
>>>> https://www.progclub.org/
>>> -- 
>>> Signature
>>>
>>>
>>> Volunteer Work Only (but you can make money)
>>> MIT SE (UC), GradDip IT (UC), BSc (ANU),
>>> GradCert Rsch Methods & Des (UC)
>>>
>>> I am here for online friends, hobbies, volunteer work, and working on
>>> business ideas, and I am on Centrelink Disability Support Pension
>>> (Schizophrenia & Aspergers). I suspect that what will probably work is
>>> people who are on a Centrelink payment such as unemployed, disability,
>>> or retired, since they can benefit from me and since it allows me to
>>> give back to Centrelink, and also, if female friends then age 49+ and
>>> less attractive so that they will find me attractive, also probably
>>> overweight since I am overweight. However I can also work with highly
>>> skilled people since I can make use of what I get from them that is
>>> useful for me to be able to work with interesting concepts. Me:
>>> Computing Researcher, Self Employed, Innovation, Management, Canberra
>>> Australia. Interests: Especially things involving the use of a computer.
>>> Creating, Doing, Improvising, Analysing for Improvement: Computer
>>> Programming Open Source including C#/VB.NET, CSS, HTML, Javascript, MS
>>> Visual Studio, My/MS Sql, PHP, WinForms, WPF. 3d animation, Acting
>>> Improvisations, Audio Podcasts, Business & Startups, Chat, Creative
>>> Writing & Poetry, Creativity, Dancing, Debating, Decisions, Digital Art,
>>> Discussion of Topics, Electronic Music Composition/Creation (FL Studio),
>>> Engineering, Friends, Ideas, Information Analysis, Innovation, IT
>>> Tuition, Jazz Piano, Jazz Singing Male Baritone & Choir & Karaoke,
>>> Management, PC Support, Photography, Public Speaking, Reading online
>>> science magazines and emails, Research, Science, Making YouTube Videos.
>>> Friendship with adults via phone, Email, Online Chat, anything we can
>>> make and send each other and collaborate on. Note: For our interactions,
>>> collaborations, hobbies, volunteer work, etc to turn out successful, it
>>> may be necessary for there to be a financial cost to me in order for you
>>> or your organization to make it work, so let me know what you suggest.
>>>
>>> Web:http://www.jevan.com.au/
>>> Email:email5 at jevan.com.au  <mailto:email5 at jevan.com.au>
>>> Tel: (02) 6152 8010 (or +61 2 6152 8010)
>>> LinkedIn:http://www.linkedin.com/in/jevanpipitone
>>> Facebook:http://www.facebook.com/jevanpipitone
>>> GitHub:http://www.github.com/jevanpipitone
>>> Twitter:http://www.twitter.com/jevanpipitone
>>> Canberra, Australia
>>>
>>>
>>>
>>> _______________________________________________
>>> ProgClub programming
>>> programming at progclub.org
>>> https://www.progclub.org/cgi-bin/mailman/listinfo/programming
>>> https://www.progclub.org/
>>>
>>
>> _______________________________________________
>> ProgClub programming
>> programming at progclub.org
>> https://www.progclub.org/cgi-bin/mailman/listinfo/programming
>> https://www.progclub.org/
> -- 
> Signature
>
>
> Volunteer Work Only (but you can make money)
> MIT SE (UC), GradDip IT (UC), BSc (ANU),
> GradCert Rsch Methods & Des (UC)
>
> I am here for online friends, hobbies, volunteer work, and working on 
> business ideas, and I am on Centrelink Disability Support Pension 
> (Schizophrenia & Aspergers). I suspect that what will probably work is 
> people who are on a Centrelink payment such as unemployed, disability, 
> or retired, since they can benefit from me and since it allows me to 
> give back to Centrelink, and also, if female friends then age 49+ and 
> less attractive so that they will find me attractive, also probably 
> overweight since I am overweight. However I can also work with highly 
> skilled people since I can make use of what I get from them that is 
> useful for me to be able to work with interesting concepts. Me: 
> Computing Researcher, Self Employed, Innovation, Management, Canberra 
> Australia. Interests: Especially things involving the use of a 
> computer. Creating, Doing, Improvising, Analysing for Improvement: 
> Computer Programming Open Source including C#/VB.NET, CSS, HTML, 
> Javascript, MS Visual Studio, My/MS Sql, PHP, WinForms, WPF. 3d 
> animation, Acting Improvisations, Audio Podcasts, Business & Startups, 
> Chat, Creative Writing & Poetry, Creativity, Dancing, Debating, 
> Decisions, Digital Art, Discussion of Topics, Electronic Music 
> Composition/Creation (FL Studio), Engineering, Friends, Ideas, 
> Information Analysis, Innovation, IT Tuition, Jazz Piano, Jazz Singing 
> Male Baritone & Choir & Karaoke, Management, PC Support, Photography, 
> Public Speaking, Reading online science magazines and emails, 
> Research, Science, Making YouTube Videos. Friendship with adults via 
> phone, Email, Online Chat, anything we can make and send each other 
> and collaborate on. Note: For our interactions, collaborations, 
> hobbies, volunteer work, etc to turn out successful, it may be 
> necessary for there to be a financial cost to me in order for you or 
> your organization to make it work, so let me know what you suggest.
>
> Web: http://www.jevan.com.au/
> Email: email5 at jevan.com.au <mailto:email5 at jevan.com.au>
> Tel: (02) 6152 8010 (or +61 2 6152 8010)
> LinkedIn: http://www.linkedin.com/in/jevanpipitone
> Facebook: http://www.facebook.com/jevanpipitone
> GitHub: http://www.github.com/jevanpipitone
> Twitter: http://www.twitter.com/jevanpipitone
> Canberra, Australia
>
>
>
> _______________________________________________
> ProgClub programming
> programming at progclub.org
> https://www.progclub.org/cgi-bin/mailman/listinfo/programming
> https://www.progclub.org/


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.progclub.org/pipermail/programming/attachments/20190606/454514d0/attachment-0001.html>


More information about the programming mailing list