Sponsored Content
Full Discussion: Concurrency in ksh
Top Forums Shell Programming and Scripting Concurrency in ksh Post 302591172 by agama on Wednesday 18th of January 2012 06:07:08 PM
Old 01-18-2012
The lock file code in your last post is bound to have problems. If two instances of your script both notice that the lock file is missing at the same time, they will both touch the lock file and go on. Not that it matters in this case, since touch will always work, you don't check the status of the touch. What ever command you use to 'create' the lock file must allow only one process to be successful and the script must test the result and loop back to try again when it detects a failure.

The reason mkdir was used is because only one process will successfully create the directory if two attempt the operation at exactly the same time. Also, the return value of mkdir is also being checked and if it didn't succeed the script assumes it does not own the lock.

Assuming the seq command is installed on your machine, you could run this to start 5 processes:

Code:
for x in `seq 1 5`
do
    shell-script >/tmp/log.$x 2>&1 &
done

Here is a version of my original code in what I think should execute under a very old version of ksh. It starts 5 copies of itself and each waits to get the lock reporting when it does and does on the tty. Cut and paste it into its own file and run it (just once) to see what happens. If this doesn't work (it does on my system) then I'm not sure what to do to help you along.
Code:

lock_it()
{
    lockname=$1.lock

    while  true
    do
        if mkdir $lockname 2>/dev/null
        then
            touch $lockname/PID$$       # mark it with our pid
            return 0
        fi

        echo "$$ ($2) waiting for lock"
        sleep 3
   done

}

unlock_it()
{
    lockname=$1.lock
    if [ -d $lockname ]
    then
        if [ -f $lockname/PID$$ ]
        then
            rm -fr $lockname
            return 0
        else
            echo "$$ unable to unlock $w: process $$ does not own the lock"
        fi
    else
        echo "$$ unable to unlock $1: lock does not exist"
    fi

    return 1
}

if [ -z $1 ]
then
    for x in `seq 1 5`
    do
        echo "starting process $x"
        $0 $x &
    done

    ps -elf|grep "$0"
    wait
    exit
fi

lock_it /tmp/test $1
echo "$$ ($1) got the lock; sleeping 30s"
sleep 30
echo "$$ ($1) releasing lock"
unlock_it /tmp/test
echo "$$ ($1) is exiting"

exit

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
lockfile-progs(1)						 Lockfile programs						 lockfile-progs(1)

NAME
lockfile-progs - command-line programs to safely lock and unlock files and mailboxes (via liblockfile). SYNOPSIS
mail-lock [--use-pid] [--retry retry-count] mail-unlock mail-touchlock [--oneshot] lockfile-create [--use-pid] [--retry retry-count] [--lock-name] filename lockfile-remove [--lock-name] filename lockfile-touch [--oneshot] [--lock-name] filename lockfile-check [--use-pid] [--lock-name] filename DESCRIPTION
Lockfile-progs provides a set a programs that can be used to lock and unlock mailboxes and files safely (via liblockfile): mail-lock - lock the current user's mailbox mail-unlock - unlock the current user's mailbox mail-touchlock - touch the lock on the current user's mailbox lockfile-create - lock a given file lockfile-remove - remove the lock on a given file lockfile-touch - touch the lock on a given file lockfile-check - check the lock on a given file By default, the filename argument refers to the name of the file to be locked, and the name of the lockfile will be filename .lock. How- ever, if the --lock-name argument is specified, then filename will be taken as the name of the lockfile itself. Each of the mail locking commands attempts to lock /var/spool/mail/<user>, where <user> is the name associated with the effective user ID, as determined by via geteuid(2). Once a file is locked, the lock must be touched at least once every five minutes or the lock will be considered stale, and subsequent lock attempts will succeed. Also see the --use-pid option and the lockfile_create(3) manpage. The lockfile-check command tests whether or not a valid lock already exists. OPTIONS
-q, --quiet Suppress any output. Success or failure will only be indicated by the exit status. -v, --verbose Enable diagnostic output. -l, --lock-name Do not append .lock to the filename. This option applies to lockfile-create, lockfile-remove, lockfile-touch, or lockfile-check. -p, --use-pid Write the current process id (PID) to the lockfile whenever a lockfile is created, and use that pid when checking a lock's validity. See the lockfile_create(3) manpage for more information. This option applies to lockfile-create, lockfile-remove, lockfile-touch, and lockfile-check. -o, --oneshot Touch the lock and exit immediately. This option applies to lockfile-touch and mail-touchlock. When not provided, these commands will run forever, touching the lock once every minute until killed. -r retry-count, --retry retry-count Try to lock filename retry-count times before giving up. Each attempt will be delayed a bit longer than the last (in 5 second incre- ments) until reaching a maximum delay of one minute between retries. If retry-count is unspecified, the default is 9 which will give up after 180 seconds (3 minutes) if all 9 lock attempts fail. EXAMPLES
Locking a file during a lengthy process: lockfile-create /some/file lockfile-touch /some/file & # Save the PID of the lockfile-touch process BADGER="$!" do-something-important-with /some/file kill "${BADGER}" lockfile-remove /some/file EXIT STATUS
0 For lockfile-check this indicates that a valid lock exists, otherwise it just indicates successful program execution. Not 0 For lockfile-check a non-zero exit status indicates that the specified lock does not exist or is not valid. For other programs it indicates that some problem was encountered. SEE ALSO
maillock(3) touchlock(3) mailunlock(3) lockfile_create(3) lockfile_remove(3) lockfile_touch(3) lockfile_check(3) AUTHOR
Written by Rob Browning <rlb@defaultvalue.org> 0.1.12 2008-02-10 lockfile-progs(1)
All times are GMT -4. The time now is 01:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy