Вы находитесь на странице: 1из 77

SETHU INSTITUE OF TECHNOLOGY

Estd. 1995 PULLOOR, KARIAPATTI

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

MASTER RECORD

OPERATING SYSTEM LAB


1

1
CONTENTS

OPERATING SYSTEM LAB

EX.NO. TITLE OF THE EXPERIMENT PAGE


NO.

DEVELOPMENT OF ROUTINES FOR INTER PROCESS


1. COMMUNICATION

PROCESS CREATION AND MANAGEMENT


2.

3. SIMULATION OF CPU SCHEDULING ALGORITHMS I

SIMULATION OF CPU SCHEDULING ALGORITHMS II


4.

SIMULATION OF PRODUCER-CONSUMER PROBLEM USING


5. SEMAPHORES

IMPLEMENTATION OF DEADLOCK AVOIDANCE AND


6. PREVENTION ALGORITHMS

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME I


7.

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME II


8.

IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHMS


9.

2
ANALYSIS OF FILE ALLOCATION ALGORITHMS
10.

WORKING WITH FILE SYSTEM COMMANDS


11.

SIMULATION OF DISK SCHEDULING ALGORITHMS


12.

Ex.No.:1 INTER PROCESS COMMUNICATION USING PIPES

A) EVEN OR ODD
AIM:
To study the usage of pipe system call to achieve inter process communication.

Algorithm
1. Get a number from the user.
2. Divide the number by 2.
3. Check the remainder of the division.
4. If it is zero then the given number is even. Otherwise the given number is odd.
5.
Program:
//even.c
#include<stdio.h>
main()
{
int p[2],pid,i,n,d[20];
if(pipe(p)==-1)
{
printf("error");
exit(1);
}
printf("\n enter the value of n");

3
scanf("%d",&n);
printf("\n enter the elements");
for(i=0;i<n;i++)
scanf("%d".&d[i]);
pid=fork();
if(pid==0)
{
close(p[0]);
for(i=0;i<n;i++)
write(p[1],d[i],20);
}
else
{
close(p[1]);
for(i=0;i<n;i++)
read(p[0],d[i],20);
for(i=0;i<n;i++)
{
if(d[i]%2==0)
printf("%d is even \n",d[i]);
else
printf("%d is odd \n",d[i]);
}
}
}

OUTPUT :
$ cc even.c
$ a.out
Enter the value of n
3

4
Enter the elements
5
10
35

5 is odd
10 is even
35 is odd

Result :
Thus the usage of pipe system call to achieve inter process communication is studied
sucessfully.

B) STRING COPY
Aim :
To write a C Program to implement inter process communication using Pipe
Algorithm:
1. Start the program
2. Create a pipe using pipe() system call
3. Create a child process. If the child process is created successfully then write the message
into the queue otherwise go to step2
4. Read the message from the pipe and display the message
5. Stop the program

Program

#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];

5
printf("\n Enter the string to enter into the pipe");
scanf("%s",a);
pipe(fd);
child=fork();
if(! child)
{
close(fd[0]);
write(fd[1],a,5);
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf("\n\n The string retrieved from the pipe is %s",a);
}
return 0;
}

OUTPUT:-

Enter the string to enter into pipe: LOKESH

The string retrieved from the pipe is LOKESH

Result:
Thus a C Program to implement inter process communication using Pipe is created and
executed successfully.

Ex no: 2 PROCESS CREATION AND MANAGEMENT

6
Aim:
To write a program to create and manage a process in UNIX.
Algorithm:
1. Start the program.
2. Declare pid as integer.
3. Create the process using Fork command.
4. Check pid is less than 0 then print error else if pid is equal to 0 then execute
command else parent process wait for child process.
Stop the program
Program:
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 200
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}

7
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}

Output:
Thus a program to create and manage a process in UNIX is executed successfully.

Ex no: 3 CPU SCHEDULING ALGORITHMS I

A) FCFS
Aim:
To find the waiting and turnaround time of all the processes based on FCFS scheduling
algorithm.

Algorithm :
1. Get the no of processes.
2. For each process assign the process id and get the process time.
3. Set the waiting time of the first process as 0 and its turn around time as process time.
4. For each process calculate,
Waiting time of process(n) = waiting time of process (n-1) + process time of process (n-1)
Turnaround time of process(n) = waiting time of process (n) + process time of process (n)
5. Calculate the average waiting time and turnaround time.
6. Print the output.

Program:
#include<stdio.h>

8
struct process
{
int burst,wait;
}p[20]={0,0};
int main()
{
int n,i,totalwait=0,totalturn=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter The Burst Time (in ms) For Process #%2d :",i+1);
scanf("%d",&p[i].burst);
}
printf("\nProcess \t Waiting Time TurnAround Time ");
printf("\n \t (in ms) (in ms)");
for(i=0;i<n;i++)
{
printf("\nProcess # %-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
p[i+1].wait=p[i].wait+p[i].burst;
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAVERAGE\n--------- ");
printf("\nWaiting Time : %f ms",totalwait/(float)n);
printf("\nTurnAround Time : %f ms\n\n",totalturn/(float)n);
return 0;
}
Output:
Enter The No Of Process :3
Enter The Burst Time (in ms) For Process # 1 :10

9
Enter The Burst Time (in ms) For Process # 2 :30
Enter The Burst Time (in ms) For Process # 3 :20
Process Waiting Time TurnAround Time
(in ms) (in ms)
Process # 1 0 10
Process # 2 10 40
Process # 3 40 60
AVERAGE
---------
Waiting Time : 16.666667 ms
TurnAround Time : 36.666667 ms

Result:
Thus the program First Come First Serve was executed and the output was verified.

B) SJF
Aim:
To find the waiting and turnaround time of all the processes based on SJF scheduling
algorithm.

Algorithm :
1. Get the no of processes.
2. For each process assign the process id and get the process time.
3. Sort the processes according to the process time.
4. Set the waiting time of the first process as 0 and its turnaround time as process time
5. For each process calculate,
Waiting time of process(n) = waiting time of process (n-1) + process time of pocess(n-1)
Turnaround time of process(n) = waiting time of process (n) + process time of process (n)
6. Calculate the average waiting time and turnaround time

Program:

10
#include<stdio.h>
struct process{
int burst,wait,no;
}p[20]={0,0};
int main(){
int n,i,j,totalwait=0,totalturn=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter The Burst Time (in ms) For Process #%2d :",i+1);
scanf("%d",&p[i].burst);
p[i].no=i+1;
}
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(p[j].burst>p[j+1].burst){
p[j].burst^=p[j+1].burst^=p[j].burst^=p[j+1].burst;
p[j].no^=p[j+1].no^=p[j].no^=p[j+1].no;
}
printf("\nProcess \t Waiting Time TurnAround Time ");
for(i=0;i<n;i++){
printf("\nProcess # %-12d%-15d%-15d",p[i].no,p[i].wait,p[i].wait+p[i].burst);
p[i+1].wait=p[i].wait+p[i].burst;
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n---------");
printf("\nWaiting Time : %f ms",totalwait/(float)n);
printf("\nTurnAround Time : %f ms\n\n",totalturn/(float)n);
return 0;
}

11
Output:
Enter The No Of Process :3
Enter The Burst Time (in ms) For Process # 1 :20
Enter The Burst Time (in ms) For Process # 2 :30
Enter The Burst Time (in ms) For Process # 3 :10
Process Waiting Time TurnAround Time
Process # 3 0 10
Process # 1 10 30
Process # 2 30 60
Average
---------
Waiting Time : 13.333333 ms
TurnAround Time : 33.333333 ms

Result:
Thus the program Shortest Job First was executed and the output was verified.

Ex no: 4 CPU SCHEDULING ALGORITHMS II

A) PRIORITY SCHEDULING
Aim :
To find the waiting and turnaround time of all the processes based on priority algorithm.

Algorithm :

1. Get the no of processes.


2. For each process assign the process id and get the process time and the priority value.
3. Sort the processes according to the priority value.
4. Set the waiting time of the first process as 0 and its turnaround time as process
time.
5. For each process calculate,

12
Waiting time of process(n) = waiting time of process (n-1) + process time of process (n-
1).
Turnaround time of process(n) = waiting time of process (n) + process time of process
(n)

5. Calculate the average waiting time and turnaround time.

Program:
#include<stdio.h>
struct process{
int pid; int bt; int wt; int tt; int prior;
} p[10],temp;
int main()
{
int i,j,n,totwt,tottt,arg1,arg2;
printf("enter the number of process");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("enter the burst time");
scanf("%d",&p[i].bt);
printf("\n enter the priority");
scanf("%d",&p[i].prior);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].prior>p[j].prior)

13
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt; p[i].bt=p[j].bt;
p[j].bt=temp.bt;
temp.prior=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=temp.prior;
} } } p[i].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\n process to\t bt \t wt \t tt");
while(i<=n)
{
printf("\n%d\t\t %d\t %d\t %d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
arg1=totwt/n;
arg2=tottt/n;
printf("\n Avg waiting time=%d \t Avg Turnround time=%d\t",arg1,arg2);

14
return 0;
}
Output:
enter the number of process 5
enter the burst time 10
enter the priority 2
enter the burst time 15
enter the priority 4
enter the burst time 5
enter the priority 1
enter the burst time 10
enter the priority 3
enter the burst time 3
enter the priority 15

process to bt wt tt
3 5 0 5
1 10 5 15
4 10 15 25
2 15 25 40
5 3 40 43
Avg waiting time=17 Avg Turnround time=25

Result:
Thus the program Priority Scheduling was executed and the output was verified.

B) ROUND-ROBIN SCHEDULING
Aim :

15
To find the waiting and turn around time of all the processes based on Round robin
scheduling algorithm.
Algorithm:
1. Accept the no of processes in the ready queue and time slice.
2. For each process in the ready queue accept the burst time.
3. Calculate the no of time slices required for each process.
4. If the burst time is less than the time slice then the no of time slice is 1.
5. Considering the ready queue as a circular queue, calculate
Total waiting time for process(n) = waiting time for process (n-1) + burst time of
process (n-1) + the time difference in getting the CPU from process(n).
Total turn around time for process(n) = waiting time for process (n) + burst time
of process (n) + the time difference in getting the CPU from process(n).
6. Calculate the average waiting time and turn around time.

Program:
#include<stdio.h>
struct process
{
int pid,bt,tt,wt;
};
int main()
{
struct process x[10],p[30]; int i,j,k,tot=0,m,n;
float wttime=0.0,tottime,a1,a2;
printf("\nEnter No.of process\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
x[i].pid=i;
printf("Enter The Burst Time: \t");

16
scanf("%d",&x[i].bt);
tot=tot+x[i].bt;
}
printf("Total Burst Time:\t%d",tot);
p[0].tt=0;
k=1;
printf("\nEnter The Time Slice:\t");
scanf("%d",&m);
for(j=1;j<=tot;j++)
{
for(i=1;i<=n;i++)
{
if(x[i].bt!=0)
{
p[k].pid=i;
if(x[i].bt-m<0)
{
p[k].wt=p[k-1].tt; p[k].bt=x[i].bt; p[k].tt=p[k].wt+x[i].bt; x[i].bt=0;
k++;
}
else
{
p[k].wt=p[k-1].tt;
p[k].tt=p[k].wt+m;
x[i].bt=x[i].bt-m;
k++;
} } }}
printf("\nprocess id\ttwt\ttt");
for(i=1;i<k;i++)
{
printf("\n\t%d\t%d\t%d",p[i].pid,p[i].wt,p[i].tt);

17
wttime=wttime+p[i].wt;
tottime=tottime+p[i].tt;
a1=wttime/n;
a2=tottime/n;
}
printf("\n\nAverage Waiting Time:\t%f",a1);
printf("\n\nAverage TurnAround Time:\t%f",a2);
return 0;
}
Output:
Enter No.of process: 3
Enter The Burst Time:9
Enter The Burst Time:6
Enter The Burst Time:3
Enter The Time Slice:3

process id twt tt
1 0 3
2 3 6
3 6 9
1 9 12
2 12 15
1 15 18
Average Waiting Time:15.000000
Average TurnAround Time:21.000000

Result:
Thus the program Round Robin was executed and the output was verified.

18
Ex No.5 . IMPLEMENTING SEMAPHORES

Aim :
To implement the Producer- Consumer problem using semaphores.

Algorithm :
1. Create two functions called producer and consumer.
2. Set semaphore variable as 1.
3. When producer active set the semaphore variable as 1 and allow producer to put data into
the buffer and don’t allow consumer to consume anything.
4. After producer complete the process release the semaphore and signal the consumer.
5. When consumer active again set the semaphore variable as 1 and allow the consumer to
get data from buffer and don’t allow the producer to add data.
6. After the consumer taken release the semaphore variable and signal the producer.

Program :
#include<stdio.h>
#include<conio.h>
int n_semaphore=0; // keep track of no of item in the buffer
int s_semaphore=1; // to enforce mutual exclution
int s=0;
int buffer[10];
int i=0;
void producer()
{
s_semaphore=0; // set semaphore to avoid access to consumer
if(!s_semaphore)
{
printf("Now producer can add data to buffer\n");

19
++i;
scanf("%d",&buffer[i]);
}
else
printf("Critical Region \n");
s_semaphore=1; // release semaphore
signal_c(); // call to consumer
}

void consumer()
{
buffer_check(); // check buffer is empty or not
s_semaphore=0; // set semaphore to avoid access to producer
if(!s_semaphore)
{
printf("Consumer takes from the buffer\n");
printf("item from buffer %d",buffer[i]);
--i;
}
else
printf("Critical Region \n");
s_semaphore=1; // release semaphore
signal_p(); // call to producer
}

signal_c()
{
n_semaphore=n_semaphore+1;
consumer();
return 0;
}

20
signal_p()
{
n_semaphore=n_semaphore-1;
printf("Enter -99 to stop\n");
scanf("%d",&s);
if(s==-99)
exit();
return 0;
}
buffer_check()
{
if(n_semaphore<=0)
{
printf("Buffer is empty\n");
exit();
}
return 0;
}

void main()
{
clrscr();
n_semaphore=0;
while(1)
{
producer();
}
}

21
Sample Output;

Now producer can add data to buffer 10


Consumer takes from the buffer
Item from buffer 10
Enter -99 to stop
-99

Result:
Thus the program Producer- Consumer problem using semaphores was executed and
the output was verified.

Ex. No. : 6 IMPLEMENTATION OF DEADLOCK AVOIDANCE AND


PREVENTION ALGORITHMS

A) DEADLOCK DETECTION
AIM:
To write a C program to implement deadlock detection.
Algorithm:
i. Let Work and Finish be vectors of length m and n, respectively. Initialize Work ==
Available. For i = 0 , 1 , . . . , n-1, if Allocation!= 0, then Finish[i] = false; otherwise,
Finish[i] = true.
ii. Find an index i such that both
a. Finish[i] = false
b. Requesti <= Work

