OS Lab Manual
OS Lab Manual
Course Outcomes:
At the end of the course, the student will be able to:
2 Simulate the following CPU scheduling algorithms to find turnaround time CO2 8-16
and waiting time a) FCFS b) SJF c) Round Robin d) Priority.
6 Develop a C program to simulate the following contiguous memory allocation CO4 26-31
Techniques:
a) Worst fit b) Best fit c) First fit.
7 Develop a C program to simulate page replacement algorithms: CO4 32-36
a) FIFO b) LRU
9 Develop a C program to simulate the Linked file allocation strategies. CO5 45-46
1 Write a Shell program to check the given number is even or odd CO1 50
2 Write a Shell program to check the given year is leap year or not CO1 51
i) fork()
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
else
{
printf("Error while forking\n");
exit(EXIT_FAILURE);
}
return 0;
}
Output:
$make fork
cc fork.c -o fork
And running the script, we get the result as below screenshot.
$ ./fork
It is the parent process and pid is 18097
It is the child process and pid is 18098
ii) exec()
In C programming on Linux and Ubuntu, consider the following example: We have two c files example.c and
hello.c.
Code
example.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
hello.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
OUTPUT:
#include<stdio.h> // printf()
#include<stdlib.h> // exit()
#include<sys/types.h> // pid_t
#include<sys/wait.h> // wait()
#include<unistd.h> // fork
pid_t pid;
pid = fork();
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid());
int i=0;
for(i=0;i<8;i++)
{
printf("%d\n",i);
}
exit(0);
}
else
{
printf("Error in forking..\n");
exit(EXIT_FAILURE);
}
return 0;
}
OUTPUT:
$ make wait
cc wait.c -o wait
And running the script, we get the result as below screenshot.
$ ./wait
CODE
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid, mypid, myppid;
pid = getpid();
printf("Before fork: Process id is %d\n", pid);
pid = fork();
if (pid < 0)
{
perror("fork() failure\n");
return 1;
} // Child process
if (pid == 0)
{
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
}
else
{
// Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
OUTPUT
{
int pid[15];
int bt[15];
int n;
for(int i=0;i<n;i++)
{
scanf("%d",&pid[i]);
}
printf("Enter burst time of all the processes: ");
for(int i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
int i, wt[n];
wt[0]=0;
float twt=0.0;
float tat= 0.0;
}
float att,awt;
awt = twt/n; //for calculating average waiting time
att = tat/n; //for calculating average turnaround time
OUTPUT:
Enter the number of processes: 3
Enter process id of all the processes: 1 2 3
Enter burst time of all the processes: 5 11 11
Process ID Burst Time Waiting Time TurnAround Time
1 5 0 5
2 11 5 16
3 11 16 27
Avg. waiting time= 7.000000
Avg. turnaround time= 16.000000
b) SJF
ALGORITHM:
1. Enter number of processes.
2. Enter the burst time of all the processes.
3. Sort all the processes according to their burst time.
4. Find waiting time, WT of all the processes.
5. For the smallest process, WT = 0.
6. For all the next processes i, find waiting time by adding burst time of all the previously completed process.
7. Calculate Turnaround time = WT + BT for all the processes.
8. Calculate average waiting time = total waiting time / no. of processes.
9. Calculate average turnaround time= total turnaround time / no. of processes.
CODE:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(j=0;j<i;j++)
{
wt[i]+=bt[j]; //individual WT by adding BT of all previous completed processes
total+=wt[i];
}
}
avg_wt=(float)total/n; //average waiting time
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //turnaround time of individual processes
totalT+=tat[i]; //total turnaround time
OUTPUT:
Enter number of process:4
c) ROUND ROBIN
ALGORITHM:
Step 1: Organize all processes according to their arrival time in the ready queue. The queue structure of the
ready queue is based on the FIFO structure to execute all CPU processes.
Step 2: Now, we push the first process from the ready queue to execute its task for a fixed time, allocated by
each process that arrives in the queue.
Step 3: If the process cannot complete their task within defined time interval or slots because it is stopped by
another process that pushes from the ready queue to execute their task due to arrival time of the next process is
reached. Therefore, CPU saved the previous state of the process, which helps to resume from the point where it
is interrupted. (If the burst time of the process is left, push the process end of the ready queue).
Step 4: Similarly, the scheduler selects another process from the ready queue to execute its tasks. When a
process finishes its task within time slots, the process will not go for further execution because the process's
burst time is finished.
Step 5: Similarly, we repeat all the steps to execute the process until the work has finished.
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
// Use for loop to enter the details of the process like Arrival time and the Burst Time
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
printf("Enter the Time Quantum for the process: \t"); // Accept the Time quantum
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
avg_wt = wt * 1.0/NOP; // represents the average waiting time and Turn Around time
avg_tat = tat * 1.0/NOP; // represents the average Turn Around time
OUTPUT:
Total number of process in the system: 4
d) PRIORITY SCHEDULEING:
ALGORITHM:
The algorithms prioritize the processes that must be carried out and schedule them accordingly. A higher
priority process will receive CPU priority first, and this will continue until all of the processes are finished. The
algorithm that places the processes in the ready queue in the order determined by their priority and chooses
which ones to go to the job queue for execution requires both the job queue and the ready queue.
Due to the process’ greater priority, the Priority Scheduling Algorithm is in charge of transferring it from the
ready queue to the work queue.
CODE:
#include <stdio.h>
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);
int burst[n],priority[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&burst[i],&priority[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int temp=priority[i],
int m=i;
for(int j=i;j<n;j++)
{
if(priority[j] > temp)
{
temp=priority[j];
m=j;
}
}
swap(&priority[i], &priority[m]);
swap(&burst[i], &burst[m]);
swap(&index[i],&index[m]);
}
int t=0;
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+burst[i]);
t+=burst[i];
}
printf("\n");
printf("Process Id\tBurst Time\tWait Time\n");
int wait_time=0;
int total_wait_time = 0;
for(int i=0;i<n;i++)
{
printf("P%d\t\t%d\t\t%d\n",index[i],burst[i],wait_time);
total_wait_time += wait_time;
wait_time += burst[i];
}
int total_Turn_Around = 0;
OUTPUT:
Enter Number of Processes: 2
Enter Burst Time and Priority Value for Process 1: 5 3
Enter Burst Time and Priority Value for Process 2: 4 2
Order of process Execution is
P1 is executed from 0 to 5
P2 is executed from 5 to 9
Process Id Burst Time Wait Time
P1 5 0
P2 4 5
Average waiting time is 2.500000
Average TurnAround Time is 4.50
3. Develop a C program to simulate producer-consumer problem using semaphores
ALGORITHM:
1. Initialize the empty semaphore to the size of the buffer and the full semaphore to 0.
2. The producer acquires the empty semaphore to check if there are any empty slots in the buffer. If there
are no empty slots, the producer blocks until a slot becomes available.
3. The producer acquires the mutex to access the buffer, inserts a data item into an empty slot in the
buffer, and releases the mutex.
4. The producer releases the full semaphore to indicate that a slot in the buffer is now full.
5. The consumer acquires the full semaphore to check if there are any full slots in the buffer. If there are
no full slots, the consumer blocks until a slot becomes available.
6. The consumer acquires the mutex to access the buffer, reads a data item from a full slot in the buffer,
and releases the mutex.
7. The consumer releases the empty semaphore to indicate that a slot in the buffer is now empty.
CODE:
#include <stdio.h>
#include <stdlib.h>
switch (n) {
case 1:
case 2:
2nd one:
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 300
ALGORITHM:
It is an extension to the traditional pipe concept on Unix. A traditional pipe is “unnamed” and lasts only
as long as the process.
A named pipe, however, can last as long as the system is up, beyond the life of the process. It can be
deleted if no longer used.
Usually a named pipe appears as a file and generally processes attach to it for inter-process
communication. A FIFO file is a special kind of file on the local storage which allows two or more
processes to communicate with each other by reading/writing to/from this file.
A FIFO special file is entered into the filesystem by calling mkfifo() in C. Once we have created a FIFO
special file in this way, any process can open it for reading or writing, in the same way as an ordinary
file. However, it has to be open at both ends simultaneously before you can proceed to do any input or
output operations on it.
CODE:
/*writer process */
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char buf[1024];
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi")); /* write "Hi" to the FIFO */
close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}
OUTPUT:
/* Reader Process */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#define MAX_BUF 1024
int main()
{
int fd;
/* A temp FIFO file is not created in reader */
char *myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Writer: %s\n", buf);
close(fd);
return 0;
}
OUTPUT:
Writer: Hi
5. Develop a C program to simulate Bankers Algorithm for Dead Lock Avoidance.
ALGORITHM:
1. Active:= Running U Blocked;
for k=1…r
New_ request[k]:= Requested_ resources[requesting_ process, k];
2. Simulated_ allocation:= Allocated_ resources;
for k=1…..r //Compute projected allocation state
Simulated_ allocation [requesting _process, k]:= Simulated_ allocation [requesting _process, k] + New_
request[k];
3. feasible:= true;
for k=1….r // Check whether projected allocation state is feasible
0 seconds of 0 secondsVolume 0% if
Total_ resources[k]< Simulated_ total_ alloc [k] then feasible:= false;
4. if feasible= true
then // Check whether projected allocation state is a safe allocation state
while set Active contains a process P1 such that
For all k, Total _resources[k] – Simulated_ total_ alloc[k]>= Max_ need [l ,k]-Simulated_ allocation[l, k]
Delete Pl from Active;
for k=1…..r
Simulated_ total_ alloc[k]:= Simulated_ total_ alloc[k]- Simulated_ allocation[l, k];
5. If set Active is empty
then // Projected allocation state is a safe allocation state
for k=1….r // Delete the request from pending requests
Requested_ resources [requesting_ process, k]:=0;
for k=1….r // Grant the request
Allocated_ resources [requesting_ process, k]:= Allocated_ resources[requesting_ process, k] + New_
request[k];
Total_ alloc[k]:= Total_ alloc[k] + New_ request[k];
CODE:
#include<stdio.h>
int main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
if(u==0)
{
for(k=1; k<=r; k++)
simalloc[k]=totalloc[k];
for(s=1; s<=n; s++)
for(i=1; i<=n; i++)
{
if(active[i]==1)
{
j=0;
for(k=1; k<=r; k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;
break;
}
}
}
if(j==0)
{
active[i]=0;
else
{
for(k=1; k<=r; k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
return 0;
}
OUTPUT:
Enter the no of processes:4
Enter the no ofresource classes:3
Enter the total existed resource in each class:3 2 2
Enter the allocated resources:1 0 0 5 1 1 2 1 1 0 0 2
Enter the process making the new request:2
Enter the requested resource:1 1 2
Enter the process which are n blocked or running:process 2:
12
process 4:
10
process 5:
10
Deadlock will occur
6. Develop a C program to simulate the following contiguous memory allocation Techniques: a) Worst fit
b) Best fit c) First fit.
a) WORST-FIT
ALGORITHM:
In this allocation technique, the process traverses the whole memory and always search for the largest
hole/partition, and then the process is placed in that hole/partition. It is a slow process because it has to
traverse the entire memory to search the largest hole
CODE:
#include<stdio.h>
#include<conio.h>
#define max 25
int main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
return 0;
}
OUTPUT:
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
OUTPUT
ALGORITHM:
1. The operating system maintains a list of all free memory blocks available in the system.
2. When a process requests memory, the operating system searches the list for the smallest free block of
memory that is large enough to accommodate the process.
3. If a suitable block is found, the process is allocated memory from that block.
4. If no suitable block is found, the operating system can either wait until a suitable block becomes
available or request additional memory from the system.
5. The best-fit allocation algorithm has the advantage of minimizing external fragmentation, as it searches
for the smallest free block of memory that can accommodate a process. However, it can also lead to more
internal fragmentation, as processes may not use the entire memory block allocated to them.
CODE:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
OUTPUT:
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
ALGORITHM:
3 Start by picking each process and check if it can be assigned to current block.
4 If size-of-process <= size-of-block if yes then assign and check for next process.
CODE:
#include<stdio.h>
#include<conio.h>
#define max 25
int main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
return 0;
}
OUTPUT:
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
7. Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU
a. FIFO
ALGORITHM:
4. Check the need of replacement from old page to new page in memory
CODE:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
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:
ENTER THE NUMBER OF PAGES: 20
ENTER THE PAGE NUMBER : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
ENTER THE NUMBER OF FRAMES : 3
Page Fault is 15
b. LRU
ALGORITHM:
1. Initialize an empty cache of size N (number of frames) and an empty queue (used to keep track of the order
of page references).
2. While processing page references:
a. Read the next page reference.
b. If the page is present in the cache (cache hit), move the corresponding entry to the front of the queue.
c. If the page is not present in the cache (cache miss):
i. If the cache is full (all frames are occupied):
1. Remove the least recently used page (the page at the end of the queue).
2. Add the new page to the cache and insert it at the front of the queue.
ii. If the cache has an empty frame:
1. Add the new page to the cache and insert it at the front of the queue.
d. Repeat steps (a) to (c) until all page references are processed.
3. At the end of processing page references, the cache will contain the most frequently used pages.
CODE:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\n The no of page faults is %d",c);
}
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2
ALGORITHM:
Step-1: Start the program.
Step-2: Declare the count, file name, graphical interface.
Step-3: Read the number of files
Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a single level
Step-7: Display the files
Step-8: Stop the program
CODE:
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
clrscr();
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 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;
default: exit(0);
}
}
getch();
}
OUTPUT:
Enter name of directory -- CSE
ALGORITHM:
Step-1: Start the program.
Step-2: Declare the count, file name, graphical interface.
Step-3: Read the number of files
Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a two level diretory.
Step-7: Display the files
Step-8: Stop the program
CODE:
#include<stdio.h>
struct
{
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;
default:exit(0);
}
}
getch();
}
OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
Directory created
File A2 is deleted
ALGORITHM:
Step 1: Create a queue 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
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.
CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for (i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
OUTPUT:
Enter how many blocks already allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2
2-------->1
3 Block is already allocated
4-------->1
Do you want to enter more file(Yes - 1/No - 0)0
10. Develop a C program to simulate SCAN disk scheduling algorithm.
ALGORITHM:
In this algorithm, the head starts to scan all the requests in a direction and reaches the end of the disk. After
that, it reverses its direction and starts to scan again the requests in its path and serves them. Due to this feature,
this algorithm is also known as the "Elevator Algorithm".
CODE:
#include <stdio.h>
int request[50];
int SIZE;
int pre;
int head;
int uptrack;
int downtrack;
struct max
{
int up;
int down;
}
kate[50];
int dist(int a, int b)
{
if (a > b)
return a - b;
return b - a;
}
void sort(int n)
{
int i, j;
j++;
i++;
}
downtrack = j;
i++;
j = 0;
while (i < n)
{
kate[j].up = request[i];
j++;
i++;
}
uptrack = j;
}
void scan(int n)
{
int i;
int seekcount = 0;
printf("SEEK SEQUENCE = ");
sort(n);
if (pre < head)
{
for (i = 0; i < uptrack; i++)
{
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].up);
head = kate[i].up;
}
for (i = downtrack - 1; i > 0; i--)
{
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
}
else
{
for (i = downtrack - 1; i >= 0; i--)
{
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
head = kate[i].up;
}
}
printf(" %d\nTOTAL DISTANCE :%d", head, seekcount);
}
int main()
{
int n, i;
OUTPUT:
Enter the disk size : 4
Enter the no of request sequence :2
Enter the request sequence :
1
2
Enter the current head :1
Enter the pre request : 2
Seek sequence = 1 0 1 2
Total distance :3
CONTENT BEYOND SYLLABUS
ALGORITHM:
CODE:
OUTPUT:
---- EVEN OR ODD IN SHELL SCRIPT -----"
Enter a number:23
Result : 23 is odd
2. Write a Shell program to check the given year is leap year or not
ALGORITHM:
CODE:
leap=$(date +"%Y")
echo taking year as $leap
if [ `expr $leap % 400` -eq 0 ]
then
echo leap year
elif [ `expr $leap % 100` -eq 0 ]
then
echo not a leap year
elif [ `expr $leap % 4` -eq 0 ]
then
echo leap year
else
echo not a leap year
fi
OUTPUT:
$ taking year as 2019
$ not a leap year
3. Write a Shell program to find the factorial of a number
ALGORITHM:
1. Get a number
2. Use for loop or while loop to compute the factorial by using the below formula
3. fact(n) = n * n-1 * n-2 * .. 1
4. Display the result.
CODE:
OUTPUT:
Enter a number:
3
6
Enter a number:
4
24
4. Write a Shell program to swap the two integers.
ALGORITHM:
1. Store the value of the first number into a temp variable.
2. Store the value of the second number in the first number.
3. Store the value of temp into the second variable.
CODE:
# Static input of the
# numbers
first=5
second=10
temp=$first
first=$second
second=$temp
OUTPUT:
The operating system is a software program that facilitates computer hardware to communicate and
operate with the computer software. It is the most important part of a computer system without it computer is
just like a box.
2) What is kernel?
Kernel is the core and most important part of a computer operating system which provides basic
services for all parts of the OS.
5) What are the four necessary and sufficient conditions behind the deadlock?
These are the 4 conditions:
1) Mutual Exclusion Condition: It specifies that the resources involved are non-sharable.
2) Hold and Wait Condition: It specifies that there must be a process that is holding a resource already
allocated to it while waiting for additional resource that are currently being held by other processes.
3) No-Preemptive Condition: Resources cannot be taken away while they are being used by processes.
4) Circular Wait Condition: It is an explanation of the second condition. It specifies that the processes in the
system form a circular list or a chain where each process in the chain is waiting for a resource held by next
process in the chain.
6) What is FCFS?
FCFS stands for First Come, First Served. It is a type of scheduling algorithm. In this scheme, if a
process requests the CPU first, it is allocated to the CPU first. Its implementation is managed by a FIFO queue.
7) What is deadlock?
Deadlock is a specific situation or condition where two processes are waiting for each other to complete
so that they can start. But this situation causes hang for both of them.
In this instance of Pre Emptive Process Scheduling, the OS allots the resources to a process for a
predetermined period of time. The process transitions from running state to ready state or from waiting state to
ready state during resource allocation. This switching happens because the CPU may assign other processes
precedence and substitute the currently active process for the higher priority process.
In this case of Non Pre Emptive Process Scheduling, the resource cannot be withdrawn from a process
before the process has finished running. When a running process finishes and transitions to the waiting state,
resources are switched.
Paging is a storage mechanism. Paging is used to retrieve processes from secondary memory to primary
memory. The main memory is divided into small blocks called pages. Now, each of the pages contains the
process which is retrieved into main memory and it is stored in one frame of memory.It is very important to
have pages and frames which are of equal sizes which are very useful for mapping and complete utilization of
memory.
11) What are Process Scheduling Algorithms in Operating System?
The Process Scheduling Algorithms in Operating Systems are:
1. First Come First Serve CPU Scheduling Algorithm
2. Priority Scheduling CPU Scheduling Algorithm
3. Shortest Job First CPU Scheduling Algorithm
4. Round Robin CPU Scheduling Algorithm
5. Longest Job First CPU Scheduling Algorithm
6. Shortest Remaining Time First CPU Scheduling Algorithm
7. Multiple Queue CPU Scheduling Algorithm
In this allocation technique, the process traverses the whole memory and always search for the largest
hole/partition, and then the process is placed in that hole/partition. It is a slow process because it has to
traverse the entire memory to search the largest hole
CreateProcess() fork()
Types of System Calls Windows Linux
WaitForSingleObject() wait()
ReadFile() read()
WriteFile() write()
CloseHandle() close()
SetConsoleMode() ioctl()
WriteConsole() write()
Sleep() sleep()
CreatePipe() pipe()
15) What is turnaround time?
Turnaround time is the interval from the time of submission to the time of completion of a process. It is
the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU,
and doing I/0 operations.