Bible Verses

Thursday, August 13, 2009

Lab Manual

KCG COLLEGE OF TECHNOLOGY
KARAPAKKAM, CHENNAI – 600 097


OPERATING SYSTEMS LABORATORY MANUAL
(FOR III B.TECH V SEM IT STUDENTS)

CS-1254 OPERATING SYSTEMS LAB

Developed by
R.N.Sharmila.Lect.,


DEPARTMENT OF INFORMATION TECHNOLOGY
AS PER THE ANNA UNIVERSITY SYLLABUS


LIST OF EXPERIMENTS


01 Study of Basic Linux commands

02 Shell programs

a) Greatest among Three numbers
b) Armstrong Number
c) Patterns
d) Expansions
e) Substitution
f) Student’s Grade System

03 CPU Scheduling

a) First Come First Serve
b) Shortest Job First
d) Priority Scheduling
e) Round Robin

04 Program using System call

a) fork ()
b) getpid() & getppid()
c) Wait ()
d)opendir(),readdir,closeddir()

05 Program using I/O System call open (), read () & write ()

06 Simulation of “ls and grep” commands

07 Producer-Consumer Problem

08 Deadlock Avoidance Memory Management Scheme

a) First fit
b) Best fit
c) Worst fit
09 Deadlock Avoidance

Ex.No.1 STUDY OF BASIC UNIX COMMANDS


AIM:

To study the basic UNIX commands for the following operations.
• Viewing the all working user
• Viewing the current user of the system
• Viewing the Date and Time
• Viewing the Calendar
• Displaying the given message
• Listing Files
• Checking the Current working directory
• Creating a directory
• Changing the Working directory
• Removing a directory
• Displaying and creating files
• Copying a file
• Deleting files
• Renaming files
• Counting number of words in a file
• Clear the screen
• Performing mathematical calculation




THEORY:
UNDERSTANDING THE UNIX COMMANDS:
In Windows operating system, you have to click on an icon to get a job done that is primarily GUI (Graphical User Interface) based. The UNIX system is heavily command based that is you have to type in a few characters to frame a command and then press the key for it to work. In UNIX system a minimum number of keystrokes achieve the maximum amount of work. That is why UNIX commands are seldom more than four characters long. All UNIX commands are single words like “ls, cat, who”.This commands are all in lowercase.
Viewing the all working user:
The UNIX system is multi-user system .In that network to see who are all currently in network, the “who” command is used to view the all working user, that is it else everyone who is currently logged in .Syntax of the “who” command is
$ who
The UNIX respond of “who” command is
[thrcse01@localhost thrcse01]$ who
root :0 Nov 4 02:11
root pts/1 Nov 4 02:20 (:0.0)
thrcse01 pts/2 Nov 4 02:25 (129.0.0.240)

There are three users of the system with their login names shown in the first column. The second column shows the devices names of their respective terminals.thrcse01 has the name pts/2 associated with his terminal, while thrcse01 terminal has the name pts/1.The third column shows the date and time of the logging in. The terminal names that you see in the who output are actually special files representing the devices. These files are available in /dev.For example, the file pts/1 can be found in the /dev directory.
Viewing the current user of the system:
The “who” command tells everyone who is currently logged in. To see who you are in network, so you should use it with two arguments “am” and “i” the “who am i” Command is used to view the current user of that particular system, that is it shows the user name and system’s name for the connection being used for that particular system, that is it shows the user name and system’s name for the connection being used for that system. Syntax of the “who am i” command is
$who am i
The UNIX respond of “who am i” command is
[thrcse01@localhost thrcse01]$ who am i
thrcse01 pts/2 Nov 4 02:25 (129.0.0.240)

Here current working user’s login names shown in the first column. The second column shows terminal of the device names of their respective terminals. The third column shows the date and time of logging in.
Viewing the Date and Time:
The UNIX system maintains an internal clock. This clock actually stores the number of seconds elapsed since january1, 1970 the “date” command shows the date and time to the nearest second. Syntax of the “date” command is
$ date
The UNIX respond of “date” command is
[thrcse01@localhost thrcse01]$ date
Fri Nov 4 02:28:47 IST 2005
Here day shown in the first column, month shown in the second column, date shown in the third column, time with nearest second shown in the fourth column.
Viewing the Calendar:
The “cal” command is used to view the present year calendar . Syntax of the “cal” command is $ cal
The UNIX respond of “cal” command is
[thrcse01@localhost thrcse01]$ cal
November 2005
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30


Displaying the given message:
The “echo” command displays it arguments. We can use this command either quoted or unquoted. Syntax of the “echo” command is
$ echo


The UNIX respond of “echo” command is

[thrcse01@localhost thrcse01]$ echo hello
In the unquoted argument the multiple spaces reduce to single space. But in the quoted argument multiple spaces are displayed.
Listing Files :
The “ls” command is used to list out the names of the files available in the current working directory. The syntax of the “ls” command is
$ ls
The UNIX respond of “ls”command is

[thrcse01@localhost thrcse01]$ ls
alm file india


Here alm, file and India are the files of the current working directory.
Checking the Current working directory:
It is remarkable feature of the UNIX system that, like a file, user also occupies a certain slot in the files system. When you log in, you are placed in a specific directory of the file system. This directory is known as your current directory. You can move around from one directory to another, but at any point of the time, only one directory is current. The “ pwd” (present working directory)command is used to move our current working directory. The syntax of the “pwd” command is
$ pwd


The UNIX respond of “pwd”command is
[thrcse01@localhost thrcse01]$ pwd
/home/thrcse01
[thrcse01@localhost thrcse01]$

Here /home /thrcse01 is a path name, which is simply a sequence of directory names separated by slashes. pwd here tells you that you are placed in the directory “thrcse01”, which has the parent directory “home” and first slash represent the root directory.
Creating a directory:
Directories can be created with the “mkdir” (Make Directory) command. The command is followed by the names of the directories to be created. The syntax of the “mkdir” command is
$ mkdir
A directory “sugan” is created under the current directory like this.

[thrcse01@localhost thrcse01]$ mkdir sugan


Changing the Working directory:
You can move around in the file system by using the “cd” (change directory) command. It changes the current directory specified as the argument. The syntax of the “cd” command is
$ cd
The directory is changed from the current directory.

[thrcse01@localhost thrcse01]$ pwd
/home/thrcse01
[thrcse01@localhost thrcse01]$ cd sugan
[thrcse01@localhost sugan]$ pwd
/home/thrcse01/sugan
[thrcse01@localhost sugan]$

