Os Lab Program Manual
Os Lab Program Manual
1. WHO COMMAND:
DESCRIPTION:
This command is used to get information about all the users who are currently
logged into the system.
SYNTAX:
$who
OUTPUT:
iicsea55 pts/12 2009-02-19 10:10 (192.168.1.66)
iicsea58 pts/8 2009-02-19 10:10 (192.168.1.49)
2. DATE COMMAND:
DESCRIPTION:
This command tells the Linux system to display the current Date Month Year
Time. SYNTAX:
$date
OUTPUT:
Thu Feb 19 10:43:29 IST 2009
3. CALENDAR COMMAND:
DESCRIPTION:
This command tells the Linux system to display the calendar for current
month. SYNTAX:
$cal
OUTPUT:
February 2009
Su Mo Tu We Th Fr Sa
1234567
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
4. CALENDAR OPTION COMMAND:
DESCRIPTION:
This command tells the Linux system to display the calendar for current
year. SYNTAX:
$cal (year)
OUTPUT:
2000
January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo
Tu We Th Fr Sa 1 1 2 3 4 5 1 2 3 4 2 3 4 5 6 7 8 6 7 8 9 10 11 12 5 6 7 8 9 10 11 9
10 11 12 13 14 15 13 14 15 16 17 18 19 12 13 14 15 16 17 18 16 17 18 19 20 21
22 20 21 22 23 24 25 26 19 20 21 22 23 24 25 23 24 25 26 27 28 29 27 28 29 26
27 28 29 30 31 30 31
….
5. ECHO COMMAND:
DESCRIPTION:
This command is used to display the sentence or string given by
us. SYNTAX:
$echo (string)
OUTPUT:
God is Great
6. MAN COMMAND:
DESCRIPTION:
This command is used to get the complete details for given
command. SYNTAX:
$man (command)
OUTPUT:
CAL(1) BSD General Commands Manual CAL(1) NAME
cal - displays a calendar
SYNOPSIS
cal [-smjy13] [[month] year]
DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the cur-
rent month is displayed. The options are as follows:
7. CLEAR COMMAND:
DESCRIPTION:
This command is used to clear the screen.
SYNTAX:
$clear
OUTPUT:
The screen is cleared.
8. WHO AM I COMMAND:
This command gives information about current users
SYTNAX:
$who am I
OUTPUT:
Student pts/1 feb 25 14:28(192.168.0.1555)
9. PWD COMMAND:
DESCRIPTION:
This command is used to show the current working
directory SYTNAX:
$pwd
OUTPUT:
/home/student
10. BC COMMAND:
DESCRIPTION:
This command is used to perform basic calculation
SYTNAX:
$bc
OUTPUT:
25+65
90
DESCRIPTION:
This command is used to give the list of files in a particular
directory. SYTNAX:
$ls (dir name)
OUTPUT:
File1 file2 today
12. COMMAND: TEE
DESCRIPTION:
It is used to read the contents from standard input or from output of another
command and reproduces the output to both in standard output and direct into output to one
or more files. SYNTAX: tee [options] <file name >
OUTPUT :
$ date | tee dat.txt
13.COMMAND: UNAME
DESCRIPTION:
It is used to display the details about the OS in which we are working.- n for node
name SYNTAX:
uname [options]
OUTPUT:
$ uname –n
administrator –Lenovo-$s510
1. MAKEDIR COMMAND
DESCRIPTION:
This command is used to create a new directory
SYTNAX:
$mkdir (dir)
OUTPUT
Required directory is created.
2. RMDIR COMMAND
DESCRIPTION:
This command is used for deleting a directory.
OPTIONS:
-i require a conformation for deletion
-r delete a directory without conformation
SYTNAX:
$rmdir (option) (dir)
OUTPUT:
Given directory is removed.
3. CD COMMAND:
DESCRIPTION:
This command is used to change the directory .
SYTNAX:
$cd (dir)
OUTPUT:
[iicsea19@localhost tariq ~]$
Current working directory is changed.
1. CAT COMMAND:
DESCRIPTION:
This command is used for creating and displaying the content of the
file. SYNTAX:
1.create: $cat>(file name)
2.display: $cat (file name)
OUTPUT:
[student@cse ~]$ cat>India
I love my India.
2. MORE COMMAND:
DESCRIPTION:
This command is used to display the content of the file one screen at a time
SYNTAX:
$more filename
OUTPUT:
$ more Vicky
Operation is performed
3. WC COMMAND:
DESCRIPTION:
This will print number of lines, words, characters in a file.
SYNTAX:
$ wc (file name)
OPTIONS:
l - line
w - word
c –character
OUTPUT:
$ wc –l Vicky
2 vicky
$ wc –w Vicky
8 vicky
$ wc –w Vicky
49 vicky
4. MV COMMAND:
DESCRIPTION:
The command is used to move a file within a directory with different file name SYNTAX:
$ mv (file name1) (file name2)
OUTPUT:
$mv raja tar
The file is renamed.
5. SPELL COMMAND:
DESCRIPTION:
It is used to find spelling errors in a file and display them as standard output in alphabetical
order
SYNTAX:
$spell (file name)
OUTPUT:
$spell raja
cd ppell.
1. COPY COMMAND:
DESCRIPTION:
Used to copy one or more files.
SYNTAX:
$cp (file name 1) (file name 2)
OUTPUT:
$cp raja vicky
The same file is copied with different name
2. CMP COMMAND:
DESCRIPTION:
This command is used to compare two file and generate the first
difference. SYNTAX:
$cmp (file1) (file2)
OUTPUT:
$cmp s sad
It there
3. COMM COMMAND:
DESCRIPTION:
This is used for comparing 2 files line by line & generates the common items.
SYNTAX:
$comm (file name 1) (file name 2)
OUTPUT:
$ comm S3 s5
Vicky Antonio bendaras
FILTER COMMANDS
1. HEAD COMMAND
DESCRIPTION:
This command is used to display the first ten lines of the file without
option. SYNTAX:
$head (filename)
OUTPUT:
First ten lines of the file are displayed.
2. TAIL COMMAND
DESCRIPTION:
This command is used to display the last ten lines of the file without
option. SYNTAX:
$tail (file name)
OUTPUT:
Last ten lines of the file are displayed.
3. PASTE COMMAND
DESCRIPTION:
This command is used to paste one or more files as vertically and displays the result
in standard output.
SYNTAX:
$paste (file1) (file2)
OUTPUT:
Hi I am Tariq Hi My Name is Tariq
FILE PERMISSION COMMANDS
1. CHMOD COMMAND
DESCRIPTION:
This is used to alter the access permission for owner, group, user or others.
SYNTAX:
$chmod (option) filename
OPTION:
4- read
2- write
1- execute
0- no permission
OUTPUT:
[student@localhost~] chmod 4 sam
---r---w---x---sam
COMMUNICATION COMMANDS
1. MAIL COMMAND:
DESCRIPTION:
This command is an interactive message processing system in which you send
and receive electronically.
SYNTAX:
$ mail (user name)
OUTPUT:
Subject: hai !!! how are you …
2. WRITE COMMAND:
DESCRIPTION:
This command is used to communicate with the users who are currently working
the operating system.
SYNTAX:
$ write user name
OUTPUT:
Message from student @ local host local domain on pts 10 at 12:04
3.WALL COMMAND:
DESCRIPTION:
This command used to broadcast by a message by super user to the entire user
currently logged in.
SYNTAX:
$ wall message
OUTPUT:
Broad cast message from student (pts/0) ( thu feb 26 12:06:30:2009)
4.MESSAGE COMMAND:
DESCRIPTION:
This command is used to permit or deny message coming from the other user write
to talk commands.
SYNTAX:
$mesg y/n
OUTPUT:
Message is accepted.
5. COMMAND:SSH
DESCRIPTION:
Program for logging into a remote machine and for executing commands on a
remote machine.
SYNTAX:
ssh [options] [user]@hostname
EXAMPLE:
ssh ¬X [email protected]
6. COMMAND:SCP
DESCRIPTION:
Secure copy (remote file copy program) -copies files between hosts on a
network SYNTAX:
scp [options] [[user]@host1:file1] [[user]@host2:file2]
EXAMPLE:
scp file1.txt [email protected]:~/Desktop/
Command : tar
Purpose : to archive a file
Syntax:tar [OPTION] DEST SOURCE
Example : tar ¬cvf /home/archive.tar /home/original
tar ¬xvf /home/archive.tar
Command : Zip
Purpose : package and compress (archive) files
Syntax:zip [OPTION] DEST SOURSE
Example : zip original.zip original
Command : unzip
Purpose : list, test and extract compressed files in a ZIP
archive Syntax:unzip filename
Example : unzip original.zip
Command : fdisk
Purpose : partition manipulator
Syntax:sudo fdisk name
Example : sudo fdisk ¬l
Command : mount
Purpose : mount a file system
Syntax:mount ¬t type device dir
Example : mount /dev/sda5 /media/target
Command : Umount
Purpose : unmount file systems
Syntax:umount [OPTIONS] dir | device...
Example : umount /media/target
Command : du
Purpose : estimate file space usage
Syntax: df [OPTION]... [FILE]...
Example : df
Command : Quota
Purpose : display disk usage and limits
Syntax:quota [OPTION]
Example : quota –v
NETWORK COMMANDS
1.ping
DESCRIPTION:
It sends packets of information to the user-defined source. If the packets are received,
the destination device sends packets back. Ping can be used for two purposes
DESCRIPTION:
View network configuration, it displays the current network adapter configuration. It
is handy to determine if you are getting transmit (TX) or receive (RX) errors.
syntax : ifconfig
Example : ifconfig
3. netstat
DESCRIPTION:
Most useful and very versatile for finding a connection to and from the host. You
can find out all the multicast groups (network) subscribed by this host by issuing
Syntax:netstat -g
Example : netstat-t,netstat-u
4. telnet
DESCRIPTION:
Connects destination host via the telnet protocol, if telnet connection establishes on
any port means connectivity between two hosts is working fine.
5 Quota
DESCRIPTION:
Display disk usage and limits
Syntax:quota [OPTION]
Example : quota -v
PROCESS COMMANDS
1. PS COMMAND:
DESCRIPTION:
This Command is used to tell the detail about actual process running in the system.
SYNTAX:
$ps (option)
OPTIONS:
1. –f
OUTPUT:
UID PID PPID C STIME TIMECMD
Student 2834 2831 0 10:13 00:00:00 bash
Student 2884 2871 0 10:13 00:00:00 bash
2. SLEEP COMMAND:
DESCRIPTION:
This command is used to suspend the execution of the system for an individual time
SYNTAX:
$sleep (time)
OUTPUT:
$ sleep 5
Operation is performed
3.KILL COMMAND:
DESCRIPTION:
This is used to terminate the process.
SYNTAX:
$kill (processed)
OUTPUT:
$ kill 89
Operation is performed.
4.AT COMMAND:
DESCRIPTION:
It is used to schedule the jobs for later execution at the specified time.
SYNTAX:
$at (time)
OUTPUT:
$ at 10:00
Job 1 at FRI mar 06 10:00:00 2009
read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
fi
if [ $c -gt $l ]
then
l=$c
fi
echo Largest of $a $b $c is $l
Sample Output:
385
Largest of 3 8 5 is 8
Test cases:
Sample Coding:
// Factorial
i=1
sum=0
echo $sum
fact=1
echo $fact
6. File manipulation
if [ $a -eq 0 ]
then
echo "$a is neither postive nor negative"
elif [ $a -gt 0 ]
then
echo "$a number is positive"
else
echo "$a number is negative"
fi
fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC,
0644); if(fd < 0)
{
printf("File creation
problem\n"); exit(-1);
}
printf("Press Ctrl+D at end in a new line:\n");
while((n = read(0, buf, sizeof(buf))) > 0)
{
len = strlen(buf);
write(fd, buf, len);
}
Close(fd);
Read()
fd = open(argv[1],
O_RDONLY); if(fd == -1)
{
Write()
fd = open(argv[1], O_APPEND|O_WRONLY|O_CREAT,0644);
if (fd < 0)
{
perror(argv[1]);
exit(-1);
}
while((n = read(0, buf, sizeof(buf))) > 0)
{
len = strlen(buf);
write(fd, buf, len);
}
close(fd);
}
Sample Coding:
/* C program to illustrate use of fork() & exec() system call */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int ret = 1;
int status;
pid = fork();
execv("ls",argv_list);
exit(0);
}
else // a positive number is returned for the pid of parent process
{
printf("Parent Of parent process, pid = %u\n",getppid());
printf("parent process, pid = %u\n",getpid());
4.1(FCFS)
Execution of the Program:
Sample Coding:
#include<stdio.h>
int main()
{
int AT[10],BT[10],WT[10],TT[10],n;
int burst=0,cmpl_T;
float Avg_WT,Avg_TT,Total=0;
printf("Enter number of the process\n");
scanf("%d",&n);
printf("Enter Arrival time and Burst time of the process\n");
printf("AT\tBT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&AT[i],&BT[i]);
}
// printing of outputs
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
avg_tat=(float)total/n;
printf("\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
4.3(SRTF ALGORITHM)
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("\nEnter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
4.4(PRIORITY ALGORITHM)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
//flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
4.5(ROUND ROBIN)
#include<stdio.h>
int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes: ");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]", i + 1);
printf("Arrival Time:");
scanf("%d", &arrival_time[i]);
printf("Burst Time:");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
5.2(DINING PHILOSOPHER’S PROBLEM
TO DEMONSTRATE PROCESS
SYNCHRONIZATION.)
#include<stdio.h>
#define n 4
struct fork{
int taken;
}ForkAvil[n];
struct philosp{
int left;
int right;
}Philostatus[n];
void goForDinner(int philID){ //same like threads concept here cases implemented
if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
printf("Philosopher %d completed his dinner\n",philID+1);
//if already completed dinner
else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
//if just taken two forks
printf("Philosopher %d completed his dinner\n",philID+1);
if(otherFork== -1)
otherFork=(n-1);
if(philID== -1)
philID=(n-1);
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
}
}
}
else if(Philostatus[philID].left==0){ //nothing taken yet
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM,
THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by philosopher %d\n",philID,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
}
}else{ //except last philosopher case
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
}
}
}else{}
}
int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;
while(compltedPhilo<n){
/* Observe here carefully, while loop will run until all philosophers complete dinner
Actually problem of deadlock occur only thy try to take at same time
This for loop will say that they are trying at same time. And remaining status will print by go
for dinner function
*/
for(i=0;i<n;i++)
goForDinner(i);
printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);
}
return 0;
}
6 TH ( IMPLEMENTATION OF BANKERS
ALGORITHM)
#include<stdio.h>
#include<conio.h>
void main() {
int
k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[
10],j,rz, count=0;
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
ff[i]=j;
highest=temp;
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
BEST FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
if(bf[j]!=1)
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
ff[i]=j;
lowest=temp;
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
FIRST FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp;
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
if(bf[j]!=1)
temp=b[j]-f[i];
if(temp>=0)
ff[i]=j;
break;
frag[i]=temp;
bf[ff[i]]=1;
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
8 TH(IMPLEMENTATION OF PAGE
REPLACEMENT ALGORITHMS)
#include <stdio.h>
int main()
{
int referenceString[10], pageFaults = 0, m, n, s, pages, frames;
printf("\nEnter the number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter reference string values:\n");
for( m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &referenceString[m]);
}
printf("\n What are the total number of frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(referenceString[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = referenceString[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = referenceString[m]; }
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
#include<stdio.h>
int main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20]; printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++) printf("\t%d",q[j]); printf("\n");
}
else
{
for(r=0;r<f;r++) {
c2[r]=0; for(j=i-1;j<n;j--)
{
if(q[r]!=p[j]) c2[r]++; else
break; }
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++) {
if(b[r]<b[j]) {
t=b[r]; b[r]=b[j]; b[j]=t; }
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0]) q[r]=p[i];
printf("\t%d",q[r]); }
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
8.3 OPTIMAL PAGE REPLACEMENT
#include<stdio.h>
int main()
{
int fr[5],i,j,k,t[5],p=1,flag=0,page[25],psz,nf,t1,u[5];
printf("enter the number of frames:");
scanf("%d",&nf);
printf("\n enter the page size");
scanf("%d",&psz);
printf("\nenter the page sequence:");
for(i=1; i<=psz; i++)
scanf("%d",&page[i]);
for(i=1; i<=nf; i++)
fr[i]=-1;
for(i=1; i<=psz; i++)
{
if (full(fr,nf)==1)
break;
else
{
flag=0;
for(j=1; j<=nf; j++)
{
if(page[i]==fr[j])
{
flag=1;
printf(" \t%d:\t",page[i]); break;
}
}
if(flag==0)
{
fr[p]=page[i];
printf(" \t%d:\t",page[i]); p++;
}
for(j=1; j<=nf; j++)
printf(" %d ",fr[j]);
printf("\n");
}
}
p=0;
for(; i<=psz; i++)
{
flag=0;
for(j=1; j<=nf; j++)
{
if(page[i]==fr[j])
{
flag=1;
break;
}
}
if(flag==0)
{
p++;
for(j=1; j<=nf; j++)
{
for(k=i+1; k<=psz; k++) {
if(fr[j]==page[k])
{
u[j]=k;
break;
}
else
u[j]=21;
}
}
for(j=1; j<=nf; j++)
t[j]=u[j];
for(j=1; j<=nf; j++)
{
for(k=j+1; k<=nf; k++) {
if(t[j]<t[k])
{
t1=t[j];
t[j]=t[k];
t[k]=t1;
}
}
}
for(j=1; j<=nf; j++)
{
if(t[1]==u[j])
{
fr[j]=page[i];
u[j]=i;
}
}
printf("page fault\t");
}
else
printf(" \t");
printf("%d:\t",page[i]);
for(j=1; j<=nf; j++)
printf(" %d ",fr[j]);
printf("\n");
}
printf("\ntotal page faults: %d",p+3); // getch();
}
int full(int a[],int n)
{
int k;
for(k=1; k<=n; k++)
{
if(a[k]==-1)
return 0;
}
return 1;
}
9TH(IMPLEMENTATION OF DISK
SCHEDULING ALGORITHMS)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for FCFS disk scheduling
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Total head moment is %d",TotalHeadMoment);
return 0;
}
9.2 SSTF (SHORTEST SEEK TIME FIRST)
ALGORITHM )
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for sstf disk scheduling
/* loop will execute until all process is completed*/ while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}
9.3 SCAN SCHEDULING ALGORITHM
# include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1); initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0); initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}
# include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for C-Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1); /*movement
max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0); initial=0;
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}
# include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for C-look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}
10 .IMPLEMENTATION OF FILE
ORGANIZATION TECHNIQUES
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}
f[10];
int main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
10(3)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d",x);
i=x-1;
printf("Index is:%d\n",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}