[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.12 Task synchronization

You can use synchronization of tasks to avoid to run treatments at the same time. For example, you can prevent a treatment of a day to start until the same treatment of day before is terminated.

The synchronization of tasks uses locks of variables. The first task of a treatment locks a variable. If this variable is already locked, the task will wait until the variable is unlocked. The last task of the treatment unlocks the variable.

In this example, we will create three tasks, which runs 15 seconds. We will create two days and we'll verify that the tasks of this two days runs at the same time. Then, we'll use a lock to make sure that this treatments runs one after the other.

We create a referential `r1' with a task `t1'. We add an operation to select it all days :

 
$ dio87c ra r1
$  
$ dio87c rta r1 t1
$ dio87c rtoa r1 t1 add always     
$

We create a job `j1' which will execute the `/bin/sleep' command with an argument of `15' seconds :

 
$ dio87c rja r1 t1 j1 
$ dio87c rjsp r1 t1 j1 "/bin/sleep"
$ dio87c rjsa r1 t1 j1 "15"        
$ 

We add two other tasks `t2' and `t3' by copying the task `t1' ; then we add dependencies to force tasks to execute one after the other :

 
$ dio87c rtd r1 t1 t2
$ dio87c rtd r1 t1 t3
$
$ dio87c rtda r1 t1 t2
$ dio87c rtda r1 t2 t3
$

Now, we create two days `d1' and `d2'. We use the etm command with the `-r' option to follow task execution :

 
$ dio87c rp r1 "2004-10-11" d1 
$ dio87c rp r1 "2004-10-11" d2 
$ 
$ dio87c -- etm -r
R	d1	t1	running
R	d2	t1	running
$

After 15 seconds, the two tasks `t2' (one per days) are executing :

 
$ dio87c -- etm -r
R	d1	t2	running
R	d2	t2	running

Finally, after 15 secondes, the two tasks `t3' run :

 
$ dio87c -- etm -r
R	d1	t3	running
R	d2	t3	running

We noted that the two treatments run at the same time. To avoid this, we'll use a lock on a variable. First, we create a variable `lock1' :

 
$ dio87c va lock1

We create two tasks `lock_start' and `lock_end' which will lock and unlock the variable. To execute the task `lock_start' at the begining of the traitment, we add a dependence between the tasks `lock_start' and `t1'. To execute the task `lock_end' at the end of the traitment, we add a dependence between `t3' and `lock_end' :

 
$ dio87c rta r1 lock_start
$ dio87c rtoa r1 lock_start add_always
$ dio87c rtda r1 lock_start t1
$
$ dio87c rta r1 lock_end
$ dio87c rtoa r1 lock_end add_always
$ dio87c rtda r1 t3 lock_end
$ 

The rtsl command is used to define the lock for each task. The `lock_start' task locks the variable `lock1' and the `lock_end' task unlocks it :

 
$ dio87c rtsl r1 lock_start lock1 lock
$ dio87c rtsl r1 lock_end lock1 unlock
$

Now, we add three days `d3', `d4' and `d5' from our referential :

 
$ dio87c rp r1 "2004-10-11" d3 
$ dio87c rp r1 "2004-10-11" d4 
$ dio87c rp r1 "2004-10-11" d5 
$ 

We use the etm command to follow task execution. At the begining, the task `t1' of the day `d3' is executing :

 
$ dio87c -- etm -r 
R       d3      t1      running

We call the vi command to print informations about the variable `lock1'. You can see that the variable is linked with six tasks : a task `lock_start' and a task `lock_end' of each days `d3', `d4' and `d5'. You will note that the variable is locked (`Value: locked') :

 
$ dio87c vi lock1
Variable name:      lock1
Value:              locked
Linked Conditions:  0
Linked Tasks:       6
$

The task `t2' then `t3' of the day `d1' are running. The tasks `lock_start' of the days `d4' and `d5' are waiting for lock (`wait_lock').

 
$ dio87c -- etm -r 
R       d3      t3      running
$
$ dio87c -- etm -w | grep lock 
W	d5	lock_start	wait_lock
[...]
j dio87c -- etm -w | grep lock 
W	d5	lock_start	wait_lock
$

Then, the task `lock_end' of the day `d3' unlock the variable. At once, this variable is locked by the next day `d4'. The tasks of this days can run :

 
$ dio87c -- etm -r 
R       d4      t1      running
[...]
$ dio87c -- etm -r 
R       d4      t2      running
[...]
$ dio87c -- etm -r 
R       d4      t3      running
$

Finally, the task `lock_end' of the day `d4' unlocks the variable, which is directly lock by the task `lock_start' of the task `d5' :

 
$ dio87c -- etm -r 
R       d5      t1      running
[...]
$ dio87c -- etm -r 
R       d5      t2      running
[...]
$ dio87c -- etm -r 
R       d5      t3      running
$

We can see that the traitments of the three days were executed one after the other and not in the same time like at the begining of our example.

Doing two tasks `batch_start' and `batch_end' with a lock on a variable is a good way to avoid treatments of different days to run at the same time.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by dom on September, 11 2005 using texi2html