Cse Lab Manual Os PDF
Cse Lab Manual Os PDF
LAB MANUAL
Academic Year: 2015-16 ODD
SEMESTER
Programme (UG/PG) : UG
Semester : v
Course Code : CS1035
Course Title : OPERATING SYSTEM
LAB
Prepared By
<B.SOWMIYA>
(<AP/O.G.>, Department of Computer Science and Engineering)
1
HARDWARE AND SOFTWARE REQUIREMENTS
Windows
RAM 256 MB 1 GB
OS Windows 8.1/8/7/Vista (32-bit and 64- Windows 8.1/8/7/Vista (32-bit and 64-bit)
bit) Windows XP SP3 (32-bit)
Windows XP SP3 (32-bit) Windows Server 2012 R2 (64-bit)
Windows Server 2012 R2 (64-bit) Windows Server 2008 R2 (64-bit)
Windows Server 2008 R2 (64-bit) Windows Server 2003 R2 (32-bit)
Windows Server 2003 R2 (32-bit)
Mac OS X
RAM 256 MB 2 GB
Disk Space 656 MB - 1.2 GB 1.2 GB for the complete installation (excluding
drivers)
2
Internal Assessment Mark Split Up
Observation : 25 Marks
Attendance : 5 Marks
Record : 10 Marks
3
SCHEDULING: FCFS
Ex. No: 1 a
OBJECTIVE:
ALGORITHM:
Step 3: Initially, Waiting time of first process is zero and Total time for the first process is the
Step 4: Calculate the Total time and Processing time for the remaining processes.
Step 5: Waiting time of one process is the Total time of the previous process.
Step 6: Total time of process is calculated by adding Waiting time and Service time.
Step 7: Total waiting time is calculated by adding the waiting time for lack process.
Step 8: Total turn around time is calculated by adding all total time of each process.
Step 9: Calculate Average waiting time by dividing the total waiting time by total number of
process.
Step 10: Calculate Average turn around time by dividing the total time by the number of
process.
4
SOURCE CODE :
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp; int totwt=0,tottat=0;
//clrscr();
printf("Enter the number of processes:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}}
for(i=0;i<n;i++)
{
if(i==0)
star[i]=arr[i]; elsestar[i]=finish[i-1];
wt[i]=star[i]-arr[i];finish[i]=star[i]+bur[i];tat[i]=finish[i]-arr[i];
}
printf("\nPName Arrtime Burtime WaitTime Start TAT Finish"); for(i=0;i<n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%6d\t%6d",pn[i],arr[i],bur[i],wt[i],star[i],tat[i],finis
h[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f",(float)totwt/n); printf("\nAverage Turn Around
Time:%f",(float)tottat/n); getch();
return 0;
}
5
SAMPLE INPUTS & OUTPUTS:
Input:
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: p1 2 4
Enter the Process Name, Arrival Time & Burst Time: p2 3 5
Enter the Process Name, Arrival Time & Burst Time: p3 1 6
Output:
PName Arrtime Burtime WaitTimeSrart TAT Finish
p3 1 6 01 6 7
p1 2 4 57 9 11
p2 3 5 811 13 16
Average Waiting Time: 4.3333333
Average Turn Around Time: 9.33333333
RESULT :
Thus the program to stimulate the FCFS scheduling algorithm is successfully executed.
6
SHORTEST JOB FIRST
Ex.No.1.b
OBJECTIVE:
ALGORITHM:
Step 3: Select the process which have shortest burst will execute first.
Step 4: If two process have same burst length then FCFS scheduling algorithm used.
Step 6: Start with the first process from it’s selection as above and let other process to be in
queue.
7
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h> void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10]; int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10]; clrscr();
printf("Enter the number of process:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name, arrival time & execution time:"); flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i]; elsest[i]=ft[i-1];
wt[i]=st[i]-at[i];ft[i]=st[i]+et[i];ta[i]=ft[i]-at[i];totwt+=wt[i]; totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]); printf("\nAverage
waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata); getch();
}
8
SAMPLE INPUT AND OUTPUT:
Input:
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 4 6
Enter the Process Name, Arrival Time & Burst Time: 2 5 15
Enter the Process Name, Arrival Time & Burst Time: 3 6 11
Output:
Pname arrivaltimeexecutiontime waitingtime tatime
1 46 0 6
3 611 4 15
2 515 16 31
Average Waiting Time: 6.6667
Average Turn Around Time: 17.3333
RESULT:
Thus the program for implementing SJF scheduling algorithm was written and
successfully executed.
9
PRIORITY SCHEDULING
Ex.No.1.c
OBJECTIVE:
ALGORITHM:
STEP 4: Start with the higher priority process from it’s initial position let other process to be
queue.
10
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h> void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10]; int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10]; clrscr();
printf("Enter the number of process:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:"); flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];wt[i]=st[i]-at[i];ft[i]=st[i]+et[i];ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];wt[i]=st[i]-at[i];ft[i]=st[i]+et[i];ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
11
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata); getch();
}
Output:
RESULT:
Thus the program for implementing Priority scheduling algorithm was written and
successfully executed.
12
ROUND ROBIN
Ex.No.1.d
OBJECTIVE:
ALGORITHM:
STEP 7: Make the CPU scheduler goes around the ready queue allocating CPU to each
STEP 8: Make the CPU scheduler picks the first process and sets time to interrupt after
STEP 9: If the process has burst less than the time quantum than the process releases the
CPU.
13
SOURCE CODE:
#include<stdio.h>
#include<conio.h> void main()
{
int et[30],ts,n,i,x=0,tot=0; char pn[10][10];
clrscr();
printf("Enter the no of processes:"); scanf("%d",&n);
printf("Enter the time quantum:"); scanf("%d",&ts); for(i=0;i<n;i++)
{
printf("enter process name & estimated time:"); scanf("%s %d",pn[i],&et[i]);
}
printf("The processes are:"); for(i=0;i<n;i++)
printf("process %d: %s\n",i+1,pn[i]); for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n %s -> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else if((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("\n %s -> %d",pn[i],et[i]); et[i]=0;}
}
}
printf("\n Total Estimated Time:%d",x); getch();
}
14
SAMPLE INPUT AND OUTPUT:
Input:
Enter the no of processes: 2
Enter the time quantum: 3
Enter the process name & estimated time: p1 12
Enter the process name & estimated time: p2 15
Output:p1 -> 3 p2 -> 3 p1 -> 3 p2 -> 3 p1 -> 3 p2 -> 3 p1 -> 3 p2 -> 3 p2 -> 3
Total Estimated Time: 27
RESULT:
Thus the program for implementing Round Robin scheduling algorithm was written
and successfully executed.
1. What is fragmentation?
2. What is internal fragmentation?
3. What is external fragmentation?
4. There are four processes stored in memory with the sizes p1= 20kb, p2=30kb,
p3=65kb, p4=5okb. The total memory size is 200kb. Divide the total memory space
into four partitions.
i) Calculate internal & external fragmentation using MFT?
ii) Calculate external fragmentation using MVT?
(These Questions should be dictated to students at the end of the lab hours. The
students must write the answers for those questions in their Observation Notebooks
before they come to next lab. )
15
MUTIPROGRAMMING VARIABLE TASK
Ex.No. 2a.
OBJECTIVE:
ALGORITHM:
Step 4: If the needed memory is available for the particular process it will be allocated and
Step 5: If not it has to tell no further memory remaining and the process will not be allocated
with memory.
16
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int m=0,m1=0,m2=0,p,count=0,i; clrscr();
printf("enter the memory capacity:"); scanf("%d",&m);
printf("enter the no of processes:"); scanf("%d",&p);
for(i=0;i<p;i++)
{
printf("\nenter memory req for process%d: ",i+1); scanf("%d",&m1);
count=count+m1;
if(m1<=m)
{
if(count==m)
printf("there is no further memory remaining:");
printf("the memory allocated for process%d is: %d ",i+1,m);m2=m-m1;
printf("\nremaining memory is: %d",m2); m=m2;
}
}
else
{
printf("memory is not allocated for process%d",i+1);
}
printf("\nexternal fragmentation for this process is:%d",m2);
}
getch();
}
RESULT:
Thus the program Multiprogramming variable task has been executed successfully.
17
MUTIPROGRAMMING FIXED TASK
Ex.No: 2b
OBJECTIVE:
ALGORITHM:
Step 3: calculate the number of partitions the total memory has to be divided.
Step 4: Get the required memory for each process if the required memory is available in that
particular partition the process will be allocated to that partition and the internal
Step 5: If the required memory not available then the process will not be allocated to that
partition.
18
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int m,p,s,p1;
int m1[4],i,f,f1=0,f2=0,fra1,fra2,s1,pos; clrscr();
printf("Enter the memory size:"); scanf("%d",&m);
printf("Enter the no of partitions:"); scanf("%d",&p);
s=m/p;
printf("Each partn size is:%d",s); printf("\nEnter the no of processes:"); scanf("%d",&p1);
pos=m;
for(i=0;i<p1;i++)
{
if(pos<s)
{
printf("\nThere is no further memory for process%d",i+1); m1[i]=0;
break;
}
else
{
printf("\nEnter the memory req for process%d:",i+1); scanf("%d",&m1[i]);
if(m1[i]<=s)
{
printf("\nProcess is allocated in partition%d",i+1); fra1=s-m1[i];
printf("\nInternal fragmentation for process is:%d",fra1); f1=f1+fra1;
pos=pos-s;
}
else
{
printf("\nProcess not allocated in partition%d",i+1); s1=m1[i];
while(s1>s)
{
s1=s1-s;pos=pos-s;
}
pos=pos-s;fra2=s-s1;f2=f2+fra2;
printf("\nExternal Fragmentation for this process is:%d",fra2);
}
}
}
19
printf("\nProcess\tallocatedmemory");
for(i=0;i<p1;i++)
printf("\n%5d\t%5d",i+1,m1[i]);
f=f1+f2;
printf("\nThe tot no of fragmentation is:%d",f); getch();
return 0;
}
Output:
Process1 is allocated in partn1
Internal fragmentation for process1 is: 2
Enter the memory req for process2: 22
Process2 is not allocated in partn2
External fragmentation for process2 is: 18
Process memory allocated
1 20 18
2 20 22
The tot no of fragmentation is: 20
RESULT :
Thus the program Multiprogramming fixed task has been executed successfully.
(These Questions should be dictated to students at the end of the lab hours. The
students must write the answers for those questions in their Observation Notebooks
before they come to next lab.
20
SEQUENTIAL FILE ALLOCATION
Ex.No. 3a
OBJECTIVE:
ALGORITHM:
b). Check whether the required locations are free from the selected location.
21
SOURCE CODE:
#include<stdio.h>
main()
{
int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}
Output:
Enter the starting block & length of file 4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
22
11->1
12->1
13->1
The file is allocated to disk
If you want to enter more files? (Y-1/N-0)
RESULT :
23
LINKED FILE ALLOCATION
Ex. No. 3.b
OBJECTIVE:
To write a C program to implement File Allocation concept using the technique Linked List
Technique.
ALGORITHM:
Step 5: If the location is free allocate and set flag =1 to the allocated locations.
24
SOURCE CODE:
#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & length");
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );}
25
OUTPUT:
Enter how many blocks are already allocated 3
Enter the blocks no’s that are already allocated 4 7 9
Enter the starting index block & length 3
7
3-> 1
4-> File is already allocated
5->1
6->1
7-> File is already allocated
8->1
9-> File is already allocated
10->1
11->1
12->1
If u want to enter one more file? (yes-1/no-0)
RESULT :
26
INDEXED FILE ALLOCATION
OBJECTIVE:
To write a C program to implement File Allocation concept using the technique indexed
allocation Technique
ALGORITHM:
27
SOURCE CODE:
#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("enter no of files on index\t");
scanf("%d",&n);
}
else
{
printf("Block already allocated\n");
goto x;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
printf("Block already allocated");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);
printf(" Enter 1 to enter more files and 0 to exit\t");
scanf("%d",&c);
if(c==1)
goto x;
else
exit();
getch();
28
}
OUTPUT:
RESULT :
(These Questions should be dictated to students at the end of the lab hours. The
students must write the answers for those questions in their Observation Notebooks
before they come to next lab. )
29
FILE ORGANIZATION TECHNIQUES
SINGLE LEVEL DIRECTORY
Ex.No. 4.a.
OBJECTIVE:
To write a C program to implement File Organization concept using the technique Single
level directory.
ALGORITHM:
30
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
cleardevice();
setbkcolor(GREEN);
puts("Enter no of files do u have?");
scanf("%d",&count);
for(i=0;i< count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("Enter file %d name",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"Root Directory");
setcolor(BLUE);
for(j=0;j< =i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[j]);
} getch();
}}
RESULT :
31
FILE ORGANIZATION TECHNIQUES
TWO LEVEL
Ex. No.4b.
OBJECTIVE:
To write a C program to implement File Organization concept using the technique two level
directory.
ALGORITHM:
Step 6: initgraph(&gd,&gm,”c:\tc\bgi”);
32
SOURCE CODE:
#include<stdio.h>
#include <graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef truct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1) (*root)->ftype=1;
else
(*root)->ftype=2;
33
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;ilink[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("How many users");
else
printf("hoe many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else
(*root)->nc=0;
if((*root)->nc==0) gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;inc;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);
34
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,roo>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}}
}
RESULT :
35
FILE ORGANIZATION TECHNIQUES
HIERARCHICAL
Ex.No. 4c.
OBJECTIVE:
ALGORITHM:
36
SOURCE CODE:
#include <stdio.h>
#include <graphics.h>
struct tree_element { char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
37
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i< 5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)>nc);
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);
38
outtextxy(root->x,root->y,root->name);
for(i=0;inc;i++)
{
display(root->link[i]);
}}
}
RESULT :
(These Questions should be dictated to students at the end of the lab hours. The students
must write the answers for those questions in their Observation Notebooks before they
come to next lab. )
39
SIMULATE BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE
Ex. No: 5
OBJECTIVE:
ALGORITHM:
Step 6: If the new request comes then check that the system is in safety or not if we allow the
request.
40
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void 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];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}}
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;i<=n;i++) {
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}}
41
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[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;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
Else
{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
42
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}
OUTPUT:
RESULT :
(These Questions should be dictated to students at the end of the lab hours. The students
must write the answers for those questions in their Observation Notebooks before they
come to next lab. )
43
SIMULATE AN ALGORITHM FOR DEAD LOCK DETECTION
Ex. No: 6
OBJECTIVE:
ALGORITHM:
44
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
45
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
46
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
//printf("\nP%d",i);
if(finish[i]==1)
{
i=n;
}}}}}}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}
}
Output
47
Enter the allocation matrix
333
203
124
Enter the available resources
120
Process allocation max available
P1 333 368 120
P2 203 433
P3 124 344
System is in deadlock and deadlock process are
P0 p1 p2
RESULT :
(These Questions should be dictated to students at the end of the lab hours. The students
must write the answers for those questions in their Observation Notebooks before they
come to next lab.
48
SIMULATE ALL PAGE REPLACEMENT ALGORITHMS
A) FIFO
Ex. No: 7 a.
OBJECTIVE:
algorithm
ALGORITHM:
49
SOURCE CODE:
#include<stdio.h>
#include<conio.h> void main()
{
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1; char f='F';
clrscr();
printf("Enter the Number of Pages:"); scanf("%d",&n);
printf("Enter %d Page Numbers:",n); for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);m++;
}
p=0;
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults:%d",m); getch();
}
50
SAMPLE INPUT AND OUTPUT:
Input:
Enter the Number of Pages: 12 Enter 12 Page Numbers:
232152453252
Output:
2 2-> F
323-> F
223
1231-> F
5531-> F
2521-> F
4524-> F
5524
3 324-> F
2 324
5 354-> F
2 352-> F
No of faults: 9
RESULT:
51
SIMULATE ALL PAGE REPLACEMENT ALGORITHMS
B) LRU
Exp. No: 7 b.
OBJECTIVE:
To write a C program to implement page replacement LRU (Least Recently Used) algorithm.
ALGORITHM:
52
SOURCE CODE:
#include<stdio.h>
#include<conio.h> void main()
{
int g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n; char f='F';
clrscr();
printf("Enter the number of pages:"); scanf("%d",&n);
printf("Enter %d Page Numbers:",n); for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
//g=1;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);m++;
}
p=0;
g=0;
if(q1==3)
{
for(k=0;k<q1;k++)
53
{
if(b[i+1]==a[k])
p=1;
}
for(j=0;j<q1;j++)
{
u=0;
k=i;while(k>=(i-1)&&(k>=0))
{
if(b[k]==a[j])
u++;k--;
}
if(u==0)
q=j;
}
}
else
{
for(k=0;k<q;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
}
printf("\nNo of faults:%d",m); getch();
}
54
SAMPLE INPUT AND OUTPUT:
Input:
Output:
22-> F
323-> F
2 23
1231-> F
5251-> F
2251
4254-> F
5254
3354-> F
2352-> F
5352
2352
No of faults: 7
RESULT:
(These Questions should be dictated to students at the end of the lab hours. The students
must write the answers for those questions in their Observation Notebooks before they
come to next lab. )
55
SHARED MEMORY AND IPC
Ex. No. 8
OBJECTIVE:
ALGORITHM:
56
SOURCE CODE:
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h>
int main()
{
int id,semid,count=0,i=1,j;
int *ptr;
id=shmget(8078,sizeof(int),IPC_CREAT|0666);
ptr=(int *)shmat(id,NULL,0);
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}u;
struct sembuf sem;
semid=semget(1011,1,IPC_CREAT|0666);
ushort a[1]={1};
u.array=a;
semctl(semid,0,SETALL,u);
while(1)
{
sem.sem_num=0;
sem.sem_op=-1;
sem.sem_flg=0;
semop(semid,&sem,1);
*ptr=*ptr+1;
printf("process id:%d countis :%d \n",getpid(),*ptr);
for(j=1;j< =1000000;j++)
{
sem.sem_num=0;
sem.sem_op=+1;
sem.sem_flg=0;
semop(semid,&sem,1);
}
}
shmdt(ptr);
}
57
SAMPLE INPUT AND OUTPUT:
enter Process id 16338992
pprocess
10
process 1
RESULT:
(These Questions should be dictated to students at the end of the lab hours. The students
must write the answers for those questions in their Observation Notebooks before they
come to next lab. )
58
SIMULATE PAGING TECHNIQUE OF MEMORY MANAGEMENT
Ex. No. 9
OBJECTIVE:
ALGORITHM:
Step 4: Calculate the physical address using the following Physical address=(frame number *
59
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
main()
{
int np,ps,i; int *sa; clrscr();
printf("enter how many pages\n"); scanf("%d",&np);
printf("enter the page size \n"); scanf("%d",&ps); sa=(int*)malloc(2*np); for(i=0;i<np;i++)
{
sa[i]=(int)malloc(ps);
printf("page%d\t address %u\n",i+1,sa[i]);
}
getch();
}
Input:
Enter how many pages: 5
Enter the page size: 4
Output:
Page1 Address: 1894
Page2 Address: 1902
Page3 Address: 1910
Page4 Address: 1918
Page5 Address: 1926
60
RESULT:
1. What is a semaphore?
2. Explain about mutexes?
3. What are the different types of thread synchronization mechanisms?
4. Explain about multilevel queue scheduler thread operation?
5. What is SMP?
(These Questions should be dictated to students at the end of the lab hours. The students
must write the answers for those questions in their Observation Notebooks before they
come to next lab. )
61
THREADING & SYNCHRONIZATION
Ex.No. 10
OBJECTIVE:
ALGORITHM:
Step 6: Check for the process creation if not print error message.
62
SOURCE CODE:
#include<stdio.h>
#include <string.h>
#include<pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_t tid[2];
int counter;
void* doSomeThing(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d started\n", counter);
for(i=0; i< (0xFFFFFFFF);i++);
printf("\n Job %d finished\n", counter);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf ("\ncan't create thread :[%s]", strerror(err));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
63
SAMPLE OUTPUT:
Job 1 started
Job 1finished
RESULT:
64