III Cs Os Record Program
III Cs Os Record Program
CERTIFICATE
Submitted for Thiruvalluvar University, Serkkadu, Vellore – 632 115, Practical Examination
PRACTICAL RECORD
NAME : _______________________________________
2. Shell Programming
Implement the following CPU scheduling
algorithms
a) Round Robin
3.
b) SJF
c) FCFS
d) Priority
Implement all file allocation strategies
a) Sequential
4.
b) Indexed
c) Linked
5. Implement Semaphores
Implement all File Organization Techniques
a) Single level directory
6. b) Two level
c) Hierarchical
d) DAG
Implement Bankers Algorithm for Dead
7.
Lock Avoidance
Implement an Algorithm for Dead Lock
8.
Detection
Implement e all page replacement
algorithms
9. a) FIFO
b) LRU
c) LFU
10. Implement Shared memory and IPC
Implement Paging Technique of memory
11.
management
Implement Threading & Synchronization
12. Applications
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Use the following commands to help you manage your Unix account.
IMPORTANT: The Unix (Ultrix) operating system is case sensitive. All
commands must be typed in lower-case letters unless noted otherwise.
COMMADS
OUTPUT:
Result:
1
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Program:
OUTPUT:
Enter a Number
5
The Factorial of the given Number is 120
-----------------------------
Enter a Number
6
The Factorial of the given Number is 720
---------------------------
Result:
a) Round Robin
Aim: To implement the CPU scheduling algorithm for Round Robin.
Program:
#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("\n 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]);
3
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
et[i]=0;
}
}
}
printf("\n Total Estimated Time:%d",x);
getch();
}
OUTPUT:
Enter the no of processes: 3
Enter the time quantum: 3
Enter Process Name & Estimated Time: A 14
Enter Process Name & Estimated Time: B 20
Enter Process Name & Estimated Time: C 24
Result: Hence to implement the CPU scheduling algorithm for Round Robin is
executed successfully.
4
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
b) SJF
Program:
#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);
}
}
5
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
for(i=0;i<n;i++)
{
if(i==0)
st[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;
for(i=0;i<n;i++)
printf("\n%s\t\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();
}
OUTPUT:
Result: Hence to implement the CPU scheduling algorithm for SJF is executed
successfully.
6
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
c) FCFS
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int 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);
}
}
}
7
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
for(i=0;i<n;i++)
{
if(i==0)
star[i]=arr[i];
else
star[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 BurstTime 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],finish[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;
}
OUTPUT:
8
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
d) PRIORITY
Program:
#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, Arrival Time, 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);
}
}
9
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
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\t Arrival Time\t Execution Time\t Priority \t Waiting Time\t
TATime");
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 Turn Around Time is: %f",ata);
getch();
}
10
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT:
Enter the number of Process: 3
Enter Process Name, Arrival Time, Execution Time & Priority: A 3 4 3
Enter Process Name, Arrival Time, Execution Time & Priority: B 5 7 1
Enter Process Name, Arrival Time, Execution Time & Priority: C 8 14 2
11
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
a) Sequential
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no. of files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file %d: ",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file %d : ",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename Start Block Length\n");
for(i=0;i<n;i++)
printf(" %d %d %d\n",i+1,t[i],b[i]);
printf("Enter File Name:");
scanf("%d",&x);
printf("\n File Name is: %d",x);
printf("\n Length is: %d",b[x-1]);
printf("\n Blocks Occupied: ");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
12
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT:
Result: Hence to implement the File Allocation Strategies for Sequential File is
executed successfully.
13
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
b) Indexed
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter No. of Files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter Starting Block and Size of File %d: ",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter Blocks occupied by File %d: ",i+1);
scanf("%d",&m[i]);
printf("Enter Blocks of File %d: ",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile Index Length\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter File Name: ");
scanf("%d",&x);
printf("\nFile Name is: %d\n",x);
i=x-1;
printf("Index is: %d",sb[i]);
printf("\nBlock Occupied are: ");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
14
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT:
Result: Hence to implement the File Allocation Strategies for Indexed File is
executed successfully.
15
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
c) Linked
Program:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter No. of Files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter File Name: ");
scanf("%s",&f[i].fname);
printf("\n Enter Starting Block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("\n Enter No.of Blocks:");
scanf("%d",&f[i].size);
printf("\n Enter Block Numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("\n FILE NAME \t START \t SIZE \t BLOCK \n");
for(i=0;i<n;i++)
{
printf("%s\t %d \t %d \t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
16
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
OUTPUT:
Result: Hence to implement the File Allocation Strategies for Indexed File is
executed successfully.
17
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Program:
#include <stdio.h>
#include <pthread.h>
typedef struct {
pthread_mutex_t mutex;
pthread_cond_t condition;
int value;
} semaphore;
printf("Thread waiting\n");
semaphore_wait(sem);
18
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
return NULL;
}
int main() {
semaphore sem;
semaphore_init(&sem, 1); // Initialize the semaphore with initial value 1
pthread_t thread;
pthread_create(&thread, NULL, thread_function, (void*)&sem);
pthread_join(thread, NULL);
return 0;
}
OUTPUT
Aim: To implement the Single Level Directory for File Organization Technique.
Program:
#include<stdlib.h>
#include<string.h>
#include<stdio.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("\n1.Create File \n2.Delete File \n3.Search File \n4.Display Files \n5.Exit
\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the Name of the File: ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter 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;
}
20
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
21
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT
1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit
1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit
1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit
1.Create File
2.Delete File
3.Search File
4.Display Files
5.Exit
22
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Result: Hence to implement the Single Level Directory for File Organization
Technique is executed successfully.
23
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Aim: To implement the Two Level Directory for File Organization Technique.
Program:
#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;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n1.Create Directory \n2.Create File \n3.Delete File");
printf("\n4.Search File \n5.Display \n6.Exit \nEnter your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter Name of Directory: ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory Created");
break;
case 2: printf("\nEnter 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]);
printf("File Created");
break;
}
24
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
if(i==dcnt)
printf("Directory %s Not Found",d);
break;
25
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
goto jmp1;
}
}
printf("File %s Not Found",f);
goto jmp1;
}
}
26
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 1
Directory Files
PRIYA
SHREE
27
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 2
Directory Files
PRIYA
SHREE
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 4
28
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 5
Directory Files
PRIYA
SHREE
1.Create Directory
2.Create File
3.Delete File
4.Search File
5.Display
6.Exit
Enter your Choice: 6
Result: Hence to implement the Two Level Directory for File Organization
Technique is executed successfully.
29
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Program:
#include<stdio.h>
#include<graphics.h>
#include<string.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;
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
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;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
30
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
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);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
}
}
OUTPUT
Result: Hence to implement the Hierarchy Level Directory for File Organization
Technique is executed successfully.
31
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int c = 0;
struct adj_list {
int dest;
struct adj_list *next;
}*np = NULL, *np1 = NULL, *p = NULL, *q = NULL;
struct Graph {
int v;
struct adj_list *ptr;
} array[6];
32
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
if (array[src].ptr == NULL) {
array[src].ptr = np;
p = array[src].ptr;
p->next = NULL;
} else {
p = array[src].ptr;
while (p->next != NULL) {
p = p->next;
}
p->next = np;
}
//addReverseEdge(src, dest);
}
void print_graph(int n) {
int i;
for (i = 0; i < n; i++) {
printf("Adjacency List of %d: ", array[i].v);
while (array[i].ptr != NULL) {
printf("%d ", (array[i].ptr)->dest);
array[i].ptr = (array[i].ptr)->next;
}
printf("\n");
}
}
int checkDAG(int n) {
int count = 0;
int size = n - 1, i, j;
for (i = 0; i < n; i++) {
//cout << "Adjacency List of " << array[i].v << ": ";
if (count == size) {
return 1;
}
if (array[i].ptr == NULL) {
count++;
for (j = 0; j < n; j++) {
33
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
array[i].ptr = (array[i].ptr)->next;
}
}
}
}
return 0;
}
int main() {
int n = 6, i;
printf("Number of vertices: %d\n", n);
OUTPUT
Number of vertices: 6
Adjacency List of 0: 1
Adjacency List of 1: 2 3
Adjacency List of 2:
Adjacency List of 3: 4
Adjacency List of 4: 5
Adjacency List of 5: 3 2
The given graph is 'Directed Acyclic Graph' : True
Program:
#include<stdio.h>
int main() {
/* array will store at most 5 process with 3 resoures if your process or
resources is greater than 5 and 3 then increase the size of array */
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3],
done[5], terminate = 0;
printf("Enter the number of process and resources");
scanf("%d %d", & p, & c);
// p is process and c is diffrent resources
printf("enter allocation of resource of all process %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & alc[i][j]);
}
}
printf("enter the max resource process required %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & max[i][j]);
}
}
printf("enter the available resource");
for (i = 0; i < c; i++)
scanf("%d", & available[i]);
35
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
/* once process execute variable done will stop them for again execution */
for (i = 0; i < p; i++) {
done[i] = 0;
}
while (count < p) {
for (i = 0; i < p; i++) {
if (done[i] == 0) {
for (j = 0; j < c; j++) {
if (need[i][j] > available[j])
break;
}
//when need matrix is not greater then available matrix then if j==c will
true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in available
resources */
for (j = 0; j < c; j++) {
available[j] += alc[i][j];
}
count++;
terminate = 0;
} else {
terminate++;
}
}
}
if (terminate == (p - 1)) {
printf("safe sequence does not exist");
break;
}
}
if (terminate != (p - 1)) {
printf("\n available resource after completion\n");
for (i = 0; i < c; i++) {
printf("%d\t", available[i]);
}
printf("\n safe sequence are\n");
36
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
return 0;
}
OUTPUT
37
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Program:
#include<stdio.h>
static int mark[20];
int i,j,np,nr;
int main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
38
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
// initialize W with avail
for(j=0;j<nr;j++)
w[j]=avail[j];
39
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
if(deadlock)
printf("\n Deadlock detected");
else
printf("\n No Deadlock possible");
}
OUTPUT
Deadlock detected
--------------------------------
40
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
DATE:
9. Implement all page replacement algorithms
a) FIFO b) LRU c) LFU
a) FIFO
Program:
#include<stdio.h>
//Counters
int page_miss=0;
int page_hits=0;
for(int j=0;j<n;j++)
{
if (symbol==frames[j])
{
flag=1;
break;
}
}
41
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
if (flag==1)
{
printf("\nSymbol: %d Frame: ",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
page_hits+=1;
}
else
{
index=(index+1)%n;
frames[index]=symbol;
page_miss+=1;
printf("\nSymbol: %d Frame: ",symbol);
for (int j=0;j<n;j++)
printf("%d ",frames[j]);
}
}
printf("\nPage hits: %d",page_hits);
printf("\nPage misses: %d",page_miss);
}
//Main function
int main(void)
{
int string[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
int no_frames=3;
int size=sizeof(string)/sizeof(int);
fifo(string,no_frames,size);
return 0;
}
42
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT
Symbol: 7 Frame: 7 -1 -1
Symbol: 0 Frame: 7 0 -1
Symbol: 1 Frame: 7 0 1
Symbol: 2 Frame: 2 0 1
Symbol: 0 Frame: 2 0 1
Symbol: 3 Frame: 2 3 1
Symbol: 0 Frame: 2 3 0
Symbol: 4 Frame: 4 3 0
Symbol: 2 Frame: 4 2 0
Symbol: 3 Frame: 4 2 3
Symbol: 0 Frame: 0 2 3
Symbol: 3 Frame: 0 2 3
Symbol: 2 Frame: 0 2 3
Symbol: 1 Frame: 0 1 3
Symbol: 2 Frame: 0 1 2
Symbol: 0 Frame: 0 1 2
Symbol: 1 Frame: 0 1 2
Symbol: 7 Frame: 7 1 2
Symbol: 0 Frame: 7 0 2
Symbol: 1 Frame: 7 0 1
Page hits: 5
Page misses: 15
43
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
b) LRU
Program:
#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
{
44
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
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("\nThe no of page faults is %d",c);
}
45
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT
Enter no of pages:10
Enter the reference string:7 1 2 5 3 2 1 6 4 2
Enter no of frames:3
7
7 1
7 1 2
5 1 2
5 3 2
1 3 2
1 6 2
1 6 4
2 6 4
46
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
b) LFU
Program:
#include<stdio.h>
void print(int frameno,int frame[])
{
int j;
for(j=0;j<frameno;j++)
printf("%d\t",frame[j]);
printf("\n");
}
int main()
{
int
i,j,k,n,page[50],frameno,frame[10],move=0,flag,count=0,count1[10]={0},
repindex,leastcount;
float rate;
printf("Enter the number of pages\n");
scanf("%d",&n);
printf("Enter the page reference numbers\n");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter the number of frames\n");
scanf("%d",&frameno);
for(i=0;i<frameno;i++)
frame[i]=-1;
printf("Page reference string\tFrames\n");
for(i=0;i<n;i++)
{
printf("%d\t\t\t",page[i]);
flag=0;
for(j=0;j<frameno;j++)
{
if(page[i]==frame[j])
{
flag=1;
count1[j]++;
printf("No replacement\n");
break;
}
47
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
}
if(flag==0&&count<frameno)
{
frame[move]=page[i];
count1[move]=1;
move=(move+1)%frameno;
count++;
print(frameno,frame);
}
else if(flag==0)
{
repindex=0;
leastcount=count1[0];
for(j=1;j<frameno;j++)
{
if(count1[j]<leastcount)
{
repindex=j;
leastcount=count1[j];
}
}
frame[repindex]=page[i];
count1[repindex]=1;
count++;
print(frameno,frame);
}
}
rate=(float)count/(float)n;
printf("Number of page faults is %d\n",count);
printf("Fault rate is %f\n",rate);
return 0;
}
48
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT
49
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Program:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
OUTPUT
50
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
Program:
#include<stdio.h>
void main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour memsize is %d ",memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}
51
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT
Your memsize is 15
Enter page size:5
52
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int MAX = 10;
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond;
void *even(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 0) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
void *odd(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 1) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
int main(){
pthread_t thread1;
pthread_t thread2;
pthread_mutex_init(&thr, 0);
pthread_cond_init(&cond, 0);
53
GOVERNMENT ARTS AND SCIENCE COLLEGE, TIRUPATTUR III B.Sc. COMPUTER SCIENCE
OUTPUT
1 2 3 4 5 6 7 8 9 10
54