Removing a directory:
The “rmdir”(Remove directory) command removes directories. The command is followed by the names of the directories to be created. The syntax of the “rmdir” command is
$ rmdir
The directory “sugan” is removed from the current working directory like this
[thrcse01@localhost sugan]$ rmdir sugan

Displaying and creating files:
“cat” command is mainly used to display the contents of the file on the terminal.The syntax of the “cat” command is
$ cat
The UNIX respond of “cat”command is
[thrcse01@localhost sugan]$ cat alm
God is Great

The above output shows the alm file’s contents.
To create a file using “cat”:
“cat” is also useful for creating a file.Enter the command “cat” , followed by >(the right chevron) character and the file name for example
[thrcse01@localhost thrcse01]$ cat > file
hai
hello
good morning

When the command line is terminated after hitting the key, the prompt vanishes. Now waits to take input from the user. Enter the Data into the file, each line followed by .Finally, press to signify the end of input to the system. When this character is entered, the system understands that no further text input will be made. The file is written and the prompt returned.
Copying a file:
The “cp” (copy) command copies a file or a group of files .The syntax requires at least two file names to be specified in the command line. The syntax is
$ cp
The first file is copied to the second. The UNIX respond of “cp”command is
[thrcse01@localhost thrcse01]$ cp alm india
[thrcse01@localhost thrcse01]$ cat india
GOD IS GREAT
[thrcse01@localhost thrcse01]$ cat alm
GOD IS GREAT

Here “alm” file’s contents are copied to the “file” file.
Deleting files:
Files can be deleted with “rm”(remove).It can delete more than one file with single instruction. The syntax of “rm” command is
$ rm
The UNIX respond of “rm”command is
[thrcse01@localhost thrcse01]$ ls
alm file india sugan
[thrcse01@localhost thrcse01]$ rm india
[thrcse01@localhost thrcse01]$ ls
alm file sugan

Renaming files :
“mv”(move) command is used to renaming the file .It has to function renaming a file (ordirectory)and moving group of files to a different directory. Unlike “cp”command the “mv”command does not create a copy of the file, it merely renames it. The syntax requires at least two file names to be specified in the command line.
The syntax is
$ mv
thrcse01@localhost thrcse01]$ mv alm file[thrcse01@localhost thrcse01]$ cat file
GOD IS GREAT
[thrcse01@localhost thrcse01]$ cat alm
cat: alm: No such file or directory

Here entire command of “alm” file contents are moved to “alm1”file.Now “alm” file has no contents everything moved to the “alm1”.That is “alm”file is renamed to “alm1”.
Counting number of words in a file:
The “wc”(word count) command is used to count the number of lines, words and character in a file, for example
[thrcse01@localhost thrcse01]$ wc file
2 3 14 file

“wc” counts 2 lines, 3 words and 14 characters. The file name has also been shown in the fourth column. Here a line is any group of characters not containing a new line character. A word is a group of characters not containing a space, tab or new line. A character is the smallest unit of information and includes all spaces , tabs and new line.
Performing mathematical calculation:
The “bc” command is used to perform basic mathematical calculation.
The UNIX respond of “bc”command is
[thrcse01@localhost thrcse01]$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2*3
6
7+5-4*3
0
[thrcse01@localhost thrcse01]$

When you invoke “bc” without arguments, the input has to be keyed in , each line terminated by pressing . After you have finished your work , use for terminating the “bc” command.
Clear the screen:
The “clear” command is used to vanish the entire content in the command screen. That is whatever has been typed so far has vanished from our view and the cursor is positioned at the top-left corner of the screen.






SHELL PROGRAMMING
EX.NO: 2a) GREATEST AMONG THREE NUMBERS

AIM :

To write a shell program to find the greatest among three numbers using loops.


ALGORITHM:

1. Start
2. Read the three value a,b,c.
3. the value of a>b and a>c then print “a” is greater.
4. Else if b>c then print “b” is greater.
5. Else print “c” is greater.
6.Stop.


PROGRAM
echo "Enter the 3 number”
read a,b,c
If test $a –gt $b –a $a -gt $c
Then
echo "$a is greater"
elif test $b -gt $c
Then
echo "$b is greater"
else
echo "$c is greater"
fi


OUTPUT:
[it3@localhost it3]$sh ggr.sh
Enter the 3 number...
5
6
7
7 is greater.




















EX.NO : 2b) ARMSTRONG NUMBER


AIM:

To write a shell program to check whether the given number is Armstrong or not.

ALGORITHM:

Step1: Enter the given number.
Step 2: Separate the digit using the formula q=`expr $n % 10`and r=`expr $n %10`.
Step 3: Calculate sum of cube of digits by the formula a=`expr $a + $r /* $r /* $r`.
Step 4: Compute the result with the given number.
Step 5: if found equal then display it as Armstrong number else display not Armstrong
number.

PROGRAM:

echo”enter the number”
read n
q=$n
a=0
while [ $q – gt 0 ]
do
r= `expr $q % 10 `
q= `expr $q / 10 `
a=`expr $a + $r /* $r /*$r `
done
if [ $a=$n ]
then
echo “the number $n is armstrong number”
else
echo“the number $n is not armstrong number”
fi



OUTPUT:
[it6@localhost pgm]$ sh arm.sh
Enter the number:
153
The number 153 is Armstrong number.



















EX.NO : 2c) PATTERNS

AIM:
To write a shell program to print the records of the employee for the given conditions using patterns.
Pattern.sh
First name Last name Rate Hours
Senthil kumar 15 20
Kousik kumar 12 0
Kumar ganesh 13 21
Mohan Raj 0 10
Shanmuga Nathan 12 11
Guna sekhar 32 48
QUESTIONS
1. Print the records of employee who didn’t work last week.
2. Print the records employee whose rate>15.
3. Print the records of employee whose first name is Guna.
4. Print the hours of employee whose hours is 20-40.
5. Print the hours of employee whose rate is greater than 18.

ALGORITHM
1. Start
2. Read the records of the employee in the file.
3. Print the records of the employee for the given conditions using echo commands
4. Stop.
PROGRAM:
First name Last name Rate Hours
Senthil kumar 15 20
Kousik kumar 12 0
Kumara ganesh 13 21
Mohan Raj 0 10
Shanmuga Nathan 12 11
Guna sekhar 32 48

