Amjathfinal
Amjathfinal
EX.NO:1
DATE:
AIM:
To write a code using Basic UNIX Commands and perform the following execerise
EXERCISE COMMAND:
Description:
ii) Nested Directories: You can use mkdir to create nested directories,
meaning directories within directories. This allows you to create a hierarchical
directory structure.
iii) Parent Directory: When creating nested directories, it's essential to ensure that
the parent directory (in this case, "CSE") exists before creating its child directories
("UG" and "PG").
Output:
1
21IT1302-OPERATING SYSTEM 211422205021
2. Create files under cse with extensions like *.txt, *.c etc..
Description:
Creating and Writing Files with cat Command: You can use the cat
command to both create and write content to files. To create files with
specific extensions, you can use redirection to send content into new
files.
Output:
Description:
l s c o m m a n d : When combined with the * . t x t pattern, the ls command
will list all files with the ".txt" extension in the specified directory.
Output:
2
21IT1302-OPERATING SYSTEM 211422205021
Output:
Description:
cp Command: The cp command is used to copy files and directories in UNIX.
It allows you to duplicate files while preserving their content.
Output:
3
21IT1302-OPERATING SYSTEM 211422205021
Description:
Output:
7. Display all the files with 2 characters filename and extension “.dat”.
Description:
l s c o m m a n d : When combined with the * . t x t pattern, the ls command
will list all files with the ".txt" extension in the specified directory.
Output:
4
21IT1302-OPERATING SYSTEM 211422205021
8. Create a file name with list of subject names and Display the first three
and last three subjects stored in the file.
Description:
i) head Command: The head command is used to display the beginning (head) of a
text file. By default, it displays the first ten lines of a file, but you can specify a different
number of lines to display.
ii) tail Command: The tail command is used to display the end (tail) of a text file.
Like head, it also defaults to displaying the last ten lines of a file, but you can specify a
different number of lines.
Output:
5
21IT1302-OPERATING SYSTEM 211422205021
9. List the contents of the file from the fourth subject from the end of the
file and also list the contents of the file from the seventh subject from the
beginning of the file.
Description:
i) head Command: The head command is used to display the beginning (head) of a
text file. By default, it displays the first ten lines of a file, but you can specify a
different number of lines to display.
ii) Tail command: The tail command is used to display the end (tail) of a text file.
Like head, it also defaults to displaying the last ten lines of a file, but you can specify a
different number of lines.
Output:
10. Create the file named INDIA which contains the following
data India is my country
All Indians are my brothers and
SisterI love my country
Description:
Filter Commands: Filter commands like grep, awk, and sed are used to
process and manipulate text data. They are particularly useful for searching, extracting,
or transforming text based on specified patterns or conditions.
Output:
a) Display all the lines that do not contain the above pattern
Description:
Filter Commands: Filter commands like grep, awk, and sed are used to
process and manipulate text data. They are particularly useful for searching,
extracting, or transforming text based on specified patterns or conditions.
Output:
6
21IT1302-OPERATING SYSTEM 211422205021
Description:
i) Using grep to Find Lines with "my": You can use the grep command to search for
lines that contain the pattern "my" in a text file. The -c option of grep can be used to
count the number of lines that match the pattern.
ii) Piping to wc for Counting: To count the lines, you can pipe the output of grep into
the wc command using the pipe operator |. Specifically, you will use wc -l to count the
number of lines in the filtered output.
Output:
7
21IT1302-OPERATING SYSTEM 211422205021
11) Create a file with 15 lines of data.Using filter commands select top 10
lines,last 8 lines and display them in two different files.Add line numbers to
the files.
Description:
i) head Command: The head command is used to display the beginning (head) of a
text file. By default, it displays the first ten lines of a file, but you can specify a
different number of lines to display.
ii) Tail command: The tail command is used to display the end (tail) of a text file.
Like head, it also defaults to displaying the last ten lines of a file, but you can specify a
different number of lines.
Output:
8
21IT1302-OPERATING SYSTEM 211422205021
Wc for Counting: To count the lines, you can pipe the output of grep into the wc
command using the pipe operator |. Specifically, you will use wc -l to count the number
of lines in the filtered output.
Output:
Description:
14) Change the modes of those files which began with the letter “s” in such a
way that the owner has read and execute permission, the group has read and
9
21IT1302-OPERATING SYSTEM 211422205021
10
21IT1302-OPERATING SYSTEM 211422205021
15) Create a file called MARK which contains the sample data as follows:
S001 RAJA dbms 78
S002 Ushaos 96
b) Display the names of the students in the alphabetic order ignoring the
cases
11
21IT1302-OPERATING SYSTEM 211422205021
Description:
sort Command: The sort command is used to sort lines of text files in various ways,
including alphabetically.
Output:
c) Display the list of the students who have scored marks between 50
and 80
Description:
awk Command: The awk command is a versatile text processing tool that
allows you to perform various operations on text files, including filtering and
manipulating data.
Output:
Description:
awk Command: The awk command is a versatile text processing tool that allows you
to extract and manipulate data from text files.
Output:
e) Sort the files ccording to the third field & dump it to the file called
SCODE
Description:
i) sort Command: The sort command is used to sort lines of text files in
various ways, including sorting based on specific fields.
ii) Sorting by Field: You can use the -k option with sort to specify the field
number that you want to use as the sorting key. In this case, you want to
sort based on the third field.
12
21IT1302-OPERATING SYSTEM 211422205021
Output:
13
21IT1302-OPERATING SYSTEM 211422205021
a) Sort the file on the employee's departement and display the name of
theemployee and the departement
Description:
i) sort Command: The sort command is used to sort lines of text files based
on specified fields.
ii) awk Command: The awk command is used for text processing
and manipulation, including field extraction and formatting.
Output:
14
21IT1302-OPERATING SYSTEM 211422205021
Description:
c) Sort the file on the employee name in the reverse order and extract their
codes and their names.
Description:
i) sort Command: The sort command is used to sort lines of text files based
on specified fields.
ii) awk Command: The awk command is used for text processing
and manipulation, including field extraction and formatting.
Output:
order Description:
sort Command: The sort command is used to sort lines of text files based on
specified fields.
Output:
15
21IT1302-OPERATING SYSTEM 211422205021
17) Display the permission of the group for the files whose names begin
with”p”
Description:
i) l s c o m m a n d : When combined with the * . t x t pattern, the ls command
will list all files with the ".txt" extension in the specified directory.
ii) grep Command: The grep command is used to search for patterns in text.
In this case, we use it to filter filenames that begin with "p."
Output:
18) Give the command to extract logins of the people whose login namestarts with
“panimalar”
Description:
grep Command: The grep command is used for searching text using regular expressions. It can
filter lines that match specific patterns in a text file.
16
21IT1302-OPERATING SYSTEM 211422205021
Output:
Output:
20) Give the command to extract the links,the file owner and the file
nameonly in the current directory.
17
21IT1302-OPERATING SYSTEM 211422205021
Description:
Output
RESULT:
Thus the Basic UNIX Commands are successfully
18
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:02
DATE:
SYSTEM CALLS
AIM:
ALGORITHM:
Step 1: Import the necessary header files for using these functions in your C program.
Step 2: Define variables to store the parent PID (parent_pid) and child PID (child_pid).
Step 3: Inside the main function, get the parent's PID and display it. \
Step 4: Create a child process using fork().
Step 5: Check if the fork() call was successful (non-negative PID means it's the parent; 0 means
it's the child).
Step 6: Inside the child process block, get the child's PID and the parent's PID (PPID), display
them, and execute a different program using exec() (e.g., "ls -l").
PROGRAM:
19
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
PROGRAM:
//C program using exec()
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
printf("I am EXEC.c called by execvp()");
printf("\n");
return 0;
}
OUTPUT:
20
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
//C program using getppid()
#include <stdio.h>
#include <conio.h>
int main()
{
int pid;
pid = fork();
if (pid == 0)
{
printf("\nParent Process id: %d", getpid()); printf("\nChild Process with parent id:%d",
getppid());
}
return 0;
}
OUTPUT:
PROGRAM:
//C program using wait()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;
21
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
PROGRAM:
//C program using exit()
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("START");
exit(0);
printf("End of program");
}
OUTPUT:
22
21IT1302-OPERATING SYSTEM 211422205021
RESULT:
Thus the System call functions are using and executed successfully.
23
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:03
DATE:
Serve AIM:
ALGORITHM:
Step 3: For each process in the ready Q, assign the process name and the burst time
Step 4: Set the waiting of the first process as '0' and its burst time as its turnaround time
Step 6: Calculate
24
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,pid[10],at[10],bt[10],ft[10],wt[10],ta[10],i,j,t,stt=0,totta=0,totwt=0;
float avgta,avgwt;
printf("ENTER THE NO.OF PROCESSES:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pid[i]=i;
printf("\n ENTER THE ARRIVAL TIME:");
scanf("%d",&at[i]);
printf("\n ENTER THE BURST TIME:");
scanf("%d",&bt[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(at[i]>at[j])
{
t=pid[i];
pid[i]=pid[j];
pid[j]=t;
t=at[i];
at[i]=at[j];
at[j]=t;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
stt=at[1];
}
}
printf("\nTHE VALUES OF THE ARRIVAL TIME IS %d",stt);
for(i=1;i<=n;i++)
{
25
21IT1302-OPERATING SYSTEM 211422205021
ft[i]=stt+bt[i];
wt[i]=stt-at[i];
ta[i]=ft[i]-at[i];
totta=totta+ta[i];
totwt=totwt+wt[i];
stt=ft[i];
}
avgta=(float)totta/n;
avgwt=(float)totwt/n;
printf("\nPNO\tARRIVAL TIME\tBURST TIME\tCOMPLETION TIME\t\tWAIT TIME\
tTAT\n");
for(i=1;i<=n;i++) printf("%d\t\t%d\t\t%d\t\t%d\t\t\t%d\t\t%d\
n",pid[i],at[i],bt[i],ft[i],wt[i],ta[i]
);
printf("\nAVERAGE TURN AROUND TIME=%f",avgta); printf("\
nAVERAGE WAITING TIME=%f",avgwt);
}
OUTPUT:
26
21IT1302-OPERATING SYSTEM 211422205021
AIM:
ALGORITHM:
Step 1: Start the process
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time.
Step 5: Set the waiting time of the first process as '0' and its turnaround time as its burst time.
Step 8: Calculate
27
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
void main()
{
int i,j,t,n,stt=0,pid[10],at[10],bt[10],ft[10],att,wt[10],ta[10],totwt=0,totta=0;
float avgwt,avgta;
printf("ENTER THE NUMBER OF PROCESSES:");
scanf("%d",&n);
printf("\nENTER THE ARRIVAL TIME:");
scanf("%d",&att);
for(i=1;i<=n;i++)
{
pid[i]=i;
at[i]=att;
printf("\nENTER THE BURST TIME:");
scanf("%d",&bt[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(bt[i]>bt[j])
{
t=pid[i];
pid[i]=pid[j];
pid[j]=t;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
stt=att;
}
}
for(i=1;i<=n;i++)
{
ft[i]=stt+bt[i];
wt[i]=stt-at[i];
ta[i]=ft[i]-at[i];
28
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
29
21IT1302-OPERATING SYSTEM 211422205021
AIM:
To write a program to stimulate the CPU scheduling algorithm Shortest job first
(Pre- emption)
ALGORITHM:
Step 1: Start the process
Step 3: For each process in the ready Q, assign the process id and accept the CPU and
process arrival time. highest burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time
Step 5: Set the waiting time of the first process as '0' and its turnaround time as its burst time.
Step 6: Sort the processes names based on their Burt time and Arrival time
PROGRAM:
30
21IT1302-OPERATING SYSTEM 211422205021
#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10],
temp[10]; int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("Enter the Total Number of Processes:");
scanf("%d", &limit);
printf("Enter Details of %d Processes",
limit); for(i = 0; i < limit; i++)
{
printf("nEnter Arrival Time:");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end – arrival_time[smallest];
}
}
31
21IT1302-OPERATING SYSTEM 211422205021
32
21IT1302-OPERATING SYSTEM 211422205021
AIM:
ALGORITHM:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where No. of time slice for process (n)
burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
(a) Waiting time for process(n) - waiting time of process(n-1)+ burst time of process(n-
1)+the time difference in getting the CPU from process(n-1)
(b) Turnaround time for process(n) = waiting time of process(n) + burst time of process(n)
+ the time difference in getting CPU from process(n)
Step 7: Calculate
33
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
int main()
{
int i,f[10],bt[10],p[10],cbt[10],wt[10],tat[10];
int j,k,n,t,q,flag;
float atat,awt;
printf("\nRound Robin Scheduling");
printf("\nEnter the number of process:");
scanf("%d",&n);
printf("\nEnter the process burst time:");
for(i=1;i<=n;i++)
{
scanf("%d",&bt[i]);
cbt[i]=bt[i];
p[i]=i;
}
printf("\nEnter the time quantum:");
scanf("%d",&q);
for(i=1;i<=n;i++)
{
wt[i]=0;
tat[i]=0;
f[i]=0;
}
do
{
for(i=1;i<=n;i++)
{
if(f[i]==0)
{
if(bt[i]<=q)
{ f[i]=
1;
t=bt[i];
}
else
t=q;
bt[i]=bt[i]-t;
for(j=1;j<=n;j++)
34
21IT1302-OPERATING SYSTEM 211422205021
{
if(j==i||f[j]==1)
continue;
wt[j]=wt[j]+t;
}
}
}
flag=0;
for(i=1;i<=n;i++)
if(f[i]==0)
flag=1;
}while(flag==1);
atat=awt=0;
for(i=1;i<=n;i++)
{
tat[i]=wt[i]+cbt[i];
atat+=tat[i];
awt+=wt[i];
}
printf("\n\tProcess id\tBurst time\twaiting time\tturnaround time");
for(i=1;i<=n;i++) printf("\n\tP%d\t\t%d\t\t%d\t\t%d\
t",p[i],cbt[i],wt[i],tat[i]); printf("\navg.turnaround
time:%f",atat/n);
printf("\navg.waiting time:%f\n",awt/n);
return 0;
}
35
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
36
21IT1302-OPERATING SYSTEM 211422205021
3) d)Priority Scheduling
AIM:
ALGORITHM:
Step 1: Start the process
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as '0' and its burst time as its turnaround time Step 6:
Arrange the processes based on process priority
Step 9: Calculate
(b) Average Turnaround time = Total Turnaround Time / Number of process Print the results
in an order.
37
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
void main()
{
int i,j,t,n,stt=0,pid[10],pr[10],at[10],bt[10],ft[10],att;
int wt[10],ta[10],totwt=0,totta=0;
float avgwt,avgta;
printf("ENTER THE NO.OF.PROCESS:");
scanf("%d",&n);
printf("\nENTER THE ARRIVAL TIME:");
scanf("%d",&att);
for(i=1;i<=n;i++)
{
pid[i]=i;
at[i]=att;
printf("\nENTER THE BURST TIME:");
scanf("%d",&bt[i]);
printf("\nENTER THE PRIORITY OF THE PROCESS:");
scanf("%d",&pr[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i]>pr[j])
{
t=pid[i];
pid[i]=pid[j];
pid[j]=t;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=pr[i];
pr[i]=pr[j];
pr[j]=t;
}
}
}
stt=att;
38
21IT1302-OPERATING SYSTEM 211422205021
for(i=1;i<=n;i++)
{
ft[i]=stt+bt[i];
wt[i]=stt-at[i];
ta[i]=ft[i]-at[i];
if(wt[i]<0)
wt[i]=0;
totwt=totwt+wt[i];
totta=totta+ta[i];
stt=ft[i];}
avgwt=(float)totwt/n;
avgta=(float)totta/n;
printf("PNO\tARR TIME\tBURST TIME\tFINISH TIME\tWAIT TIME\tTURN
TIME\n");
for(i=1;i<=n;i++)
{
printf("\n%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d",pid[i],at[i],bt[i],ft[i],wt[i],ta[i]);
}
printf("\n THE AVERAGE WAITING TIME IS:%f\n",avgwt);
printf("\n THE AVERAGE TURN TIME IS:%f\n",avgta);
}
OUTPUT:
39
21IT1302-OPERATING SYSTEM 211422205021
RESULT:
40
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:04
DATE:
IMPLEMENTATION OF SEMAPHORES
AIM:
ALGORITHM:
Step 1: Start
Assign s=1
Return
Assign s=0
Return
Step 5: Create threads for producer and consumer function to make it run concurrently
Step 6: Stop
41
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define num_loops 5
union semun
{
int val;
struct semid_ds *buf;
short *array;
};
int main(int argc,char *argv[])
{
int semset_id;
union semun sem_val;
int child_pid;
int i;
struct sembuf sem_op;
int rc;
struct timespec delay;
semset_id=semget(IPC_PRIVATE,1,0600);
if(semset_id==-1)
{
perror("semget");
exit(1);
}
printf("SEMAPHORE SET CREATED SEMAPHORE SET ID%d\n",semset_id);
sem_val.val=0;
rc=semctl(semset_id,0,SETVAL,sem_val);
child_pid=fork();
switch(child_pid)
{
case 1:perror("fork");exit(1);
case 0:for(i=0;i<num_loops;i++)
{
42
21IT1302-OPERATING SYSTEM 211422205021
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(semset_id,&sem_op,1);
printf("Consumer consumed item %d\n",i);
fflush(stdout);
}
break;
default:for(i=0;i<num_loops;i++)
{
printf("Producer produced item %d\n",i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(semset_id,&sem_op,1);
if(rand()>3*(RAND_MAX/4))
{
delay.tv_sec=0;
delay.tv_sec=10;
nanosleep(&delay,NULL);
}
}
break;
}
return 0;
}
43
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
RESULT:
Thus the producer and consumer problem using semaphores was executed
successfully.
44
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:05
DATE:
AIM:
To develop a client-server application program, this uses shared memory using IPC
ALGORITHM:
Server
Step3: Create a shared memory segment using shmget with key & IPC CREAT as
parameter. Step4: If shared memory identifier shmid is -1, then stop.
Step6: Attach server process to the shared memory using shimmat with shmid as parameter.
Step9. Write a-z onto the shared memory. Step10 Wait till client reads the shared
memory contents
Step11: Detatch process from the shared memory using shimdt system call
Step12: Remove shared memory from the system using shmeti with IPC_RMID argument
Step13: Stop
Client
45
21IT1302-OPERATING SYSTEM 211422205021
Step3: Obtain access to the same shared memory segment using same
key, If obtained then display the shmid else print "Server not started"
Step4: Attach client process to the shared memory using shimmat with shmid as parameter. If
pointer to the shared memory is not obtained, then stop.
Step7: Stop
PROGRAM:
SENDER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<string.h>
#define msgsz 50
typedef struct msgbuffer
{
long mtype;
char mtext[msgsz];
}mbuffer;
main()
{
int msgflg=IPC_CREAT|0666;
key_t key;
int msqid,buffer;
mbuffer mb;
key=1234;
if((msqid=msgget(key,msgflg))<0)
{
perror("msgget");
return 1;
}
else
46
21IT1302-OPERATING SYSTEM 211422205021
mb.mtype=1;
strcpy(mb.mtext," HAI TO ALL ");
buffer=strlen(mb.mtext);
if(msgsnd(msqid,&mb,buffer,msgflg)<0)
{
printf("%d%ld%s%d",msqid,mb.mtype,mb.mtext,buffer);
perror("msgsnd");
return 1;
}
else
printf(" MESSAGE : %s IS SENT ",mb.mtext);
return 0;
}
OUTPUT:
RECEIVER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<stdio.h>
#define msgsz 70
typedef struct msgbuffer{
long mtype;
char mtext[msgsz];
}
mbuffer;
main()
{
key_t key;
int msqid,buffer;
mbuffer mb;
key=1234;if((ms
qid=msgget(key,0
47
21IT1302-OPERATING SYSTEM 211422205021
666))<0)
{
perror("msgget");
return 0;
}
if(msgrcv(msqid,&mb,msgsz,1,0)<0)
{
perror("msgrev");
return 1;
}
printf(" MESSAGE : %s IS RECEIVED\n",mb.mtext);
return 0;
}
OUTPUT:
48
21IT1302-OPERATING SYSTEM 211422205021
RESULT:
Thus the inter process communication using IPC was executed successfully.
49
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:06)i)
DATE:
AIM:
ALGORITHM:
Step1: Start the program.
Step8: If the new request comes then check that the system is in
Step11: end
50
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.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("********** Banker's Algo ************\n");
input();
show();
cal();
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]);
51
21IT1302-OPERATING SYSTEM 211422205021
}
}
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++)
{
52
21IT1302-OPERATING SYSTEM 211422205021
finish[i]=0;
}
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++)
53
21IT1302-OPERATING SYSTEM 211422205021
{
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");
}
}
54
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
55
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:06)ii)
DATE:
AIM:
ALGORITHM 1:
Simply detects the existence of a Cycle:
Step3: Until a vertex repeats (there is a cycle) or one cannot continue (there is no cycle)
Step4: Stop.
ALGORITHM 2:
On a copy of the graph:
Step2: If so satisfy the needs with holds and remove that Process and all the Resources it
holds from the graph.
Step4: If all Processes are finally removed by this procedure there is no Deadlock in the original
graph, if not there is.
Step5: Stop.
56
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.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 Algorithm ************\n");
input();
show();
cal();
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]);
}
57
21IT1302-OPERATING SYSTEM 211422205021
58
21IT1302-OPERATING SYSTEM 211422205021
{
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;
}
}
}
}
}
}
j=0;
flag=0; for(i=0;i<n;i+
+)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
59
21IT1302-OPERATING SYSTEM 211422205021
}
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");
}
}
60
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
61
21IT1302-OPERATING SYSTEM 211422205021
RESULT:
Thus the Deadlock Detection and Avoidance using Bankes Algorithm was
executed successfully.
62
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:07
DATE:
AIM:
ALGORITHM:
1. Start the program.
5. If not, start at the hole that is sharing the previous best fit search end.
PROGRAM:
63
21IT1302-OPERATING SYSTEM 211422205021
#include <stdio.h>
int main() {
int p[10], np, b[10], nb, ch, c[10], d[10], alloc[10], flag[10] = {0}, i, j;
do {
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nFirst Fit\n");
for (i = 0; i < np; i++) {
for (j = 0; j < nb; j++) {
if (p[i] <= b[j] && flag[j] == 0) {
alloc[j] = p[i];
printf("\nAlloc[%d]: %d", j, alloc[j]);
printf("Process %d of size %d is allocated in block %d of size %d\n", i,
p[i], j, b[j]);
flag[j] = 1;
64
21IT1302-OPERATING SYSTEM 211422205021
break;
}
}
if (flag[i] == 0) {
printf("Process %d of size %d is not allocated\n", i, p[i]);
}
}
break;
case 2:
printf("\nBest Fit\n");
for (i = 0; i < nb; i++) {
for (j = i + 1; j < nb; j++) {
if (c[i] > c[j]) {
int temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
printf("After sorting block sizes:\n");
for (i = 0; i < nb; i++) {
printf("Block %d: %d\n", i, c[i]);
}
for (i = 0; i < np; i++) {
for (j = 0; j < nb; j++) {
if (p[i] <= c[j] && flag[j] == 0)
{ alloc[j] = p[i];
printf("\nAlloc[%d]: %d", j, alloc[j]);
printf("Process %d of size %d is allocated in block %d of size %d\n", i,
p[i], j, c[j]);
flag[j] = 1;
break;
}
}
if (flag[i] == 0) {
printf("Process %d of size %d is not allocated\n", i, p[i]);
}
}
break;
case 3:
65
21IT1302-OPERATING SYSTEM 211422205021
printf("\nWorst Fit\n");
for (i = 0; i < nb; i++) {
for (j = i + 1; j < nb; j++) {
if (d[i] < d[j]) {
int temp = d[i];
d[i] = d[j];
d[j] = temp;
}
}
}
printf("After sorting block sizes:\n");
for (i = 0; i < nb; i++) {
printf("Block %d: %d\n", i, d[i]);
}
for (i = 0; i < np; i++) {
for (j = 0; j < nb; j++) {
if (p[i] <= d[j] && flag[j] == 0) {
alloc[j] = p[i];
printf("\nAlloc[%d]: %d", j, alloc[j]);
printf("Process %d of size %d is allocated in block %d of size %d\n", i,
p[i], j, d[j]);
flag[j] = 1;
break;
}
}
if (flag[i] == 0) {
printf("Process %d of size %d is not allocated\n", i, p[i]);
}
}
break;
default:
printf("Invalid Choice...!\n");
break;
}
} while (ch <= 3);
} else {
printf("Number of processes exceeds the number of blocks. Unable to allocate.\n");
}
return 0;
}
66
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
67
21IT1302-OPERATING SYSTEM 211422205021
RESULT:
Thus the implementation of Memory Allocation Strategies for First Fit,Best Fit,Worst Fit
programs was successfully executed
68
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:08
DATE:
AIM:
ALGORITHM:
Step1: Read all the necessary input from the keyboard.
Step4: Calculate the physical address using the following Physical address = ( Frame number *
Frame size) + offset
69
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
int main()
{
int ms, ps, nop, np, rempages, i, j, x, y
, pa, offset;
int s[10], fno[10][20]; printf("\
nEnter the memory size --");
scanf("%d",&ms);
printf("\nEnter the page size --");
scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are --%d ",nop);
printf("\nEnter number of processes --");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
printf("\nEnter no. of pages required for p[%d]--",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages -s[i]; printf("\
nEnter pagetable for p[%d] ---",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\nEnter Logical Address to find Physical Address "); printf("\
nEnter process no. and pagenumber and offset --"); scanf("%d %d
%d",&x,&y, &offset);
if(x>np || y>=s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;
70
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
RESULT:
Thus the implementation of Paging Techniques programs was successfully executed.
71
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:09
DATE:
EX.NO:09)i)
FIFO page replacement algorithm
AIM:
ALGORITHM:
Step1:Start the process
Step6: Replace the page with circular queue, while re-placeing check page availability in the
frame
72
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("\
n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no; count+
+; for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
OUTPUT:
73
21IT1302-OPERATING SYSTEM 211422205021
74
21IT1302-OPERATING SYSTEM 211422205021
75
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:09)ii)
LRU page replacement algorithm
AIM:
ALGORITHM:
Step1:Start the process
76
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10],
flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){ counter+
+;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
77
21IT1302-OPERATING SYSTEM 211422205021
78
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
79
21IT1302-OPERATING SYSTEM 211422205021
80
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:09)iii)
LFU page replacement algorithm
AIM:
ALGORITHM:
Step1:Start Program
Frame
Stepб:If No Frames Is Free Replace The Page With The Page that is least Used
Step8:Stop process.
PROGRAM:
#include<stdio.h>
int n;
main()
{
int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s,pf=0;
int count=1,p=0;
float pfr;
printf("enter max limit of the sequence:");
scanf("%d",&max);
printf("enter the sequence:");
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
printf("enter the no of frames:");
81
21IT1302-OPERATING SYSTEM 211422205021
scanf("%d",&n);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j])
flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count] );
count++;
pf++;
} i+
+;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1; for(j=0;j<n;j+
+)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
82
21IT1302-OPERATING SYSTEM 211422205021
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=-1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==-1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==-1)
{
s=k;
break;
}
}
}
pf++;
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\n theno of page faults are:%d",pf);
printf("\n page fault rate:%f",pfr);
83
21IT1302-OPERATING SYSTEM 211422205021
}
int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}
OUTPUT:
84
21IT1302-OPERATING SYSTEM 211422205021
RESULT:
Thus the implementation of Paging Techniques programs was successfully executed.
85
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:10)i)
DATE:
AIM:
ALGORITHM:
Step 1:Start
Step 3: Initialize graph function as Initgraph(& gd, &gm," c:/tc/bgi"), Clear device():
Step 8: mid=640/count;
PROGRAM:
#include<stdio.h>
86
21IT1302-OPERATING SYSTEM 211422205021
#include<stdlib.h>
#include<string.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\
t5. Exit\nEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
87
21IT1302-OPERATING SYSTEM 211422205021
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++) printf("\t
%s",dir.fname[i]);
}
break;
case 5: exit(0);
break;
default:
printf("\nterminate");
}
}
}
88
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
89
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:10)ii)
Two Level Directory Structure
AIM:
ALGORITHM:
Step 1:Start
root; root-NULL;
dname; fflush(stdin):
gets((root)->name);
Step 12: if check (lev=0 lev-1) if check(root)->level=0 print "how many users else print "how
many files" print (root)->name
read (root)->nc
create(&((root)->link[i]),lev+1,(root)->name,
Ix+gap*i,lx+gapi gap,lx+gap*i+gap/2),
then
(root)->nc 0;
Step 15: Initialize e display function Initialize I set textstyle(2,0.4); set textjustify(1,1); set
fillstyle(1,BLUE); setcolor(14); step 13:if check root!=NULL
Step 19:End
PROGRAM:
#include<stdio.h>
struct
{
91
21IT1302-OPERATING SYSTEM 211422205021
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
92
21IT1302-OPERATING SYSTEM 211422205021
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f); for(k=0;k<dir[i].fcnt;k+
+)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
93
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
94
21IT1302-OPERATING SYSTEM 211422205021
95
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:10)iii)
ALGORITHM:
Step 1:Start
Then
Gap=rx-lx/*root->nc
Then rot->nc-0
Step 10: display the directory tree in graphical mood Display nood *root
96
21IT1302-OPERATING SYSTEM 211422205021
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];
};
void main()
int gd=DETECT,gm;
node*root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr(); initgraph(&gd,&gm,"..\\
bgi"); display(root);
getch();
closegraph();
97
21IT1302-OPERATING SYSTEM 211422205021
int i,gap;
if(*root==NULL)
(*root)=(node*)malloc(sizeof(node));
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)
else
98
21IT1302-OPERATING SYSTEM 211422205021
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;
return;
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);
99
21IT1302-OPERATING SYSTEM 211422205021
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]);
return;
OUTPUT:
100
21IT1302-OPERATING SYSTEM 211422205021
RESULT:
Thus the C programs was successfully executed.
101
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:11
DATE:
AIM:
ALGORITHM:
Step 1: Start the program. Step 2: Get the number of files. Step 3: Get the memory requirement
of each file.
Step 4: Allocate the required locations to each in sequential order a) Randomly select a location
from available location
s1= random(100); b) Check whether the required locations are free from the selected location.
if(b[sl] flag-0)
PROGRAM:
102
21IT1302-OPERATING SYSTEM 211422205021
#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];
for(i=0;i<n;i++)
{
for(j=0;j<b1[i];j++)
blocks[i][j]=st[i]+j;
}
do
{
printf("\nEnter the Filename ::");
scanf("%s",S);
for(i=0;i<n;i++)
{
if(strcmp(S,F[i])==0)
103
21IT1302-OPERATING SYSTEM 211422205021
{
printf("\nFname\tStart\tNblocks\tBlocks\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);
}
OUTPUT:
104
21IT1302-OPERATING SYSTEM 211422205021
105
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:11)ii)
Index File Allocation
AIM:
ALGORITHM:
Step 1: Start the program.
Step 3: Get the memory requirement of each file. Step 4: Allocate the required locations by
selecting a location randomly q= random(100),
locations. q=random(100);
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 ::");
106
21IT1302-OPERATING SYSTEM 211422205021
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]);
}}
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("\nDo U want to continue ::(Y:n)");
107
21IT1302-OPERATING SYSTEM 211422205021
scanf("%d",&ch);
}while(ch!=0);
}
OUTPUT:
108
21IT1302-OPERATING SYSTEM 211422205021
109
21IT1302-OPERATING SYSTEM 211422205021
EX.NO:11)iii)
Linked File Allocation.
AIM:
To allocate the files in the secondary storage using Linked allocation techniques
ALGORITHM:
Step 1: Start the program.
b) If the location is free allocate and set flag-1 to the allocated locations. While allocating
next location address to attach it to previous location
for(i=0;i<n;i++) {
{ p=r[i][j-1]; b[p].next=q; }
110
21IT1302-OPERATING SYSTEM 211422205021
PROGRAM:
#include<stdio.h>
void main()
{
char a[10];
int i,sb,eb,fb1[10];
printf("\n enter the file name:");
scanf("%s",a);
printf("\n Enter the starting block:");
scanf("%d",&sb);
printf("Enter the ending Block:");
scanf("%d",&eb);
for(i=0;i<5;i++)
{
printf("Enter the free block %d",i+1);
scanf("%d",&fb1[i]);
}
printf("\n File name \t Starting block \t Ending block \n");
printf("%s \t\t %d\t\t %d",a,sb,eb);
printf("\n %s File Utilization of Linked type of following blocks:",a);
printf("\n %d->",sb);
for(i=0;i<5;i++)
{
printf("%d->",fb1[i]);
}
printf("%d\n",eb);
}
111
21IT1302-OPERATING SYSTEM 211422205021
OUTPUT:
RESULT:
Thus the implementation of Paging Techniques programs was successfully executed.
112
21IT1302-OPERATING SYSTEM 211422205021
113