22
c. If no such exists, go to step 4.
iii. Work = Work + Allocationi
a. Finish[i] = true
b. Go to step 2.
iv. If Finish[i] == false, for some, 0=<i<n, then the system is in a deadlocked state.
Moreover, if Finish[i] == false, then process Pi is deadlocked.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
struct process
{
char pname[15];
int allocation[10],request[10],finish;
};
int available[10],avail[10];
int no_p,no_r,i,j,x,k;
struct process p[10],temp,*temp1;
char c;
char p_req[10];
int detect();
void resource_status(int[]);
void process_status();
void main()
{
clrscr();
printf("\nEnter number of processes: ");
scanf("%d",&no_p);
for(i=0;i<no_p;i++)
{

23
printf("\nEnter %d process name: ",(i+1));
scanf("%s",&p[i].pname);
}
printf("\nEnter number of resources: ");
scanf("%d",&no_r);
printf("\nEnter availability of each resource.");
for(i=0;i<no_r;i++)
{
printf("\n\tEnter availability of %d resource: ",(i+1));
scanf("%d",&available[i]);
avail[i]=available[i];
}
for(i=0;i<no_p;i++)
{
printf("\n\nEnter allocated resource of process %d: ",(i+1));
for(j=0;j<no_r;j++)
{
lab2:
printf("\n\tResource %d---->",(j+1));
scanf("%d",&p[i].allocation[j]);
if(p[i].allocation[j]>avail[j])
{
printf("\n\tAllocated resource is greater than available resource.Please enter smaller amount.");
goto lab2;
}
avail[j]=avail[j]-p[i].allocation[j];
}
}
printf("\n\nEnter request of each process: ");
for(i=0;i<no_p;i++)
{

24
printf("\n\nEnter request of process %d: ",(i+1));
for(j=0;j<no_r;j++)
{
printf("\n\tResource %d---->",(j+1));
scanf("%d",&p[i].request[j]);
}
}
clrscr();
printf("\nInitially Available Resources: ");
resource_status(available);
process_status();
printf("\nCurrent Status of availability: ");
resource_status(avail);
k=detect();
if(k==0)
{
for(i=0;i<no_p;i++)
{
for(j=i+1;j<no_p;j++)
{
if(p[i].request[0]>p[j].request[0])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
k=detect();
}
getch();

25
}
void resource_status(int a[])
{
for(i=0;i<no_r;i++)
{
printf("\n\tResource %d---->%d",(i+1),a[i]);
}
}
void process_status()
{
printf("\nPROCESS\t\tALLOCATION\tREQUEST");
for(i=0;i<no_p;i++)
{
printf("\n%s\t",p[i].pname);
printf("\t");
for(j=0;j<no_r;j++)
{
printf("%d ",p[i].allocation[j]);
}
printf("\t\t");
for(j=0;j<no_r;j++)
{
printf("%d ",p[i].request[j]);
}
}
}
int detect()
{
int work[10];
int x=0,y=0;
for(i=0;i<no_r;i++)

26
{
work[i]=avail[i];
}
for(i=0;i<no_p;i++)
{
for(j=0;j<no_r;j++)
{
if(p[i].allocation[j]!=0)
{
p[i].finish=0;
}
else
{
p[i].finish=1;
}
}
}
for(i=0;i<no_p;i++)
{
if(p[i].finish==0)
{
x=0;
for(j=0;j<no_r;j++)
{
if(p[i].request[j]<=work[j])
{
work[j]=work[j]+p[i].allocation[j];
x++;
}
if(x==no_r)
p[i].finish=1;

27
}
}
}
y=0;
for(i=0;i<no_p;i++)
{
if(p[i].finish==0)
{
printf("\nThe system is in deadlocked state because of the process %s",p[i].pname);
break;
}
else
{
y++;
}
}
if(y==no_p)
{
printf("\nThe system is in safe state.No deadlock");
printf("\nThe safe sequence is: ");
for(i=0;i<no_p;i++)
printf("%s ",p[i].pname);
return 1;
}
else
{
return 0;
}
}

OUTPUT:

28
Case1:
Enter number of processes: 5
Enter 1 process name: p0
Enter 2 process name: p1
Enter 3 process name: p2
Enter 4 process name: p3
Enter 5 process name: p4
Enter number of resources: 3
Enter availability of each resource.
Enter availability of 1 resource: 7
Enter availability of 2 resource: 2
Enter availability of 3 resource: 6
Enter allocated resource of process 1:
Resource 1---->0
Resource 2---->1
Resource 3---->0
Enter allocated resource of process 2:
Resource 1---->2
Resource 2---->0
Resource 3---->0
Enter allocated resource of process 3:
Resource 1---->3
Resource 2---->0
Resource 3---->3
Enter allocated resource of process 4:
Resource 1---->2
Resource 2---->1
Resource 3---->1
Enter allocated resource of process 5:
Resource 1---->0
Resource 2---->0

29
Resource 3---->2
Enter request of each process:
Enter request of process 1:
Resource 1---->0
Resource 2---->0
Resource 3---->0
Enter request of process 2:
Resource 1---->2
Resource 2---->0
Resource 3---->2
Enter request of process 3:
Resource 1---->0
Resource 2---->0
Resource 3---->0
Enter request of process 4:
Resource 1---->1
Resource 2---->0
Resource 3---->0
Enter request of process 5:
Resource 1---->0
Resource 2---->0
Resource 3---->2
Initially Available Resources:
Resource 1---->7
Resource 2---->2
Resource 3---->6
PROCESS ALLOCATION REQUEST
p0 0 1 0 0 0 0
p1 2 0 0 2 0 2
p2 3 0 3 0 0 0
p3 2 1 1 1 0 0

30
p4 0 0 2 0 0 2
Current Status of availability:
Resource 1---->0
Resource 2---->0
Resource 3---->0
The system is in safe state: No deadlock
The safe sequence is: p0 p1 p2 p3 p4
Case2:
Enter number of processes: 5
Enter 1 process name: p0
Enter 2 process name: p1
Enter 3 process name: p2
Enter 4 process name: p3
Enter 5 process name: p4
Enter number of resources: 3
Enter availability of each resource.
Enter availability of 1 resource: 7
Enter availability of 2 resource: 2
Enter availability of 3 resource: 6
Enter allocated resource of process 1:
Resource 1---->0
Resource 2---->1
Resource 3---->0
Enter allocated resource of process 2:
Resource 1---->2
Resource 2---->0
Resource 3---->0
Enter allocated resource of process 3:
Resource 1---->3
Resource 2---->0
Resource 3---->3

31
Enter allocated resource of process 4:
Resource 1---->2
Resource 2---->1
Resource 3---->1
Enter allocated resource of process 5:
Resource 1---->0
Resource 2---->0
Resource 3---->2
Enter request of each process:
Enter request of process 1:
Resource 1---->0
Resource 2---->0
Resource 3---->0
Enter request of process 2:
Resource 1---->2
Resource 2---->0
Resource 3---->2
Enter request of process 3:
Resource 1---->0
Resource 2---->0
Resource 3---->1
Enter request of process 4:
Resource 1---->1
Resource 2---->0
Resource 3---->0
Enter request of process 5:
Resource 1---->0
Resource 2---->0
Resource 3---->2
Initially Available Resources:
Resource 1---->7

32
Resource 2---->2
Resource 3---->6
PROCESS ALLOCATION REQUEST
p0 0 1 0 0 0 0
p1 2 0 0 2 0 2
p2 3 0 3 0 0 1
p3 2 1 1 1 0 0
p4 0 0 2 0 0 2
Current Status of availability:
Resource 1---->0
Resource 2---->0
Resource 3---->0
The system is in deadlocked state because of the process p2

RESULT:
Thus the C program for deadlock detection is written successfully and tested with various
samples.

B) DEADLOCK AVOIDANCE
AIM:
To write a C program to implement banker’s algorithm for deadlock avoidance.
Algorithms:
Safety Algorithm:
1. Let Work and Finish be vectors of length m and n, respectively. Initialize
Work = Available and Fnish[i] = false for i = 0 , 1 , ..., n - l .
2. Find an i such that both
Finish[i] ==false

33
Need <= Work
If no such i exists, go to step 4.
3. Work = Work + Allocation,
Finish[i] = true
Go to step 2.
4. If Finish[i] == true for all. i, then the system is in a safe state.
Resource Request Algorithm:
1. If Requesti <= Need, go to step 2. Otherwise, raise an error condition, since the process
has exceeded its maximum claim.
2. If Requesti <= Available, go to step 3. Otherwise, Pi must wait, since the resources are
not available.
3. The system pretend to have allocated the requested resources to process Pi by modifying
the state as follows:
a. Available = Available - Request;
b. Allocation = Allocation + Request;
c. Needi = Needj - Request;
4. If the resulting resource-allocation state is safe, the transaction is completed, and process
Pi is allocated to its resources. However, if the new state is unsafe, then Pi must wait for
Request Pi, and the old resource-allocation state is restored.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
struct process
{
char pname[15];
int max[10],allocation[10],need[10],finish;
};
int available[10],avail[10];
int no_p,no_r,i,j,x,k;

34
struct process p[10],temp,*temp1;
int request[10];
char c;
char p_req[10];
int safety();
void resource_status(int[]);
void process_status();
void resource_request(struct process *,int req[]);
void main()
{
clrscr();
printf("\nEnter number of processes: ");
scanf("%d",&no_p);
for(i=0;i<no_p;i++)
{
printf("\nEnter %d process name: ",(i+1));
scanf("%s",&p[i].pname);
}
printf("\nEnter number of resources: ");
scanf("%d",&no_r);
printf("\nEnter availability of each resource.");
for(i=0;i<no_r;i++)
{
printf("\n\tEnter availability of %d resource: ",(i+1));
scanf("%d",&available[i]);
avail[i]=available[i];
}
printf("\nEnter the maximum need of resources for each process: ");
for(i=0;i<no_p;i++)
{
printf("\n");

35
for(j=0;j<no_r;j++)
{
lab1:
printf("\n\tEnter max need of resource %d for %s: ",(j+1),p[i].pname);
scanf("%d",&p[i].max[j]);
if(p[i].max[j]>available[j])
{
printf("\n\tMaximum need is greater than available resource. please
enter smaller value.");
goto lab1;
}
}
}
for(i=0;i<no_p;i++)
{
printf("\n\nEnter allocated resource of process %d: ",(i+1));
for(j=0;j<no_r;j++)
{
lab2:
printf("\n\tResource %d---->",(j+1));
scanf("%d",&p[i].allocation[j]);
if(p[i].allocation[j]>avail[j])
{
printf("\n\tAllocated resource is greater than available
resource.Please enter smaller amount.");
goto lab2;
}
p[i].need[j]=p[i].max[j]-p[i].allocation[j];
avail[j]=avail[j]-p[i].allocation[j];
}
}

36
clrscr();
printf("\nInitially Available Resources: ");
resource_status(available);
process_status();
printf("\nCurrent Status of availability: ");
resource_status(avail);
k=safety();
if(k==0)
{
for(i=0;i<no_p;i++)
{
for(j=i+1;j<no_p;j++)
{
if(p[i].need[0]>p[j].need[0])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
}
k=safety();
while(1)
{
printf("\n\nAny Request for resources?(y/n): ");
c=getche();
if(c=='n'||c=='N')
break;
printf("\nWhich process is requesting resources: ");
scanf("%s",&p_req);

37
for(i=0;i<no_p;i++)
{
x=strcmp(p_req,p[i].pname);
if(x==0)
{
temp1=&p[i];
break;
}
}
printf("\nEnter the request of %s: ",temp1->pname);
for(i=0;i<no_r;i++)
{
scanf("%d",&request[i]);
}
resource_request(temp1,request);
}
getch();
}
void resource_status(int a[])
{
for(i=0;i<no_r;i++)
{
printf("\n\tResource %d---->%d",(i+1),a[i]);
}
}
void process_status()
{
printf("\nPROCESS\tMAXIMUM\tALLOCATION\tNEED");
for(i=0;i<no_p;i++)
{
printf("\n%s\t",p[i].pname);

38
for(j=0;j<no_r;j++)
{
printf("%d ",p[i].max[j]);
}
printf("\t");
for(j=0;j<no_r;j++)
{
printf("%d ",p[i].allocation[j]);
}
printf("\t\t");
for(j=0;j<no_r;j++)
{
printf("%d ",p[i].need[j]);
}
}
}
int safety()
{
int work[10];
int x=0,y=0;
for(i=0;i<no_r;i++)
{
work[i]=avail[i];
}
for(i=0;i<no_p;i++)
{
p[i].finish=0;
}
for(i=0;i<no_p;i++)
{
if(p[i].finish==0)

39
{
x=0;
for(j=0;j<no_r;j++)
{
if(p[i].need[j]<=work[j])
{
work[j]=work[j]+p[i].allocation[j];
x++;
}
if(x==no_r)
p[i].finish=1;
}
}
}
y=0;
for(i=0;i<no_p;i++)
{
if(p[i].finish==1)
y++;
}
if(y==no_p)
{
printf("\nThe system is in safe state");
printf("\nThe safe sequence is: ");
for(i=0;i<no_p;i++)
printf("%s ",p[i].pname);
return 1;
}
else
{
printf("\nThe system is not in safe state");

40
printf("\nThe sequence is: ");
for(i=0;i<no_p;i++)
printf("%s ",p[i].pname);
return 0;
}
}
void resource_request(struct process *p,int request[])
{
int k=0;
for(i=0;i<no_r;i++)
{
if(request[i]<=p->need[i])
{
if(request[i]<=avail[i])
{
k++;
}
}
}
if(k==no_r)
{
for(i=0;i<no_r;i++)
{
if(request[i]<=p->need[i])
{
if(request[i]<=avail[i])
{
avail[i]=avail[i]-request[i];
p->allocation[i]=p->allocation[i]+request[i];
p->need[i]=p->need[i]-request[i];
}

41
}
}
clrscr();
printf("\nRequested resources are allocated.");
process_status();
resource_status(avail);
}
else
{
printf("\nRequested resources cannot be granted since the resources
are not available.");
}
}
OUTPUT:
Enter number of processes:5
Enter 1 process name: P0
Enter 2 process name: P1
Enter 3 process name: P2
Enter 4 process name: P3
Enter 5 process name: P4
Enter number of resources: 3
Enter availability of each resource.
Enter availability of 1 resource: 10
Enter availability of 2 resource: 5
Enter availability of 3 resource: 7
Enter the maximum need of resources for each process:
Enter max need of resource 1 for p0: 7
Enter max need of resource 2 for p0: 5
Enter max need of resource 3 for p0: 3
Enter max need of resource 1 for p1: 3
Enter max need of resource 2 for p1: 2

42
Enter max need of resource 3 for p1: 2
Enter max need of resource 1 for p2: 9
Enter max need of resource 2 for p2: 0
Enter max need of resource 3 for p2: 2
Enter max need of resource 1 for p3: 2
Enter max need of resource 2 for p3: 2
Enter max need of resource 3 for p3: 2
Enter max need of resource 1 for p4: 4
Enter max need of resource 2 for p4: 3
Enter max need of resource 3 for p4: 3
Enter allocated resource of process 1:
Resource 1---->0
Resource 2---->1
Resource 3---->0
Enter allocated resource of process 2:
Resource 1---->2
Resource 2---->0
Resource 3---->0
Enter allocated resource of process 3:
Resource 1---->3
Resource 2---->0
Resource 3---->2
Enter allocated resource of process 4:
Resource 1---->2
Resource 2---->1
Resource 3---->1
Enter allocated resource of process 5:
Resource 1---->0
Resource 2---->0
Resource 3---->2
Initially Available Resources:

43
Resource 1---->10
Resource 2---->5
Resource 3---->7
PROCESS MAXIMUM ALLOCATION NEED
p0 7 5 3 0 1 0 7 4 3
p1 3 2 2 2 0 0 1 2 2
p2 9 0 2 3 0 2 6 0 0
p3 2 2 2 2 1 1 0 1 1
p4 4 3 3 0 0 2 4 3 1
Current Status of availability:
Resource 1---->3
Resource 2---->3
Resource 3---->2
The system is not in safe state
The sequence is: p0 p1 p2 p3 p4
The system is in safe state
The safe sequence is: p3 p1 p4 p2 p0
Any Request for resources?(y/n):y
Which process is requesting resources: p1
Enter the request of p1: 1
0
2
Requested resources are allocated.
PROCESS MAXIMUM ALLOCATION NEED
p3 2 2 2 2 1 1 0 1 1
p1 3 2 2 3 0 2 0 2 0
p4 4 3 3 0 0 2 4 3 1
p2 9 0 2 3 0 2 6 0 0
p0 7 5 3 0 1 0 7 4 3
Resource 1---->2
Resource 2---->3

44
Resource 3---->0
Any Request for resources?(y/n):y
Which process is requesting resources: p4
Enter the request of p4: 3
3
0
Requested resources cannot be granted since the resources are not available.
Any Request for resources?(y/n): n

RESULT:
Thus the C program for deadlock avoidance is written successfully and tested with
various samples.

Ex.No: 7 IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME I


(PAGING)
Aim:
Write a ‘C’ program to simulate the paging concept.
Algorithm:
1. Get the no of pages and page size.
2. Page number and its corresponding base address is calculated.
3. Physical address of search element is calculated by,
Physical address=(base address * page size) + position of the element in the
respective block.

Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{

45
int b[4]={5,6,1,3},i,j,pn,ps,pa,ch;
int pd[10][10],se;
clrscr();
printf("enter page no\n");
scanf("%d",&pn);
printf("enter the page size\n");
scanf("%d",&ps);
printf("enter data\n");
for(i=0;i<pn;i++)
{
for(j=0;j<ps;j++)
{
scanf("%d",&pd[i][j]);
}
}
for(i=0;i<pn;i++)
{
printf("page no %d and its base address %d\n",i,b[i]);
}
while(1)
{
printf("enter the search element\n");
scanf("%d",&se);
for(i=0;i<pn;i++)
{
for(j=0;j<ps;j++)
{
if(se==pd[i][j])
{
pa=(b[i]*ps)+j;
printf("physical address is %d\n",pa);

46
}
}
}
printf("if you want to continue (enter 1/0)\n");
scanf("%d",&ch);
if(ch==1)
break;
}
getch();
}

Sample Output:
Enter the page no
2
Enter the page size
2
Enter the data
1
2
3
4
page 0 base address 5
page 1 base address 6
search elemnt
1
physical address = 11

Result:
Thus the C program for Memory management Scheme I (Paging)is written successfully
and tested with Various samples.

47
Ex.No: 8 IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME II
(SEGMENTATION)

Aim:
To write a C program to implement memory management using segmentation
Algorithm:
Step1 : Start the program.
Step2 : Read the base address, number of segments, size of each segment, memory limit.
Step3 : If memory address is less than the base address display “invalid memory limit”.
Step4 : Create the segment table with the segment number and segment address and display it.
Step5 : Read the segment number and displacement.
Step6 : If the segment number and displacement is valid compute the real address and display the
same.
Step7 : Stop the program.

Ex.No.9 IMPLEMENTING PAGE REPLACEMENT ALGORITHMS

A) First In First Out page replacement algorithm


Aim:
To implement the First In First Out page replacement algorithm using c program.
Algorithm:
A) Create a queue to hold all pages in memory
B) Insert the first page from the top of the queue.
C) Now the new pages is inserted at the tail of the queue

48
D) Check wether all the pages are full, if so replace the page.
E) When the page is required replace the page at the head of the queue
F) Program for FIFO page replacement algorithm

Program:

#include<stdio.h>
int fr[3];
void main()
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0;

for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<12;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;

49
}
}
if(flag1==0)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display();
}
printf("Number of page faults : %d ",pf);

}
void display()
{
int i;
printf("\n");

50
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}

Output

2 -1 -1
2 3 -1
2 3 -1
2 3 1
5 3 1
5 2 1
5 2 4
5 2 4
3 2 4
3 2 4
3 5 4
3 5 2 Number of page faults : 6

Result:
Thus the C program for First In First Out page replacement algorithm is written
successfully and tested with Various samples.

b) Least Recently Used Paging Algorithm

Aim:

To implement the Least Recently Used page replacement algorithm using c program.

Algorithm:

51
1. Create a queue to hold all pages in memory
2. When the page is required replace the page at the head of the queue
3. Now the new page is inserted at the tail of the queue
4. Create a stack
5. When the page fault occurs replace page present at the bottom of the stack

Program:
#include<stdio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;

for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}

52
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;

53
}
display();
}
printf("\n no of page faults :%d",pf);

}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}

OUTPUT :
2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 5 1
2 5 1
2 5 4
2 5 4
3 5 4
3 5 2
3 5 2
3 5 2
no of page faults : 4
Result:
Thus the C program for Least Recently Used Paging Algorithm is written successfully
and tested with Various samples.

