OS Lab Record
OS Lab Record
2a
SHELL PROGRAMMING
EVEN OR ODD
AIM:
To write a program to find whether a number is even or odd .
ALGORITHM:
STEP 1: Read the input number.
STEP 2: Perform modular division on input number by 2.
STEP 3: If remainder is 0 print the number is even.
STEP 4: Else print number is odd.
STEP 5: Stop the program.
PROGRAM
//evenodd.sh
echo "enter the number"
read num
echo "enter the number"
read num
if [ `expr $num % 2` -eq 0 ]
then
echo "number is even"
else
echo "number is odd"
fi
OUTPUT:
[students@localhost ~]$ sh evenodd.sh
enter the number: 5 the number is odd.
RESULT:
Thus the Shell program for finding the given number ODD or EVEN was implemented
and output verified using various samples.
Ex.No.2b
AIM:
To write a Shell Program to Find Biggest In Three Numbers.
ALGORITHM:
STEP 1: Read The Three Numbers.
STEP 2: If A Is Greater Than B And A Is Greater Than C Then Print A Is Big.
STEP 3: Else If B is greater Than C Then C Is Big.
STEP 4: Else Print C Is Big.
STEP 5: Stop The Program.
PROGRAM:
// big3.sh
echo "enter three numbers"
read a b c
Ex.No.2c
FACTORIAL OF NUMBER
AIM:
To find a factorial of a number using Shell programming.
ALGORITHM:
Step 1: read a number.
Step 2: Initialize fact as 1.
Step 3: Initialize I as 1.
Step 4: While I is lesser than or equal to no.
Step 5: Multiply the value of I and fact and assign to fact increment the value of I by 1.
Step 6: print the result.
Step 7: Stop the program.
PROGRAM:
// fact.sh
echo "enter the number"
read n
fact=1
i=1
while [ $i -le $n ]
do
fact=`expr $i \* $fact`
i=`expr $i + 1`
done
echo "the fcatorial number of $ni is $fact
OUTPUT:
[students@localhost ~]$ sh fact.sh
Ex.No.2d
FIBONACCI SERIES
AIM :
To write a Shell program to display the Fibonacci series.
ALGORITHM:
Step 1: Initialize n1& n2 as 0 & 1.
Step 2: enter the limit for Fibonacci.
Step 3: initialize variable as 0
Step 4: Print the Fibonacci series n1 and n2.
Step 5: While the var number is lesser than lim-2
Step 6: Calculate n3=n1+n2.
Step 7: Set n1=n2 and n2=n3
Step 8: Increment var by 1 and print n2
Step 9: stop the program.
PROGRAM:
// fib.sh
echo " ENTER THE LIMIT FOR FIBONNACI SERIES"
read lim
n1=0
n2=1
var=0
echo "FIBONACCI SERIES IS "
echo "$n1"
echo "$n2"
while [ $var -lt `expr $lim - 2` ]
do
n3=`expr $n1 + $n2 `
n1=`expr $n2 `
n2=`expr $n3 `
var=`expr $var + 1 `
echo "$n2"
done
OUTPUT :
[students@localhost ~]$ sh fib.sh
enter the limit for Fibonacci: 5
The Fibonacci series is:0,1,1,2,3
RESULT:
Thus the Shell program for generating Fibonacci series was implemented and output
verified using various samples.
Other Shell Programs:
1. MULTIPLICATION TABLE
AIM:
Write a shell script to print the for the given number.
Algorithm:
1.Start.
2.Read n.
3.i=1.
4.f = n * i.
5.i = i + 1.
6.Display f.
7.Repeat 4,5,6 steps until i = 10.
8.End.
Source Code:
#! /bin/sh
echo enter the number
read n
for i in 1 2 3 4 5 6 7 8 9 10
do
x= `expr $n \* $i`
done
2. COPIES MULTIPLE FILES INTO A DIRECTORY
AIM: Write a shell script that copies multiple files into a directory.
Algorithm:
1.Start.
2.Read *.c files into i.
3.Copy $i to the root directory.
4.Repeat 2,3 steps until all values of $i..
5.End.
Source Code:
#! /bin/sh
for i in `ls *.c`
do
cp $i /root
done
echo file are copied into root
3. NO. OF WORDS AND CHARACTERS IN A GIVEN FILE
AIM:
Write a shell script to find no. of words and characters in a given file.
Algorithm:
1. Start.
2. cc = 0,w=0.
3. give file name as commandline argument (i.e $1).
4. for i in `cat $1`.
5. w = w + 1.
6. c = expr $i : *.
7. cc = cc + c.
8. Repeat 4,5,6,7 steps until eof.
9. End.
Source Code:
# /bin/sh
w=0
cc=0
for i in cat $1
do
j= $i
echo $j
w=`expr $w + 1`
c=`expr $j:.*`
cc = `expr $cc + $c`
done
echo no.of characters $cc
echo no.of words $w
4. TO DISPLAY ALL FILES IN A GIVEN DIRECTORY
AIM: Write a shell script to display all files in a given directory.
Algorithm:
1.Start.
2.Read i value from ls sss.
3.Display i value .
4.Repeat above 2 steps until end of directory.
5.End.
Source Code:
#! /bin/sh
for i in `ls sss`
do
echo $i
done
5. CALCULATOR
AIM: Write a shell script to perform simple calculator.
Algorithm:
1.Start
2.Read a and b
3.Read op
4.Depending on value of operator perform case
Operation.
5.End
Source Code:
#! /bin/sh
echo enter the value for a
read a
echo enter the value for b
read b
echo enter operator
read op
case $op in
+) c=`expr $a + $b`;;
-) c = `expr $a -$b`;;
\*) c = `expr $a \* $b`;;
/) c = `expr $a / $b` ;;
esac
echo $c
RESULT:
Thus the Shell programs were implemented and outputs are verified using various
samples.
Ex.No:3a
AIM:
To write a C - Program to implement the FCFS (First Come First Serve) CPU scheduling
Algorithm
ALGORITHM:
1. START the program
2. Get the number of processors
3. Get the Burst time of each processors
4. Calculation of Turn Around Time and Waiting Time
a) tot_TAT = tot_TAT + pre_TAT
b) avg_TAT = tot_TAT/num_of_proc
c) tot_WT = tot_WT + pre_WT + PRE_BT
d) avg_WT = tot_WT/num_of_proc
5. Display the result
6. STOP the program
PROGRAM: (FCFS Scheduling)
#include<stdio.h>
void main()
{
int n,i,j;
int ser[10],start[10],finish[10];
float avgturn=0.0,avgwait=0.0;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the service time of %d process:",i+1);
scanf("%d",&ser[i]);
}
start[0]=0;
finish[0]=ser[0]+start[0];
for(i=1;i<n;i++)
{
start[i]=start[i-1]+ser[i-1];
finish[i]=ser[i]+start[i];
}
for(i=0;i<n;i++)
{
avgwait+=start[i];
avgturn+=finish[i];
}
avgwait/=n;
avgturn/=n;
printf("\nService Start Turn\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\n",ser[i],start[i],finish[i]);
printf("\nAverage waiting time=%f",avgwait);
printf("\nAverage turn around time=%f",avgturn);
}
OUTPUT: (FCFS Scheduling Algorithm)
[students@localhost ~]$ cc fcfs.c
[students@localhost ~]$ ./a.out
Enter the number of processes:4
Enter the service time of 1 process:5
Enter the service time of 2 process:6
Enter the service time of 3 process:3
Enter the service time of 4 process:7
Service
5
6
3
7
Start
0
5
11
14
Turn
5
11
14
21
AIM:
To write a C - Program to implement the SJF (Shortest Job First) CPU scheduling Algorithm
ALGORITHM:
1. START the program
2. Get the number of processors
3. Get the Burst time of each processors
4. Sort the processors based on the burst time
5. Calculation of Turn Around Time and Waiting Time
e) tot_TAT = tot_TAT + pre_TAT
f) avg_TAT = tot_TAT/num_of_proc
g) tot_WT = tot_WT + pre_WT + PRE_BT
h) avg_WT = tot_WT/num_of_proc
6. Display the result
7. STOP the program
PROGRAM: (SJF Scheduling)
#include<stdio.h>
void main()
{
int temp,j, avgw,avgt;
int n,i,sumw=0,sumt=0;
int burst[20],wait[20],turn[20];
printf("enter the no of processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("burst[%d]:",i);
scanf("%d",&burst[i]);
}
for(i=0;i<n;i++)
{
for( j=0;j<n-1;j++)
{
if(burst[j]>burst[j+1])
{ temp=burst[j];
burst[j]=burst[j+1];
burst[j+1]=temp;
}
}
}
wait[0]=0;
for(i=1;i<n;i++)
{
wait[i]=wait[i-1]+burst[i-1];
sumw=sumw+wait[i];
}
for(i=0;i<n;i++)
{
turn[i]=wait[i]+burst[i];
sumt=sumt+turn[i];
}
avgw=sumw/n;
avgt=sumt/n;
printf("SJF scheduling");
printf("\n\n process \t burst time \t waiting time \t turnaround time \t");
for(i=0;i<n;i++)
{
printf("\n%d \t\t %d \t\t %d \t\t %d \t", i,burst[i],wait[i],turn[i]);
}
printf("\naverage waiting time =%d",avgw);
printf("\naverage turnaroundtime =%d",avgt);
}
OUTPUT: (SJF Scheduling Algorithm)
[students@localhost ~]$ cc sjf.c
[students@localhost ~]$ ./a.out
enter the no of processes 4
burst[0]:10
burst[1]:2
burst[2]:25
burst[3]:20
SJF scheduling
process
burst time
0
2
1
10
2
20
3
25
average waiting time =11
average turnaroundtime =25
RESULT:
Thus the C - program for Shortest Job First (SJF) CPU Scheduling algorithm was
implemented and output verified using various samples.
Ex.No:3c
AIM:
To write a C - Program to implement the Priority CPU scheduling Algorithm
ALGORITHM:
1. START the program
2. Get the number of processors
3. Get the Burst time of each processors
4. Get the priority of all the processors
5. Sort the processors based on the priority
6. Calculation of Turn Around Time and Waiting Time
i) tot_TAT = tot_TAT + pre_TAT
j) avg_TAT = tot_TAT/num_of_proc
k) tot_WT = tot_WT + pre_WT + PRE_BT
l) avg_WT = tot_WT/num_of_proc
7. Display the result
8. STOP the program
PROGRAM: (Priority Scheduling)
#include<stdio.h>
void main()
{
int avgw,avgt;
int n,i,sumw=0,sumt=0;
int burst[n],wait[n],turn[n];
printf("enter the no of processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("burst[%d]:",i);
scanf("%d",&burst[i]);
}
wait[0]=0;
for(i=1;i<n;i++)
{
wait[i]=wait[i-1]+burst[i-1];
sumw=sumw+wait[i];
}
for(i=0;i<n;i++)
{
turn[i]=wait[i]+burst[i];
sumt=sumt+turn[i];
}
avgw=sumw/n;
avgt=sumt/n;
printf("fcfs scheduling");
printf("\n\n process \t burst time \t waiting time \t turnaround time \t");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t", i,burst[i],wait[i],turn[i]);
printf("\naverage waiting time =%d",avgw);
printf("\naverage turn around time =%d",avgt);
}
OUTPUT: (Priority Scheduling Algorithm)
[students@localhost ~]$ cc priority.c
[students@localhost ~]$ ./a.out
enter the no of processes 4
burst[0]:10
burst[1]:12
burst[2]:13
burst[3]:15
fcfs scheduling
process burst time
waiting time turnaround time
0
10
0
10
1
12
10
22
2
13
22
35
3
15
35
50
average waiting time =16
average turn around time =29
RESULT:
Thus the C - program for Priority CPU Scheduling algorithm was implemented and
output verified using various samples.
Ex.No:3d
AIM:
To write a C - Program to implement the Round Robin CPU scheduling Algorithm
ALGORITHM:
1. START the program
2. Get the number of processors
3. Get the Burst time(BT) of each processors
4. Get the Quantum time(QT)
5. Execute each processor until reach the QT or BT
6. Time of reaching processors BT is its Turn Around Time(TAT)
7. Time waits to start the execution, is the waiting time(WT) of each processor
8. Calculation of Turn Around Time and Waiting Time
m) tot_TAT = tot_TAT + cur_TAT
n) avg_TAT = tot_TAT/num_of_proc
o) tot_WT = tot_WT + cur_WT
p) avg_WT = tot_WT/num_of_proc
9. Display the result
10. STOP the program
PROGRAM: (Round Robin Algorithm)
#include<studio.h>
void main()
{
int p[10],bt[10],tq=0,tat[10],wt[10],n=0,i=0;
int tbt[10],lmore,TRUE=1,FALSE=0,time=0;
printf("Enter the no of process"):
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d burst time",i+1);
scanf("%d",&bt[i]);
tbt[i]=bt[i];
tat[i]=0;
wt[i]=0;
}
printf("Enter the time quantum");
scanf("%d",&tq);
lmore=TRUE;
while(lmore==TRUE)
{
lmore=FALSE;
for(i=0;i<n;i++)
{
if(tbt[i]!=0)
{
if(tbt[i]<=0)
{
time+=tbt[i];
tbt[i]=0;
tat[i]=time;
wt[i]=tat[i]-bt[i];
}
else
{
tbt[i]-=tq;
time+=tq;
lmore=TRUE;
}
}
}
}
printf("\nProcess_no\t Burst time\t Waiting time\t Turn around time\t\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t\n",i+1,bt[i],wt[i],tat[i]);
}
}
OUTPUT: (Round Robin Scheduling Algorithm)
[students@localhost ~]$ cc rr.c
[students@localhost ~]$ ./a.out
Enter the no of process5
Enter the 1 burst time5
Enter the 2 burst time3
Enter the 3 burst time6
Enter the 4 burst time1
Enter the 5 burst time7
Enter the time quantum2
Process_no
Burst time
16
21
15
18
14
15
15
22
RESULT:
Thus the C - program for Round Robin CPU Scheduling algorithm was implemented and
output verified using various samples.
Ex.No:04
AIM:
To Write a C Program to implement file allocation technique.
1. Sequential File allocation
ALGORITHM:
1. Start the process
2. Declare the necessary variables
3. Get the number of files
4.Get the total no. of blocks that fit in to the file
5. Display the file name, start address and size of the file.
6.Stop the program.
PROGRAM:
#include<stdio.h>
main()
{
int f[50],i,st,len,c,count=0;
for(i=0;i<50;i++)
f[i]=0;
do
{
printf("\n enter starting block & length of files");
scanf("%d%d",&st,&len);
for(i=st;i<(st+len);i++)
if(f[i]==0)
count++;
if(len==count)
{
for(i=st;i<(st+len);i++) if(f[i]==0)
{
f[i]=1;
printf("\n%d\t%d",i,f[i]);
if(i==(st+len-1))
printf("\n the file is allocated to disk");
}
}
else
printf("file is not allocated");
count=0;
printf("\n if u want to enter more files(y-1/n-0)");
scanf("%d",&c);
}while(c==1);
}
OUTPUT
[students@localhost ~]$ cc contiguous.c
[students@localhost ~]$ ./a.out
Enter the number of file:4
Enter the number of blocks needed for each file:
3
5
6
1
Filename start size of file
File 1
File 2
File 3
03
35
86
if(count==n)
{ for(j=0;j<nj++)
f[index[j]=1;
printf("\nallocated");
printf("\n file
indexed");
for(k=0;k<n;k++)
printf("\n%d->%d:%d",i,index[k],f[index[k]);
}
else
{
printf("\n file in the index already allocation");
printf("\nenter another file indexed");
goto y;
}
printf("\n index is already allocated");
count=0;
printf("\n if u enter one more block(1/0)");
scanf("%d",&c);
if(c==1)
goto x;
getch( );
}
3. Linked File allocation
ALGORTHIM
:
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location
randomly q= random(100);
a). Check whether the selected location is free . b). If the location is free allocate and
set flag=1 to the allocated locations. While allocating next location address to attach it to
previous location
for(i=0;i<n;i++)
{
for(j=0;j<s[i];j++)
{ q=random(10
0);
if(b[q].flag==0
) b[q].flag=1;
b[q]
.fno=j;
r[i]
[j]=q;
if(j>0)
{
p=r[i][j-1];
b[p].next=q;
}
}
}
Step 5: Print the results fileno, lenth ,Blocks
allocated. Step 6: Stop the program
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n;
for(i=0;i<50;i++)
f[i]=0;
printf("enter how many blocks already allocated");
scanf("%d",&p);
printf("\nenter the blocks nos");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("enter index sarting block & length");
scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)\
{
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++;}}}
else
printf("\nif u enter one more (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}
Result:
Thus the C - program for Contiguous file allocation technique was implemented and
output verified using various samples.
Ex.No:5
IMPLEMENT SEMAPHORES
PRODUCER CONSUMENT PROBLEM USING SEMAPHORES
AIM:
To write a C - Program to solve the Producer Consumer Problem using Semaphores.
ALGORITHM:
1. START the program
2. If the request is for Producer
a. Get the count of number of items to be produced
b. Add the count with current Buffer size
c. If the new Buffer size is full
Dont produce anything; display an error message
Producer has to wait till any consumer consume the existing item
d. Else
Produce the item
Increment the Buffer by the number of item produced
3. If the request is for Consumer
a. Get the count of number of items to be consumed
b. Subtract the count with current Buffer size
c. If the new Buffer size is lesser than 0
Dont allow the consumer to consume anything; display an error message
Consumer has to wait till the producer produce new items
d. Else
Consume the item
Decrement the Buffer by the number of item consumed
4. STOP the program
PROGRAM:
/*Name - program.c*/
#include<stdio.h> // using semaphore
#include<pthread.h>
#include<semaphore.h>
#include<stdio.h>
void* producer(void *arg);
void* consumer(void *arg);
char buff[20];
sem_t full,empty;
int main()
{ pthread_t pid,cid;
sem_init(&empty,0,1);
sem_init(&full,0,0);
pthread_create(&pid,NULL,producer,NULL);
pthread_create(&cid,NULL,consumer,NULL);
pthread_join(pid,NULL);
pthread_join(cid,NULL);
return 1;
}
void* producer(void*arg)
{
int run=1;
while(run)
{
sem_wait(&empty);
printf(\nEnter Mes to be add into buffer:);
scanf(%s,buff);
if(strncmp(buff,end,3)==0)
run=0;
sem_post(&full);
}
return NULL;
}
void* consumer(void *arg)
{
int run=1;
while(run)
{
sem_wait(&full);
printf(\nConsumed item is %s\n,buff);
if(strncmp(buff,end,3)==0)
run=0;
sem_post(&empty);
}
return NULL;
}
// To Compile gcc -o program program.c -lpthread
// To run ./program
OUTPUT: (Producer Consumer problem)
RESULT:
Thus the C - program for producer consumer problem was solved using semaphores and
output verified using various samples.
Ex.No: 06
AIM:
To write a C - Program to implement File Organization Techniques.
PROGRAM:
SINGLE LEVEL DIRECTORY:
#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);
printf("enter number of files");
scanf("&d",&count);
if(i<count)
// for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("enter %d file 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);
i++;
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[i]);
}
}
getch();
}
output:-
(*root)-> ftype=1;
else
(*root)->ftype=2;
(*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(lev==0 || lev==1)
{
if((*root)->level==0)
printf("how many users");
else
printf(" how 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;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:
Thus a c program for implementation of all file organisationt technique was implemented and
output was verified with various example.
{
for(j=0;j<m;j++)
{
if(need[i][j]<=avail[j])
{
k++;
}
}
if(k==m&& completed[i]==0 )
{
printf("P[%d]\t",i);
completed[i]=1;
for(j=0;j<m;j++)
{
avail[j]=avail[j]+allot[i][j];
}
count1++;
}
k=0;
}
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock \n");
break;
}
}
}
OUTPUT(bankers algorithm):
[fosslab@fosslab ~]$ vi bank.c
[fosslab@fosslab ~]$ cc bank.c
[fosslab@fosslab ~]$ ./a.out
enter the no of process4
enter no of resources3
enter no of available instances3 3 2
enter the max.no of instances of resource that a process needfor p[0]7 5 3
for p[1]3 2 2
for p[2]9 0 4
for p[3]2 2 2
enter no.of instances already allocated to process of a resourcefor p[0]0 1 0
for p[1]2 0 0
for p[2]4 0 1
for p[3]2 1 1
safe sequence isp[1]p[3]p[0]p[2][fosslab@fosslab ~]$
RESULT:
Thus the C - program for Bankers Algorithm for Dead Lock Avoidance was
implemented and output verified using various samples.
Ex.no.08
AIM:
To write a C - Program to implement Dead Lock Detection.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{ int i,j,a=0,b=0,c=0,f[10],t[10][10],al[10][10],ta[10]
[10]; int a1[10][10], max[10][10], n[10][10],
n1,p,k=0; printf(\n enter no.of resources);
scanf(%d,n1);
printf(\nenter the max no .of resources for each type);
for(i=0;i<n1;i++)
scanf(%d,&t[b][i]);
printf(\nenter no .of process);
scanf(%d,&p);
printf(\nenter allocation resources);
for(i=0;i<p;i++)
{ f[i]=0;
for(j=0;j<n1;j++)
scanf(%d,&a1[i][j]);
} for(i=0;i<p;i+
+)
for(j=0;j<n1;j++)
{
if(a1[i][j]<=t[b][j])
{ t[b][j]+=a1[i]
[j]; continue;
}
else
printf(\n wrong resourcesallocation);
printf(\n chance of deadlock occurrence after
allocation);
for(j=0;j<n1;j++)
printf(%d,a1[b][j]);
printf(\n enter the max resources for every process);
for(i=0;i<p;i++)
for(j=0;j<n1;j++);
{ scanf(%d,&max[i]
[j]); n[i][j]=max[i][j]a1[i][j];
}
j=0;
printf(\n needed resources for every process to start
execution);
for(i=0;i<p;i++)
printf(\n%d %d%d,n[i][j],n[i][j+1],n[i][j+2]);
printf(\n safe sequence the sequence of process to compute their execution);
for(a=0;a<(p-c);) for(i=0;i<p;i++)
{ j=
0;
b=0;
if(f[i]==0)
{ if(n[i][j]<=a1[b][j]&&n[i][j+1]<=a1[b]
[j+1]&& n[i][j+2]<=a1[b][j+2])
{
printf(\n process %d execution started and
completed,i+1);
for(k=0;k<n-1;k++)
a1[b][k]+=a1[i][k];
f[i]=1;
c++;
}
else
f[i]=0;
}
}
getch();
}
Ex.No 9
(a)FIFO:
AIM:
To write C - Program to implement FIFO page replacement algorithm
ALGORITHM:
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame_page[6],no,k,page-avl,cont=0;
printf(\n enter Total page in Queue:\n);
scanf(%d,&n);
printf(\n ENTER THE PAGE NUMBER :\n);
for(i=1;i<=n;i++)
scanf(%d,&a[i]);
printf(\n TOTAL frame_pageS ARE :);
scanf(%d,&no);
for(i=0;i<no;i++)
frame_page[i]= -1;
j=0;
printf(\tref string\t page frame_pages\n);
for(i=1;i<=n;i++)
{
printf(%d\t\t,a[i]);
page-avl=0;
for(k=0;k<no;k++)
if(frame_page[k]==a[i])
page-avl=1;
if (page-avl==0)
{
frame_page[j]=a[i];
j=(j+1)%no;
cont++;
for(k=0;k<no;k++)
printf(%d\t,frame_page[k]);
}
printf(\n);
}
printf(Page Fault Is %d,cont);
return 0;
}
OUTPUT
ENTER THE NUMBER OF PAGES: 10
enter number of pages are page-avlable :
TOTAL frame_pageS ARE :3
ref string
page frame_pages
8
8
-1
-1
0
8
0
-1
1
8
0
1
6
6
0
1
0
3
6
3
1
0
6
3
0
9
9
3
0
6
9
6
0
3
9
6
3
0
0
6
3
3
6
1
0
1
3
6
0
1
6
0
1
8
8
1
6
0
8
0
6
1
8
0
1
Page Fault Is 15
80160309630361601801
RESULT:
Thus the C - Program for FIFO page replacement was implemented and output verified
using various samples.
(b)LRU:
AIM:
To write a C - program to implement LRU page replacement algorithm
ALGORITHM :
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define high 37
void main()
{
int fframe[10],used[10],index;
int count,n1,k,nf,np=0,page[high],tmp;
int flag=0,pf=0;
clrscr();
printf("Enter no. of frames:");
scanf("%d",&nf);
for(i=0;count<nf;count++)
fframe[count]=-1;
printf(" lru page replacement algorithm in c ");
printf("Enter pages (press -999 to exit):\n");
for(count=0;count<high;count++)
{
scanf("%d",&tmp);
if(tmp==-999) break;
page[count]=tmp;
np++;
}
for(count=0;count<np;count++)
{
flag=0;
for(n1=0;n1<nf;n1++)
{
if(fframe[n1]==page[count])
{
printf("\n\t");
flag=1;break;
}
}
/* program for lru page replacement algorithm in c */
if(flag==0)
{
for(n1=0;n1<nf;n1++) used[n1]=0;
for(n1=0,tmp=count-1;n1<nf-1;n1++,tmp--)
{
for(k=0;k<nf;k++)
{
if(fframe[k]==page[tmp])
used[k]=1;
}
}
for(n1=0;n1<nf;n1++)
if(used[n1]==0)
index=n1;
fframe[index]=page[count];
printf("\nFault: ");
pf++;
}
for(k=0;k<nf;k++)
printf("%d\t",fframe[k]);
} // lru algorithm in c
printf("\nnumber of total page faults is: %d ",pf);
getch();
}
OUTPUT:
Enter no. of frames Enter pages (press -999 to exit):
2321
5245
3 2 5 -999
-1 -1 2 Fault:
-1 -1 2 Fault:
-1 3 2 Fault:
1 3 2 Fault:
132
1 5 2 Fault:
4 5 2 Fault:
452
4 5 3 Fault:
2 5 3 Fault:
253
253
Result:
Thus the C - program for LRU Page replacement was implemented and output verified
using various samples.
Ex.No:10
(a)Shared memory:
AIM:
To write a C Progarm to implement the interprocess communication using shared memory.
ALGORITHM:
1. Start the program
2. Declare the necessary variables
3. shmat() and shmdt() are used to attach and detach shared memory segments. They are
prototypes as follows:
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
4. shmat() returns a pointer, shmaddr, to the head of the shared segment associated with a valid
shmid. shmdt() detaches the shared memory segment located at the address indicated by shmaddr
5. Shared1.c simply creates the string and shared memory portion.
6. Shared2.c attaches itself to the created shared memory portion and uses the string
(printf)
7. Stop the program.
PROGRAM:
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child, shmid, I;
char *shmptr;
child=fork();
if(!child)
{
shmid=shmget(4041,32,0666| IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf(\nParent writing:\t);
for(I=0;I<10;I++)
{
shmptr[I]=a+I;
putchar(shmptr[I]);
}
printf(\n);
wait(NULL);
}
else
{
shmid=shmget(4041,32,0666);
shmptr=shmat(shmid,0,0);
printf(\nChild is reading:\t);
for(I=0;I<10;I++)
putchar(shmptr[I]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
printf("\n");
return 0;
}
OUTPUT:
[root@localhost ~]# ./ipc
Parent writing: abcdefghij
Child is reading: abcdefghij
RESULT:
Thus the C - program for interprocess communication using shared memory was
implemented and output verified using various samples.
(b)PIPES:
AIM:
To write a C Program to implement the interprocess communication using pipes.
ALGORITHM:
1. Start the program
2. Create the child process using fork()
3. Create the pipe structure using pipe()
4. Now close the read end of the parent process using close()
5. Write the data in the pip[e using write()
6. Now close the write end of child process using close()
7. Read the data in the pipe using read()
8. Display the string
9. Stop the program.
PROGRAM:
#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];
printf(Enter the string to enter into the pipe:\t);
scanf(%s, a);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],a,5);
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf(The string received from the pipe is %s, a);
}
return 0;
}
OUTPUT:
[root@localhost ~]# ./pipes
Enter the string to enter into the pipe: Welcome
The string retrieved from the pipe is Welcome
RESULT:
Thus the C - program for interprocess communication using pipes was implemented and
output verified using various samples.
(c)message queue:
AIM:
To write a C Program to implement the Interprocess communication using message passing.
ALGORITHM:
1. Start the program
2. Create two files msgsend and msgrecv.c
3. Msgsend creates a message queue with a basic key and message flag msgflg =IPC_CREAT |
0666 -- create queue and make it read and appendable by all, and sends one message to the queue.
4. Msgrecv reads the message from the queue
5. A message of type (sbuf.mtype) 1 is sent to the queue with the message ``Did you get
this?''
6. The Message queue is opened with msgget (message flag 0666) and the same key as
message_send.c
7. A message of the same type 1 is received from the queue with the message ``Did you get
this?'' stored in rbuf.mtext.
8. Stop the program.
PROGRAM:
// msgsend.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#define MSGSZ 128
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
message_buf sbuf;
size_t buf_length;
key = 1234;
(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\
%#o)\n",
key, msgflg);
if ((msqid = msgget(key, msgflg )) < 0) {
perror("msgget");
exit(1);
}
else
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
sbuf.mtype = 1;
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
(void) strcpy(sbuf.mtext, "Did you get this?");
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
buf_length = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
printf("Message: \"%s\" Sent\n", sbuf.mtext);
}
// msgrecv.c
exit(0);
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGSZ 128
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid; key_t
key; message_buf
rbuf;
key = 1234;
if ((msqid = msgget(key, 0666)) < 0) {
perror("msgget");
exit(1);
}
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
perror("msgrcv");
exit(1);
}
printf("%s\n", rbuf.mtext);
eixit(0);
}
OUTPUT:
[students@localhost ~]$ cc msgrecv.c
[students@localhost ~]$ ./a.out
Did you get this?
RESULT:
Thus the C - program for interprocess communication using message passing was
implemented and output verified using various samples.
Ex.No.11
AIM:
A program to simulate Paging technique of memory management.
IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING FIRST FIT:
ALGORITHM:
1. Start the program.
2. Enter the number of blocks.
3. Enter the base address and size for each block.
4. Enter the size of process.
5. Compute and allocate the first block which is large enough to hold the process
request.
6. Stop the program.
PROGRAM
:
#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,psize,baddr[20];
line();
printf("\n\t\tFIRST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}
OUTPUT:
-------------------------------------------------------------------------------FIRST FIT PLACEMENT ALGORITHM
-------------------------------------------------------------------------------Enter the number of blocks:4
Enter base address of block no 1:10
Enter free block size no. 1:45
Enter base address of block no 2:60
Enter free block size no. 2:75
Enter base address of block no 3:150
Enter free block size no. 3:100
Enter base address of block no 4:300
Enter free block size no. 4:140
Enter the size of the process to be fit:50
-------------------------------------------------------------------------------BEFORE ALLOCATION:
Block No.
Base Address
1
10
45
2
60
75
Block Size
3
150
100
4
300
140
-------------------------------------------------------------------------------AFTER ALLOCATION:
Block No.
Base Address Block Size
1
10
45
2
110
25
3
150
100
4
300
140
IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING BEST FIT
ALGORITHM:
1. Start the program.
2. Enter the number of blocks.
3. Enter the base address and size for each block.
4. Enter the size of process.
5. Compute and allocate the smallest block which is large enough to hold the process
request.
6. Stop the program.
PROGRAM
:
#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,j,temp,psize,baddr[20];
line();
printf("\n\t\tBEST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(bsize[j]>bsize[j+1])
{
temp=bsize[j];
bsize[j]=bsize[j+1];
bsize[j+1]=temp;
temp=baddr[j];
baddr[j]=baddr[j+1];
baddr[j+1]=temp;
}
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}
OUTPUT:
-------------------------------------------------------------------------------BEST FIT PLACEMENT ALGORITHM
-------------------------------------------------------------------------------Enter the number of blocks:4
Enter base address of block no 1:10
Enter free block size no. 1:45
Enter base address of block no 2:60
Enter free block size no. 2:75
Enter base address of block no 3:150
Enter free block size no. 3:100
Enter base address of block no 4:300
Enter free block size no. 4:140
Enter the size of the process to be fit:50
-------------------------------------------------------------------------------BEFORE ALLOCATION:
Block No.
Base Address Block Size
1
10
45
2
60
75
3
150
100
4
300
140
-------------------------------------------------------------------------------AFTER ALLOCATION:
Block No.
Base Address Block Size
1
10
45
2
110
25
3
150
100
4
300
140
-------------------------------------------------------------------------------RESULT:
Thus the C - program for Paging Technique of memory management was implemented
and output verified using various samples
Ex.No.12
IMPLEMENT
TH R E A D I N G
&
S Y N C H R O N I ZATI O N AP P L I C ATI O N S
AIM:
To implement Threading and Synchronization applications using C-Programming.
Program:
Thread Cretation:
#include <pthread.h>
pthread_attr_t tattr;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret;
/* default behavior*/
ret = pthread_create(&tid, NULL, start_routine, arg);
/* initialized with default attributes */
ret = pthread_attr_init(&tattr);
/* default behavior specified*/
ret = pthread_create(&tid, &tattr, start_routine, arg);
Thread Termination:
#include <pthread.h>
pthread_t tid;
int ret;
int status;
/* waiting to join thread "tid" with status */
ret = pthread_join(tid, &status);
/* waiting to join thread "tid" without status */
ret = pthread_join(tid, NULL);
Process 1:
// for WinXp
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
int main()
{
// one process creates the mutex object.
HANDLE hMutex;
char * MName = "MyMutex";
hMutex = CreateMutex(
NULL,
// no security descriptor
FALSE, // mutex not owned
MName); // object name
if (hMutex == NULL)
printf("CreateMutex(): error: %d.\n", GetLastError());
else
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
printf("CreateMutex(): opened existing %s mutex.\n", MName);
else
printf("CreateMutex(): new %s mutex successfully created.\n", MName);
}
return 0;
}
Process 2:
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hMutex1;
hMutex1 =
OpenMutex( MUTEX_ALL_ACCESS, // request
full access FALSE,
// handle not inheritable
MName);
// object name
if (hMutex1 == NULL)
printf("OpenMutex(): error: %d.\n", GetLastError());
else
printf("OpenMutex(): %s mutex opened successfully.\n", MName);
return 0;
}
Prcess 3:
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
int main()
{
// one process creates the mutex object.
HANDLE hMutex;
char * MName = "MyMutex";
hMutex = CreateMutex(
NULL,
// no security descriptor
FALSE, // mutex not owned
MName); // object name
if (hMutex == NULL)
printf("CreateMutex(): error: %d.\n", GetLastError());
else
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
else
{
printf( the last error: %d \n, GetLatError());
printf(Create New Semaphore(): New semaphore was successfully created:\n);
}
// if new semaphore was created, return the handle. return
hSem;
}
int main()
{
LPSECURITY_ATTRIBUTES lpsa = NULL; LONG
cInitial = 0;
LONG cMax = 10;
LPTSTR lpszName = "MySemaphore";
HANDLE hSemaphore = CreateNewSemaphore(lpsa, cInitial, cMax, lpszName);
return 0;
}
RESULT:
Thus the C - program for implementing Threading and Synchronization
application was implemented and output verified using various samples.