AL3452 OS Lab Manual (1)
AL3452 OS Lab Manual (1)
OF ENGINEERING AND
TECHNOLOGY
VADAPUDUPATTI, THENI-625531
NAME : __________________________________________________
REGISTER NUMBER : __________________________________________________
BRANCH : __________________________________________________
YEAR/SEM : __________________________________________________
SUB.CODE/SUB.NAME : ___________________________________________________
PAGE MARKS STAFF
EX.NO DATE NAME OF THE EXPERIMENT
NO OBTAINED SIGNATURE
2b Shell Programming
AIM :
To install the OS in the system using the USB Media
PROCEDURE
You can install Windows 10 by creating a bootable USB flash drive or DVD that contains the
Windows 10 installation files. You'll need a USB flash drive that's at least 8GB, or any blank DVD to
get started.
If you already have Windows 10 installed on the PC and just want to reinstall it, it'll be easiest to
reinstall it from within Windows 10 instead of creating installation media.
If you want to upgrade from Windows 7 or Windows 8.1, you won't need to create an installation
disc or drive. However, you will need to follow most of this method to start the upgrade.
Head to Settings > Update & Security > Activation from the current installation—if the activation
status says Windows is activated with a digital license, click Add an account and follow the
on-screen instructions to link your Microsoft account.
● If you're upgrading from an earlier version and your PC qualifies for a free upgrade, you
won't need a product key.
Go to https://www.microsoft.com/en-us/software-download/windows10%20. This is the official
download site for Windows 10.
Click Download tool now. This is a blue button in the middle of the page. This downloads the
Media Creation Tool, which you'll use to create your installation media (or start your upgrade.
Double-click the downloaded file. Its name begins with "MediaCreationTool" and ends with
".exe." You'll find it in your default download folder, which is usually called Downloads.
● Click Yes when prompted to allow the installer to run.
Select "Create installation media" and click OK. This option lets you create a Windows
installationdisc or drive that will work on any compatible PC, not just the one you're using now.
If you're updating your PC from an earlier version of Windows, select Upgrade this PC now instead,
and then follow the on-screen instructions to install Windows 10. You're done!
Select
your preferences and click Next. If you're installing Windows on the current PC, you can keep the
default options. If you need to install on a different PC, make sure you choose the language and edition
for which you have a license, and select the architecture (64-bit or 32-bit) that matches the PC you're
going to install on.
If you're not sure about the architecture, you can choose Both from the menu.
Create your installation media. The steps are a little different depending on what you're doing:
Flash drive: Select your flash drive from the list, click Next, and wait for the installation files to install.
When the process is complete, click Finish.
.
Booting from Windows 10 Installation Media
Boot the PC into the BIOS. If your PC is not already set up to boot from your flash or optical drive, rebooting
from your installation media won't work. You'll need to make a quick change in your BIOS to change the boot order.
There are a few ways to get in:
Wait for your computer to restart. Once your computer finishes restarting, you'll see a window here
with your geographical data. You're now ready to begin setting up your Windows 10 installation.
Follow the on-screen instructions to install Windows 10. You'll be asked to perform a few tasks, such
as connecting to Wi-Fi and choosing some preferences. Once the installation is complete, you'll have a
fresh new installation of Windows 10.
If you're upgrading from an earlier version of Windows, you'll be asked if you want to upgrade
the current operating system or do a custom install. If you choose Upgrade, you'll preserve
existing apps and files.
RESULT
Thus the windows Operating system installed successfully
Ex.No:2 a) Basics of UNIX commands.
AIM :
To Study about the UNIX Commands
COMMAND :
1.Date Command :
This command is used to display the current data and time.
Syntax :
$date
$date +%ch
Options : -
a = Abbreviated weekday.
A = Full weekday.
b = Abbreviated month.
B = Full month.
c = Current day and time.
C = Display the century as a decimal number.
d = Day of the month.
D = Day in „mm/dd/yy‟ format
h = Abbreviated month day.
H = Display the hour.
L = Day of the year.
m = Month of the year.
M = Minute.
P = Display AM or PM
S = Seconds
T = HH:MM:SS format u
= Week of the year.
y = Display the year in 2 digit.
Y = Display the full year.
Z = Time zone .
To change the format:
Syntax:
$date „+%H-%M-%S‟
2. Calender Command :
This command is used to display the calendar of the year or the particular month of
calendar year.
Syntax:
a. $cal <year>
b.$cal <month> <year>
Here the first syntax gives the entire calendar for given year & the second Syntax
gives the calendar of reserved month of that year.
3. Echo Command:
This command is used to print the arguments on the screen.
Syntax: $echo <text>
To have the output in different line, the following command can be used.
Syntax: $echo “text
>line2
>line3”
4. Banner Command:
It is used to display the arguments in „#‟ symbol.
Syntax: $banner <arguments>
5. ’who’ Command:
It is used to display who are the users connected to our computer currently.
Syntax: $who – option‟s
Options: -
H–Display the output with headers.
b–Display the last booting date or time or when the system was lastely rebooted.
6. ’who am i’ Command:
Display the details of the current working directory.
Syntax: $who am i
7. ’tty’ Command:
It will display the terminal name.
Syntax: $tty
w
8. ’Binary’ Calculator Command:
It will change the „$‟ mode and in the new mode, arithematic operations such as +,-
,*,/,%,n,sqrt(),length(),=, etc can be performed . This command is used to go to the binary
calculus mode.
Syntax:
$bc operations^d$
Base:
1 base –inputbase
0 base – outputbase are used for base conversions. Decimal = 1
Arguments:
1. Clear – to clear the screen.
2. Longname – Display the complete name of the terminal.
3. SMSO – background become white and foreground become black color.
4. rmso – background become black and foreground becomes white color.
5. Cop R C – Move to the cursor position to the specified location.
6. Cols – Display the number of columns in our terminals.
2. MKDIR Command:
To create or make a new directory in a current directory.
Syntax: $mkdir <directory name>
3. CD Command:
To change or move the directory to the mentioned directory.
Syntax: $cd <directory name.
4. RMDIR Command:
To remove a directory in the current directory & not the current directory itself.
Syntax: $rmdir <directory name>
2. DISPLAY A FILE:
To display the content of file mentioned we use CAT command without „>‟ operator.
Syntax:
$cat <filename.
Options –s = to neglect the warning /error message.
3. COPYING CONTENTS:
To copy the content of one file with another. If file doesnot exist, a new file is created
and if the file exists with some data then it is overwritten.
Syntax:
$ cat <filename source> >> <destination
filename>
Options: - Syntax: $ cat <source filename> >> <destination
filename> it is avoid overwriting.
$cat –n <filename>
4. SORTING A FILE:
To sort the contents in alphabetical order in reverse order.
Syntax:
$sort <filename >
Option:
$ sort –r <filename>
COPYING CONTENTS FROM ONE FILE TO ANOTHER:
To copy the contents from source to destination file. So that both contents are same.
Syntax:
$cp <source filename> <destination filename>
$cp <source filename path > <destination filename path>
5. MOVE Command :
To completely move the contents from source file to destination file and to remove the
source file.
Syntax:
$ mv <source filename> <destination filename>
6. REMOVE Command :
To permanently remove the file we use this command.
Syntax:
$rm <filename>
7. WORD Command :
To list the content count of no of lines , words, characters .
Syntax: Options:
$wc<filename>
-c – to display no of characters.
-l – to display only the lines.
-w – to display the no of words.
8. LINE PRINTER:
To print the line through the printer, we use lp command.
Syntax:
$lp <filename>
9. PAGE Command:
This command is used to display the contents of the file page wise & next page can be
viewed by pressing the enter key.
Syntax:
$pg <filename>
RESULT
Thus the Linux command executed successfully
Ex.No:2 b)
Shell Programming
AIM:
ALGORITHM:
PROGRAM:
2. Write a Shell program to check the given year is leap year or not
ALGORITHM:
STEP 4: If the value of b equals 0 then print the year is a leap year
STEP 5: If the value of r not equal to 0 then print the year is not a leap year.
PROGRAM :
ALGORITHM:
STEP 4: If the value of i is greater than 1 then calculate „n=expr $n \* $i‟ and „i=expr $i – 1‟
PROGRAM:
i=`expr $n - 1` p=1
while [ $i -ge 1 ] do
ALGORITHM:
PROGRAM:
RESULT
Thus the Shell command executed successfully
Programs using the following system calls of UNIX operating system fork,
Ex.No:3
exec, getpid, exit, wait, close, stat, opendir, readdir
AIM:
To write C Programs using the following system calls of UNIX operating system
fork, exec, getpid, exit, wait, close, stat, opendir, readdir.
ALGORITHM:
PROGRAM:
#include<stdio
.h>
#include<diren
t.h> struct
dirent *dptr;
int main(int argc, char *argv[])
{
char
buff[10
0]; DIR
*dirp;
printf(“\n\n ENTER DIRECTORY
NAME”); scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{printf(“The given directory does not exist”); exit(1);
}while(dptr=readdir(dirp)){
printf(“%s\n”,dptr->d_name);
}closedir(dirp);
2. PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING
SYSTEM (fork, getpid, exit)
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables pid,pid1,pid2. STEP 3: Call fork() system call to create
process. STEP 4: If pid==-1, exit.
STEP 5: Ifpid!=-1 , get the process id using getpid(). STEP 6: Print the process id.
STEP 7:Stop the program
PROGRAM:
\#include<stdio.h> #include<unistd.h> main()
{
int pid,pid1,pid2; pid=fork(); if(pid==-1)
{
printf(“ERROR IN PROCESS CREATION \n”);
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf(“\n the parent process ID is %d\n”, pid1);
}
else
{
pid2=getpid();
printf(“\n the child process ID is %d\n”, pid2);}
RESULT
Thus the UNIX operating Program was executed successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4 a)
PRIORITY
AIM:
To write a C program for implementation of Priority scheduling algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer, totwtime and totttime is equal
to zero. Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time
and priority. Step 5: Assign wtime as zero .
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and assign as
turnaround time. Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int pno; int pri; int pri; int btime;
int wtime;
}sp;
int main()
{
int i,j,n;
int
tbm=0,totwtime=0,tottti
me=0; sp *p,t;
printf("\n PRIORITY SCHEDULING.\n");
printf("\n enter the no of process. \n");
scanf("%d",&n);
p=(sp*)malloc(siz
eof(sp));
printf("enter the burst time and
priority:\n"); for(i=0;i<n;i++)
{
printf("process%d:”,i+1);
scanf("%d%d",&p[i].btime,
&p[i].pri);
p[i].pno=i+1;
p[i].wtime=0;
}
for(i=0;i
<n-1;i++
)
for(j=i+1
;j<n;j++)
{
if(p[i].pri>p[j].pri)
{
t=p[i]; p[i]=p[j]; p[j]=t;
}
}
printf("\n process\tbursttime\twaiting time\tturnaround
time\n"); for(i=0;i<n;i++)
{
totwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pno,p[i
].btime);
printf("\t\t%d\t\t%d",p[i].wtime,p[i].wtime+p[i].btime);
}
totttime=tbm+totwtime;
printf("\n total waiting time:%d",totwtime);
printf("\n average waiting
time:%f",(float)totwtime/n); printf("\n total
turnaround time:%d",totttime); printf("\n avg
turnaround time:%f",(float)totttime/n);
}
\
RESULT
Thus the Priority Scheduling Program was executed successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4.b
ROUND ROBIN SCHEDULING
AIM:
ALGORITHM:
PROGRAM:
RESULT
Thus the Round Robin scheduling algorithms. Program was executed
successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4.c
FCFS
AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
PROGRAM:
RESULT
Thus the FCFS Program was executed successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4.d
SJF SCHEDULING
AIM:
To write a C program for implementation of SJF scheduling algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate wait time
and turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total number of
process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h
> typedef struct
{
int pid;
int btime;
int wtime;
}
sp;
int main()
{
int i,j,n,tbm=0,towtwtime=0,totttime
sp*p,t;
printf("\n sjf schaduling ..\n");
printf("enter the no of processor");
scanf("%d",&n);
p=(sp*)malloc(sizeof(sp));
printf("\n enter the burst time");
for(i=0;i<n;i++)
{
printf("\n process %d\t",i+1);
scanf("%d",&p[i].btime);
p[i].pid=i+1;
p[i].wtime=0;
printf("\n process scheduling\n");
printf("\n process \tburst time \t w
for(i=0;i<n;i++)
{
towtwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pid,p[i].bt
printf("\t\t%d\t\t%d",p[i].wtime,p[i
}
totttime=tbm+towtwtime;
printf("\n total waiting time :%d", totwtime );
printf("\n average waiting time
:%f",(float)totwtime/n); printf("\n total turn around
time :%d",totttime);
printf("\n average turn around time: :%f",(float)totttime/n);
}
RESULT
Thus the SJF Scheduling Program was executed successfully
Ex.No:5 INTERPROCESS COMMUNICATION
AIM
To write a C program to implement shared memory and inter process communication.
ALGORITHM:
PROGRAM
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h> int
main()
{
int id,semid,count=0,i=1,j; int
*ptr;
id=shmget(8078,sizeof(int),IPC_CREAT|0666); ptr=(int
*)shmat(id,NULL,0);
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}u;
struct sembuf sem;
semid=semget(1011,1,IPC_CREAT|0666); ushort
a[1]={1};
u.array=a; semctl(semid,0,SETALL,u);
while(1)
{
sem.sem_num=0;
sem.sem_op=-1;
sem.sem_flg=0;
semop(semid,&sem,1);
*ptr=*ptr+1;
printf("process id:%d countis :%d \n",getpid(),*ptr);
for(j=1;j<=1000000;j++)
{
sem.sem_num=0; sem.sem_op=+1;
sem.sem_flg=0;
semop(semid,&sem,1);
}
}
shmdt(ptr);
}
RESULT:
Thus the Interprocess communication program was executed Successfully
Ex.No:6 PRODUCER CONSUMER PROBLEM USING SEMAPHORES
AIM:
To write a C-program to implement the producer – consumer problem using
semaphores.
ALGORITHM:
PROGRAM:
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1) {
printf("\nENTER YOUR CHOICE\n");
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;
}
}
}
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("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex); }
RESULT:
AIM:
To write a C program to implement banker‟s algorithm for deadlock avoidance.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Baner's Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
\ scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t
Available\t"); for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}}}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}}
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}}}}}}
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
else
{printf("P%d->",i);
}}
if(c1==n)
{printf("\n The system is in safe state");
}
Else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}}
RESULT
Thus the Banker Algorithm Program was executed successfully
Ex.No:8 ALGORITHM FOR DEADLOCK DETECTION
AIM:
To write a C program to implement algorithm for deadlock detection.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t"); scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t
Available\t"); for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}}}
void cal()
{ int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}}
while(flag)
{flag=0;
for(i=0;i<n;i++)
{int c=0;
for(j=0;j<r;j++)
{if((finish[i]==0)&&(need[i][j]<=avail[j]))
{c++;
if(c==r)
{
for(k=0;k<r;k++)
{avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}//printf("\nP%d",i);
if(finish[i]==1)
{i=n;
}}}}}}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{dead[j]=i;
j++;
flag=1;
}}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{printf("P%d\t",dead[i]);
}}
else
{
printf("\nNo Deadlock Occur"); }}
RESULT
Thus the Deadlock detection Program was executed successfully
Ex.No:9 THREADING & SYNCHRONIZATION APPLICATIONS
AIM:
To write a c program to implement Threading and Synchronization Applications.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err; while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err)); else
printf("\n Thread created successfully\n");
i++;
}
sleep(5); return 0;
}
RESULT
Thus the Threading and Sync Program was executed successfully
Ex.No:10 IMPLEMENTATION OF PAGING TECHNIQUE OF MANAGEMENT
AIM:
To write a C program to implement the concept of Paging
ALGORITHM:
PROGRAM
#include<stdio.h
> main()
{
int
i,j,arr[100],pt[20],val,pgno,offset,phymem,fs,nf;
printf("\n Memory Management paging\n");
printf("enter the size of physical memory");
scanf("%d",&phymem);
for(i=20,j=0;i<phymem+20,j<phymem;i++,j++)
arr[i]=j;
printf("\n Enter size of frame or
page"); scanf("%d",&fs);
nf=phymem/fs;
printf("No of frame available
are\t%d",nf); printf("\n Enter the page
table"); for(i=0;i<nf;i++)
{
scanf("%d",&pt[i]);
}
printf("\n Enter the page
no"); scanf("%d",&pgno);
printf("\n Enter the offset");
scanf("%d",&offset);
val=(fs*pt[pgno])+offset;
printf("the physical address is:%d\n",arr[val]);
}
RESULT:
AIM:
To write a C program for implementation memory allocation methods for fixed partition
using first fit.
ALGORITHM:
PROGRAM:
#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;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
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) //if bf[j] is not allocated
{
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;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
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();
}
RESULT
Thus the FIRST FIT Program was executed successfully
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:11.b
WORST FIT
AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst
Fit"); printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
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:\tFragement");
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();
}
RESULT
Thus the WORST FIT Program was executed successfully
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:11.c
BEST FIT
AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
PROGRAM:
#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;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
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("\nFile No\tFile Size \tBlock No\tBlock
Size\tFragment"); for(i=1;i<=nf && ff[i]!=0;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();
}
RESULT
Thus the Best Fit Program was executed successfully
PAGE REPLACEMENT ALGORITHMS
Ex.No:12.a
FIFO
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
int main()
{
int i=0,j=0,k=0,i1=0,m,n,rs[30],flag=1,p[30];
system("clear");
printf("FIFO page replacement algorithm. \\n");
printf("enter the no. of frames:");
scanf("%d",&n);
printf("enter the reference string:");
while(1)
{
scanf("%d",&rs[i]);
if(rs[i]==0)
break;
i++;
}
m=i;
for(j=0;j<n;j++)
p[j]=0;
for(i=0;i<m;i++)
{
flag=1;
for(j=0;j<n;j++)
if(p[j]==rs[i])
{
printf("data already in page \n");
flag=0;
break;
}
if(flag==1)
{
p[i1]=rs[i];
i1++;
k++;
if(i1==n)
i1=0;
for(j=0;j<n;j++)
{
printf("\n page %d:%d",j+1,p[j]);
if(p[j]==rs[i])
printf("*");
}
printf("\n\n");
}
}
printf("total no page faults=%d",k);
}
RESULT
Thus the FIFO Program was executed successfully
PAGE REPLACEMENT ALGORITHMS
Ex.No:12.b
LRU
AIM:
To write a c program to implement LRU page replacement algorithm.
ALGORITHM:
ROGRAM:
#include<stdio.h>
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);
}
RESULT
Thus the LRU Program was executed successfully
FILE ORGANIZATION TECHNIQUE
Ex.No:13.a
SINGLE LEVEL DIRECTORY
AIM:
To write C program to organize the file using single level directory.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:/tc/bgi")
; cleardevice();
setbkcolor(GREEN);
printf("enter number of files");
scanf("&d",&count);
if(i<count)
// for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("enter %d file
name:",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"root directory");
setcolor(BLUE);
i++;
for(j=0;j<=i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[i]);
}}
getch();
}
RESULT
Thus the SIngle Level Program was executed successfully
FILE ORGANIZATION TECHNIQUE
Ex.No:13.b
TWO LEVEL DIRECTORY
AIM:
To write C program to organize the file using two level directory.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev ,char *dname,int lx,int rx,int x)
{int i, gap;
if(*root==NULL)
{(*root)=(node*)malloc(sizeof(node));
printf("enter the name of dir file name %s",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0 || lev==1)
(*root)-> ftype=1;
else
(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50
; (*root)->x=x;
(*root)->lx=lx ;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL
; if((*root)->ftype==1)
{if(lev==0 || lev==1)
{if((*root)->level==0)
printf("how many users");
else
printf(" how many files");
printf("(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
}else
(*root)->nc=0;
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}else
(*root)->nc=0;
}}
display(node *root)
{int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{for(i=0;i<root->nc;i++)
{line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}if(root->ftype==1)
bar3d(root->x-20, root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{display(root->link[i]);
}}}
RESULT:
Thus the Two level directory Program was executed successfully
FILE ALLOCATION STRATEGIES
Ex.No:14.a
SEQUENTIAL
AIM:
To write a C program for sequential file for processing the student information.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h
> typedef struct
{int sno;
char name[25];int m1,m2,m3;
}STD;
void display(FILE *);
int search(FILE *);
void main()
{int i,n,sno_key,opn;
FILE *fp;
clrscr();
printf(“How many records ?”);
scanf(“%d”,&n);
fp=fopen(“stud.dat”,”w”);
for(i=0;i<n;i++)
{printf(“Enter the student information : %d(sno,Name,M1,M2,M3):”,i+1);
scanf(“%d%s%d%d%d,&s.sno,s.name,&s.m1,&s.m2,&s.m3);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
fp=fopen(“stdu.dat”,”r”);
do
{printf(“1-DISPLAY\n2.SEARCH\n 3.EXIT\n YOUR OPTION: “);
scanf(“%d”,&open);
switch(opn)
{
case 1:
printf(“\n Student Records in the file \n”);
display(fp);
break;
case 2:
printf(“Read sno of the student to be searched :”);
scanf(“%d”,&sno_key);
if(search(fp,sno_key)){
printf(“success!! Record found in the file\n”);
printf(“%d\t%s\t%d\t%d\t%d\n”, s.sno,s.name,s.m1,s.m2,s.m3);
}
else
printf(“Failure!! Record %d not found\n”,sno_key);
break;
case 3:
printf(“Exit !! press key”);
getch();
break;
default:
printf(“Invalid option!!! Try again!!\n”);
break;
}
}while(opn!=3);
fclose(fp);
}
Void display(FILE *fp)
{rewind(fp);
while(fread(&s,sizeof(s),1,fp))
printf(“%d\t%s\t%d\t%d\t%d\n”,s.sno,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp,int sno_key)
{rewind(fp);
while(fread(&s,sizeof(s),1,fp))
If(s.sno==sno_key)
return 1;
return 0;
}
RESULT
Thus the SEQUENTIAL Program was executed successfully
FILE ALLOCATION STRATEGIES
Ex.No:14.b
LINKED
AIM:
To write a C program for random access file for processing the employee details.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct record
{
char empname[20];
int age;
float salary;
};
typedef struct record person
FILE *people;
void main()
{
person employee;
int I,n;
FILE *fp;
printf(“How many records:”);
scanf(“%d”,&n);
fp=fopen(“PEOPLE.txt”,”w”);
for(i=0;i<n;i++)
{
printf(“Enter the employee information :%d(EmpName, Age,Salary):”,i+1);
scanf(“%s%d%f”,employee.empname,&employee.age,& employee.salary);
fwrite(,&employee.sizeof(employee,1,people);
}
fclose(fp);
int rec,result;
people=fopen(“PEOPLE.txt”,”r”);
printf(“Which record do you want to read from file?);
scanf(“%d”,&rec);
while(rec>=0)
{
fseek(people,rec*sizeof(employee),SEEK_SET);
result=fread(&em[ployee,sizeof(employee),1,people)
if(result==1)
{
printf(“\n RECORD %d\n”,rec);
printf(“Given name:%s\n”, employee.empname);
printf(“Age:%d years\n”,employee.age);
printf(“Current salary:$ %8.2f\n\n”,employee.salary);
}
else
printf( “\n RECORD %d not found !\n\n”,rec);
printf(“Which record do you want(0to3)”):
scanf(“%d”<&rec):
}
fclose(people):
getch():
}
RESULT
Thus the Linked Management Program was executed successfully
FILE ALLOCATION STRATEGIES
Ex.No:14.c
INDEXED
AIM:
To write a C program for random access file for processing the employee details.
ALGORITHM:
PROGRAM:
#include
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("enter no of files on index\t");
scanf("%d",&n);
}
else
{
printf("Block already allocated\n");
goto x;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++) if(f[inde[i]]==1)
{
printf("Block already allocated");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1; printf("\n
allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]); printf("
Enter 1 to enter more files and 0 to exit\t");
scanf("%d",&c);
if(c==1) goto x; else exit();getch();
}
RESULT
Thus the Indexed Management Program was executed successfully
DISK SCHEDULING
Ex.No:15 a)
FIRST COME FIRST SERVED
AIM:
Write a C program to simulate disk scheduling algorithms FCFS
ALGORITHM
PROGRAM
#include<stdio.h>
main()
{
int t[20], n, I, j, tohm[20], tot=0;float avhm; clrscr();
printf(“enter the no.of tracks”); scanf(“%d”,&n);
printf(“enter the tracks to be traversed”); for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+); for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i]; if(tohm[i]<0) tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i]; avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”); for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+); printf("\nAverage header movements:%f",avhm); getch();
}
RESULT
Thus the First come First served Program was executed successfully
DISK SCHEDULING
Ex.No:15 b)
C-SCAN ALGORITHM
AIM:
Write a C program to simulate disk scheduling algorithms C-SCAN
ALGORITHM:
PROGRAM
#include<stdo.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20], temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n"); scanf("%d",&max);
printf("Enter the initial head position\n"); scanf("%d",&head);
printf("Enter the size of queue request\n"); scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n"); for(i=1;i<=n;i++)
{
scanf("%d",&temp); if(temp>=head)
{
queue1[temp1]=temp; temp1++;
queue2[temp2]=temp; temp2++;
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i]; queue1[i]=queue1[j]; queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i]; queue2[i]=queue2[j]; queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++) queue[i]=queue1[j]; queue[i]=max; queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++) queue[i]=queue2[j]; queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]); seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek); avg=seek/(float)n;
printf("Average seek time is %f\n",avg); return 0;
}
RESULT
Thus the C-Scan Algorithm Program was executed successfully