Dining philosophers problem BASH


Is my simple bash script for resolve “ dining philosophers problem“.
New version 2.0 all philosophers always finish eat

filename: FILOZOFOWIE

#!/bin/bash
#FILO GENERATOR Ver 2.0
#Author JACEK ZALESKI
#Script generate lock when file witch name PID is not empty
# FORK is file witch name of PID
#Philosoper generate file (is fork), when finished eating remove fork

#remember add chmod for this file witch law for execution
function SIADAMY_PRZY_STOLE {



arr=$(echo $LIST_F | tr ";" "\n")
z=0
for x in $arr
do

    #echo "$z"
    ADDR[$z]="$x"
    z=$(($z+1))
done
}

function przykim_siedze {
FILOZOFOW=${#ADDR[@]}
	for (( Fi = 0 ; Fi < $FILOZOFOW ; Fi++ ))
	do
		if [ "${ADDR[$Fi]}" -eq "$FILOZOFNAME" ]
		then
		   if [ "$Fi" -eq "0" ]
		    then
		  MOJ_LEWY_SASIAD="${ADDR[$(($FILOZOFOW-1))]}"
		  TWOJE_MIEJSCE_PRZY_STOLE="$i"
		    else
		    
		  MOJ_LEWY_SASIAD="${ADDR[$(($Fi-1))]}"
		  TWOJE_MIEJSCE_PRZY_STOLE="$Fi"
			fi
  		fi
	done
}


function losujczas {
	randzik=$RANDOM
 ILE_CZEKAC=$((randzik %=4+1 ))
	
}

function sprawdzwidelec_moj {
	if [ !  -s $STOL$FILOZOFNAME ]; then
    widelecprawy="1"
    
    	fi
}
	
function sprawdzwidelec_sasiada {
	if [[ ! -s $STOL$MOJ_LEWY_SASIAD ]] ; then
	wideleclewy="1"
	fi
}
function lockf_moj {
	echo "$FILOZOFNAME" > $STOL$FILOZOFNAME
	}
function lockf_sasiada {
	echo "$FILOZOFNAME" > $STOL$MOJ_LEWY_SASIAD
}
function ulockf_moj {
	echo -n "" > $STOL$FILOZOFNAME
	}
function ulockf_sasiada {
	echo -n "" > $STOL$MOJ_LEWY_SASIAD
}
}
function fucztuje {
 echo "$(date +%H:%M:%S)"": Name:" "$FILOZOFNAME" "Status: Eating" >> rozgrywka.dat
}
function fdyskutuje {
 echo "$(date +%H:%M:%S) "": Name:" "$FILOZOFNAME " "Status: Chating" >> rozgrywka.dat
}
function fczek {
 echo "$(date +%H:%M:%S)"":Name:" "$FILOZOFNAME" "Status: Waiting for fork" >> rozgrywka.dat
}

function koniec {
			echo  "$(date +%H:%M:%S)"": Nazwa FILOZOFA:" "$FILOZOFNAME" "$(date +%H:%M:%S)"": Name:" "$FILOZOFNAME" "Status:
 ---Finish eating---" >> rozgrywka.dat   2>&1 
			#rm -f $STOL$FILOZOFNAME
			echo $FILOZOFNAME >> bild.dat
			kill $FILOZOFNAME 
}


#######
#Kod
#######
ILE_X_MA_JESC=$1
FILOZOFNAME=$$
STOL="STOL/"
echo -n "" > $STOL$FILOZOFNAME
sleep 3
LIST_F=$(ls -l $STOL | awk 'NR!=1 && !/^d/ {print $NF}' | sort -n)
#echo "$LIST_F"
SEMAFOR0=0

if [ $# -lt 1 ]
then
	echo "Brak parametrow" >>   rozgrywka.dat
	exit
fi



while [ $SEMAFOR0 -lt $ILE_X_MA_JESC ]
	do 
	SIADAMY_PRZY_STOLE
	przykim_siedze
	losujczas
	#echo "$SEMAFOR0"
	fdyskutuje
	sleep $ILE_CZEKAC
	sprawdzwidelec_moj

	if [[ $widelecprawy -eq 1 ]];
	 then 
		lockf_moj
		sprawdzwidelec_sasiada
		if [[ $wideleclewy -eq 1 ]]; 
			then
			lockf_sasiada
			SEMAFOR0=$(($SEMAFOR0+1))
			fucztuje
			sleep $ILE_CZEKAC
			ulockf_moj
			ulockf_sasiada
			else
			sleep $ILE_CZEKAC
			fczek
			if [[ $wideleclewy -eq 1 ]]; 
				then
					lockf_sasiada
					SEMAFOR0=$(($SEMAFOR0+1))
					fucztuje
					sleep $ILE_CZEKAC
					ulockf_moj
					ulockf_sasiada
				else
				ulockf_moj
				fi
		fi
	fi
done
koniec



Second file for runing program , generate bash dialog for parameters and to show result.

filename: run.sh

I#!/bin/bash 
#wypluwascz

dialog --title "Number of philoshopers" --backtitle "Jacek Zaleski" --inputbox "Number of philoshopers" 8 60 2>plik.$$.dat
dialog --title "How many time they must use fork (two forks)" --backtitle "Jacek Zaleski " --inputbox "How many time they must use fork (two forks)" 8 60 2>plik1.$$.dat
Iluf=$(cat plik.$$.dat)
Ilej=$(cat plik1.$$.dat)





rm rozgrywka.dat
rm STOL/*
rm bild.dat
rm -f plik*
for ((i=1; i <= "$Iluf";i++)); do

         procent=$((($i * 100) / $Iluf)) 
	./FILOZOFOWIE $Ilej $i &
	 echo "$procent" | dialog --guage "Generuje filowów" 6 70 0


done 
echo;
sleep 2

while  true ; do
       bildd=$(cat bild.dat | wc -l)
       dialog --title " $bildd / $Iluf Filozofowie Jacek Zaleski-consolechars.wordpress.com" --infobox "$(cat rozgrywka.dat | tail -30 )" 34 85
       sleep 2
       if [ "$bildd" -eq "$Iluf" ]; then
            rm STOL/*
            dialog --title "Waiter  "  --msgbox "\n Finished \n https://consolechars.wordpress.com"  10 56             
            exit
       fi
         
done



Advertisements

6 Comments Add yours

  1. john says:

    didnt work…

    1. dzaku says:

      Its work 🙂 , i create one bug, because few university have this exercise.

      1. what is the bug? 😉 I have this exercise on my university too 😉

      2. Where is bug ? i need this for ma university 😉

  2. I have fixed the bug, also I did a little modification to print the program without any error messages log (such as “No such file or directory” while seeking for the PID)…

    1. dzaku says:

      Great job 🙂 . I have hope you has a lot of fun with play this my old code 🙂 .

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s