Operating System Programs
Operating System Programs
V SEMESTER
Prepared By :
LIST OF EXPRIMENTS
LIST OF EXPERIMENTS
S.NO TITLE OF EXPERIMENTS
1. STUDY OF OS LINUX COMMANDS
2. SHELL PROGRAMMING
2.a. BIGGEST OF TWO NUMBERS
2.b. LEAP YEAR OR NOT
2.c. EVEN OR ODD NUMBER
2.d. SUM OF N NUMBERS
2.e. FACTORIAL OF A NUMBER
3 . SYSTEM CALLS
3.a. CREATE A CHILD PROCESS USING FORK
3.b. FORK AND WAIT SYSTEM CALL
3.c. CREATE A CHILD PROCESS USING SLEEP
3.d. DIRECTORY SYSTEM CALL
3.e. FILE SYSTEM CALL USING OPEN AND CLOSE
4 . SIMULATION OF LINUX COMMANDS
4.a. SIMULATION OF GREP
4.b. SIMULATION OF ls COMMAND
5 . PROCESSES SCHEDULING
5.a. IMPLEMENTATION OF FCFS SCHEDULING
5.b. IMPLEMENTATION OF SJF SCHEDULING ZERO ARRIVALTIME
5.c. IMPLEMENTATION OF SJF SCHEDULING NON ZERO ARRIVALTIME
5.d. IMPLEMENTATION OF PRIORITY SCHEDULING ZERO ARRIVALTIME
5.e. IMPLEMENTATION OF PRIORITY SCHEDULING NON ZERO ARRIVALTIME
5.f. IMPLEMENTATION OF RR SCHEDULING
6 . SEMAPHORE
6.a. PRODUCER AND CONSUMER PROBLEM
6.b. DINING PHILOSOPHER'S PROBLEM
6.c. READERS WRITER PROBLEM USING SEMAPHORE
7. DEADLOCK PREVENTION
7 SIMULATE ALGORITHM FOR DEADLOCK PREVENTION
8 . MEMORY MANAGEMENT
8.a. IMPLEMENTATION OF FIRST FIT
8.b. IMPLEMENTATION OF BEST FIT
8.c. IMPLEMENTATION OF WORST FIT
9 . PAGE REPLACEMENT
9.a. FIFO PAGE REPLACEMENT ALGORITHM
9.b. LRU PAGE REPLACEMENT ALGORITHM
9.c. OPTIMAL PAGE REPLACEMENT ALGORITHM
10 . DISK SCHEDULING
10.a. FCFS
10.b. SSTF
10.c. SCAN
10.d. C-SCAN
AIM:
To study the basic commands in Linux.
COMMANDS:
1. Calendar
NAME : calendar
(i) SYNTAX : cal
DESCRIPTION : Displays a simple calendar. If arguments are not
Specified, the current month is displayed.
EXAMPLE : cal
OUTPUT :
June 2014
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
2. Date
3. Script
NAME : SCRIPT – makes typescript of terminal session
DESCRIPTION : Makes a typescript of everything printed on your terminal. It is useful
for students who need a hardcopy record of an interactive session as
proof of an assignment, as the typescript file can be printed out later
with lpr(1).
SYNTAX : script scriptname
………..
………..
………..
Exit
OPENING A SCRIPT:
SYNTAX : vi scriptname
EXAMPLE : vi date.txt
OUTPUT : ~date
~ Mon Jul 23 12:17:50 IST 2012
~
~
INSERT
4. ls
(ii) SYNTAX : ls –l
DESCRIPTION : Displays files in long listing format.
OUTPUT :
csea08 csea08 1384962 Jul 23 12:17 [23.07.2012]
csea08 csea08 20325 Jul 23 12:30 [23.07.12]
csea08 csea08 138 Jul 23 12:33 [01;32case.sh]
csea08 csea08 13830 Jul 23 12:37 [01;34cse]
csea08 csea08 183 Jul 23 12:40 [01;32mdigit.sh]
csea08 csea08 530 Jul 23 12:50 [01;32memp.sh]
csea08 csea08 730 Jul 23 12:59 [01;32mgreatest.sh]
(iii) SYNTAX : ls -r
DESCRIPTION : Displays the files in reverse sorted order.
OUTPUT :
num.sh mouse.txt greatest.sh emp.sh
digit.sh cse.txt case.sh
(iv) SYNTAX : ls -s
DESCRIPTION : Displays the size of each files.
OUTPUT :
8 num.sh 4 mouse.txt 8 greatest.sh 8 emp.sh
8 digit.sh 4 cse.txt 8 case.sh
(v) SYNTAX : ls -S
DESCRIPTION : Displays the files in sorted order.
OUTPUT :
case.sh cse.txt digit.sh emp.sh
greatest.sh mouse.txt num.sh
5. cp
NAME : cp – copy files and directories
SYNTAX : cp fi f2
DESCRIPTION : Copies f1 to f2
EXAMPLE : cp cse.txt new.txt
6. rm
7. mv
8. mkdir
9. rmdir
10. Pwd
11. Cd
EXAMPLE : cd New
(ii) SYNTAX : cd ..
DESCRIPTION : Quits from the current directory.
12. Cat
NAME : cat- concatenate & open files and print on the standard output
(i) SYNTAX : cat > filename
DESCRIPTION : This command is used to open a new file.
EXAMPLE : cat > a.txt
OUTPUT :
NAME : ZZZZ
ROLL NO: XX
a.txt=>
NAME : ZZZZ
ROLL NO: XX
b.txt=>
COLLEGE.SMVEC
c.txt=>
NAME : ZZZZ
ROLL NO: XX
COLLEGE.SMVEC
sample. txt=>
hai
how are u?
new. txt=>
hai
how are u?
13. Whoami
NAME : Displays the current user login and identity.
SYNTAX : whoami
OUTPUT : csea08
14. Man
15. Head
NAME : head
SYNTAX : head filename
DESCRIPTION : Displays the first ten lines in the file.
EXAMPLE : head fruits.txt
OUTPUT : apple
banana
cherry
jack fruit
strawberry
orange
pineapple
mango
grape
papaya
16. Tail
NAME : tail
SYNTAX : tail filename
DESCRIPTION : Displays the last ten lines in the file.
EXAMPLE : tail fruits.txt
OUTPUT : apple
banana
cherry
jack fruit
strawberry
orange
pineapple
mango
grape
papaya
17. Clear
NAME : clear
SYNTAX : clear
DESCRIPTION : Clears the content of the command prompt.
18. Sort
NAME : sort
(i) SYNTAX : sort filename
DESCRIPTION : Sorts the content of the file in ascending order.
EXAMPLE : sort names.txt
OUTPUT :
Arun
Balu
Chandra
David
Edwards
19. Who
NAME : who
SYNTAX : who
DESCRIPTION : Displays all the users currently logged it.
OUTPUT :
csea13 pts/1 Jul 23 10:08 (172.17.22.38)
csea06 pts/10 Jul 23 10:13 (172.17.21.11)
csea12 pts/12 Jul 23 10:13 (172.17.21.35)
csea18 pts/11 Jul 23 10:13 (172.17.20.9)
csea24 pts/8 Jul 23 10:14 (172.17.22.33)
csea05 pts/13 Jul 23 10:15 (172.17.21.36)
root :0 Jul 23 10:20
csea20 pts/16 Jul 23 10:22 (172.17.21.43)
csea11 pts/20 Jul 23 10:28 (172.17.21.27)
csea23 pts/5 Jul 23 10:37 (172.17.22.14)
csea03 pts/0 Jul 23 10:37 (172.17.222.37)
csea01 pts/4 Jul 23 11:00 (172.17.22.13)
csea21 pts/15 Jul 23 11:00 (172.17.21.34)
csea07 pts/18 Jul 23 11:00 (172.17.20.28)
csea08 pts/14 Jul 23 11:01 (172.17.22.16)
csea16 pts/7 Jul 23 11:01 (172.17.21.24)
csea02 pts/19 Jul 23 11:01 (172.17.21.45)
staff pts/26 Jul 23 12:20 (172.17.21.21)
20. Finger
NAME : finger
SYNTAX : finger
21. Last
NAME : last
SYNTAX : last
DESCRIPTION : Displays the list of last logged-in users for a month.
OUTPUT :
22. And
23. Or
NAME : or - ||
SYNTAX : cmd1 || cmd2
DESCRIPTION : Displays the output for one command which is true.
EXAMPLE : whoami || date
OUTPUT : csea23
24. . Alias
NAME : alias
SYNTAX : alias name="value"
DESCRIPTION : To create simple names or abbreviations for commands
EXAMPLE : alias p="pwd"
p
OUTPUT : /home/mca1
25. Edit
NAME : edit
SYNTAX : vi filename
DESCRIPTION : Edits the content of the file. To edit press I and to save press esc:wq
EXAMPLE : vi names.txt
26. cut
NAME : cut
(i) SYNTAX : cut OPTION -m [FILE]...
DESCRIPTION : extracts m characters from the beginning of each line from the specified file.
EXAMPLE : cut –c -3 test.txt
OUTPUT :
BEFORE EXECUTION : cat > test.txt
Smvec
manakula
vinayagar
AFTER EXECUTION : smv
man
vin
27. Touch
NAME : touch
SYNTAX : touch filename
DESCRIPTION : Creates an empty file.
EXAMPLE : touch hello.txt
OUTPUT :
BEFORE EXECUTION : ls
Sample.txt welcome.txt
AFTER EXECUTION : ls
28. Uniq
NAME : uniq
SYNTAX : uniq option filename1 filename2
DESCRIPTION : Discard all but one of successive identical lines from filename1 to filename2
EXAMPLE : uniq –d a.txt b.txt
OUTPUT :
BEFORE EXECUTION : cat > a.txt
smvec
smvec
manakula
vinayagar
AFTER EXECUTION : cat b.txt
smvec
29. Semicolon
30. Echo
2 lines
12 characters
4 words
32. Grep
NAME : grep
(i) SYNTAX : grep pattern filename
DESCRIPTION : To search for a regular expression or a pattern in a file
EXAMPLE : grep apple b.txt
OUTPUT : b.txt=>
This is an apple
This is a fruit
apple
33. Read
NAME : read – reads a value(s)
SYNTAX : read identifier
DESCRIPTION : Reads a value(s)
EXAMPLE : read a
OUTPUT 10
read a
34. Fgrep
NAME : fgrep
(i) SYNTAX : fgrep pattern f1 f2
DESCRIPTION : To search for a regular expression or a pattern in two files
EXAMPLE : fgrep hai a.txt d.txt
OUTPUT : a.txt=>
hai
how are u
d.txt=>
this is a file
the type of the file is text
a.txt: hai
d.txt=>
this is a file
the type of the file is text
a.txt : 0
d.txt : 2
d.txt=>
this is a file
the type of the file is text
d.txt=>
this is a file
the type of the file is text
a.txt : hai
35. Pipe
NAME : Pipe - |
SYNTAX : cmd1 | cmd2 | cmd3
DESCRIPTION : Makes the output of one command as input for another command.
EXAMPLE : date | wc -w
OUTPUT 6
36. Tee
NAME : Tee
SYNTAX : cmd1 | tee filename
DESCRIPTION : Used to read the standard input and then write to standard output or
file.
EXAMPLE : date | tee f.txt | wc -w
OUTPUT 6
37. Write
NAME : write
SYNTAX : write login_name
DESCRIPTION : Used to communicate with other logged in users.
EXAMPLE : write csea25
38. Mail
NAME : Mail
(i) SYNTAX : mail login_name
DESCRIPTION : Used to send mail to a user.
EXAMPLE : write csea25
39.Terminal Name
NAME : tty
SYNTAX : tty
DESCRIPTION : Used to display the terminal path name.
OUTPUT : /dev/pts/23
40. Expression
NAME : expr
SYNTAX : `expr expression`
DESCRIPTION : Used to evaluate an expression
EXAMPLE : echo `expr 10 + 10`
OUTPUT 20
41. . free
NAME : free
SYNTAX : free
DESCRIPTION : Displays the total amount of free and used physical and swap memory in the
system
EXAMPLE : free
OUTPUT :
[mca1@redhat ~]$ free
total used free shared buffers cached
Mem: 1016820 647688 369132 0 59916 414316
-/+ buffers/cache: 173456 843364
Swap: 2064380 0 2064380
42. Find
ping
NAME : ping
(i) SYNTAX : ping Ip-address
DESCRIPTION : Ping is used diagnostically to ensure that a host computer the user is trying to
reach is actually operating.
EXAMPLE : ping 172.17.10.111
OUTPUT :
[mca1@redhat ~]$ ping 172.17.10.111
43. Password
NAME : Password
SYNTAX : passwd
DESCRIPTION : Used to change the password.
EXAMPLE : passwd
OUTPUT : Changing password for user csea09.
Changing password for csea09
(current) UNIX password:
New password:
44.Exit
NAME : Logout
SYNTAX : logout
DESCRIPTION : Used to logout.
45.Uname
NAME : Uname
SYNTAX : uname
DESCRIPTION : Used to display the name of the system being used.
OUTPUT : Linux
46. Sequence
NAME : seq
SYNTAX : seq starting_value ending_value
DESCRIPTION : print a sequence of numbers
EXAMPLE : seq 1 4
OUTPUT :
1
2
3
4
DESCRIPTION : Used to open a file and position the cursor at the first line.
EXAMPLE : vi +b.txt
OUTPUT :
hi bala
:wq
"new.txt" 7L, 42C written
48. Compare
NAME : Compare
SYNTAX : comm filename1 filename2
DESCRIPTION : Compares the sorted files line by line.
EXAMPLE : comm a.txt b.txt
OUTPUT : OS is a program that acts as an interface.
49. Type
NAME : Type
SYNTAX : type filename
DESCRIPTION : To view the content of the file.
EXAMPLE : type names.txt
OUTPUT :
Arun
Balu
Chandra
David
Edwards
RESULT:
Thus all the Linux commands are executed.
AIM:
To write various shell programming.
PROGRAM:
clear
echo "enter the value of a & b"
read a
read b
if [$a -gt $b]
then
echo "A is Greater"
else
echo "B is Greater "
fi
OUTPUT:
B is big
AIM:
To find whether the given year is a leap year or not.
ALGORITHM:
1. Start the program.
2. Enter the value for year.
3. Divide year by 4.Check the remainder.If the remainder is equal to zero goto step 4 else goto step 5.
4. Print the given year is leap year goto step 6.
5. Print the given year is not a leap year goto step 6.
6. Stop the program execution.
PROGRAM:
clear
echo "enter year"
read year
if [ ` expr $year % 4` -eq 0 ]
then
echo "$year is leap year"
else
echo "$year is not leap year"
fi
OUTPUT:
Enter year
2004
2004 is leap year
Enter year
2005
2005 is not leap year
AIM:
To find whether the given number is odd or even .
ALGORITHM:
1. Start the program.
2. Get the value for variable n.
3. Initialize the variable i=0.
4. Check whether i is lessthan n ,if so divide the value of i by 2.If the remainder is equal to zero then
goto step 5 else goto step 6.
5. Print the given number is even number goto step 7.
6. Print the given number is odd number goto step 7.
7. Increment the value of i by 1, goto step 4.
8. Stop the program execution.
PROGRAM:
clear
echo "enter the number"
read n
i=0
while [ $i -lt $n ]
do
if [ ` expr $i % 2` -eq 0 ]
then
echo " $i is a even number"
else
echo " $i is a odd number"
fi
i=` expr $i + 1`
done
OUTPUT:
SUM OF N NUMBERS
AIM:
To find the sum of n natural numbers of a given number.
ALGORITHM:
1. Start the program.
2. Get the value for num.
3. Initialize the variable i=1 and sum=0.
4. Check whether i is less than n, if so sum the number one by one by doing the following
steps.otherwise goto step 6.
sum=$(( $sum + $i ))
i=$(( $i + 1 ))Goto step 4.
5. Print the value of sum of digit.
6. Stop the program execution.
PROGRAM:
echo -n "Enter number : "
read n
OUTPUT:
Enter number : 4
Sum of all digit is 10
FACTORIAL NUMBER
AIM:
To write a program to calculate the factorial of a given number.
ALGORITHM:
1. Get a number
2. Use for loop or while loop to compute the factorial by using the below formula
3. fact(n) = n * n-1 * n-2 * .. 1
4. Display the result.
PROGRAM:
echo "Enter a number"
read num
fact=1
echo $fact
OUTPUT:
Enter a number
5
120
RESULT:
Thus the various shell programs has been entered and verified
DATE:
AIM:
To write various system calls.
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("\n HELLO \n");
if(pid==0)
{
printf("\n CHILD PROCESS IS EXECUTED \n");
printf("\n THE ID NUMBER OF THE CHILD PROCESS IS %d \n",getpid());
}
else if(pid>0)
{
printf("\n PARENT PROCESS IS EXECUTED");
printf("\n THE ID NUMBER OF PARENT PROCESS IS %d\n",getppid());
}
else
printf("\n FORK PROCESS IS NOT EXECUTED");
}
OUTPUT:
[iiiit01@localhost cpro]$ cc pro2.c
[iiiit01@localhost cpro]$ ./a.out
HELLO
HELLO
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("\n HELLO \n");
if(pid==0)
{
wait(20);
printf("\n CHILD PROCESS IS EXECUTED \n");
printf("\n THE ID NUMBER OF THE CHILD PROCESS IS %d \n",getpid());
}
else if(pid>0)
{
wait(50);
printf("\n PARENT PROCESS IS EXECUTED");
printf("\n THE ID NUMBER OF PARENT PROCESS is %d",getpid());
}
}
OUTPUT:
HELLO
HELLO
ALGORITHM:
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("\n HELLO \n");
if(pid==0)
{
printf("\n CHILD PROCESS IS CREATED \n");
printf("\n THE ID NUMBER OF THE CHILD PROCESS IS %d \n",getpid());
sleep(30);
}
else if(pid>0)
{
sleep(20);
printf("\n PARENT PROCESS IS RUNNING");
printf("\n THE ID NUMBER OF PARENT PROCESS IS %d\n",getppid());
}
}
OUTPUT:
HELLO
HELLO
ALGORITHM:
1. Start the program.
2. Get the name of the directory through the command line argument.
3. Open directory by executing dirname=opendir(argv[1]).
4. Read the content of the directoryand assigned to the variable preaddr.
5. Check preaddr=null,if so close the directory goto step 6 else print the content of the directory goto
step 4.
6. Stop the execution.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
main(int argc,char *argv[])
{
DIR *dirname;
struct dirent *preaddr;
dirname=opendir(argv[1]);
while(1)
{
preaddr=readdir(dirname);
if(preaddr==NULL)
{
closedir(dirname);
exit(0);
}
printf("\n\nFOUND ENTRY %s:%s:",argv[1],preaddr->d_name);
}
}
OUTPUT:
ALGORITHM:
1. Start the program.
2. Get the name of the file through the command line argument.
3. Open the file by executing fd=open(argr[1],0),if fd is equal to -1 then print error occur
else create a new file by using the statement cr=creat(argr[2],9999).
4. If cr=-1 then print file is not created else a file is successfully created .
5. Read the content of the file by using the statement rd=read(fd,s,size) and write the read
content to another file using wd=write(cr,s,size).
6. Stop the execution.
PROGRAM:
#include<stdio.h>
#define size 10
main(int argc,char *argr[])
{
int i,n,rd,wd,cr,fd;
char s[size];
if(argc<3)
{
printf("illegal input");
exit(1);
}
fd=open(argr[1],0);
if(fd==-1)
{
printf("error occured");
exit(1);
}
cr=creat(argr[2],9999);
if(cr==-1)
{
printf("file not created");
exit(1);
}
rd=read(fd,s,size);
while(rd>0)
{
wd=write(cr,s,size);
rd=read(fd,s,size);
}
close(fd);
close(cr);
printf("file completed");
}
OUTPUT:
RESULT:
Thus the various system calls has been entered and verified
DATE:
AIM:
To write a c program to simulate the Linux command grep.
ALGORITHM:
PROGRAM :
#include<stdio.h>
main()
{
FILE *f;
char str[10],strf[10],c[10];
int flag=0;
printf("\nENTER THE PATTERN:");
scanf("%s",str);
f=fopen("cse.txt","r");
while(!feof(f))
{
fscanf(f,"%s",strf);
if(strcmp(str,strf)==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("\nTHE PATTERN IS FOUND\n");
else
printf("\nTHE PATTERN IS NOT FOUND\n");
return 0;
}
OUTPUT:
RESULT:
Thus a C program to simulate grep command of Unix is written and executed successfully.
SIMULATION OF ls COMMAND
AIM:
Algorithm:
1. Start the program.
2. Get the name of the directory.
3. Open the directory
4. Read the content of the directory
5. Display the content of the directory.
6. Stop the execution.
PROGRAM:
#include<dirent.h>
#include<sys/stat.h>
main()
{
DIR *dp;
struct dirent *dir;
char d[10];
printf("enter the directory name");
scanf("%s" ,d);
dp=opendir(d);
if(dp!=NULL)
{
while((dir=readdir(dp))!=NULL)
printf(" %s \n ",dir->d_name);
}
else
printf("\n no such directory found");
closedir(dp);
exit(0);
}
OUTPUT:
Enter the directory name:s05cse15
F1
F2
Vc++
RESULT:
DATE:
AIM:
To write a c program to implement FCFS scheduling.
ALGORITHM:
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu;
}p[10];
main()
{
int pro,i;
float awt=0,atat=0;
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&pro);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<pro;i++)
{
scanf("%d",&p[i].bst);
}
printf("\nENTER ARRAIVAL TIME FOR EACH PROCESS\n");
for(i=0;i<pro;i++)
{
scanf("%d",&p[i].ar);
}
p[i].exu=0;
for(i=0;i<pro;i++)
{
p[i+1].exu=(p[i].exu+p[i].bst);
}
for(i=0;i<pro;i++)
{
p[i].wt=p[i].exu-p[i].ar;
p[i].tat=p[i].wt+p[i].bst;
}
for(i=0;i<pro;i++)
{
awt=awt+p[i].wt;
atat=atat+p[i].tat;
}
printf("\nPROCESS BRUSTTIME ARRIVALTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<pro;i++)
{
printf("\np %d %d %d %d
%d ",i+1,p[i].bst,p[i].ar,p[i].wt,p[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/pro);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/pro);
return 0;
}
OUTPUT:
RESULT:
The the C program to implement FCFS scheduling has been executed successfully and the output has
been verified.
AIM:
To write c programs to implement non preemptive sjf scheduling with zero arrival time and non zero
arrival time.
ALGORITHM:
ZERO ARRIVALTIME
1: start the program.
2: Declare a structure with burst time ,execution time,waiting time, and turnaround time ,processes
no,variables and create objects for the structure
3: In main function get the number of processes and burst timeArrival time for each process.
4: Sort the burst time and processes number according to burst time .
5: calculate the execution time by using the following loop.
Initially first process execution is zero.
Next processes execution =previous processes execution
time + previous process burst time.
6 : waiting time and turn around time can be calculated by
WAITING TIME=EXECUTIONTIME.
TURNARROUND TIME=WAITING TIME+BURST TIME.
7: Average waiting time and average turn around can be Calculated by
Average waiting time=sum of waiting time /No of process.
Average Turnaround time= sum of Turnaround time/ No of processes.
8: Print processes Burst time Waiting time Turnaround time for each processes and Average waiting
and Turnaround time.
9 : Terminate the program.
ZERO ARRIVALTIME
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,o,k;
float awt=0,atat=0;
printf(" ************ARRIVAL TIME IS ZERO**************");
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].bst);
for(i=0;i<=n;i++)
{ a[i].pro=i+1;
a[i].bst=p[i].bst;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].bst>a[j].bst)
{t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
k=a[i].pro;
a[i].pro=a[j].pro;
a[j].pro=k;
}}}
a[0].exu=0;
for(i=0;i<n;i++)
a[i+1].exu=a[i].exu+a[i].bst;
for(i=0;i<n;i++)
{
a[i].wt=a[i].exu;
a[i].tat=a[i].wt+a[i].bst;
}
for(i=0;i<n;i++)
{
awt=awt+a[i].wt;
atat=atat+a[i].tat;
}
printf("\nPROCESS BRUSTTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<n;i++)
{
printf("\np %d %d %d %d ",a[i].pro,a[i].bst,a[i].wt,a[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/n);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/n);
return 0;
}
OUTPUT:
************ARRIVAL TIME IS ZERO**************
ENTER NUMBER OF PROCESS
4
ALGORITHM:
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,k,z;
float awt=0,atat=0;
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].bst);
printf("\nENTER ARRAIVAL TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i].ar);
}
for(i=0;i<n;i++)
{
for(i=0;i<n;i++)
{
awt=awt+a[i].wt;
atat=atat+a[i].tat;
}
printf("\nPROCESS BRUSTTIME ARRIVALTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<n;i++)
{
printf("\np %d %d %d %d %d ",a[i].pro,a[i].bst,a[i].ar,a[i].wt,a[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/n);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/n);
return 0;
}
OUTPUT:
ENTER NUMBER OF PROCESS:
4
RESULT:
The C programs to implement non preemptive SJF scheduling with zero arrival time and non zero
arrival time has been executed successfully and the output has been verified.
AIM:
To write C programs to implement non preemptive priority scheduling with zero arrival time and
non zero arrival time.
ALGORITHM:
ZERO ARRIVALTIME
1: start the program.
2: Declare a structure with burst time, execution time, waiting time, and turnaround time ,processes no,
variables and create objects for the structure
3: In main function get the number of processes and burst time Arrival time for each process.
4: Sort the burst time and process numbers according to priority.
5: calculate the execution time by using the following loop.
Initially first process execution is zero.
Next processes execution =previous processes execution
time + previous process burst time.
6 : waiting time and turn around time can be calculated by
WAITING TIME=EXECUTIONTIME.
TURNARROUND TIME=WAITING TIME+BURST TIME.
7: Average waiting time and average turn around can be Calculated by
Average waiting time=sum of waiting time /No of process.
Average Turnaround time= sum of Turnaround time/ No of processes.
8: Print processes Burst time Waiting time Turnaround time for each processes and Average waiting
and Turnaround time.
9: Terminate the program.
ALGORITHM:
ZERO ARRIVALTIME
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro,pr;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,k,z;
float awt=0,atat=0;
printf(" ************ARRIVAL TIME IS ZERO**************");
printf("\n*******LOW NUMBERS HAVE HIGH PRIORITY********");
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].bst);
printf("\nENTER PRIORITY FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pr);
for(i=0;i<n;i++)
{
a[i].pro=i+1;
a[i].bst=p[i].bst;
a[i].pr=p[i].pr;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].pr>a[j].pr)
{
z=a[i].pr;
a[i].pr=a[j].pr;
a[j].pr=z;
t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
k=a[i].pro;
a[i].pro=a[j].pro;
a[j].pro=k;
} } }
a[0].exu=0;
for(i=0;i<n;i++)
a[i+1].exu=a[i].exu+a[i].bst;
for(i=0;i<n;i++)
{
OUTPUT:
**************ARRIVAL TIME IS ZERO*****************
*******LOW NUMBERS HAVE HIGH PRIORITY********
ENTER NUMBER OF PROCESS
5
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro,pr;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,k,z,x;
float awt=0,atat=0;
z= a[i].ar ;
a[i].ar=a[j].ar;
a[j].ar=z;
} }}
a[0].exu=0;
for(i=1;i<n;i++)
a[i].exu=a[i-1].exu+a[i-1].bst;
for(i=0;i<n;i++)
{
a[i].wt=a[i].exu-a[i].ar;
a[i].tat=a[i].wt+a[i].bst;
}
for(i=0;i<n;i++)
{
awt=awt+a[i].wt;
atat=atat+a[i].tat;
}
printf("\nPROCESS BRUSTTIME ARRIVALTIME PRIORITY
WAITINGTIME TURNARROUNDTIME");
for(i=0;i<n;i++)
{
printf("\np %d %d %d %d %d %d",
a[i].pro,a[i].bst,a[i].ar,a[i].pr,a[i].wt,a[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/n);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/n);
return 0;
}
OUTPUT:
RESULT:
The c programs to implement non preemptive priority scheduling with zero arrival time and non zero
arrival time has been executed successfully and the output has been verified.
IMPLEMENTATION OF RR SCHEDULING
AIM:
To write a c program to implement Round Robin scheduling.
ALGORITHM:
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,exu,n,ar;
}p[10],a[10],b[10];
main()
{
int pn,i,j,ts,t,x,y=0;
float awt=0,atat=0;
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&pn);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<pn;i++)
{
scanf("%d",&p[i].bst);
}
printf("\nENTER ARRAIVAL TIME FOR EACH PROCESS\n");
for(i=0;i<pn;i++)
{
scanf("%d",&p[i].ar);
}
for(i=0;i<pn;i++)
{
a[i].bst=p[i].bst;
b[i].bst=p[i].bst;
}
for(i=0;i<pn;i++)
{
for(j=i+1;j<pn;j++)
{
if(a[i].bst>a[j].bst)
{
t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
} } }
x=a[pn-1].bst/ts;
for(j=0;j<x+5;j++)
{
for(i=0;i<pn;i++)
{
if(p[i].bst>ts)
{
y=y+ts;
p[i].exu=y;
p[i].bst=p[i].bst-ts;
p[i].n=p[i].n+1;
}
else if(p[i].bst>0)
{
y=y+p[i].bst;
p[i].exu=y;
p[i].bst=0;
p[i].n=p[i].n+1;
}}}
for(i=0;i<pn;i++)
{
p[i].wt=p[i].exu-b[i].bst-p[i].ar;
p[i].tat=p[i].wt+b[i].bst;
}
for(i=0;i<pn;i++)
{
awt=awt+p[i].wt;
atat=atat+p[i].tat;
}
printf("\nPROCESS BRUSTTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<pn;i++)
{
printf("\np %d %d %d %d ",i,b[i].bst,p[i].wt,p[i].tat);
}
printf("\nAVERAGE WAIRING TIME= %f",awt/pn);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/pn);
return 0;
}
OUTPUT:
RESULT:
The C program to implement RR scheduling has been executed successfully and the output has been
verified.
EX.NO.6 SEMAPHORE
DATE:
AIM:
To write a c program to implement the following using semaphore
1. Producer and consumer problem
2. Dining philosopher's problem
3. Readers writer problem using semaphore
PROGRAM
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
static int full,empty,mutex;
int buffer[5],in=0,out=0;
void wait(int *a);
void signal(int *b);
void producer()
{
int nextp;
printf("producer\n");
wait(&empty);
wait(&mutex);
nextp=rand()%10+1;
buffer[in]=nextp;
printf("produced item is %d\n",nextp);
in=(in+1)%5;
signal(&mutex);
signal(&full);
printf("full=%d\t empty=%d\n",full,empty);
}
void consumer()
{
int nextc;
printf("consumer\n");
wait(&full);
wait(&mutex);
nextc=buffer[out];
printf("consumerd item is %d\n",nextc);
out=(out+1)%5;
signal(&mutex);
signal(&empty);
printf("full=%d\t empty=%d\n",full,empty);
}
main()
{
int c;
mutex=1;
empty=5;
full=0;
clrscr();
while(1)
{
printf("1.producer\t 2.consumer\t 3.both\t 4.Exit\n");
printf("choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
if(empty==0)
printf("producer has to wait\n");
else
{
producer();
}
break;
case 2:
if(full==0)
printf("consumer has to wait");
else
{
consumer();
} break;
case 3:
if(!empty)
{
printf("producer has to wait\n");
consumer();
}
else if(!full)
{
printf("consumer has to wait\n");
producer();
}
else
{
consumer();
producer();
}
break;
case 4:
exit(0);
break;
}
}
getch();
return 0;
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
char state[10],self[10],spoon[10];
void test(int k)
{
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e'))
{
state[k]='e';
self[k]='s';
spoon[k]='n';
spoon[(k+4)%5]='n';
}
}
void pickup(int i)
{
state[i]='h';
test(i);
if(state[i]!='e')
self[i]='w';
}
void putdown(int i)
{
state[i]='t';
spoon[i]='s';
spoon[(i+4)%5]='s';
test((i+4)%5);
test((i+1)%5);
}
main()
{
int ch,a,n,i;
clrscr();
printf("\n Dining Philosopher's Problem:");
printf("\n. ......................................... \n");
for(i=0;i<5;i++)
{
state[i]='t';
self[i]='s';
spoon[i]='s';
}
printf("\n Initial state of each philososphers:");
printf("\n Phil No : \t Think/Eat \t Status \t Spoon");
for(i=0;i<5;i++)
printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]);
printf("\n Enter the option : \n1.Exit \n 2.Hungry \n 3.Thinking \n");
printf("\n Enter your choice :");
scanf("%d",&ch);
while(ch!=1)
{
switch(ch)
{
case 1:
return 0;
case 2:
printf("\n Enter which philosophers is Hungry :");
scanf("%d",&n);
pickup(n-1);
break;
case 3:
printf("\n Enter which Philosopher is Thinking :");
scanf("%d",&n);
putdown(n-1);
break;
}
printf("\n State of each Philosepher :");
printf("\n Philno: \t Think/Eat \t Status \t Spoon");
for(i=0;i<5;i++)
printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]);
printf("\n Enter the option : \n1.Exit \n 2.Hungry \n 3.Thinking \t :");
scanf("%d",&ch);
}
}
INPUT/OUTPUT
Algorithm
1. Start the program.
2. Initialize the following variables
mutex=1;
wrt=1;
readcount=0;
3. Display the menu and get the choice.
4. If choice is 1, the reader process is executed. Go to step 9.
5. If choice is 2, the writer process is executed. Go to step 10.
6. If choice is 3,then first reader ,second writer processes are executed i.e go to step 9 and then
10.Finally go to step 3.
7. If choice is 4 ,then first writer ,second reader processes are executed. .i.e go to step 10 and then
9.Finally go to step 3.
8. If choice is 5,go to step 11.
9. Execute mutex variable ,increment read count by 1.Check whether read count equals 1,then execute
wait(&wrt). If read count is equal to 0 then execute signal(&wrt);Go to step 3.
10. Execute wait(&wrt),get the string to write, then the writer writes. Perform signal(&wrt).Go to step
3.
11. Stop the program.
PROGRAM:
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
static int mutex;
int wrt,readcount,ch;
char buffer[50]="hello";
void wait(int*);
void signal(int*);
void print();
void reader()
{
wait(&mutex);
readcount++;
if(readcount==1)
wait(&wrt);
signal(&mutex);
cout<<"\nREADER READS:"<<buffer;
wait(&mutex);
readcount--;
if(readcount==0)
signal(&wrt);
signal(&mutex);
}
void writer()
{
wait(&wrt);
cout<<"\nEnter the string to write:";
gets(buffer);
cout<<"\nWriter writes:"<<buffer;
signal(&wrt);
}
main()
{
mutex=1;
wrt=1;
readcount=0;
do
{
cout<<"\n\nREADER-WRITER PROBLEM\n\n1.READER\n2.WRITER\n3.READAND
WRITE\n4.WRITE AND READS\n5.EXIT\n\nENTER THE CHOCIE:";
cin>>ch;
switch(ch)
{
case 1:
reader();
break;
case 2:
writer();
break;
case 3:
reader();
writer();
break;
case 4:
writer();
reader();
break;
case 5:
exit(0);
}
}
while(ch<6);
getch();
}
OUTPUT
READER-WRITER PROBLEM
1.READER
2.WRITER
3. READAND WRITE
4. WRITE AND READS
5.EXIT
READER READS:hello
READER-WRITER PROBLEM
1.READER
2.WRITER
3. READAND WRITE
4. WRITE AND READS
5.EXIT
REUSLT:
Thus the C program to implement semaphore has been entered and verified
AIM: To write a C program to implement bankers algorithm for dead lock avoidance
ALGORITHM:
SOURCE CODE:
#include
#include
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++) for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p); printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0; run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;i<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++) for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
Else
{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}
OUTPUT:
Enter the no of resources: 4
Enter the no of resource classes: 3
Enter the total existed resources in each class: 3 2 2
Enter the allocated resources: 1 0 0 5 1 1 2 1 1 0 0 2
Enter the process making the new request: 2
Enter the requested resource: 1 1 2
Enter the processes which are n blocked or running: Process 1: 1 2
Process 3: 1 0
Process 4: 1 0
Deadlock will occur
REUSLT:
Thus the C program to implement algorithm for deadlock prevention has been entered and verified.
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
struct fc
{
int mem,po,pm;
}p[10];
main()
{
int n,i,j,x,t[10];
printf("\nENTER NUMBER OF MEMORY PARTISION:\n");
scanf("%d",&x);
printf("\nENTER THE SIZE OF MEMORY PARTISION:\n");
for(i=0;i<x;i++)
scanf("%d",&p[i].mem);
printf("\nENTER NUMBER OF PROCESS:\n");
scanf("%d",&n);
printf("\nENTER THE MEMORY SIZE FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pm);
for(i=0;i<x;i++)
t[i]=p[i].mem;
for(i=0;i<n;i++)
{
for(j=0;j<x;j++)
{
if(p[i].pm<=p[j].mem)
{
p[j].mem=i+1;
break;
}
}
}
printf("PROCESS MEMORY MEMORYPARTISION MEMORY ALLOCATION");
for(i=0;i<x;i++)
printf("\n %d %d %d ",p[i].pm,t[i],p[i].mem);
return 0;
}
OUTPUT:
RESULT:
The C program to implement first fit algorithm has been executed successfully and the output has
been verified.
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
struct fc
{
int mem,po,pm;
}p[10],a[10];
main()
{
int n,i,j,x,t;
printf("\nENTER NUMBER OF MEMORY PARTISION:\n");
scanf("%d",&x);
printf("\nENTER SIZE OF MEMORY PARTISION:\n");
for(i=0;i<x;i++)
scanf("%d",&p[i].mem);
printf("\nENTER NUMBER OF PROCESS:\n");
scanf("%d",&n);
printf("\nENTER THE MEMORY SIZE FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pm);
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(p[i].mem>p[j].mem)
{
t=p[i].mem;
p[i].mem=p[j].mem;
p[j].mem=t;
}}}
for(i=0;i<x;i++)
OUTPUT:
RESULT:
The C program to implement best fit algorithm has been executed successfully and the
output has been verified.
AIM:
To write a C program to implement worst fit algorithm.
ALGORITHM:
PROGRAM:
#include<stdio.h>
struct fc
{
int mem,po,pm;
}p[10],a[10];
main()
{
int n,i,j,x,t;
printf("\nENTER NUMBER OF MEMORY PARTISION\n");
scanf("%d",&x);
printf("\nENTER SIZE OF MEMORY PARTISION\n");
for(i=0;i<x;i++)
scanf("%d",&p[i].mem);
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER THE MEMORY SIZE FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pm);
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(p[i].mem<p[j].mem)
{
t=p[i].mem;
p[i].mem=p[j].mem;
p[j].mem=t;
} }}
for(i=0;i<x;i++)
a[i].mem=p[i].mem;
for(i=0;i<n;i++)
{
for(j=0;j<x;j++)
{
if(p[i].pm<=p[j].mem)
{
p[j].mem=i+1;
break;
} }}
printf("\n PROCESS MEMORY MEMORY PARTISION
MEMORY ALLOCATION");
for(i=0;i<x;i++)
printf("\n %d %d %d ",p[i].pm,a[i].mem,p[i].mem);
return 0;
}
OUTPUT:
RESULT:
The C program to implement worst fit algorithm has been executed successfully and the output has been
verified.
DATE:
AIM :
To write a C program to implement the following page replacement algorithms
1)FIFO
2)LRU
3)OPTIMAL
ALGORITHM:
PROGRAM
#include<stdio.h>
#include<conio.h>
int fr[100],n;
void main()
{
void display();
int i,j, page[100],p,pages;
int flag1=0,flag2=0,pf=0,top=0;
clrscr();
printf("\n Enter total number of frames :\n");
scanf("%d",&n);
printf("\n Enter the toal number of page reference string :\n");
scanf("%d",&pages);
for(i=0;i<n;i++)
{
fr[i]=-1;
}
printf("\nEnter the page reference string :");
for(i=0;i<pages;i++)
{
scanf("%d",&p);
page[i]=p;
}
for(j=0;j<pages;j++)
{
flag1=0;
flag2=0;
for(i=0;i<pages;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
// j=0;
if(flag1==0)
{
for(i=0;i<n;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
pf++;
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=n)
top=0;
}
display();
}
printf("\n Number of page faults : %d ",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<n;i++)
printf("%d\t",fr[i]);
}
OUTPUT
1 -1 -1 -1
1 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
6 2 3 4
6 5 3 4
6 5 1 4
6 5 1 2
3 5 1 2
ALGORITHM:
1: Read the number of pages in the reference string
2: Get the reference string
3: Get the number of frames
4: Get the page reference
5: Perform LRU operation and display
6: Display efficient algorithm
7: End processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fr[100],n;
void main()
{
void display();
int page[100],i,j,fs[3];
int index,k,l,p,pages,flag1=0,flag2=0,pf=0;
clrscr();
printf("\n Enter total number of frames :\n");
scanf("%d",&n);
printf("\n Enter the toal number of page reference string :\n");
scanf("%d",&pages);
for(i=0;i<n;i++)
{
fr[i]=-1;
}
printf("\nEnter the page reference string :");
for(i=0;i<pages;i++)
{
scanf("%d",&p);
page[i]=p;
}
for(j=0;j<pages;j++)
{
flag1=0,flag2=0;
for(i=0;i<n;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<n;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
pf++;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<n;i++)
fs[i]=0;
for(k=j-1,l=1;l<=n-1;l++,k--)
{
for(i=0;i<n;i++)
{
if(fr[i]==page[k])
fs[i]=1;
}
}
for(i=0;i<n;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=page[j];
pf++;
}
display();
}
printf("\n no of page faults :%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<n;i++)
printf("\t%d",fr[i]);
}
Enter total number of frames :
4
1 -1 -1 -1
1 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 6 4
1 2 6 5
1 2 6 5
1 2 6 5
1 2 3 5
no of page faults :7
ALGORITHM:
1: Read functions optimal outside main class
2: Inside main function
3: Read number of pages in reference string
4: Read number of frames
5: Read number of page faults if all the condition satisfies the following statements are displayed
6: replacement stratergies are efficient
7: optimal page replacement stratergy is efficient
8: End processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fr[100],n;
void main()
{
void display();
int page[100],pages,p,i,j,fs[3];
int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0;
clrscr();
printf("\n Enter total number of frames :\n");
scanf("%d",&n);
printf("\n Enter the toal number of page reference string :\n");
scanf("%d",&pages);
for(i=0;i<n;i++)
{
fr[i]=-1;
}
printf("\nEnter the page reference string :");
for(i=0;i<pages;i++)
{
scanf("%d",&p);
page[i]=p;
}
for(j=0;j<pages;j++)
{
flag1=0;
flag2=0;
for(i=0;i<n;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<n;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
pf++;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<n;i++)
{ lg[i]=0;
}
for(i=0;i<n;i++)
{
for(k=j+1;k<pages;k++)
{
if(fr[i]==page[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<n;i++)
{
if(lg[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<n;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=page[j];
pf++;
}
display();
}
printf("\n no of page faults:%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<n;i++)
printf("\t%d",fr[i]);
}
Enter total number of frames :
4
1 -1 -1 -1
1 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 6
1 2 3 5
1 2 3 5
1 2 3 5
1 2 3 5
no of page faults:6
RESULT:
Thus the C programs for page replacement algorithms are entered and verified.
DATE:
AIM:
To implement various disk scheduling algorithm
1. FCFS
2. SSTF
3. SCAN
4. C-SCAN
FCFS
ALGORITHM:
PROGRAM:
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
//#include<process.h>
int h,a[20],b=0,i,j,n;
main( )
{
// clrscr( );
printf("FIRST COME FIRST SERVE DISK SCHEDULING\n");
printf("\nEnter the Number of Requests : ");
scanf("%d",&n);
printf("\nEnter the requests : \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nEnter the Starting Head Position : ");
scanf("%d",&h);
printf("\nThe Order of Visiting the Cylinders:\n");
for(i=1;i<=n;i++)
printf("\t%d",a[i]);
for(i=1;i<=n;i++)
{
b+=abs(h-a[i]);
h=a[i];
}
printf("\nThe Seek Time is %d\n",b);
// getch( );
}
[staff@localhost diskpgm]$ cc -o p fcfs.c
[staff@localhost diskpgm]$ ./p
Output:
SSTF
ALGORITHM:
1: read the number of cylinders
2: Enter the values of cylinders
3: read the starting head position
4: enter the starting head position
5: enter the order of visiting the cylinder
6: seek=seek+abs(hr-d[i]);
hr=d[i];
7: then calculate the seek time
8: then declare the sstf function
9: h=a[c];
d[z]=a[c];
z++;
a[c]=9999;
10: return(0)
11: end processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int sstf();
int seek=0,t1,t2,n,a[20]={{0}},z=1,d[20]={{0}},h=0;
void main()
{
int i,j,hr;
clrscr();
printf("\t\t SHORTEST SEEK TIME FIRST SCHEDULING \n\n");
printf("Enter the Number of requests : ");
scanf("%d",&n);
printf("\nEnter the requests : \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nEnter the Starting Head Position : ");
scanf("%d",&h);
hr=h;
printf("\nThe Order of Visiting the Cylinders : \n");
for(i=1;i<=n;i++)
sstf();
printf("\n");
for(i=1;i<=n;i++)
{
seek=seek+abs(hr-d[i]);
hr=d[i];
}
printf("\nThe Seek Time is %d",seek);
getch();
}
int sstf()
{
int b[20]={{0}},c=1,i,j;
for(i=1;i<=n;i++)
b[i]=abs(h-a[i]);
for(i=1;i<=n;i++)
if(b[c]>b[i])
c=i;
printf("\t%d",a[c]);
h=a[c];
d[z]=a[c];
z++;
a[c]=9999;
return(0);
}
OUTPUT:
SCAN
ALGORITHM:
1: read the ending position of the disc
2: Enter the number of cylinders
3: enter the starting head position
4: enter the order of visiting the cylinder
5: right=right+abs(h-a[i]);
h=a[i];
6: right=right+abs(h-end);
h=end;
7: finally calculate the seek time
8: end processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,req[30],head,i,t,j,move,n,ch,g[30],c,h,cy,s[15];
clrscr();
printf("Disk Scheduling - SCAN");
printf("\nTotal number of cylinders: ");
scanf("%d",&cy);
printf("Enter the current head position: ");
scanf("%d",&head);
h=head;
printf("Enter the number of requests:");
scanf("%d",&n);
move=0;
printf("Enter the requests (1 to %d):\n",cy);
req:
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
s[i]=req[i];
if(req[i]>cy||req[i]<1)
{
printf("Enter requests only between 1-%d..",cy);
goto req;
}
}
OUTPUT:
Total number of cylinders: 7
Enter the current head position: 4
Enter the number of requests:4
Enter the requests (1 to 7):
5
1
6
2
4 5 1
5 6 1
6 7 6
7 2 5
2 1 1
Order of servicing:4 5 6 2 1
Total Head Movements: 9
C-SCAN
ALGORITHM:
1: read the ending position of the disc
2: Enter the number of cylinders
3: enter the starting head position
4: enter the order of visiting the cylinder
5: right=right+abs(h-a[i]);
h=a[i];
6: right=right+abs(h-end);
h=end;
7: finally calculate the seek time
8:end processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,req[30],head,i,t,j,move,n,ch,g[30],c,h,cy,s[15];
clrscr();
printf("Disk Scheduling - CSCAN");
printf("\nTotal number of cylinders: ");
scanf("%d",&cy);
printf("Enter the current head position: ");
scanf("%d",&head);
h=head;
printf("Enter the number of requests:");
scanf("%d",&n);
move=0;
printf("Enter the requests (1 to %d):\n",cy);
req:
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
s[i]=req[i];
if(req[i]>cy||req[i]<1)
{
printf("Enter requests only between 1-%d..",cy);
goto req;
}
}
OUTPUT:
Total number of cylinders: 7
Enter the current head position: 4
Enter the number of requests:4
Enter the requests (1 to 7):
5
1
6
2
4 5 1
5 6 1
6 7 1
7 0 7
0 1 1
1 2 1
Order of servicing:4 5 6 1 2
Total Head Movements: 12
RESULT:
Thus the various disk scheduling algorithm has been entered and verified.