[it3@1local host it3]$ cat ah0011
Awk `$4==0{print $1 $2}’ah0011.sh
Awk `$3 > 15{print $1 $2 $4}’ah0011.sh
Awk `$1==”guna”{print $1 $2}’ah0011.sh
Awk `$3 > 25{print $1 $4}’ah0011.sh
Awk `$4 > 40{print $1 $2 $3 $4 }’ah0011.sh
OUTPUT
[it3@1local host it3]$ cat ah0011
Kaushikkumar
First name Last name Hours
Guna sekhar 48
First name Hours
Guna 48
Kumaraganesh
First name Last name Rate Hours
Guna sekhar 32 48










EX.NO : 2d) EXPANSIONS
AIM:
To write a shell program to implement variable expansion using command line argument to test the files in the directory.
ALGORITHM:
1. Start.
2. Open a file in vi editor.
3. Assign current directory to the variable.
4. Change directory to the assigned variable.
5. List all the files using ls command.
6. Stop.
PROGRAM:
a=$*
cd $a
ls
OUTPUT
00 aa it057 ree
01 aart it57pattern.sh sri
015 sshh it19 thy








EX.NO : 2e) SUBSTITUTION
AIM:
To write a shell program to count the number of words in a file using substitution.

ALGORITHM:

1.Start.
2.Open a file in vi editor.
3.Assign current directory to the variable.
4.Count the number of words,line,characters using echo and wc command.
5.Stop.

PROGRAM:

Echo”enter the file name”
Read a
Echo”wc of aa is”
Echo “$(wc $a)”

OUTPUT
[it3@localhost it3]$sh aa
Enter the file name
Coat
Wc of coat is
1 1 3 coat





EX.NO : 2f) STUDENT GRADE SYSTEM

AIM:
To write a program to compute the grade of the student using substitution.

ALGORITHM:
1. Start.
2. Open a file in vi editor.
3. read the student name and the subject name.
4. calculate the total and the average.
5. If the average is greater than 75,print first class.
6. If the average is greater than 35.print secound class.
7. Else print fail.
8. print student name along with the grade.
9. Stop.

PROGRAM:
Echo”enter the name of the student “
Read a
Echo”enter the 3 marks”
Read b
Read c
Read d
F=`expr $b + $c + $d`
G=`expr $F / 3`
If test $g –gt 75
Then
Echo”1st class”
Elif test $g –gt 50
Then
Echo”2nd class”
Else
Echo”fail”
Fi

OUTPUT
[it3@localhost it3]sh aa
Enter the name of the student
Hema
Enter the 3 marks
98
97
76
1st class
















Ex.No:3a) CPU SCHEDULING: FIRST COME FIRST SERVE


AIM:

To write a C program to implement the CPU scheduling algorithm FIRST COME FIRST SERVE.

PROBLEM DESCRIPTION:

Cpu scheduler will decide which process should be given the CPU for its execution.For
this it uses different algorithm to choose among the process. One among that algorithm is FCFS
algorithm.In this algorithm the process which arrive first is given the cpu after finishing its
request only it will allow cpu to execute other process.

ALGORITHM:

Step 1: Create the number of process.
Step 2: Get the ID and Service time for each process.
Step 3: Initially, Waiting time of first process is zero and Total time for the first process is the starting time of that process.
Step 4: Calculate the Total time and Processing time for the remaining processes.
Step 5: Waiting time of one process is the Total time of the previous process.
Step 6: Total time of process is calculated by adding Waiting time and Service time.
Step 7: Total waiting time is calculated by adding the waiting time for lack process.
Step 8: Total turn around time is calculated by adding all total time of each process.
Step 9:Calculate Average waiting time by dividing the total waiting time by total number of process.
Step 10: Calculate Average turn around time by dividing the total time by the number of process.
Step 11: Display the result.

PROGRAM CODING:

#include
int main()
{
int n,b[10],t=0,i,w=0,r=0,a=0;
float avg,avg1;
printf("\nEnter number of processes:");
scanf("%d",&n);
printf("\nEnter the burst times : \n");
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
printf("\n Gantt chart ");
for(i=1;i<=n;i++)
printf("P%d\t",i);
printf("\n\nProcess BurstTime WaitingTime TurnaroundTime\n");
for(i=1;i<=n;i++)
{
t=t+w;
r=r+b[i];
printf("P%d\t\t%d\t\t%d\t\t%d\t\t\n",i,b[i],w,r);
w=w+b[i];
a=a+r;
}
avg=(float)t/n;
avg1=(float)a/n;
printf("\n Average WaitingTime is %f",avg);
printf("\n Average TurnaroundTime is %f\n",avg1);
return(0);
}

OUTPUT:
[cse6@localhost Pgm]$ cc prog9a.c -o prog9a.out
[cse6@localhost Pgm]$ ./prog9a.out
Enter number of processes : 3
Enter the burst times :
24
5
3
Gantt chart P1 P2 P3
Process BurstTime WaitingTime TurnaroundTime
P1 24 0 24
P2 5 24 29
P3 3 29 32
Average WaitingTime is 17.666666
Average TurnaroundTime is 28.333334
[cse6@localhost Pgm]$























Ex.No:3b) CPU SCHEDULING: SHORTEST JOB FIRST


AIM:

To write a C program to implement the CPU scheduling algorithm for Shortest job first.

PROBLEM DESCRIPTION:

Cpu scheduler will decide which process should be given the CPU for its execution. For
this it uses different algorithm to choose among the process. One among that algorithm is SJF algorithm.In this algorithm the process which has less service time given the cpu after finishing its request only it will allow cpu to execute next other process.

ALGORITHM:


Step 1: Get the number of process.
Step 2: Get the id and service time for each process.
Step 3: Initially the waiting time of first short process as 0 and total time of first short is
process the service time of that process.
Step 4: Calculate the total time and waiting time of remaining process.
Step 5: Waiting time of one process is the total time of the previous process.
Step 6: Total time of process is calculated by adding the waiting time and service time of
each process.
Step 7: Total waiting time calculated by adding the waiting time of each process.
Step 8: Total turn around time calculated by adding all total time of each process.
Step 9: Calculate average waiting time by dividing the total waiting time by total
number of process.
Step 10: Calculate average turn around time by dividing the total waiting time by total
number of process.
Step 11: Display the result.


PROGRAM CODING:
#include
int main()
{
int n,w[100],tot[100],i,j,awt,atot;
float avwt,avtot;
struct
{
int p,bt;
}
sjf[10],temp;
printf("Enter the number of Processes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the Burst time for Process%d : ",i);
scanf("%d",&sjf[i].bt);
sjf[i].p=i;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(sjf[j].bt>sjf[i].bt)
{
temp=sjf[i];
sjf[i]=sjf[j];
sjf[j]=temp;
}
w[1]=0;
tot[1]=sjf[1].bt;
for(i=2;i<=n;i++)
tot[i]=tot[i-1]+sjf[i].bt;
awt=0;
atot=0;
for(i=1;i<=n;i++)
{
w[i]=tot[i]-sjf[i].bt;
awt+=w[i];
atot+=tot[i];
}
avwt=(float)awt/n;
avtot=(float)atot/n;
printf("\n\nProcessId\tWaiting time\t TurnaroundTime");
for(i=1;i<=n;i++)
printf("\n\t%d\t\t%d\t\t%d",sjf[i].p,w[i],tot[i]);
printf("\n\nTotal Waiting Time :%d",awt);
printf("\n\nTotal Turnaround Time :%d",atot);
printf("\n\nAverage Waiting Time :%.2f",avwt);
printf("\n\nAverage Turnaround Time :%.2f",avtot);
}








OUTPUT:

[cse6@localhost Pgm]$ cc prog9b.c
[cse6@localhost Pgm]$ ./a.out
Enter the number of Processes:3
Enter the Burst time for Process1 : 24
Enter the Burst time for Process2 : 5
Enter the Burst time for Process3 : 3
ProcessId Waiting time TurnaroundTime
3 0 3
2 3 8
1 8 32
Total Waiting Time :11
Total Turnaround Time :43
Average Waiting Time :3.67
Average Turnaround Time :14.33
[cse6@localhost Pgm]$















Ex.No:3c) CPU SCHEDULING: PRIORITY SCHEDULING

AIM:

To write a C program to implement the CPU scheduling algorithm for Priority.

PROBLEM DESCRIPTION:

Cpu scheduler will decide which process should be given the CPU for its execution. For this it uses different algorithm to choose among the process. One among that algorithm is Priority algorithm.In this algorithm the processes will be given the priorities. The process which is having the highest priority is allocated the cpu first.After finishing the request the cpu is allocated to the next highest priority and so on.

ALGORITHM:

Step 1: Get the number of process
Step 2: Get the id and service time for each process.
Step 3: Initially the waiting time of first short process as 0 and total time of first short is
process the service time of that process.
Step 4: Calculate the total time and waiting time of remaining process.
Step 5: Waiting time of one process is the total time of the previous process.
Step 6: Total time of process is calculated by adding the waiting time and service time of
each process.
Step 7: Total waiting time calculated by adding the waiting time of each process.
Step 8: Total turn around time calculated by adding all total time of each process.
Step 9: Calculate average waiting time by dividing the total waiting time by total
number of process.
Step 10: Calculate average turn around time by dividing the total waiting time by total
number of process.
Step 11: Display the result.



PROGRAM CODING:
#include
int main()
{
int n,temp=0,w[20],b[20],
p[20], t2[20],j,t1,d[20],i,
te=0,b1[20],t3=0;
float t,r;
w[1]=0;
printf("\nEnter no. of processes:");
scanf("%d",&n);
printf("\nEnter the burst times : ");
for(i=1;i<=n;i++)
{
rintf("P%d : ",i);
scanf("%d",&b[i]);
d[i]=i;
}
printf("Enter the priorities:");
for(i=1;i<=n;i++)
{
printf("P%d : ",i);
scanf("%d",&p[i]);
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(p[i]{
temp=p[i];
t1=d[i];
te=b[i];
p[i]=p[j];
d[i]=d[j];
b[i]=b[j];
p[j]=temp;
d[j]=t1;
b[j]=te;
}
printf("\nGantt Chart : ");
for(i=1;i<=n;i++)
printf("P%d\t",d[i]);
printf("\nProcess \t Priority\tBurst Time\t Waiting Time\t Turnaround Time");
for(i=1;i<=n;i++)
{
t=d[i];
w[i+1]=w[i]+b[i];
t2[i]=b[i]+w[i];
t3+=t2[i];
printf("\nP%d\t\t%d\t\t%d\t\t%d\t\t%d",d[i],p[i],b[i],w[i],t2[i]);
}
temp=0;
for(i=1;i<=n;i++)
temp+=w[i];
t=(float)temp/n;
r=(float)t3/n;
printf("\nAverage Waiting time : %.2f",t);
printf("\nAverage Turnaround time : %.2f",r);
}


OUTPUT:
[cse6@localhost Pgm]$ cc prog10a.c
[cse6@localhost Pgm]$ ./a.out
Enter the no. of processes : 3
Enter the burst times
P1 : 24
P2 : 5
P3 : 3
Enter the priorities
P1 : 2
P2 : 1
P3 : 3
Gantt Chart : P2 P1 P3
ProcessID Priority BurstTime WaitingTime TurnaroundTime
P2 1 5 0 5
P1 2 24 5 29
P3 3 3 29 32

Average Waiting Time : 11.33
Average Turnaround Time : 22.00
[cse6@localhost Pgm]$























EX.NO : 3d) CPU SCHEDULING: ROUND ROBIN SCHEDULING


AIM :

To simulate the round robin program.

PROBLEM DESCRIPTION:

CPU scheduler will decide which process should be given the CPU for its execution .For
this it use different algorithm to choose among the process .one among that algorithm is Round robin algorithm.In this algorithm we are assigning some time slice .The process is allocated according to the time slice ,if the process service time is less than the time slice then process itself will release the CPU voluntarily .The scheduler will then proceed to the next process in the ready queue .If the CPU burst of the currently running process is longer than time quantum ,the timer will go off and will cause an interrupt to the operating system .A context switch will be executed and the process will be put at the tail of the ready queue.

ALGORITHM:

Step 1: Initialize all the structure elements
Step 2: Receive inputs from the user to fill process id,burst time and arrival time.
Step 3: Calculate the waiting time for all the process id.
i) The waiting time for first instance of a process is calculated as:
a[i].waittime=count + a[i].arrivt
ii) The waiting time for the rest of the instances of the process is calculated as:
a) If the time quantum is greater than the remaining burst time then waiting time is calculated as: a[i].waittime=count + tq
b) Else if the time quantum is greater than the remaining burst time then waiting time is calculated as: a[i].waittime=count - remaining burst time
Step 4: Calculate the average waiting time and average turnaround time
Step 5: Print the results of the step 4.

PROGRAM CODING:

#include
void main()
{
int i,tbt=0,nop,ts=0,flag[20], rem[20];
int from,wt[20],tt[20],b[20], twt=0,ttt=0;
int dur;
float awt,att;
clrscr();
printf("Enter no. of Processes: ");
scanf("%d",&nop);
printf("Enter the time slice: ");
scanf("%d",&ts);
printf("Enter the Burst times..\n");
for(i=0;i{
wt[i]=tt[i]=0;
printf("P%d\t: ",i+1);
scanf("%d",&b[i]);
rem[i]=b[i];
tbt+=b[i];
flag[i]=0;
}
from=0;
i=0;
printf("\n\t Gantt Chart");
printf("\n ProcessID\tFrom Time\tTo Time\n");
while(from{
if(!flag[i])
{
if(rem[i]<=ts)
{
dur=rem[i];
flag[i]=1;
tt[i]=dur+from;
wt[i]=tt[i]-b[i];
}
else
dur=ts;
printf("%7d%15d%15d\n",i+1, from,from+dur);
rem[i] -= dur;
from += dur;
}
i=(i+1)%nop;
}
for(i=0;i{
twt+=wt[i];
ttt+=tt[i];
}
printf("\n\n Process ID \t Waiting Time \t Turn Around Time");
for(i=0;i{
printf("\n\t%d\t\t%d\t\t%d",i+1,wt[i],tt[i]);
}
awt=(float)twt/(float)nop;
att=(float)ttt/(float)nop;
printf("\nTotal Waiting Time:%d",twt);
printf("\nTotal Turn Around Time:%d",ttt);
printf("\nAverage Waiting Time:%.2f",awt);
printf("\nAverage Turn Around Time:%.2f\n",att);
getch();
}

OUTPUT:
Enter no. of Processes: 3
Enter the time slice: 3
Enter the Burst times..
P1 : 24
P2 : 5
P3 : 3

ProcessID From Time To Time
1 0 3
2 3 6
3 6 9
1 9 12
2 12 14
1 14 17
1 17 20
1 20 23
1 23 26
1 26 29
1 29 32


Process ID Waiting Time Turn Around Time
1 8 32
2 9 14
3 6 9

Total Waiting Time:23
Total Turn Around Time:55
Average Waiting Time:7.67
Average Turn Around Time:18.33





Ex.No:4a) PROGRAM USING SYSTEM CALL FORK ()


AIM :


To execute programs based on system calls of UNIX operating system.


FORK():

Fork creates a child process that differs from the parent process only in its PID and PPID.








On success,the PID of the child process is returned in the parents tread of execution and

ALGORITHM :

Step 1 : Declare the variable pid.
Step 2 : Get the pid value using system call fork().
Step 3 : If pid value is less than zero then print as “Fork failed”.
Step 4 : else if pid value is equal to zero include the new process in the system’s file
using execlp function.
Step 5 : else if pid is greater than zero then it is the parent process then make it to wait
till the child completes.
Step 6 : Then print “Child complete”.

PROGRAM CODING :


#include
main(int argc,char *arg[])
{
int pid;
pid=fork();
if(pid<0)
{

fprintf(strerr,"fork failed");
exit(-1);
}
else if(pid==0)
{
execlp("bin\1s","7s",NULL);
}
else
{
wait(NULL);
printf("Child Complete");
exit(0);
}
}

OUTPUT:

[cse6@localhost Pgm]$ cc prog4a.c
[cse6@localhost Pgm]$ ./a.out
Child Complete














Ex.No.4b) PROGRAM USING SYSTEM CALL
getpid() & getppid()


AIM :

To write the program to implement the system call getpid() and getppid().

ALGORITHM :


Step 1 : Declare the variables pid , parent pid , child id and grand child id.
Step 2 : Get the chid id value using system call fork().
Step 3 : If child id value is less than zero then print as “error at fork() child”.
Step 4 : If child id !=0 then using getpid() system call get the process id.
Step 5 : Print “I am parent” and print the process id.
Step 6 : Get the grand child id value using system call fork().
Step 7 : If grand child id value is less than zero then print as “error at fork() grand
child”.
Step 8 : If grand child id !=0 then using getpid system call get the process id.
Step 9 : Assign the value of pid to my pid.
Step 10 : Print “I am child” and print the value of my pid.
Step 11 : Get my parent pid value using system call getppid().
Step 12 : Print “My parent’s process id” and its value.
Step 13 : Else print “I am the grand child”.
Step 14 : Get the grand child’s process id using getpid() and print it as “my process id”.
Step 15 : Get the grand child’s parent process id using getppid() and print it as “my
parent’s process id”.

PROGRAM CODING:
int my_pid;
int my_parent_pid;
main()
{
int child_id;
if((child_id=fork())==-1)
{
perror("error at fork() child");
exit(0);
}
if(child_id!=0)
{
my_pid=getpid();
printf("i am a parent \n");
printf("my process ID is:%d\n",my_pid);
exit(0);
}
else
{
int grand_child_id;
if((grand_child_id=fork())==-1)
{
perror("Error at Fork() grandchild");
exit(0);
}
if(grand_child_id!=0)
{
my_pid=getpid();
printf("i am child \n");
printf("my process ID is %d\n",my_pid);
my_parent_pid=getppid();
printf("my parents process ID is %d\n",my_parent_pid);
exit(0);
}
else
{
printf("i am the grand child \n");
my_pid=getpid();
printf("my process ID is %d\n",my_pid);
my_parent_pid=getppid();
printf("my parents process ID is %d\n",my_parent_pid);
exit(0);
}
}
}

OUTPUT:
[cse6@localhost Pgm]$ cc prog4b.c -o prog4b.out
[cse6@localhost Pgm]$ ./prog4b.out
i am the grand child
my process ID is 10297
my parents process ID is 10296
i am child
my process ID is 10296
my parents process ID is 10295
i am a parent
my process ID is:10295
[cse6@localhost Pgm]$



Ex.No.4c) PROGRAM USING SYSTEM CALL WAIT()

AIM :

To execute programs based on system call wait () of UNIX operating system.

WAIT():

Wait, waitpid is used to wait for process termination.









The wait function suspends execution of the current process until a child has exits or until a signal is delivered where action is to termin ate the current process of to call a signal handling function.

1->which means to wait for any child process,this is the same behaviour which wait exhibits.
0->which means to wait for any child process groupid is equal to that of the calling process.


PROGRAM :
#include
#include< sys/wait.h>
Main()
{
Int i=0,pid;
Printf(“ready to fork()\n\n”);
Pid=fork();
If(!pid)
{
Printf(“child starts…\n”);
For(i=0;i<100;i++)
Printf(“%d/t”,i);
Printf(“parent ends”\n”);
}
}
Ex.No.4d) PROGRAM USING SYSTEM CALL
opendir (), readdir() & closeddir()

AIM :

To execute programs based on system call opendir(),readdir(),closedir() of UNIX operating system.

OPENDIR():

The directory is opened using the opendir() function and the pointer returned by this function and is assigned to dirname.













READDIR()


The directory is read using the readdir().










CLOSEDDIR():

This function is used to close the directory closedir().



PROGRAM CODING:
#include
#include
#include
Main(int argc,char *argv[])
{
DIR *dirname;
Struct dirent *rdir;
Dirname=opendir(argv[1]);
While(rdir=readdir(dirname))
{
If(strcmp(rdir=readdir->d_name,argv[2]==0)
{
Closedir(dirname);
Printf(“found entry %s\t\n”,argv[2]);
Exit(0);
}
}
Printf(“could not find entry %s written %s\n”,argv[2],argv[1]);
Closedir(dirname);
}


OUTPUT
[cse6@localhost PGM]$ cc 011f7.c
[cse6@localhost PGM]$ ./a.out 011fork fork
Found entry fork







Ex.No.5) PROGRAM USING I/O SYSTEM CALL
open (), read() & write()

AIM :

To write the program to implement the system call open(),read() and write().

ALGORITHM :

Step 1 : Declare the structure elements.
Step 2 : Create a temporary file named temp1.
Step 3 : Open the file named “test” in a write mode.
Step 4 : Enter the strings for the file.
Step 5 : Write those strings in the file named “test”.
Step 6 : Create a temporary file named temp2.
Step 7 : Open the file named “test” in a read mode.
Step 8 : Read those strings present in the file “test” and save it in temp2.
Step 9 : Print the strings which are read.

PROGRAM CODING:
#include
struct
{
int i;
char c[15];
}temp1,temp2;
char *pc;
int temp_size;
main()
{
FILE *fp,*fopen();
pc=(char *)&temp1;
temp_size=sizeof(temp1);
fp=fopen("test","w");
printf("\nEnter a string : ");
scanf("%c%s",&temp1.i,&temp1.c);
fwrite(pc,temp_size,1,fp);
fclose(fp);
pc=(char *)&temp2;
temp_size=sizeof(temp2);
fp=fopen("test","r");
fread(pc,temp_size,1,fp);
printf("%c%s\n",temp2.i,temp2.c);
fclose(fp);
}
OUTPUT:
[cse6@localhost PGM]$ cc prog5.c -o prog5.out
[cse6@localhost PGM]$ ./prog5.out
Enter a string: Hello
Hello
[cse6@localhost PGM]$



















Ex.No:6 SIMULATION OF UNIX COMMAND “LS & GREP”

AIM:

To write a C program to simulate the operation of “LS & GREP” command in Unix.

ALGORITHM :

SIMULATION OF LS COMMANDS:

Step 1 : Start.
Step 2 : Get the name of the directory using command line argument.
Step 3 : While directory is not empty, print the contents of the directory.
Step 4 : Stop.

PROGRAM CODING:
#include
#include
int main()
{
FILE *fp;
char w[20],w1[20],w2[20],str[20];
int fag=0;
fp=fopen(“it53abc.txt”,”r’);
printf("\n Enter the pattern: ");
scanf("%s",&str);
fscanf(fp,”%s%s%s”,w,w1,w2);
while(!feof(fp))
{
If(strcmp(str,w)==0)
{
Printf(“%s \t %s \t %s \t %s”,w,w1,w2);
Flag=1;
Break; }
If(strcmp(str,w)==0)
{
Printf(“%s \t %s \t %s \t %s”,w,w1,w2);
Flag=1;
Break; }
fscanf(fp,“%s%s%s%s”,w,w1,w2);
If(flag==0)
{
Printf(“Pattern not found”); } }
}

OUTPUT:

[cse6@localhost Pgm]$ cc ls53.c
[cse6@localhost Pgm]$ .a/.out sha
San
Sha
[cse6@localhost Pgm]$
































SIMULATION OF GREP COMMANDS:


Step 1 : Start.
Step 2 : Get the name of the file where pattern is to be searched.
Step 3 : While end of file is not reached,search for the pattern.
Step 4 :If the pattern is found,print the entire sentence.
Step 5:Else print the pattern is not found.
Step 6: Stop.

PROGRAM CODING:
#include
#include
int main()
{
FILE *fp;
char w[20],w1[20],w2[20],str[20];
int fag=0;
fp=fopen(“it53abc.txt”,”r’);
printf("\nEnter the pattern: ");
scanf("%s",&str);
fscanf(fp,”%s%s%s”,w,w1,w2);
while(!feof(fp))
{
If(strcmp(str,w)==0)
{
Printf(“%s \t %s \t %s \t %s”,w,w1,w2);
Flag=1;
Break;
}
If(strcmp(str,w)==0)
{
Printf(“%s \t %s \t %s \t %s”,w,w1,w2);
Flag=1;
Break;
}
fscanf(fp, (“%s%s%s%s”,w,w1,w2);
If(flag==0)
{
Printf(“Pattern not found”);
}
}
}

OUTPUT:
[cse6@localhost Pgm]$ cc grep53.c
[cse6@localhost Pgm]$ .a/.out
Enter the pattern:how
How are u?
[cse6@localhost Pgm]$



















Ex.No.7 PRODUCER CONSUMER PROBLEM

AIM:

To write a C program to implement the Producer & consumer Problem (Semaphore)

ALGORITHM:

Step 1: The Semaphore mutex, full & empty are initialized.
Step 2: In the case of producer process
i) Produce an item in to temporary variable.
ii) If there is empty space in the buffer check the mutex value for enter into the critical section.
iii) If the mutex value is 0, allow the producer to add value in the temporary variable to the buffer.
Step 3: In the case of consumer process
i) It should wait if the buffer is empty
ii) If there is any item in the buffer check for mutex value, if the mutex==0, remove item from buffer
iii) Signal the mutex value and reduce the empty value by 1.
iv) Consume the item.
Step 4: Print the result


PROGRAM CODING:
#include
#include
#include
#include
#include
#include
#define NUM_ELEM 10
#define SEM_EMPTY 0
#define SEM_FULL 1
main()
{
int rc,pid,semid,i,j;
int shmid,status;
char elem;
union semun
{
int val;
}mysemun;
struct sembuf waitempty={SEM_EMPTY,-1,SEM_UNDO};
struct sembuf signalempty={SEM_EMPTY,1,IPC_NOWAIT};
struct sembuf waitfull={SEM_FULL,-1,SEM_UNDO};
struct sembuf signalfull={SEM_FULL,1,IPC_NOWAIT};
struct shmid_ds myshmid_ds;
void *shmptr;
semid=semget(IPC_PRIVATE,2,0666|IPC_CREAT);
mysemun.val=NUM_ELEM;
semctl(semid,SEM_EMPTY,SETVAL,mysemun);
mysemun.val=0;
semctl(semid,SEM_FULL,SETVAL,mysemun);
shmid=shmget(IPC_PRIVATE,NUM_ELEM,0666|IPC_CREAT);
pid=fork();
if(pid==0)
{
shmptr=shmat(shmid,0,SHM_R);
for(i=0;i<10;i++)
{
//semop(semid,&signalempty,1);
sleep(1);
}
exit(0);
}
else
{
shmptr=shmat(shmid,0,SHM_W);
for(i=0;i<10;i++)
{
semop(semid,&waitempty,1);
elem='a'+i;
printf("Produced element %c\n",elem);
elem=*((char*)shmptr+(i%NUM_ELEM))=elem;
semop(semid,&signalfull,1);
sleep(2);
//semop(semd,&waitfull,1);
}
wait(&status);
shmctl(shmid,IPC_RMID,&myshmid_ds);
semctl(semid,SEM_EMPTY,IPC_RMID,mysemun);
exit(0);
}
}




OUTPUT:
[cse6@localhost Pgm]$ cc ex11.c -o ex11.out
[cse6@localhost Pgm]$ ./ex11.out
Produced element a
Consumed element a
Produced element b
Consumed element b
Produced element c
Consumed element c
Produced element d
Consumed element d
Produced element e
Consumed element e
Produced element f
Consumed element f
Produced element g
Consumed element g
Produced element h
Consumed element h






Ex.No. 7a) FIRST FIT ALGORITHM




AIM:

To write a C program to implement Memory Management concept using the technique
FirstFit Algoithm.



PROBLEM DESCRIPTION:

Inorder to dynamically allocate the memory we go for this technique First Fit Algorithm.In this first fit algorithm, the allocator keeps the list of free blocks and on receiving a request for memory ,scans along the list for the first block that is large enough to satisfy the request. If the chosen block is significantly larger than that requested ,then it is usually split,and the remainder added to the list as another free block. The first fit algorithm performs resonably well, as it ensures that allocations are quick. In recycling free blocks, there is a choice is to where to add the blocks to the free list, effectively in what order the free list is kept.




ALGORITHM:

The following are the steps to implement first fit algorithm.

Step 1: Get the number of free space available.
Step 2: Get the starting address of each freespace and how much space available.
Step 3: While getting the address and space we have to check whether there is
duplication,if there is a duplication then give the error message for duplication & get the address and space once again.
Step 4: Get the space for the process.
Step 5: Search the first memory location from the available list its size should be greater
than or equal to the process size an or equal to available space in fifo order.
Step 6: If there is no sufficient space output the error message.
Step 7: Display the process and its corresponding allocated memory space






PROGRAM CODING:
#include
int main()
{
int p[20],n,np,i,j,b[50];
printf("\nEnter the number of unallocated segments: ");
scanf("%d",&n);
printf("Enter the memory sizes: ");
for(i=0;iscanf("%d",&b[i]);
printf("Enter the number of processes : ");
scanf("%d",&np);
printf("Enter the Memory requirements of Processes.\n");
for(i=0;i{
printf("p%d: ",i+1);
scanf("%d",&p[i]);
}
printf("Process ID\t First Fit\n");
for(i=0;i{
for(j=0;jif(b[j]>=p[i])
{
printf("%d\t\t %d\n",i+1,b[j]);
b[j]=-1;
break;
}
if(j==n)
printf("%d\t\tNot enough memory\n",i+1);
}
return 0;
}













OUTPUT
[cse6@localhost CSE6]$cc prog12a.c
[cse6@localhost CSE6]$ ./a.out
Enter the number of unallocated segments: 3
Enter the memory sizes:
210
120
330
Enter the number of processes: 3
Enter the Memory requirements of Processes.
p1: 120
p2: 310
p3: 450
Process ID First Fit
1 210
2 330
3 Not enough memory

[cse6@localhost CSE6]$ cc prog12a.c
[cse6@localhost CSE6]$ ./a.out
Enter the number of unallocated segments: 3
Enter the memory sizes:
210
120
330
Enter the number of processes : 3
Enter the Memory requirements of Processes.
p1: 120
p2: 80
p3: 230
Process ID First Fit
1 210
2 120
3 330

[cse6@localhost CSE6]$ cc prog12a.c
[cse6@localhost CSE6]$ ./a.out







Ex.No:7b) BEST FIT ALGORITHMS




AIM:

To write a C program to implement Memory Management concept using the technique BestFit Algoithm.



PROBLEM DESCRIPTION:

Inorder to dynamically allocate the memory we go for this technique Best Fit Algorithm.In this algorithm ,a search is performed very much like firstfit ,except that it does not stop when a suitable block has been found.Instead ,the search continues to try all the blocks in the list.The block choosen is a closest to the desired size.This avoids unnecessarily fragmenting very large allocation spaces.



ALGORITHM:

Step 1: Get the number of free space available.
Step 2: Get the starting address of each free space and how much space available.
Step 3: While getting the address and space we have to check whether there is
duplication,if there is a duplication then give the error message for duplication
& get the address and space once again.
Step 4: Get the space for the process.
Step 5: Sort the available list in ascending order based on the space.
Step 6: Search the first memory location from the available list its size should be greater
than or equal to the process size an or equal to available space in FIFO order.
Step 7: If there is no sufficient space output the error message.
Step 8: Display the process and its corresponding allocated memory space.











PROGRAM CODING:
#include
int main()
{
int a,b[50],i,j,temp,p[50],n,m;
printf("\nEnter the no.of allocated space: ");
scanf("%d",&n);
printf("\nEnter the allocated Memory space: ");
for(i=0;iscanf("%d",&b[i]);
for(i=0;ifor(j=i+1;jif(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
printf("\nEnter the Number of processes: ");
scanf("%d",&m);
for(i=0;i{
printf("Enter the memory space for the given process: ");
scanf("%d",&p[i]);
}
printf("Process ID\t Best fit\n");
for(i=0;i{
printf("p%d\t\t ",i+1);
for(j=0;j{
if(p[i]<=b[j])
{
printf("%d\n",b[j]);
b[j]=-1;
break;
}
}
if(j==n)
printf("Not Enough memory\n");
}
return 0;
}


OUTPUT:

[cse6@localhost Pgm]$ cc prog12b.c
[cse6@localhost Pgm]$ ./a.out
Enter the no.of allocated space: 3
Enter the allocated Memory space: 200
120
150
Enter the Number of processes: 3
Enter the memory space for the given process: 130
Enter the memory space for the given process: 210
Enter the memory space for the given process: 140
Process ID Best fit
p1 150
p2 Not Enough memory
p3 200

















Ex. No. 7d) WORST FIT ALGORITHM




AIM:

To write a C program to implement Memory Management concept using the technique
Best Fit Algorithm.



PROBLEM DESCRIPTION:

Inorder to dynamically allocate the memory we go for this technique Worst Fit Algorithm.In this algorithm ,a search is performed very much like firstfit ,except that it does not stop when a biggest block has been found.Instead ,the search continues to try all the blocks in the list.The block choosen is the largest one to the desired size.





ALGORITHM:

Step 1: Get the number of free space available.
Step 2: Get the starting address of each free space and how much space available.
Step 3: While getting the address and space we have to check whether there is
duplication,if there is a duplication then give the error message for duplication & get the address and space once again.
Step 4: Get the space for the process.
Step 5: Sort the available list in descending order based on the space.
Step 6: Search the first memory location from the available list its size should be greater
than or equal to the process size an or equal to available space in FIFO order.
Step 7: If there is no sufficient space output the error message.
Step 8: Display the process and its corresponding allocated memory space.






PROGRAM CODING:
#include
int main()
{
int a,b[50],i,j,temp,p[50],n,m;
printf("\nEnter the no.of allocated space: ");
scanf("%d",&n);
printf("\nEnter the allocated Memory space: ");
for(i=0;iscanf("%d",&b[i]);
for(i=0;ifor(j=i+1;jif(b[i]{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
printf("\nEnter the Number of processes: ");
scanf("%d",&m);
for(i=0;i{
printf("Enter the memory space for the given process: ");
scanf("%d",&p[i]);
}
printf("Process ID\t Worst fit\n");
for(i=0;i{
printf("p%d\t\t ",i+1);
for(j=0;j{
if(p[i]<=b[j])
{
printf("%d\n",b[j]);
b[j]=-1;
break;
}
}
if(j==n)
printf("Not Enough memory\n");
}
return 0;
}


OUTPUT:
[cse6@localhost Pgm]$ cc prog12c.c -o prog12c.out
[cse6@localhost Pgm]$ ./prog12c.out
Enter the no.of allocated space: 3
Enter the allocated Memory space: 200
150
170
Enter the Number of processes: 3
Enter the memory space for the given process: 120
Enter the memory space for the given process: 160
Enter the memory space for the given process: 160
Process ID Worst fit
p1 200
p2 170
p3 Not Enough memory


















Ex.No:9 DEADLOCK AVOIDANCE

AIM:

To implement the deadlock avoidance concept using banker’s algorithm.

ALGORITHM:

1.Start.
2.let work and finish to the vectors of length m,n respectively.Intialize wait->available
and finish[i]->false for i=1,2,…..n.
3. Initialize work->available find “i” such that
(i)finish[i]=false
(ii)need[i]=work
If no such “i” exists goto step5.
4.If condition is true then
Work=work allocation
Finish[i]=true,then go to5 and check the remaining values.
5.if finish[i]=true,for all “I”then system is said to be in safe state. therefore there is no
deadlock.
6.Stop.


POGRAMCODE:

#include
#include
Int compare(int need[],int work[],int numres)
{
Int i;
For(i=0;i{
If(need[i]>=work[i])

{
Return 0;
}
Return 1;
}
}
Main()
{
Int numproc,numers,i,j,k;
Int allocation[6][6],max[6][6],available[6][6],need[6][6],finish[6];
Int work[6];
Clrscr();
Printf(:\n bankers algorithm \n”);
Printf(“enter the number of process: ”);
Scanf(“%d”,&numproc);
Printf (“enter the number of resource :”);
Scanf(“%d”,&numres);
Printf(“\n\n enter the values:\n\n);
For(i=0;i{
Printf(“\n process %d “i++);
Printf(“\n enter the values for allocation:”);
For(j=0;jScanf(“%d”,&allocation[i][j]);
Printf(“\n enter the values for max:”);
For(j=0;jScanf(“%d”,&max[i][j]);
For(j=0;jNeed[i][j]=max[i][j]-allocation[i][j];
}
Printf(“\n the need matrix is\n”);
For(i=0;i{
Printf(“\n p%d\t”,i+1);
For(j=0;jPrintf(“%d\t”,need[i][j]);
}
Printf(“\n enter the values for available:”);
For(i=0;i{
Scanf(“%d”,&available[0][i]);
Work[i]=available[0][i];
}
Printf (“\n order of finishing\n”);
For (i=0; i{
For (j=0; j{
If((finish[j]==0)&&(compare(need[j],work,numers)==1))
{
For(k=0;kWork[k]=work[k]+allocation[j][k];
For(k=0;kAvailable[j][k]=work[k];
Printf(“\n p%d\t”,j+1);
Finish[j]=1;
}
}
}

Printf(“\n the available matrix is\n”);
For(i=0;i{
Printf(“\n p%d\t”,i+1);
For(j=0;j{
printff(“%d\t”,available[i][j]);
}
}
i-0;
For(k=0;k{
If(finish[k]==1
{
Printf(“\n\n process %d has finished \n”,k+1);
}
Else
{
Printf(“\n\n deadlock at process %d!!\n”,k+1);
}
}
Printf(“the system is in safe state:no deadlock:”);
Getch();
}




















OUTPUT:
Bankers algorithm
Enter the number of process:5
Enter the number of resource :3
Enter the values:
Process 1
Enter the values for allocation:0
1
0
Enter the values for max:7
5
3
Process 2
Enter the allocation:2
0
0
Enter the values for max:3
2
2
Process 3
Enter the values for allocation:3
0
2
Enter the values for max:9
0
2
Process 4
Enter the allocation:2
1
1
Enter the values for max:2
2
2
Process 5
Enter the allocation:0
0
2
Enter the values for max:4
3
3
The need matrix is
P1 7 4 3
P2 1 2 2
P3 6 0 0
P4 0 1 1
P5 4 3 1
Enter the values for allocation:3
3
2
Ordering of finishing:
P2
P4
P5
P3
P1
The available matrix is:
P1 10 5 7
P2 5 3 2
P3 10 4 7
P4 7 4 3
P5 7 4 5

Process 1 has finished
Process 2 has finished
Process 3 has finished
Process 4 has finished
Process 5 has finished
The system is in safe state. No deadlock.


















0 comments:

  © Blogger templates Psi by Ourblogtemplates.com 2008

Back to TOP