Sponsored Content
Full Discussion: Concurrency in ksh
Top Forums Shell Programming and Scripting Concurrency in ksh Post 302590575 by agama on Monday 16th of January 2012 09:53:49 PM
Old 01-16-2012
Yes, your for loop will start n concurrent executions of the same script.

The statement you have in red attempts to make a directory. If it is successful, the if should evaluate to true and the loop should be exited. I ran your lock wait function and it works for me.

As an alternative example, here is a stripped down version of what I use. Similar, but different enough to give you a second perspective. You can try running it twice from the command line to see if it is able to create a lock and hold it for a while.

Code:
# pass file to lock as $1
function lock_it
{
    typeset lockname=$1.lock

    while ! mkdir $lockname 2>/dev/null    # if we can make it, we own the lock
    do
        echo "$$ waiting for lock"      # didn't get it, wait and try again
        sleep 3
   done

    touch $lockname/PID$$       # mark it with our pid (humans can easily tell which process owns the lock this way)
}

# pass file to unlock as $1
function unlock_it
{
    typeset lockname=$1.lock
    if [[ -d $lockname ]]      # ensure it exists
    then
        if [[ -f $lockname/PID$$ ]]    # ensure this process owns it
        then
            rm -fr $lockname
        else
            echo "unable to unlock $w: process $$ does not own the lock"
        fi
    else
        echo "unable to unlock $1: lock does not exist"
    fi
}


# simple test case
lock_it /tmp/test
echo "$$ got it"
sleep 30
echo "$$ releasing it"
unlock_it /tmp/test

In the code that I use in production I allow for a max amount of time before the lock function gives up and returns an error. It also supports a no-wait option that allows for a non-blocking approach.
This User Gave Thanks to agama For This Post:
 

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

concurrency issue while Accessing Mail Box from shell script

Hi Bros, I am in a serious trouble with concurrency issue while using mailx. I have a shell script which reads all the emails of a unix user account and create a copy (another mbox for processing and archive purpose). This script works fine for 99.99% of the time but sometime it start creating... (2 Replies)
Discussion started by: Sumit_Fundoo
2 Replies

2. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

3. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

4. UNIX for Dummies Questions & Answers

Difference Between executing llike ./myscript.ksh and . ./myscript.ksh

Hi , What is the diffence between executing the script like ./myscript.ksh . ./myscript.ksh I have found 2 difference but could not find the reason 1. If i export a variable in myscript.ksh and execute it like . ./myscript.ksh the i can access the other scripts that are present in... (5 Replies)
Discussion started by: max_hammer
5 Replies

5. Shell Programming and Scripting

different behaviour for ksh and ksh -x

I'm getting different behaviour when executing below script in debug option. $ cat ss.ksh ff=$(pwd) echo " ff : $ff" $ ksh ss.ksh ff : /tmp $ ksh -x ss.ksh + + pwd ff= + echo ff : ff : I was getting this behaviour in my actuall script i'm able to reproduce this in simple script... (4 Replies)
Discussion started by: luckybalaji
4 Replies

6. UNIX for Dummies Questions & Answers

Open-source projects to learn concurrency-managed network programming in Unix?

Hi, I am a mid-career programmer with extensive experience in object-oriented design and development in C, C++, and C#. I've written a number of multi-threaded server applications and background services, although my grasp of networking protocols is a bit weak: my current job drifted away from... (2 Replies)
Discussion started by: TheTaoOfPhil
2 Replies

7. UNIX for Advanced & Expert Users

Writing a REST server to run on FreeBSD -- how to structure for concurrency?

Hi All, I want to write a domain specific REST/HTTP server to run on FreeBSD. I have control over both the server side and the primary client intended to consume the service. My question is: how do you think it should be designed to support multiple connections, given: It will run on a... (6 Replies)
Discussion started by: LittleCookieMon
6 Replies
SHLOCK(1)						    BSD General Commands Manual 						 SHLOCK(1)

NAME
shlock -- create or verify a lock file for shell scripts SYNOPSIS
shlock [-du] [-p PID] -f lockfile DESCRIPTION
The shlock command can create or verify a lock file on behalf of a shell or other script program. When it attempts to create a lock file, if one already exists, shlock verifies that it is or is not valid. If valid, shlock will exit with a non-zero exit code. If invalid, shlock will remove the lock file, and create a new one. shlock uses the link(2) system call to make the final target lock file, which is an atomic operation (i.e. "dot locking", so named for this mechanism's original use for locking system mailboxes). It puts the process ID ("PID") from the command line into the requested lock file. shlock verifies that an extant lock file is still valid by using kill(2) with a zero signal to check for the existence of the process that holds the lock. The -d option causes shlock to be verbose about what it is doing. The -f argument with lockfile is always required. The -p option with PID is given when the program is to create a lock file; when absent, shlock will simply check for the validity of the lock file. The -u option causes shlock to read and write the PID as a binary pid_t, instead of as ASCII, to be compatible with the locks created by UUCP. EXIT STATUS
A zero exit code indicates a valid lock file. EXAMPLES
BOURNE SHELL #!/bin/sh lckfile=/tmp/foo.lock if shlock -f ${lckfile} -p $$ then # do what required the lock rm ${lckfile} else echo Lock ${lckfile} already held by `cat ${lckfile}` fi C SHELL #!/bin/csh -f set lckfile=/tmp/foo.lock shlock -f ${lckfile} -p $$ if ($status == 0) then # do what required the lock rm ${lckfile} else echo Lock ${lckfile} already held by `cat ${lckfile}` endif The examples assume that the file system where the lock file is to be created is writable by the user, and has space available. HISTORY
shlock was written for the first Network News Transfer Protocol (NNTP) software distribution, released in March 1986. The algorithm was sug- gested by Peter Honeyman, from work he did on HoneyDanBer UUCP. AUTHORS
Erik E. Fair <fair@clock.org> BUGS
Does not work on NFS or other network file system on different systems because the disparate systems have disjoint PID spaces. Cannot handle the case where a lock file was not deleted, the process that created it has exited, and the system has created a new process with the same PID as in the dead lock file. The lock file will appear to be valid even though the process is unrelated to the one that cre- ated the lock in the first place. Always remove your lock files after you're done. BSD
June 29, 1997 BSD
All times are GMT -4. The time now is 11:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy