Operating System Lab
Operating System Lab
Operating System Lab
COMMAND: Linux command is a utility of Linux operating system. All basic and advanced tasks can be done by executing
commands. The commands can be broadly categorized as follows:
i. touch: Used to create empty files. (Syntax: touch <file+name>, touch <file 1><file 2>…)
ii. cat. Used to create a file, display content of file, copy content of one file to another and more.
(Syntax: cat [OPTION]...[FILE]..)
iii. rm: Used to remove a file. (Syntax: rm <file_name>)
iv. ср: Used to сору a file. (Syntax: cp <existing file_name> <new file_name>)
v. mv: Used to move a file or directory from one location to another. (Syntax: mv <file_name> <directory_path>)
i. head: Used to display content of file. It displays the first 10 lines of file. (Syntax: head <file_name>)
ii. tail: Similar to head command. It displays last 10 line of file. (Syntax: tail <file_name>)
iii. tac: Reverse of cat command. It displays file content in reverse order. (Syntax: tac<file>)
iv. more: Used to display screenful output at a time. (Syntax: more <file_name>)
v. less: Similar to more command with additional features such as 'adjustment in width and height of the terminal (Syntax: less
<file_name>)
i. cut: Used to select specific column of file. (Syntax: cut-d(delimiter) - f(ColumnNumber) <file_name>)
ii. grep: Used to search content from a file. (Syntax: command|grep <SearchWord>)
iii. comm: Used to compare two files or streams. (Syntax: comm <file1> <file2>)
iv. tr: Used to translate file content like from lower case to upper case. (Syntax: command|tr< 'old'> <new'>)
v. wc: Used to count the lines, words, and characters in file. (Syntax: wc <file_name>)
THEORY: A thread is a lightweight process that runs with other threads concurrently within a process. Each thread has its own stack
and register sets. However, they share common code, data and file services. They are often implemented to improve the performance
of applications by allowing multiple tasks to be executed concurrently.
A process, is a dynamic instance of program under execution. It is an independent entity. Each process has its own memory and set of
OS resources like files and CPU time. Each process is isolated from the other providing security and stability.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include<pthread.h>
sleep (1)
return NULL;}
int main(){
pthred_t thread_id;
exit (0);}
SAMPLE OUTPUT:
Before thread
After thread
Lab 3
AIM: To write a C program to implement producer-consumer problem using semaphores
THEORY: Producer consumer problem is a synchronization problem. There is a fixed size buffer where producer produces item and
consumer consumes it. One solution uses the shared memory. To allow both to run concurrently, there must be buffer of items
available that can be filled by producer and emptied by the consumer.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
void producer(){
--mutex;
++full;
--empty;
x++;
++mutex;}
void consumer(){
--mutex;
--full;
++empty;
x--;
++mutex;}
int main(){
int n,i;
scanf("%d", &n);
switch(n){
case 1:
else
printf("Buffer is full");
break;
case 2:
else
printf("Buffer is empty");
break;
case 3:
exit (0);
printf("Exiting!");
break;}}}
SAMPLE OUTPUT:
Buffer is empty
Exiting!
Lab 4
AIM: To write a C program to simulate Dining-philosopher problem
THEORY: Dining-philosophers problem is a classic synchronization and concurrency problem that illustrates the challenges of
resource allocation and deadlock avoidance. In this scenario, five philosophers sit round a circular table, each thinking and eating.
Between, each pair of philosopher, there is a single fork. The goal is to ensure that each philosopher can pick up both forks (left and
right) to eat without causing a dead lock.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define N5
sem_wait(&forks [leftFork]);
sem_wait (&forks[rightFork]);
printf(“Philosopher %d is eating\n”philID);
sem_post(&forks[leftFork]);
sem-post(&forks[rightFork]);
pthread_exit(NULL);}
int main(){
int i;
for(i=0;i<N;i++)
sem_init(&forks[i],0,1);
for(i=0;i<N;i++){
int *philID=(int*)malloc(sizeof(int));
philID=i;
pthread_create(&philosopher[i],NULL,philosopher,philID);}
for(i=0;i<N;i++)
pthread_join(philosophers[i],NULL);
for(i=0;i<N;i++)
sem_destroy(&forks[i]);}
return 0;}
SAMPLE OUTPUT:
Philosopher 0 is thinking…
Philosopher 0 is eating
Philosopher 4 is thinking…
Philosopher 4 is eating
Philosopher 3 is thinking…
Philosopher 3 is eating
Philosopher 1 is thinking…
Philosopher 1 is eating
Philosopher 2 is thinking…
Philosopher 2 is eating
AIM: To simulate the CPU scheduling algorithm First Come First serve (FCFS)
ALGORITHM:
Step 3: For each process in the ready Q, assign the process name and the burst time
Step 4: Set the waiting time of the first process as 0 and its burst time as its turnaround time
Step 6: Calculate
Step 7: Stop
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
main() {
clrscr();
scanf("%d", &n);
for(i=0;i<n;i++) {
scanf("%d", &bt[i]); }
wt[0] = wtavg = 0;
for(i=1;i<n;i++) {
getch(); }
SAMPLE OUTPUT:
P0 24 0 24
P1 3 24 27
P2 3 27 30
b) SJF
ALGORITHM:
Step 3: For each process in ready Q, assign the process id and accept the cpu burst time
Step 4: Start the ready & according to the shortest burst time by sorting according to lowest to highest burst time.
Step 5: Set the waiting time for first process as 0 and its turnaround time as burst time.
Step 7: Calculate:
SOURCE 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;
scanf("%d",&n);
for(i=0;i<n;i++){
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;}
for(i=0;i<n;i++){
pos=i;
for(j=i+1;j<n;j++){
if(bt[j]<bt[pos])
pos=j;}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i]; }
avg_wt=(float)total/n;
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
totalT+=tat[i];
avg_tat=(float)totalT/n;
ALGORITHM:
Stop 2: Accept the number of process in ready queue. Step3: For each process in ready Q, assign the process id and accept CPU burst
time
stopy: Sort the ready queue according to priority step 5. set waiting of first process as o and its burst
Step 6: Arrange the process based on process priority Step 7. For each process in ready a, calculate
a) waiting time (n) = waiting time (n-1)+ burst time (nm) b) turnaround time (n) = burst time (n) + waiting time(n) Step 9: Calculate
a) Average WT: Total WT/NO. of process 6) Average TAT = Total TAT/NO. of process
SOURCE CODE:
#include <stdio.h>
int temp=*a;
*a=*b;
*b=temp;}
int main(){
int n;
scanf("%d",&n);
int b[n],p[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",&b[i],&p[i]);
index[i]=i+1;}
for(int i=0;i<n;i++){
int a=p[i],m=i;
for(int j=i;j<n;j++){
a=p[j];
m=j;}}
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);}
int t=0;
for(int i=0;i<n;i++){
t+=b[i];}
printf("\n");
int wait_time=0;
for(int i=0;i<n;i++){
wait_time += b[i];}
return 0;}
Lab 6
AIM: To simulate preemptive CPU scheduling algorithm in C
a) Round robin
#include<stdio.h>
#include<conio.h>
int main() {
scanf("%d", &NOP);
y = NOP;
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
scanf("%d", &at[i]);
scanf("%d", &bt[i]);
temp[i] = bt[i]; }
scanf("%d", &quant);
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; ) {
if(temp[i] <= quant && temp[i] > 0) // define the conditions {
temp[i] = 0;
count=1; }
y--;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0; }
if(i==NOP-1)
{i=0;}
else if(at[i+1]<=sum)
{ i++; }
else {
i=0; }}
avg_wt = wt * 1.0/NOP;
return 0; }
b) Priority
#include<stdio.h>
struct process{
int WT,AT,BT,TAT,PT;};
int main(){
int n,temp[10],t,count=0,short_p;
float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
scanf("%d",&n);
printf("Enter the arrival time , burst time and priority of the process\n");
printf("AT BT PT\n");
for(int i=0;i<n;i++) {
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);
temp[i]=a[i].BT;}
a[9].PT=10000;
for(t=0;count!=n;t++){
short_p=9;
for(int i=0;i<n;i++){
short_p=i;}}
a[short_p].BT=a[short_p].BT-1;
if(a[short_p].BT==0) {
count++;
a[short_p].WT=t+1-a[short_p].AT-temp[short_p];
a[short_p].TAT=t+1-a[short_p].AT;
total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT;}}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
printf("ID WT TAT\n");
for(int i=0;i<n;i++){
printf("%d %d\t%d\n",i+1,a[i].WT,a[i].TAT);}
return 0;}
Lab 7
AIM: To simulate Banking algorithm for deadlock avoidance
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main() {
scanf("%d", &no_of_resources);
for (i=0;i<no_of_resources;i++) {
availability[i]=0;
printf("%c= ",(i+97));
scanf("%d",&instance[i]);}
scanf("%d", &process);
for (i=0;i<no_of_resources;i++)
printf(" %c",(i+97));
printf("\n");
P[i]=i;
printf("P[%d] ",P[i]);
for (j=0;j<no_of_resources;j++) {
scanf("%d",&allocated[i][j]);
availability[j]+=allocated[i][j];}}
for (i=0;i<no_of_resources;i++) {
printf(" %c",(i+97));
availability[i]=instance[i]-availability[i];}
printf("\n");
printf("P[%d] ",i);
for (j=0;j<no_of_resources;j++)
scanf("%d", &MAX[i][j]);}
printf("\n");
A: a=-1;
cnt=0;
b=P[i];
for (j=0;j<no_of_resources;j++) {
need[b][j] = MAX[b][j]-allocated[b][j];
if(need[b][j]<=availability[j])
cnt++;}
if(cnt==no_of_resources) {
op[k++]=P[i];
for (j=0;j<no_of_resources;j++)
availability[j]+=allocated[b][j];
} else
P[++a]=P[i];}
if(a!=-1) {
process=a+1;
goto A;}
printf("\t <");
for (i=0;i<k;i++)
printf(">");
return 0();}
Lab 9
AIM: To simulate MVT and MFT memory management techniques
THEORY: MVT (Multi-programming with a variable number of tasks) is the memory management technique in which each job gets
the right amount of memory it needs. That is, partitioning of memory is dynamic and changes as jobs enter and leave the system. MVT
is a more efficient user of resources. MFT (Multi-programming with a Fixed number of Tasks) is one of the oldest memory
management techniques in which the memory is partitioned into fixed sized partitions and each job is assigned into a partition. The
memory assigned to a partition does not change. MFT suffers from internal fragmentation while MVT suffters from external
fragmentation.
SOURCE CODE:
int main(){
char ch=’y’;
scanf("%d", &ms);
temp=ms;
scanf(“%d”&mp[i]);
if (mp[i]<=temp){
temp=temp-mp[i];}
else{
printf(“Memory full”);
break;}
scanf(“%c”,&ch);}
printf(“\n\tProcess\tMemory allocated”);
for(i=0;i<n;i++)
printf(“\n\t%d\t%d”,i+1,mp[i]);
return 0;}
SAMPLE OUTPUT:
Memory full
1 400
2 275
#include <stdio.h>
int main(){
int ms,bs,nob,ef,n,mp[10],fif = 0;
int i,p: 0;
scanf(“%d", &ms);
scanf("%d", &bs);
nob= ms/bs;
ef= ms-nob*bs;
scanf(“%d", &n);
scanf("%d", &mpli]);}
printf("\n%d\t%d",i+1, mp[i]);
if (mp[i]>bs)
print("\t NO\t");
else{
tif=tif+bs-mp[i];
p++;}}
if (i<n)
return 0;}
SAMPLE OUTPUT:
1 275 Yes 25
2 400 NO --
3 290 Yes 10
4 293 Yes 7
THEORY: Paging is a memory management scheme wherein the CPU stores and retrieves data from secondary storage for use in
primary memory at time of execution. The OS retrieves data from secondary memory in same size blocks known as pages. Page is a
memory block of same size. Paging technique plays an important role in virtual memory which is portion of disk reserved to emulate
computer's RAM. Size of process is measured in terms of number of pages.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main(){
scanf("%d",&ms);
scanf("%d",&ps);
nop = ms/ps;
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++){
scanf("%d",&s[i]);
if(s[i] >rempages){
printf("\nMemory is Full");
break;}
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);}
pa=fno[x][y]*ps+offset;
return 0;}
SAMPLE OUTPUT:
Enter the memory size – 1000 Enter the page size -- 100
Memory is Full
Enter Logical Address to find Physical Address Enter process no. and pagenumber and offset -- 2 3 60
THEORY: One of the simplest methods for memory allocation is to divide memory into equal sized partitions. Each partition may
contain exactly one process. First fit is simplest algorithm. In this case, it allocates the first available partition that is large enough to
accommodate the process. It searches for suitable partition from the beginning.
SOURCE CODE:
#include<stdio.h>
int i, j;
int allocation[n];
allocation[i] = -1; }
allocation[i] = j;
blockSize[j] -= processSize[i];
break; } } }
printf("%i\t\t\t\t", processSize[i]);
if (allocation[i] != -1)
else
printf("Not Allocated");
printf("\n"); } }
int main() {
int m;
int n;
m = sizeof(blockSize) / sizeof(blockSize[0]);
n = sizeof(processSize) / sizeof(processSize[0]);
return 0 ;}
Lab 12
AIM: To simulate BEST-FIT contiguous memory allocation technique in C
THEORY: Best-fit is another memory allocation technique. It allocates the smallest available partition that is large enough to
accommodate the process. It requires searching the entire list of partition to bind the best match.
SOURCE CODE:
#include <stdio.h>
int allocation[proccesses];
int occupied[blocks];
allocation[i] = -1;}
occupied[i] = 0;}
if (indexPlaced == -1)
indexPlaced = j;
indexPlaced = j;} }
if (indexPlaced != -1){
allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;} }
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");}}
int main(){
return 0 ;}
SAMPLE OUTPUT:
1 10 2
2 30 1
3 60 4
4 30 4
Lab 13
AIM: To simulate WORST-FIT contiguous memory allocation technique in C
THEORY: Worst-fit is another contiguous memory allocation technique used in fixed sized partition. It allocates the largest available
partition. It requires searching the entire list of partitions to fi the largest available space.
SOURCE CODE:
#include <stdio.h>
int allocation[processes];
int occupied[blocks];
allocation[i] = -1;}
occupied[i] = 0;}
if (indexPlaced == -1)
indexPlaced = j;
indexPlaced = j;}}
if (indexPlaced != -1)
allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
blockSize[indexPlaced] -= processSize[i];}}
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");}}
int main()
return 0;
SAMPLE OUTPUT:
Process No. Process Size Block no.
1 40 4
2 10 1
3 30 2
4 60 Not Allocated
Lab 14
AIM: To simulate FIFO page replacement algorithm in C
THEORY: FIFO is the simplest page replacement algorithm. In this algorithm, the OS keeps tracks of all pages in memory in queue,
the oldest page is in the front of queue. When a page needs to be replaced page in the front of queue is selected for removal.
ALGORITHM:
Step 4: Check the need of replacement from old to new page in memory.
SOURCE CODE:
int main() {
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
temp[m] = -1; }
s = 0;
if(incomingStream[m] == temp[n]) {
s++;
pageFaults--; }}
pageFaults++;
else if(s == 0) {
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
if(temp[n] != -1)
else
printf(" - \t\t\t"); }}
return 0; }
SAMPLE OUTPUT:
4 4 - -
1 4 1 -
2 4 1 2
4 4 1 2
5 5 1 2
Lab 15
AIM: To simulate LRU page replacement algorithm in C
THEORY: LRU is a technique used in operating system to manage memory and improve system performance. LRU algorithm keeps
track of the pages that have been accessed recently and discards the ones that have not been used for the longest time.
ALGORITHM:
Step 9: Stop
SOURCE CODE:
#include<stdio.h>
int main(){
int m, n, position, k, l;
int a = 0, b = 0, page_fault = 0;
int total_frames = 3;
int frames[total_frames];
int temp[total_frames];
frames[m] = -1;}
a = 0, b = 0;
if(frames[m] == pages[n]){
a = 1;
b = 1;
break;}}
if(a == 0){
if(frames[m] == -1){
frames[m] = pages[n];
b = 1;
page_fault++;
break;}}}
if(b == 0){
temp[m] = 0;}
if(frames[m] == pages[k]){
temp[m] = 1;}}}
if(temp[m] == 0)
position = m;}
frames[position] = pages[n];
page_fault++;}
printf("%d\t", frames[m]);}
printf("\n");}
return 0;}
Lab 16
AIM: To simulate LFU page replacement algorithm in C
THEORY: LFU algorithm keeps track of how frequently each page is accessed and replaces the page that has been accessed the least
number of times. This is based on the principle of locality of reference, which states that recently used pages are more liked to be used
again in the near future.
ALGORITHM:
Step 1: Start
Step 6: If no frames is free, replace the page that is used the least
Step 8: Stop.
SOURCE CODE:
#include<stdio.h>
int main(){
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
scanf("%d",&f);
for(i=0;i<f;i++){
cntr[i]=0; a[i]=-1;}
printf(“\nThe Page Replacement Process is – \n“);
for(i=0;i<m;i++){
for(j=0;j<f;j++)
if(rs[i]==a[j]){
cntr[j]++;
break;
if(j==f)
{ min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i]; cntr[min]=1;
pf++;}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)}
return 0;}
SAMPLE OUTPUT:
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
523
523
5 2 1 PF No. 6
5 2 4 PF No. 7
THEORY: Optimal page replacement is a memory management technique that replaces the page which will not be used for the
longest period of time in the future. It is the most efficient page replacement algorithm, but also the most difficult to implement. It
works by keeping track of the future references to each page in memory. When a page needs to be replaced, the algorithm selects page
that will not be referenced again for the longest period of time.
ALGORITHM:
Step 1: Start
Step 8: Stop.
SOURCE CODE:
#include<stdio.h>
int main(){
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
scanf("%d", &no_of_frames);
scanf("%d", &no_of_pages);
scanf("%d", &pages[i]);}
frames[i] = -1;}
flag1 = flag2 = 0;
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;}}
if(flag1 == 0){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;}}}
if(flag2 == 0){
flag3 =0;
temp[j] = -1;
if(frames[j] == pages[k]){
temp[j] = k;
break;}}}
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;}}
if(flag3 ==0){
max = temp[0];
pos = 0;
max = temp[j];
pos = j;}}}
frames[pos] = pages[i];
faults++;}
printf("\n");
printf("%d\t", frames[j]);
return 0;}
SAMPLE OUTPUT:
2 -1 -1
2 3 -1
234
234
134
134
734
534
534
534
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];
scanf("%s",mdname);
scanf("%d",&nf);
do{
scanf("%s",name);
for(i=0;i<nf;i++){
if(!strcmp(name,fname[i]))
break;}
if(i==nf){
strcpy(fname[j++],name);
nf++;}
else
scanf("%d",&ch);}
while(ch==1);
for(i=0;i<j;i++)
printf("\n%s",fname[i]);
return 0;}
SAMPLE OUTPUT:
aaa
bbb
ccc
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
struct{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main(){
int i,ch,dcnt,k;
dcnt=0;
while(1){
scanf("%d",&ch);
switch(ch){
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0){
scanf("%s",dir[i].fname[dir[i].fcnt]);
printf("File created");
break;}
if(i==dcnt)
break;
scanf("%s",d);
for(i=0;i<dcnt;i++){
if(strcmp(d,dir[i].dname)==0){
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++){
if(strcmp(f, dir[i].fname[k])==0){
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;}}
goto jmp;}}
jmp : break;
scanf("%s",d);
for(i=0;i<dcnt;i++){
if(strcmp(d,dir[i].dname)==0){
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++){
if(strcmp(f, dir[i].fname[k])==0){
goto jmp1;}}
goto jmp1;}}
jmp1: break;
case 5: if(dcnt==0)
else{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++){
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);}}
break;
default:exit(0);}}}
c) Hierarchical directory
#include<stdio.h>
#include<graphics.h>
#include<string.h>
struct tree_element{
char name[20];
void main(){
int gd=DETECT,gm;
node *root;
root=NULL;
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
closegraph();}
int i, gap;
if(*root==NULL){
(*root)=(node *)malloc(sizeof(node));
fflush(stdin);
gets((*root)->name);
scanf("%d",&(*root)->ftype);
(*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((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap,
lx+gap*i+gap/2);}
else
(*root)->nc=0;}}
display(node *root){
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL){
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);}}
Lab 19
AIM: To simulate all file allocation strategies in C
a) Sequential Allocation
THEORY: File allocation strategies determine how files are stored and accessed on a storage device, Sequential allocation is the
simplest file allocation strategy. Files are stored in a contiguous block of disk space, and each file is assigned a starting block number
when a fbutteile is read or written, the disk head moves sequentially from the beginning et the file to the end.
SOURCE CODE:
#include<conio.h>
int main(){
for(i=0;i<50;i++)
f[i]=0;
x: count=0;
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count){
for(j=st;j<(st+len);j++)
if(f[j]==0){
f[j]=1;
printf("%d\t%d\n",j,f[j]);}
if(j!=(st+len-1))
else
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
return 0;}
SAMPLE OUTPUT:
14 1
15 1
16 1
THEORY: Linked list file allocation is technique used in OS to manage the storage of files on disk. It works by creating a linked list
of blocks, where each block represents a portion of the file. The blocks are linked together in a chain and the OS keeps track of
location of each block, OS can allocate additional blocks as needed dynamically when file is deleted, the OS frees the blocks that were
allocated to it.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
Int main(){
for(i=0;i<50;i++)
f[i]=0;
scanf("%d",&p);
for(i=0;i<p;i++){
scanf("%d",&a);
f[a]=1;}
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{
k++;}}}
else
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
return 0;}
SAMPLE OUTPUT:
2-------->1
4-------->1
a) FCFS
#include<stdio.h>
int main(){
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
scanf("%d",&max);
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++){
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
avg=seek/(float)n;
return 0;}
b) SCAN
#include<stdio.h>
int main(){
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&temp);
if(temp>=head){
queue1[temp1]=temp;
temp1++;}
Else{
queue2[temp2]=temp;
temp2++;}}
for(i=0;i<temp1-1;i++){
for(j=i+1;j<temp1;j++){
if(queue1[i]>queue1[j]){
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;}}}
for(i=0;i<temp2-1;i++){
for(j=i+1;j<temp2;j++){
if(queue2[i]<queue2[j]){
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;}}}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++){
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff);}
avg=seek/(float)n;
return 0;}
c) LOOK
#include <stdio.h>
#include <stdlib.h>
int main(){
scant("%d", &n);
scanf("%d", &RQ[i]);
scant("%d", &initial);
scant("%d", &size);
printf("Enter the head movement direction for high 1 and for low 0");
scanf("%d", &move”);"
for(i=0; i<n;i++){
if (RQ[j]>RQ[j+1]){
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;}}}
int index;
for(i=0;i<n;i++){
if(initial<RQ[i])
index=i; break;}
if(move ==1){
initial=RQ[i];}
for(i=index-1;i>=0;i--){
TotalHeadMovement+=abs(RQ[i]-initial;
initial=RQ[i];}}
else{
for(i=index-1;i>=0;i--){
TotalHeadMovement+=abs(RQ[i]-initial;
initial=RQ[i];}
TotalHeadMovement+=abs(RQ[i]-initial);
initial=RQ[i];}}
return 0;}
SAMPLE OUTPUT:
Enter the head movement direction for high 1 and for low 0: 1