Professional Documents
Culture Documents
Os Lab
Os Lab
Os Lab
INDO-AMERICANCOLLEGE
CHEYYAR- 604 407
Reg.No: ________________________________________
Name : __________________________________________
Class : __________________________________________
Subject: ___________________________________________
2022-2023.
Examiners
,
Place: 1.
Date: 2.
INDEX
2 SHELL PROGRAMMING
5 SEMAPHORES
8 DEADLOCK DETECTIONS
12 THREADING A
SYNCHORNIZATION
APPLICATIONS
Ex. No: 1
BASIC UNIX COMMANDS
Date:
AIM:
To write a basic commands in Linux
CODING:
1) date
2) cal
3)Echo
Echo “text”
Output :text
4)Hostname
Output: ubuntu
5)rm
Syn:rm filename
Output: remove file
6)touch
7)cp(copies)
Syn:cp source file to destination file
Output:cp filename
8)mv (rename)
Syn:mv old file to new file
Output:mv file name
9)more
Syn:more file name
10)clear
Syn: clear
Output: clear the current screen
RESULT:
AIM:
To write a shell program for factorial
ALGORITHM:
Step-4:From the value of the integer up to 1, multiply each digit and update the final value
Step-5:The final value at the end of all the multiplication till 1 is the factorial
OUTPUT:
Enter the number:5
120
RESULT:
Thus the Program was Executed and Output Verified Successfully.
AIM:
To write a program for Round Robin scheduling
ALGORITHM:
Step-1:Start the program
RESULT:
AIM:
To write a program for shortest job first algorithm
ALGORITHM:
Step-1:Start the program
OUTPUT:
enter the number of process:5
enter the process id:1
enter the burst time:9
enter the process id:2
enter the burst time:7
enter the process id:3
enter the burst time:5
enter the process id:4
enter the burst time:3
enter the process id:5
enter the burst time:1
pid burst wait tarnd
5 1 0 1
4 3 1 4
3 5 4 9
2 7 9 16
1 9 16 25
RESULT:
Date:
AIM:
To write the program for First Come First serves scheduling
ALGORITHM:
Step-1:Start the program
#include<stdio.h>
void main()
{
int i,n,pid[5],bst[5],wait=0,tarnd;
printf("\n Enter the number of process id");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the process id:\n");
scanf("%d",&pid[i]);
printf("Enter the burst time;");
scanf("%d",&bst[i]);
}
printf("\npid\t burst\t wait\t tarnd");
for(i=1;i<=n;i++)
{
tarnd=wait+bst[i];
printf("\n %d\t %d\t %d\t %d",pid[i],bst[i],wait,tarnd);
wait=tarnd;
}
}
OUTPUT:
RESULT:
AIM:
To write a program for Priority scheduling
ALGORITHM:
Step-1:Start the program
RESULT:
Date:
AIM:
ALGORITHM:
b)check whether the required location are from the selected location
#include<stdlib.h>
void main()
int f[50],i,st,j,len,c,k;
for(i= 0;i<50;i++)
f[i]=0;
x:
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
break;
if(j==(st+len))
scanf("%d",&c);
if(c==1)
goto x;
else
exit;
OUTPUT:
3->1
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
RESULT:
Date:
AIM:
To write a program for file allocation concept using Indexed allocation techniques
ALGORITHM:
Step-1:Start the program
OUTPUT:
enter index block : 5
enter no of files on index: 4
6
7
8
9
allocated
file indexed
5->6:1
5->7:1
5->8:1
5->9:1enter 1 to enter more file and 0 to exit:0
RESULT:
Date:
AIM:
To write a program for file allocate concept using linked list technique
ALGORITHM:
Step-1:Start the program
Step-5:If the location is free allocated and set flag=1 to the allocated location
OUTPUT:
Date:
AIM:
ALGORITHM:
Step-1:Start the program
OUTPUT:
Enter the no of element:3
1.proceducer
2.customer
3.exit:
RESULT:
AIM:
To write a program for file organization techniques using single –level directory
ALGORITHM:
#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;
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)
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
if(strcmp(f, dir.fname[i])==0)
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
if(i==dir.fcnt)
else
dir.fcnt--;
break;
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
if(strcmp(f, dir.fname[i])==0)
break;
if(i==dir.fcnt)
break;
case 4: if(dir.fcnt==0)
else
{
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
break;
default: exit(0);
getch();
OUTPUT:
File B is deleted
RESULT:
Date:
AIM:
To write a program file organization techniques using Two –level directory
ALGORITHM:
Step-6: initgraph(&gd,&gm,”c:\tc\bgi”);
struct
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
int i,ch,dcnt,k;
clrscr();
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]);
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;
}
printf("File %s not found",f);
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)
printf("\nNo Directory's ");
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);
getch();
OUTPUT:
1. Create Directory 2. Create File 3. Delete File
Directory created
File created
File created
File created
Directory Files
DIR1 A1 A2
DIR2 B1
File A2 is deleted
RESULT:
Date:
AIM:
To write a C program File Organization concept using the technique hierarchical level directory .
ALGORITHM:
#include<stdio.h>
#include<graphics.h>
struct tree_element
char name[20];
int x,y,ftype,lx,rx,nc,level;
};
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();
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);
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:
AIM:
To write a C program File Organization concept using the technique DAG
ALGORITHM:
#include<conio.h>
#include<graphics.h>
#include<string.h>
struct tree_element
char name[20];
int x,y,ftype,lx,rx,nc,level;
};
char from[20];
char to[20];
}link; link
L[10]; int
nofl;
node * root;
void main()
int gd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
read_links()
int i;
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");
fflush(stdin);
gets(L[i].to);
draw_link_lines()
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
int i;
if(root!=NULL)
if(strcmpi(root->name,s)==0)
*x=root->x;
*y=root->y;
return;
else
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
int i,gap;
if(*root==NULL)
(*root)=(node *)malloc(sizeof(node));
gets((*root)->name);
file:"); 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)
>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++)
(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
else
(*root)->nc=0;
{
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:
Enter Name of dir/file (under root): ROOT
File/Dir: HTML
RESULT:
AIM:
ALGORITHM:
void main()
{
int i,j;
printf("Deadlock Avoidance");
input();
show();
call();
getch();
}
void input()
{
int i,j;
printf("Enter no of processes:");
scanf("%d",&n);
printf("Enter no of resource instances:");
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]);
}
void show()
{
int i,j;
printf("Process Allocation Maximum Available\n");
for(i=0;i<n;i++)
{
printf("\nP%d :",i);
for(j=0;j<r;j++)
{
printf("%d ",all[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 call()
{
int finish[20],temp,need[20][20],flag=1,k,c1=0;
int safe[20];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
need[i][j]=max[i][j]-all[i][j];
}
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0) && (need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=all[i][j];
finish[i]=1;
flag=1;
}
printf("P%d ->",i);
if(finish[i]==1)
{
i=n;
}
} //if
}//if
} //for
} //for
} //while
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
}
if(c1==n)
{
printf("\n The system is in Safe State");
}
else
{
printf("\nProcess are in dead lock");
printf("\n The System is in unsafe state");
}
}
OUTPUT:
DEADLOCK AVOIDANCE
Enter no of processes:
2
Enter no of resource instances:
3
Enter the max matrix:
1 0 2
2 2 1
Enter the allocation matrix:
101
111
111
P0 : 1 0 1 1 0 2 1 1 1
P1 : 1 1 1 3 2 1
P0 ->P1->
RESULT:
AIM:
ALGORITHM:
#include<stdio.h>
int max[20][20],all[20][20],need[20][20],avail[20];
int n,r;
void input();
void show();
void call();
void main()
{
int i,j;
printf("Deadlock Detection");
input();
show();
call();
getch();
}
void input()
{
int i,j;
printf("\nEnter no of processes:");
scanf("%d",&n);
printf("Enter no of resource instances:");
scanf("%d",&r);
void show()
{
int i,j;
printf("Process Allocation Maximum Available\n");
for(i=0;i<n;i++)
{
printf("\nP%d :",i);
for(j=0;j<r;j++)
{
printf("%d ",all[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 call()
{
int finish[20],temp,need[20][20],flag=1,k;
int dead[20],safe[20];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
need[i][j]=max[i][j]-all[i][j];
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0) && (need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=all[i][j];
finish[i]=1;
flag=1;
}
if(finish[i]==1)
{
i=n;
} // if
} //if
} //if
} //for
} //for
}//while
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 system is in deadlock and the dealdock process are \n");
for(i=0;i<n;i++)
{
printf("P%d :",dead[i]);
}
}
else
printf("\n no deadlock occur");
}
OUTPUT:
DEADLOCK DETECTION
Enter no of processes:
2
Enter no of resource instances:
3
Enter the max matrix:
1 1 1
2 1 2
Enter the allocation matrix:
100
201
000
P0 : 1 0 0 1 1 1 0 0 0
P1 : 2 0 1 2 1 2
P0 :P1:
RESULT:
Date: FIFO
AIM:
ALGORITHM:
#include<stdio.h>
void main()
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
scanf("%d",&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("\n No of faults:%d",m);
OUTPUT:
1 1-->F
2 12-->F
3 123-->F
4 423-->F
5 453-->F
1 451-->F
2 251-->F
3 231-->F
4 234-->F
5 534-->F
No of faults:10
RESULT:
Date: LRU
AIM:
ALGORITHM:
#include<stdio.h>
void main()
int g=0,a[5],b[20],p=0,q=0,m=0,n,k,i,q1=1,j,u,h;
char f='F';
scanf("%d",&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;
g=0;
if(q1==3)
for(k=0;k<q1;k++)
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("\n no.fault:%d",m);
OUTPUT:
2 2-->F
3 23-->F
2 23
1 231-->F
5 251-->F
2 251
4 254-->F
5 254
3 354-->F
2 352-->F
3 352
2 352
no.fault:7
RESULTS:
Date: LFU
AIM:
ALGORITHM:
Step-6: If No Frames Is Free Repalce The Page With The Page That Is Leastly Used
#include<stdio.h>
void main()
int rs[50],i,j,k,m,f,cntr[20],a[20],min,pf=0;
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;
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)
printf("\t PF no %d",pf);
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
5 2 3 PF no 5
5 2 3 PF no 5
5 2 3 PF no 5
5 2 3 PF no 5
5 2 1 PF no 6
5 2 4 PF no 7
5 2 3 PF no 8
RESULT:
Date:
AIM:
ALGORITHM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
void main()
int i;
void*shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345,1024,0666|IPC_CREAT);
shared_memory=shmat(shmid,NULL,0);
read(0,buff,100);
strcpy(shared_memory,buff);
printf("you wrote:%s\n",(char*)shared_memory);
OUTPUT:
Hello World
Date:
AIM:
ALGORITHM:
Step-5:Calculate the physical address using the following physical address=(frame number*Frame
size)+offset.
#include<stdio.h>
#include<conio.h>
Void main()
Int np,ns,i;
Int *sa;
Clrscr();
Scanf(“%d”,&np);
Scanf(“%d”,&ps);
Sa=(int*)malloc(2*np);
For(i=0;i<np;i++)
Sa[i]=(int)malloc(ps);
Getch();
}
OUTPUT:
Page1 Address:1894
Page2 Address:1902
Page3 Address:1910
Page4 Address:1918
Page5 Address:1926
RESULT:
Date:
AIM:
ALGORITHM:
#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)
counter+=1;
for(i=0;i<(0xFFFFFFFF);i++);
return NULL;
int main(void)
int i=0;
int err;
while(i<2)
err=pthread_create(&(tid[i]) ,NULL,&dosomething,NULL);
if(err!=0)
i++;
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
return 0;
OUTPUT:
job1 started
job2 started
job2 finished
job2 finished
RESULT: