Os Lab Manual
Os Lab Manual
YEAR/SEM : II/IV
REGULATION : 2021
Vision
To facilitate technical education with high quality and ethics for developing
Mission:
Information Technology
transfer
SHELL PROGRAMMING
2
5 IMPLEMENTATION OF SEMAPHORES
10 IMPLEMENTATION OF INTERPROCESS
COMMUNICATION
Head
This command displays the initial part of the file. By default it displays first 10 lines of the file.
Syntax: $head filename
Example: $head file1 (It will show the top 10 lines of file1)
Tail
This command displays the later part of the file. By default it displays first 10 lines of the file.
Syntax: $tail filename
Example: $tail -4 file1 (It will show the last 4 lines of the file1)
Line, Word, character counting (wc)
This command is used to count the number of lines, words and characters of information contained in
a file.
Syntax: $wc [option] filename
Options, l – to count the number of lines
c – to count the number of characters
w – to count the number of words
Example: $wc file1
Sort a file or directory (sort)
Used to sort file or directory contents in ascending and descending order.
Syntax: $sort filename (ascending) $sort –r filename (descending)
Example: $sort file1
Data structures
Fundamentals
Problem solving
$sort –r file1
Problem solving
Fundamentals
Data structures
Find file contents (grep)
This command is used to find particular information in a file
Syntax: $grep [pattern] filename
Example: $grep hai file1
hai
Example: $chmod u+r file1 (read mode enabled to the users of the file1)
Absolute Mode:
Syntax: $chmod [octalnumber] filename
Example: $chmod 641 file1
Find file (or) directory (find)
Used to find a particular file or a directory
Syntax: $find filename
Example: $find file1
file1
ln(link)
Establish link between more than one file. The modification of one file reflects in others also.
Syntax: $ln filename1 filename2
Example: $ln raja surya
od(octaldump)
Used to display the ascii octal value of the file contents.
Syntax: od [option] filename
Options, c – to display content of the file in characters
b – to display octal value of the content of file
cd – to display octal value and its character of the file.
Example: od –c file1
Who
It displays user names who are all logging on a system
Syntax: $who
Example: student pts/1 Apr 20 14:40(192.17.6.2)
student2 pts/2 Apr 20 14:10(192.17.6.34)
student5 pts/5 Apr 20 14:16(192.17.6.5)
Who am i
This command tells the login details of a user.
Syntax: $who am i
Example: student pts/1 Apr 20 14:40(192.17.6.2)
Man
The man command displays the online manual pages.
Syntax: $man command name
Example: man ls
tty
Knowing the name of the terminal.
Syntax: $tty Output: /dev/tty 02
Echo
This command used to display the list after the keyword echo.
Syntax: $echo list
Example: $echo unix lab
unix lab
Date
This command used to display system date information
Syntax: $date
Example: $date - To display the system date and time.
Mon Apr 20 15:41:50 EDT 2009
$date +%m - To display only the month number.
4
$date +%h - To display month in character.
April
$date +”%h%m” - To display month in character and number
April 4
Write
This write command used to send message when the user logging in.
Syntax: $write username message
Example: $write user01 hello
Mail
It allows to send message if the user doesn’t logging in.
Syntax: $mail username
Subject:
//message//
CC:
Example: $mail student
Subject: hai
*****
*****
Pipe symbol
The pipe enables the user to take the output from command1 and feed it directly into the input of
command2.This is done the character “|”, which is placed between the two commands.
Syntax: $command1|command2|…|commandn
Example: $who|wc –l|temp
Combining commands
More than one command can be given in the same command line. Each command must be separated
by a semicolon (;). There are two types
Sequential commands
Give several commands on the same line by separating them with a semicolon (;)
Syntax: $command1; command2; commandn
Example:: $chmod + rwx emp;ls – l emp
Command grouping
We can group commands using parentheses. Each command inside the parentheses must be separated
by semicolon. The grouped commands output will be sent to another file. We can see the output by
opening the file.
Syntax: $(commnad1;command2)>filename
Example: $(date;ls –l emp)>temp
Process management:
In Linux process management commands are used to the following purpose:
Identifying process
Monitoring and controlling the process
Modifying and obtaining various information from process
The most familiar process management commands are ps, top, pstree and kill, etc,
Ps:
This command is used to display the process status
Syntax: $ps
Example: $ps
Output:
PID Terminal Time Command
1096 pts/1 00.01.19 bash
1081 pts/1 00.05.23 man
Pgrep:
It provides an interface, enabling to select processes using simple pattern matching
It lists the process IDs (PIDs) of processes matching the specified pattern.
Syntax: $pgrep [option] [pattern]
Example: $pgrep bash
Pidof
It enables user to locate the Process ID (PID) of a process by name
Syntax: $pidof [option] process
Example: $pidof man
Pstree:
Pstree displays all running process as a tree with init as root.
It will display the parent – child relationship between processes in a clear manner.
Syntax: $pstree [options]
Top:
Top command displays real time CPU and memory usage and current system uptime
information.
It is tool in every system administrator toolbox.
Syntax: $top [options]
Tload:
The tload command display a real-time text mode graph of the system’s load average.
Syntax: $tload[options]
Terminating the process:
These terminating commands are used to stop the individual or all process.
Kill:
This is used to terminating the process.
This is the /bin/kill command.
Syntax: $kill [option] IDs
Example: $kill 1029
Kill all:
Kill processes by command name. If more than one process is running the specified command, kill all
of them. Treat command names that contain a / as files; kill all processes that are executing that file.
Syntax: $killall [options] names
Modifying process priorities:
These commands are used to modify the running process.
Nice:
This command enables the user to start a program with a higher or lower nice number.
The priority values are “-20 to +19”. The smaller values represent a higher priority relative to
other programs
Syntax: $nice [option] [command [arguments]]
Renice:
It alters the scheduling priority of one or more processes.
It may be applied to a process, process group, or user (target).
Syntax: $renice [priority] [options] [target]
CONCLUSION:
Thus the study and execution of basic shell commands has been completed successfully.
Ex.No 2.
SHELL PROGRAMMING
Aim
To get the input and display the academic, personal details.
Algorithm
1. Get name, age, and address from the user.
2. Print that message as similar.
3. Get mark1, mark2, and mark3 from the user.
4. Print that message as similar.
Program
#display.sh
echo –n “Enter the name”
read s
echo –n “Enter the age”
read a
echo –n “Enter the address”
read adr
echo –n “The name is $s”
echo –n “The age is $a”
echo –n “The address is $adr”
echo –n “Enter the mark1”
read m1
echo –n “Enter the mark2”
read m2
echo –n “Enter the mark3”
read m3
echo –n “The mark1 is $m1”
echo –n “The mark2 is $m2”
echo –n “The mark3 is $m3”
Sample Input & Output
$ Enter the name john
Enter the age 17
Enter the address madurai
The name is john
The age is 17
The address is madurai
Enter the mark1 90
Enter the mark2 90
Enter the mark3 80
The mark1 is 90
The mark2 is 90
The mark3 is 80
RESULT :
Thus the shell program to getting and displaying the academic and personal details was written and
executed successfully.
Aim
To perform all arithmetic operations
Algorithm
1. Read two numbers from the user.
2. Add both the numbers.
3. Print the summation of two numbers.
4. Subtract the second number from the first number
5. Print the result.
6. Multiply the two numbers
7. Print the result.
8. Divide the first number by second number.
Program
Aim
To check whether the given number is odd or even.
Algorithm
1. Get a number from the user.
2. Divide the number by 2.
3. Check the remainder of the division.
4. If it is zero then the given number is even.
5. Otherwise the given number is odd.
Program
11 is a odd number
RESULT :
Thus the shell program using conditional statements was written and executed successfully.
Ex. No .2d POSITIVE OR NEGATIVE
Aim
To check whether the given number is +ve or –ve or zero.
Algorithm
1. Get a number from the user.
2. Check the number is greater than or less than zero or equal to zero.
3. If it is greater than zero then the given number is +ve.
4. If it is less than zero then the given number is –ve. Otherwise the given number is zero.
Program
RESULT :
Thus the shell program to check whether the given number is +ve or –ve or zero was written and executed
successfully.
Ex. No .2e BIGGEST AMONG THREE NUMBERS
Aim
To find a biggest number among three numbers.
Algorithm
Program
RESULT :
Thus the shell program to find a biggest number among three numbers was written and executed
successfully.
Aim
Algorithm
Program
echo “Enter two numbers” read a b
echo “menu” echo “1.add” echo “2.sub” echo “3.mul” echo “ 4. div”
echo “Enter your option” read option
case $option in
1)c=`expr $a + $b`;; 2)c=`expr $a - $b`;; c=`expr $a \* $b`;; c=`expr $a / $b`;; esac
echo $c
RESULT :
Thus the shell program to perform all the arithmetic operations using case statements was written and
executed successfully.
Ex. No .2f Finding the area of circle, square, rectangle and triangle
Aim
To find the area of circle, square, rectangle and triangle using case statements.
Algorithm
Program
RESULT :
Thus the shell program to find the area of circle, square, rectangle and triangle using case statements was
written and executed successfully.
Aim
Algorithm
Program
RESULT :
Thus the shell program to generate a fibonacci series of first n numbers was written and executed
successfully.
Aim
Algorithm
Program
echo -n "enter the number" read num
x=$num
sum=0
while [ $num -gt 0 ] do
y=`expr $num % 10` z=`expr $y \* $y \* $y` sum=`expr $sum + $z` num=`expr $num / 10`
done
if [ $x -eq $sum ] then
echo $x is an Armstrong number else
echo $x is not an Armstrong number
fi
RESULT :
Thus the shell program to check whether the given number is Armstrong or not was written and executed
successfully.
Aim
To write a shell program to find the sum of even numbers
Algorithm
1. Start the program
2. Read the number
3. Initialize i=2
4. Calculate sum=sum+1
5. Print the sum
6. Stop the program execution
Program
echo “enter the number of upper limit” read n
I=2
C=0
while [ $I –lt $n ] do
echo “number is $I” I=`expr $I + 2‟ C=`expr $I + $C‟ done
echo “ the sum is $c”
RESULT :
Thus the shell program to write a shell program to find the sum of even numbers was written and executed
successfully.
Ex. No .3 IMPLEMENT THE FOLLOWING CPU SCHEDULING ALGORITHMS
DESCRIPTION
The FCFS scheduler simply executes processes to completion in the order they are submitted.
This code implements the First come First Served Scheduled algorithm. It gets the number of
processes and the process name, burst time and arrival time for each user from the user. It then sorts the
processes depending upon the arrival time. Calculates and displays the average
turnaround time and waiting time. Then it will display the Gantt chart.
ADVANTAGES
simple
easy to understand first come, first served
DISADVANTAGES
This scheduling method is nonpreemptive, that is, the process will run until it finishes. Because of this
nonpreemptive scheduling, short processes which are at the back of the queue have to wait for the long
process at the front to finish
ALGORITHM:
1. Start the program.
2. Get the number of processes and their burst time.
3. Initialize the waiting time for process 1 is 0.
4. The waiting time and turn around time for other processes are calculated as
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
5. The waiting time and turn around time for all the processes are summed and then the
6. average waiting time and turn around time are calculated.
7. The average waiting time and turn around time are displayed.
8. Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct process
{
int pid;
int wt,tt,bt;
}
p[100];
void main()
{
int tottt,totwt,avgtt,avgwt,i,j,n;
clrscr();
printf("\nEnter Number of process"); scanf("\n%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\nEnter %dst Burst Time:",i); scanf("\n%d",&p[i].bt);
}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].tt; p[i].tt=p[i].bt+p[i].wt; i++;
}
totwt=tottt=0;
i=1;
printf("\n\t============================="); printf("\n\tProcess id\tBT\tWT\tTT");
printf("\n\t============================="); while(i<=n)
{
printf("\n\t%d\t%d\t%d\t%d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
totwt=totwt/n;
tottt=tottt/n;
printf("\n\n\n\tAVGWT\tAVGTT");
printf("\n\t%d\t%d",totwt,tottt);
getch();
}
OUTPUT
Enter Number of process3
Enter 1st Burst Time:12
Enter 2st Burst Time:34
Enter 3st Burst Time:56
===============================
Process id BT WT TT
===============================
1 12 0 12
2 34 12 46
3 56 46 102
VGWT AVGTT
19 53
RESULT
The program for first come first serve algorithm has been executed and shown the results as above.
DESCRIPTION
Associate with each process the length of its next CPU burst. Use these lengths to schedule the process
with the shortest time Two schemes:
non preemptive once CPU given to the process it cannot be preempted until completes its CPU
burst
preemptive if a new process arrives with CPU burst length less than remaining time of
current executing process, preempt. This scheme is known as the Shortest-Remaining-
Time-First (SRTF) SJF is optimal gives minimum average waiting time for a given set of processes
objective
EXPECTED OUTPUT AND ITS FORM
Performs SJF scheduling ie) the process that has the shortest burst time is processed first
ADVANTAGES
simple
easy to understand
shortest job is served first
DISADVANTAGES
Long-burst (CPU-intensive) processes are hurt with a long mean waiting time. In fact, if shortburst
processes are always available to run, the long-burst ones may never get scheduled. Moreover, the
effectiveness of meeting the scheduling criteria relies on our ability to estimate the CPU burst time.
ALGORITHM:
1. Start the program.
2. Get the number of processes and their burst time.
3. Initialize the waiting time for process 1 is 0.
4. The processes are stored according to their burst time.
5. The waiting time and turn around time for other processes are calculated as
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
6. The waiting time and turn around time for all the processes are summed and then the
7. average waiting time and turn around time are calculated.
8. The average waiting time and turn around time are displayed.
9. Stop the program
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct process
{
int pid;
int wt,tt,bt;
}p[100];
void main()
{
struct process temp;
int tottt,totwt,avgtt,avgwt,i,j,n; clrscr();
printf("\nEnter Number of process");
scanf("\n%d",&n); for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\nEnter %d Burst Time:",i);
scanf("\n%d",&p[i].bt);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].bt>p[j].bt)
{
/*temp.pid=p[j].pid;
p[j].pid=p[i].pid;
p[i].pid=temp.pid;
temp.bt=p[j].bt;
p[j].bt=p[i].bt; p[i].bt=temp.bt; */
temp=p[j]; p[j]=p[i];
p[i]=temp;
}}}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].tt; p[i].tt=p[i].bt+p[i].wt; i++;
}
totwt=tottt=0;
i=1;
printf("\n\t=================================");
printf("\n\t Process id \tBT\tWT\tTT");
printf("\n\t================================");
while(i<=n)
{
printf("\n\t%d\t%d\t%d\t%d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
totwt=totwt/n;
tottt=tottt/n;
printf("\n\n\n\tAVGWT\tAVGTT");
printf("\n\t%d\t%d",totwt,tottt);
getch();
}
OUTPUT
Enter Number of process:5
Enter 1 Burst Time: 8
Enter 2 Burst Time: 4
===================================
Process id BT WT TT
===================================
4 2 0 2
2 4 2 6
3 7 6 13
1 8 13 21
5 9 21 30
AVGWT AVGTT
8 14
RESULT:
The program for shortest job first algorithm has been executed and shown the results as above.
DESCRIPTION
Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time
has elapsed, the process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the
CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
Performance q large –FIFO q small- q must be large with respect to context switch, otherwise overhead is
too high
EXPECTED OUTPUT AND ITS FORM
Performs round robin scheduling ie) the process that is assigned with a Quantum time and the
process is processed for the given quantum time in fcfs fashion.
ADVANTAGES:
simple,
low overhead,
works for interactive systems
DISADVANTAGES:
if quantum is too small, too much time wasted in context switching
if too large (i.e., longer than mean CPU burst), approaches
ALGORITHM:
1. Start the program.
2. Get the number of processes, their burst time and time slice for each process.
3. Calculate the total burst time.
4. To check whether the burst time of process is less then the time slice, the required time only
allotted to the process.
5. If the burst time of process is greater than the time slice one time slice is allotted for the process
and burst time is subtracted by one time slice.
6. Step 4 and 5 is repeated up to process burst time is 0.
7. The waiting time and turn around time for all the processes are summed and then the
average waiting time and turn around time are calculated.
8. The average waiting time and turn around time are displayed.
9. Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
struct process
{
int pid,bt,tt,wt; };
int main()
{
struct process x[10],p[30]; int i,j,k,tot=0,m,n;
float wttime=0.0,tottime,a1,a2; clrscr();
printf("\nEnter No.of process\t"); scanf("%d",&n);
for(i=1;i<=n;i++)
{
x[i].pid=i;
printf("Enter The Burst Time \t");
scanf("%d",&x[i].bt); tot=tot+x[i].bt;
}
printf("Total Burst Time:\t%d",tot); p[0].tt=0;
k=1;
printf("\nEnter The Time Slice:\t");
scanf("%d",&m); for(j=1;j<=tot;j++){
for(i=1;i<=n;i++){
if(x[i].bt!=0)
{
p[k].pid=i;
if(x[i].bt-m<0)
{
p[k].wt=p[k-1].tt; p[k].bt=x[i].bt;
p[k].tt=p[k].wt+x[i].bt; x[i].bt=0;
k++;
}
else{
DESCRIPTION
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest integer º highest priority)
- Preemptive
- non preemptive
SJF is a priority scheduling where priority is the predicted next CPU burst time
Problem Starvation – low priority processes may never execute
Solution Aging – as time progresses increase the priority of the process
Performs priority scheduling ie) the process that is assigned with a highest priority is processed first.
ADVANTAGES:
simple,
low overhead,
works for interactive systems
DISADVANTAGES:
The job remains in the queue for a long time.
ALGORITHM:
1. Start the program.
2. Get the number of processes, their burst time and priority.
3. Initialize the waiting time for process 1 is 0.
4. Based upon the priority processes are arranged.
5. The waiting time and turn around time for other processes are calculated as
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
6. The waiting time and turn around time for all the processes are summed and then the
7. average waiting time and turn around time are calculated.
8. The average waiting time and turn around time are displayed.
9. Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct process
{
int pid;
int wt,tt,bt,prior;
}
p[100],temp;
void main()
{
int tottt,totwt,avgtt,avgwt,i,j,n; clrscr();
printf("\nEnter Number of process");
scanf("\n%d",&n); for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\nEnter %dst Burst Time:",i);
scanf("\n%d",&p[i].bt);
printf("\nEnter %dpriority",i); scanf("\n%d",&p[i].prior);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].prior>p[j].prior)
{
temp=p[j];
p[j]=temp;
}}}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].tt; p[i].tt=p[i].bt+p[i].wt; i++; }
totwt=tottt=0;
i=1;
printf("\n\t===============================");
printf("\n\tPSSID\tBT\tWT\tTT");
printf("\n\t===============================");
while(i<=n)
{
printf("\n\t%d\t%d\t%d\t%d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
totwt=totwt/n;
tottt=tottt/n;
printf("\n\n\n\tAVGWT\tAVGTT");
printf("\n\t%d\t%d",totwt,tottt);
getch();
OUTPUT:
Enter Number of process3
Enter 1st Burst Time:5
Enter 1priority3
Enter 2st Burst Time:7
Enter 2priority1
Enter 3st Burst Time:8
Enter 3priority2
===============================
PSSID BT WT TT
===============================
1 5 0 5
2 7 5 12
3 8 12 20
AVGWT AVGTT
5 12
RESULT
The program for priority scheduling has been executed and shown the results as above.
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\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
OUTPUT:
1 2 4
2 5 10
RESULT
The program for implement Sequential File Allocation method has been executed and shown the results as
above.
Algorithm:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.
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\t index\tlength\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("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
OUTPUT:
2 5 4 6 7 2 6 4 7
1 2 10
2 3 5
RESULT
The program for implement Sequential File Allocation method has been executed and shown the results as
above.
ALGORITHM:
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("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\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]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
OUTPUT:
Enter no. of files:2
Enter file name:venkat
Enter starting block:20
Enter no.of blocks:6
Enter block numbers: 4
12
15
45
32
25
Enter file name:rajesh
Enter starting block:12
Enter no.of blocks:5
Enter block numbers:6
5
4
3
2
File start size block
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2
RESULT
The program to implement Linked File Allocation method has been executed and shown the results as
above.
ALGORITHM:
Step 1: Start the program
Step 2: Get the number of jobs from the user
Step3: When job1 is processing, job 2 is also starts processing
Step 4: When job 1 enters critical section, next job starts processing
Step 5: When job1 comes out of the critical section, the other job enters the critical section
Step 6: The above 3 steps are performed for various programs Step 7: End the program.
PROGRAM:
#include<stdio.h>
main()
{
int i,a=1,h=2,n;
printf("\n Enter the no of jobs"); scanf("%d",&n);
for(i=0;i<n;i++)
{
if(a==1)
{
printf("processing %d ...................! \n", i+1);
a++;
}
if(h>1)
{
if(i+2<=n)
{
printf("\n processing %d ...................! \n",i+2);
}
printf("\n Process %d Enters Critical section", i+1);
printf("\n Process %d Leaves Critical section", i+1);
}
h+1;
}
}
OUTPUT:
Enter the no of jobs
2
processing 1....................!
processing 2...................!
Process 1 Enters Critical section
Process 1 Leaves Critical section
Process 2 Enters Critical section
Process 2 Leaves Critical section
Result
Thus the program to implement semaphores in c was executed and the output was
verified.
OBJECTIVE:
To write a C program for the implementation of file organization echniques in two level directory.
OUTCOME:
Students become expertise to write a C coding to implement different file organization techniques.
SAMPLE TITLE:
C program for the implementation of file organization techniques in directories
#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:-
Result
Thus the program to implementation of file organization techniques was executed and the output was
verified.
OBJECTIVE:
To write a C program for the implementation of file organization echniques in two level directory.
OUTCOME:
Students become expertise to write a C coding to implement different file organization techniques.
SAMPLE TITLE:
C program for the implementation of file organization techniques in two level directories
PROGRAM:-
#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,"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 the name of ir file name %s",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0 || lev==1)
(*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 the program to implementation of file organization techniques was executed and the output was
verified.
#include< stdio.h>
#include< conio.h>
void main()
{
int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15]; int
pno,rno,i,j,prc,count,t,total;
count=0;
clrscr();
printf("\n Enter number of process:"); scanf("%d",&pno);
printf("\n Enter number of resources:"); scanf("%d",&rno);
for(i=1;i< =pno;i++)
{
flag[i]=0;
}
printf("\n Enter total numbers of each resources:"); for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
printf("\n Enter Max resources for each process:"); for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i); for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources for each process:"); for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i); for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
}
printf("\n available resources:\n"); for(j=1;j<= rno;j++)
{
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf("%d \t",work[j]);
}
do
{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}
printf("\n Allocated matrix Max need"); for(i=1;i<= pno;i++)
{
printf("\n");
for(j=1;j<= rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0;
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:"); for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j]; allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
}
}
}while(count!=pno&&prc!=0); if(count==pno)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
OUTPUT:
available resources:
2 3 0
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
3 0 2| 3 2 2| 0 2 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 2 completed
Available matrix: 5 3 2
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 4 completed
Available matrix: 7 4 3
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 1 completed
Available matrix: 7 5 3
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 3 completed
Available matrix: 10 5 5
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 5 7
The system is in a safe state!!
Result
Thus the program to implement banker’s algorithm for Dead Lock Avoidance was executed
OUTPUT:
RESULT
Thus the program to implement banker’s algorithm for Dead Lock Detection was executed and the
output was verified.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k=0,flg1,flg2,x=5,y,pfc=0;
clrscr();
printf("\n enter page reference string:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
fp[i]='x';
clrscr();
printf("\n page replacement technique :: FIFO algorithm:");
printf("\n .............................................");
printf("\n F-Page Fault \t H- Page Hit \n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
if(fp[j]==prs[i]){
ps='H';
flg1=1;
break;
}
if(flg1==0)
{
flg2=0;
for(j=0;j<fs;j++)
if(fp[j]=='x')
{
fp[j]=prs[i];
pfc++;
flg2=1;
break;
}
if(flg2==0)
{
pfc++;
fp[k]=prs[i];
k++;
if(k==fs)
k=0; } }
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("--");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n \n\n\n\n Total page Faults=%d",pfc);
getch();
}
RESULT
Thus the program to implement Page Replacement algorithms –FIFO was executed and the output
was verified.
9.BPage Replacement algorithm-LRU:-
Program
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("enter the page reference string:");
gets(prs);
printf("enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='x';
ru[i]=0;
}
clrscr();
printf("PAGE REPLACEMENT TECHNIQUE::LRU algorithm\n");
printf("-----------------------------------\n");
printf("F-Page fault\tH-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
ru[j]=i;
flg1=1;
break;
}}
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=i;
flg2=1;
break;
}}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>ru[j])
min=j;
}
fp[min]=prs[i];
ru[min]=i;
}}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("- -");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n\n total page faults=%d",pfc);
getch();
}
RESULT
Thus the program to implement Page Replacement algorithms –LRU was executed and the output
was verified
10. C program that illustrates inter process communication using shared memory system calls:-
Program
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc, char *argv[ ])
{
int shmid,cntr;
key_t key;
char *segptr;
char buff[]="poooda......";
key=ftok(".",'s');
if((shmid=shmget(key, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666))== -1)
{
if((shmid=shmget(key,SEGSIZE,0))==-1)
{
perror("shmget");
exit(1);
}
}
else
{
printf("Creating a new shared memory seg \n");
printf("SHMID:%d",shmid);
}
system("ipcs –m");
if((segptr=(char*)shmat(shmid,0,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
printf("Writing data to shared memory…\n");
strcpy(segptr,buff);
printf("DONE\n");
printf("Reading data from shared memory…\n");
printf("DATA:-%s\n",segptr);
printf("DONE\n");
printf("Removing shared memory Segment…\n");
if(shmctl(shmid,IPC_RMID,0)== -1)
printf("Can’t Remove Shared memory Segment…\n");
else
printf("Removed Successfully");
}
output:-
RESULT
Thus the program to implement inter process communication using shared memory was executed
and the output was verified
OBJECTIVE
Learn about paging techniques in memory management.
OUTCOME:
Students become expertise to write a C coding to implement paging techniques PRE-REQUEST: C-
Basics.
SAMPLE PROGRAM TITLE:
Implementation of Memory management
ALGORITHM:
Step 1: Start the program.
Step 2: Enter the number of process and block. Step 3: Enter the size of process.
Step 4: Enter the size of each block.
Step 5: Allocate the blocks to its process as it is for first fit.
Step 6: Allocate the first block to a smallest process and vice versa for best fit. Step 7: Allocate the
first block to the largest process and vice versa for worst fit. Step 8: Stop the program.
PROGRAM:
#include<stdio.h> void main()
{
int p[10],np,b[10],nb,ch,c[10],d[10],alloc[10],flag[10],i,j; printf("\nEnter the no of process:");
scanf("%d",&np);
printf("\nEnter the no of blocks:"); scanf("%d",&nb);
printf("\nEnter the size of each process:"); for(i=0;i<np;i++)
{
printf("\nProcess %d:",i); scanf("%d",&p[i]);
}
printf("\nEnter the block sizes:"); for(j=0;j<nb;j++)
{
printf("\nBlock %d:",j);
scanf("%d",&b[j]);c[j]=b[j];d[j]=b[j];
}
if(np<=nb)
{
printf("\n1.First fit 2.Best fit 3.Worst fit");
do
{
printf("\nEnter your choice:"); scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nFirst Fit\n"); for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=b[j])
{
alloc[j]=p[i];printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block:%d of size:%d",i,p[i],j,b[j]);
flag[i]=0,b[j]=0;break;
}else flag[i]=1;
}}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}break;
case 2: printf("\nBest Fit\n"); for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(c[i]>c[j])
{
int temp=c[i];c[i]=c[j];c[j]=temp;
}}}
printf("\nAfter sorting block sizes:"); for(i=0;i<nb;i++)
printf("\nBlock %d:%d",i,c[i]); for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=c[j])
{
alloc[j]=p[i];printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block %d of size %d",i,p[i],j,c[j]); flag[i]=0,c[j]=0;
break;
}
else
flag[i]=1;
}}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}break;
case 3: printf("\nWorst Fit\n"); for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(d[i]<d[j])
{
int temp=d[i];d[i]=d[j];d[j]=temp;
}
}
}
printf("\nAfter sorting block sizes:"); for(i=0;i<nb;i++)
printf("\nBlock %d:%d",i,d[i]); for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=d[j])
{
alloc[j]=p[i];printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block %d of size %d",i,p[i],j,d[j]); flag[i]=0,d[j]=0;
break;
}
else
flag[i]=1;
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}
break;
default:printf("Invalid Choiceǥ!");break;
}
}while(ch<=3);
}
}
OUTPUT:
[com39@localhost os]$ gcc ex8.c
[com39@localhost os]$ ./a.out
Enter the no of process:3
Enter the no of blocks:3
Enter the size of each process: process 0:10
Process 1:20
Process 2:30
Enter the block sizes:
Block 0:50
Block 1:40
Block 2:90
1.First fit 2.Best fit 3.Worst fit Enter your choice:3
Worst Fit
After sorting block sizes:
Block 0:90
Block 1:50
Block 2:40
Alloc[10]
Process 0 of size 10 is allocated in block 0 of size 90
Alloc[20]
Process 1 of size 20 is allocated in block 1 of size 50
Alloc[30]
Process 2 of size 30 is allocated in block 2 of size 40
Enter your choice:2
Best Fit
After sorting block sizes:
Block 0:40
Block 1:50
Block 2:90
Alloc[10]
Process 0 of size 10 is allocated in block 0 of size 40
Alloc[20]
Process 1 of size 20 is allocated in block 1 of size 50
Alloc[30]
Process 2 of size 30 is allocated in block 2 of size 90 Enter your choice:1
First Fit
Alloc[10]
Process 0 of size 10 is allocated in block:0 of size:50
Alloc[20]
Process 1 of size 20 is allocated in block:1 of size:40
Alloc[30]
Process 2 of size 30 is allocated in block:2 of size:90 Enter your choice:4
Invalid Choiceǥ!
[com39@localhost os]$
Result:
Thus the c program for implementing paging technique of memory management using first fit was
executed and the output was verified.
PROGRAM:
#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;
}
OUTPUT:
Job 1 started
Job 2 started
Job 2 finished
Job 2 finished
Result:
Thus the C program for threading and synchronization applications was created and executed
successfully.
PRODUCER-CONSUMER PROBLEM
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
intmutex=1,full=0,empty,x=0;
void main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\nImplementation of producer-consumer problem using semaphores");
printf("\n------------------------------------------------------------");
printf("\n1.Producer\n2.Consumer\n3.Exit\n");
printf("\nEnter buffer size:");
scanf("%d",&empty);
while(1)
{
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
}
Output:
Thus the C program to simulate UNIX system calls fork, getpid, wait has been executed
successfully.
OBJECTIVE:
Acquire operating system skills through System calls & Commands.
OUTCOME:
Students become expertise to write a C coding to simulate UNIX system calls. PRE-REQUEST: C-
Basics.
SAMPLE PROGRAM TITLE:
Demonstration of UNIX system calls opendir(), readdir(), closedir() and exit(). ALGORITHM:
Step 1: Start the program.
Step 2: Create a directory entry in the dirent structure and a directory variable. Step 3: Get the name of the
directory to be opened.
Step 4: Using the system call opendir(),open the given directory and store it in the
directory variable.
Step 5: Read the contents of the directory using readdir()system call and print the list of
files in the directory.
Step 6: Close the directory after the read and write process. Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
#include<stdlib.h>
struct dirent *dptr;
int main(int argc,char *argv[])
{
char buff[256];
DIR *dirp;
printf("\n \n enter directory name"); scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("error");
exit(0);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}
OUTPUT:
[com39@localhost ~]$ cd os
[com39@localhost os]$ gcc ex1b.c
[com39@localhost os]$ ./a.out
ENTER A DIRECTORY NAME: oslabpgms
ex5b.c
ex1b.c
ex1a.c
ex4a.c
ex2.c
ex3a.c
ex1d.c
ex4b.c
ex3b.c
ex1c.c
ex5a.c
[com39@localhost os]$
Result:
Thus the C program to simulate UNIX system calls opendir, readdir, closedir and exit has been
executed successfully.
wait(NULL);
printf("Child Complete \n");
exit(0);
}
OUTPUT:
[com39@localhost ~]$ cd os
[com39@localhost os]$ gcc ex1d.c
[com39@localhost os]$ ./a.out
a.out ex1b.c ex1b.c~ ex1c.c ex1c.c~ ex1d.c ex1d.c~ Child Complete
[com39@localhost os]$
Result:
Thus the C program to simulate UNIX system call execlp has been executed successfully.
ALGORITHM:
PROGRAM:
#include<stdio.h> #include<conio.h> #define max 25 void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0; static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit"); printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:"); scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n"); for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n"); for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i]; if(temp>=0) if(highest<temp)
{
ff[i]=j; highest=temp;
}
}
}
frag[i]=highest; bf[ff[i]]=1; highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement"); for(i=1;i<=nf;i++) printf("\n
%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]); getch();
}
OUTPUT:
OUTPUT
File No File Size Blo F
Block No ck r
Siz a
e g
m
e
n
t
1 1 3 7 6
2 4 1 5 1
RESULT:
Thus the C program for implementation memory allocation methods for fixed partition using first
fit has been executed successfully.
AIM:
To write a C program for implementation memory allocation methods for fixed partition using
worst fit.
ALGORITHM:
PROGRAM:
#include<stdio.h> #include<conio.h> #define max 25 void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp; static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit"); printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:"); scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n"); for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n"); for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i]; if(temp>=0)
{
ff[i]=j; break;
}
}
}
frag[i]=temp; bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement"); for(i=1;i<=nf;i++) printf("\n
%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]); getch();
}
OUTPUT:
O
U
TP
U
T
Fil Fi Bl Blo F
e le oc ck r
No Si k Siz a
ze N e g
o m
e
n
t
1 1 1 5 4
2 4 3 7 3
AIM:
To write a C program for implementation memory allocation methods for fixed partition using Best
fit.
ALGORITHM:
PROGRAM:
#include<stdio.h> #include<conio.h> #define max 25 void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000; static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:"); scanf("%d",&nb);
printf("Enter the number of files:"); scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n"); for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n"); for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i]; if(temp>=0) if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment"); for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]); getch();
}
OUTPUT:
I
N Enter the number of blocks: 3
P
U
T
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
OUTPUT
File No File Size Blo F
Block No ck r
Siz a
e g
m
e
n
t
1 2 2 1
1
4 1 5 1
2
RESULT:
To write a C program for implementation memory allocation methods for fixed partition using Best
fit.
AIM:
To Write a C program to simulate disk scheduling algorithms
a) FCFS b) SCAN c) C-SCAN
PROGRAM
O
U Tracks traversed Difference between tracks
T
P 55 45
U 58 3
T 60 2
70 10
18 52
90 72
150 60
160 10
184 24
15 5
0 0
16 1
0 0
18 2
4 4
90 9
4
70 2
0
60 1
0
58 2
55 3
18 3
7
for(i=0;i<=
n+2;i++)
if(t[i]==h);
j=i;
bre
ak;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j]; j++;
p++;
}
atr[p]=t[j]; p++;
i=0;
while(p!=(n+3) && t[i]!=t[h])
{
atr[p]=t[i]; i++; p++;
}
for(j=0;j<n+2;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("total header movements%d",sum); printf("avg is %f",(float)sum/n);
getch();
}
INPUT
Enter the track position : 55 58 60 70 18 90 150 160 184
Enter starting position : 100
OUTPUT
RESULT:
Thus the C program for simulate disk scheduling algorithms was executed successfully.