54
C, LFU (LEAST FREQUENTLY USED )PAGE REPLACEMENT ALGORITHM

AIM: To implement the Least Frequently Used page replacement algorithm using c program.

ALGORITHM:

Step 1: Create a queue to hold all pages in memory


Step 2:Insert the first page from the top of the queue.
Step 3: Create a array
Step 4: When the page fault occurs replace page that will not be used for the longest
period of time

Program :
#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;

printf("Enter no of frames : ");


scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);

for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)

55
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}

56
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}
Output:

Enter no of frames :3 Enter no of pages :10 Enter page no : 2 3 4 2 1 3 7 5 4 3

57
2 -1 -1
2 3 -1
234
234
214
213
273
275
245
243
Page hit = 1

Result:
Thus the C program for LFU (Least Frequently Used )Page Replacement algorithm is
written successfully and tested with Various samples.

Ex.No 10 FILE ALLOCATION ALGORITHMS


A. Sequential file Allocation

Aim: Write a C Program to implement Sequential File Allocation method.


Algorithm:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is
big enough is encountered. It allocates that memory block for the requesting
process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can

58
be allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and
allocates it to the process.
Step 7: Analyses all the three memory management techniques and display the best
algorithm which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int st[20],b[20],b1[20],ch,i,j,n,blocks[20][20],sz[20];
char F[20][20],S[20];
clrscr();
printf("\n Enter no of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name :",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb):",i+1);
scanf("%d",&sz[i]);
printf("\n Enter Starting block of %d:",i+1);
scanf("%d",&st[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
b1[i]=(sz[i]*1024)/b[i];

59
for(i=0;i<n;i++)
{
for(j=0;j<b1[i];j++)
blocks[i][j]=st[i]+j;
}
do
{
printf("\n Enter the Filename:");
scanf("%s",S);
for(i=0;i<n;i++)
{
if(strcmp(S,F[i])==0)
{
printf("\n Fname \t Start \t Nblocks \t Blocks \n");
printf("\n-----------------------------------------------\n");
printf("\n %s \t %d \t %d \t",F[i],st[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue : (y:n)?");
scanf("%d",&ch);
if(ch!=1)
break;
}while(1);
}

Input and Output:


Enter no of Files:2

60
Enter file 1 name:x.c
Enter file1 size(in kb):4
Enter Starting block of 1:100
Enter blocksize of File1(in bytes):512
Enter file 2 name :y.c
Enter file2 size(in kb):2
Enter Starting block of 2:500
Enter blocksize of File2(in bytes):256
Enter the Filename :y.c
Fname Start Nblocks Blocks
---------------------------------------------
y.c 500 8 500->501->502->503->504-
>505->506->507->
---------------------------------------------
Do U want to continue :(y:n)? n

Result:
Thus the C program for Sequential files Allocation Method is written successfully and
tested with Various samples.

B.Linked file Allocation Method

AIM: Write a C Program to implement Linked File Allocation method.

ALGORITHM:

Step 1: Create a quee to hold all pages in memory


Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue

61
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack
Step 6: Stop the allocation.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
int sb[20],eb[20],x;
clrscr();
printf("\n Enter no of Files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name :",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb):",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes):",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n Enter Starting block of file %d:",i+1);
scanf("%d",&sb[i]);

62
printf("\n Enter Ending block of file %d:",i+1);
scanf("%d",&eb[i]);
printf("\n Enter blocks for file %d:\n",i+1);
for(j=0;j<b1[i]-2;)
{
printf("\n Enter the %d block :",j+1);
scanf("%d",&x);
if(x>sb[i]&&x<eb[i])
{
blocks[i][j]=x;
j++;
}
else
printf("\n Invalid block:");
}
}
do
{
printf("\n Enter the Filename :");
scanf("%s",&S);
for(i=0;i<n;i++)
{
if(strcmp(F[i],S)==0)
{
printf("\n Fname \t Fsize \t Bsize \t Nblocks \t Blocks \n");
printf("\n--------------------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
printf("%d->",sb[i]);
for(j=0;j<b1[i]-2;j++)
printf("%d->",blocks[i][j]);
printf("%d->",eb[i]);

63
}
}
printf("\n--------------------------------------------------------\n");
printf("\nDo U want to continue (y:n)? ");
scanf("%d",&ch);
}
while(ch!=0);
}

Input and Output:


Enter file1 size(in kb):1
Enter blocksize of File1(in bytes):512
Enter file 2 name :sudee
Enter file2 size(in kb):1
Enter blocksize of File2(in bytes):1024
Enter Starting block of file:1100
Enter Ending block of file:1600
Enter blocks for file1:
Enter the 1block :102
Enter the 2block :104
Enter Starting block of file:2200
Enter Ending block of file:2500
Enter blocks for file2:
Enter the 1block :201
Enter the Filename :daya
Fname Fsize Bsize Nblocks Blocks
---------------------------------------------
daya 1 512 2 100->102->104-
>600->
---------------------------------------------
Do U want to continue ::(y:n)?1

64
Enter the Filename:sudee
Fname Fsize Bsize Nblocks Blocks
---------------------------------------------
sudee 1 1024 1 200->201->500->
---------------------------------------------
Do U want to continue :(y:n)? 0
Result:

Thus the C program for Linked file Allocation Method is written successfully and tested with
Various samples.

C.Indexed file Allocation Method

AIM: Write a C Program to implement Indexed File Allocation method.

Algorithm:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required

65
Step 11: Terminate the process.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
clrscr();
printf("\n Enter no. of Files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name :",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb):",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes):",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n \nEnter blocks for file%d",i+1);
for(j=0;j<b1[i];j++)
{
printf("\n Enter the %dblock :",j+1);
scanf("%d",&blocks[i][j]);

66
}
}
do
{
printf("\nEnter the Filename :");
scanf("%s",&S);
for(i=0;i<n;i++)
{
if(strcmp(F[i],S)==0)
{
printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}
}
printf("\n---------------------------------------------\n");
printf("\n Do U want to continue :(y:n)?");
scanf("%d",&ch);
}
while(ch!=0);
}

Input and Output:


Enter no of Files :2
Enter file 1 name :x.c
Enter file1 size(in kb):1
Enter blocksize of File1(in bytes):512
Enter file 2 name :y.c
Enter file2 size(in kb):1

67
Enter blocksize of File2(in bytes):512
Enter blocks for file1
Enter the 1block :1000
Enter the 2block :1001
Enter blocks for file2
Enter the 1block :2000
Enter the 2block :2001
Enter the Filename :x.c
Fname Fsize Bsize Nblocks Blocks
---------------------------------------------
x.c 1 512 2 1000->1001->
---------------------------------------------
Do U want to continue :(y:n)? 1
Enter the Filename :y.c
Fname Fsize Bsize Nblocks Blocks
---------------------------------------------
y.c 1 512 2 2000->2001->
---------------------------------------------
Do U want to continue :(y:n)? 0

Result:

Thus the C program for Indexed file Allocation Method is written successfully and tested with
Various samples.
Ex.No: 11 WORKING WITH FILE SYSTEM COMMANDS

Aim:
To write a commands to work with file system in UNIX.
Commands:
awk -search for and process patterns in a file,
cat -display, or join, files

68
cd -change working directory
chgrp -change the group that is associated with a file
chmod -change the access mode of a file
chown -change the owner of a file
comm -compare sorted files
cp -copy files
df -display the amount of available disk space
diff -display the differences between two files
du -display information on disk usage
file -display file classification
find -find files
fsck -check and repair a file system
grep -search for a pattern in files
head -display the first few lines of a file
ln -create a link to a file
lp -print files (System V)
lpr -print files (Berkeley)
ls -list information about files
mkdir -create a directory
more -display a file one screen at a time (System V)
mv -move and/or rename a file
od -dump a file
pg -display a file one screen at a time (Berkeley)
pr -paginate a file
pwd -print the working directory
rm -remove (delete) files
rmdir -remove (delete) a directory
sed -stream editor (non-interactive)
sort -sort and/or merge files
spell -check a file for spelling errors
tail -display the last few lines of a file

69
tar -store or retrieve files from an archive file
umask -set file creation permissions
uniq -display the lines in a file that are unique
wc -counts lines, words and characters in a file
whatis -list man page entries for a command
whereis -show where executable is located in path
which -locate an executable program using "path"

Basic Commands:

Command : Who
Purpose : It is used to get the information about all the users currently working in
the system.
Syntax : who
Example : $ who

Command : Who am i
Purpose : It is used to know in which terminal the user is currently logged on.
Syntax : who am i
Example : $ who am I

Command : Date
Purpose : It is used to display the system date and time.
Syntax : date
Example : $ date

Command : Cal
Purpose : It prints the calender for the specified year and month.
Syntax : cal <month> <year>
Example : $ cal 05 2003

70
Command : Id
Purpose : It is used to display the login name.
Syntax : id
Example : $ id

Command : Clear
Purpose : It is used to clear the screen.
Syntax : clear
Example : $ clear

Command : Uname
Purpose : It is used to display the details about the OS in which we are working.
Syntax : uname [options]
Example : $ uname –n

Command : Tty
Purpose : It is used to know the terminal name on which we work.
Syntax : tty
Example : $ tty

Command : Pwd
Purpose : It is used to display the absolute pathname of current working directory.
Syntax : pwd
Example : $ pwd

Command : Bc
Purpose : It is used to perform simple mathematical calculations.
Syntax : bc <operation>
Example : $ bc 3+5 8 ^d

71
Command : Ls
Purpose : It is used to display the files in the current working directory.
Syntax : ls [options] <arguments>
Example : $ ls –p

Command : Echo
Purpose : It echoes the argument on the standard output device.
Syntax : echo [options] <string>
Example : $ echo ‘BOOM’

Command : Man
Purpose : It gives details about the unix commands.
Syntax : man < command name >
Example : $ man echo

Directory Manipulation Commands:

Command : mkdir
Purpose : It is used to create new directory or more than one directory.
Syntax : mkdir <directory name >
Example : $ mkdir booma

Command : rmdir
Purpose : It is used to remove the directory if it is empty.
Syntax : rmdir <directory name >
Example : $ rmdir booma

Command : cd
Purpose : It is used to change the control from one working directory to another
specified directory.
Syntax : cd <directory name >

72
File Manipulation Commands:

Command : Cat
Purpose : It is used create a new file and write content to that file.
Syntax : cat > <file name >
Example : $ cat >devi

Command : Cat
Purpose : It is used to display the contents of the file .
Syntax : cat <file name >
Example : $ cat devi

Command : More
Purpose : It is used to display the contents of the file on the screen at a time.
Syntax : more <file name >
Example : $ more devi

Command : Wc
Purpose : It is used to count the number of lines ,words and characters in a file or
group of files.
Syntax : wc [options] <file name >
Example : $ wc –l devi

Command : File
Purpose : It is used to determine the type of the file.
Syntax : file <file name >
Example : $ file devi

Command : Spell
Purpose : It is used to find the spelling errors in the file.
Syntax : spell [options] <file name >

73
Example : $ spell -b devi

Command : Split
Purpose : It is used to split the given file into smaller pieces of given size.
Syntax : split –size <file name > < splitted file name >
Example : $ split –2 devi de

Command : Cp
Purpose : It is used to copy one or more files.
Syntax : cp <source file name > <destination file name>
Example : $ cp devi latha

Command : Mv
Purpose : It is used to move a file within a directory with different names and also
used to move a file to different directory with its original name.
Syntax : mv <source file name > <destination file name>
Example : $ mv devi jeya

Command : Rm
Purpose : It is used to remove a file from the disk.
Syntax : rm <file name >
Example : $ rm devi

Filter Commands:

Command : Head
Purpose : It is used to display the top portion of the file.
Syntax : head [options] <file name>
Example : $ head -5 devi

74
Command : Tail
Purpose : It is used to display the bottom portion of the file.
Syntax : tail [options] <file name >
Example : $ tail –5 devi

Command : Pr
Purpose : It is used to display the contents of the file by separating them into pages
and each page begins with the header information.
Syntax : pr [options] <file name >
Example : $ pr devi

Command : Cut
Purpose : It is used to extract selected fields or columns from each line of one or
more files and display them on the standard output device.
Syntax : cut [options] <file name >
Example : $ cut –c5 devi

Command : Paste
Purpose : It concatenates the line from each input file column by column with tab
characters in between them.
Syntax : paste [options] <file name >
Example : $ paste f1 f2

Command : Join
Purpose : It is used to extracts common lines from two sorted files and there should
be the common field in both file.
Syntax : join [options] <file name1 > <file name 2>
Example : $ join –a1 f1 f2

Command : Uniq
Purpose : It compares adjacent lines in the file and displays the output by

75
eliminating duplicate adjacent lines in it.
Syntax : uniq [options] <file name >
Example : $ uniq -c devi

Command : Sort
Purpose : It sorts one or more files based on ASCII sequence and also to merge the
file.
Syntax : sort [options] <file name >
Example : $ sort -r devi

Command : Nl
Purpose : It is used to add the line numbers to the file.
Syntax : nl [options] [filename]
Example : $ nl devi

Command : Tr
Purpose : It is used to translate or delete a character or a string from the standard
input to produce the required output.
Syntax : tr [options] <string1> <string2>
Example : $ tr –s “a” “b” < devi

Command : grep
Purpose : It is used to search the specified pattern from one or more files.
Syntax : grep [options] <pattern> <file name >
Example : $ grep “anand” devi

Result:
Thus various commands in unix was executed and the output was verified.

76
77

Вам также может понравиться