source: trunk/lamport.sh@ 26

Last change on this file since 26 was 2, checked in by Mattia Monga, 14 years ago

Importazione sorgenti

  • Property svn:executable set to *
File size: 1.2 KB
Line 
1#! /bin/bash
2
3N=2
4
5function assegna(){
6 echo $2 >> $1
7}
8
9function leggi(){
10 tail -1 $1
11}
12
13
14for j in $(seq 1 $N); do
15 echo "0">choosing$j
16 echo "0">number$j
17done
18echo "1">alternanza
19
20function max(){
21 local m=$(leggi number1)
22 for j in $(seq 1 $N); do
23 m=$(( $m > $(leggi number$j) ? $m : $(leggi number$j) ))
24 done
25 echo $m
26}
27
28
29function p(){
30 local p
31 for p in $(seq $(( $1 * 10 )) $(( $1 * 10 + 9 )) ); do
32 while true; do
33 # doorway
34 assegna choosing$1 "1"
35 assegna number$1 "$(( 1 + max ))"
36 # bakery
37 assegna choosing$1 "0"
38 local j
39 for j in $(seq 1 $N); do
40 while [ $(leggi choosing$j) -ne 0 ]; do
41 sleep 0.01
42 done
43 while [ \( $(leggi number$j) -ne 0 \) -a \
44 \( \( $(leggi number$j) -lt $(leggi number$1) \) -o \
45 \( $(leggi number$j) -eq $(leggi number$1) \) -a \
46 \( $j -lt $1 \) \) ]; do
47 sleep 0.01
48 done
49 done
50 # critical section
51 if [ $(leggi alternanza) -eq $1 ]; then
52 echo $p
53 assegna alternanza "$(( ( $(leggi alternanza) % 2 ) + 1))"
54 assegna number$1 "0"
55 break
56 else
57 assegna number$1 "0"
58 fi
59 # non critical section
60 done
61 done
62}
63
64p 1 &
65p 2 &
66
67wait %1 %2
Note: See TracBrowser for help on using the repository browser.