CS3461 OS - Lab - Manual
CS3461 OS - Lab - Manual
CS3461
INTRODUCTION TO OPERATING SYSTEM
LAB MANUAL
Ex. Pg. Rema
Date NAME OF THE EXERCISE Sign
No No. rks
1 STUDY OF UNIX AND BASIC SHELL 2
COMMANDS
SHELL PROGRAMMING
2
3A II YEAR IV SEM
IMPLEMENTATION OF ROUND ROBIN
SCHEDULING ALGORITHM
3B
IMPLEMENTATION OF SJF
3C
SCHEDULING
IMPLEMENTATION OF FIRST COME R2021
FIRST SERVE SCHEDULING
IMPLEMENTATION
ALGORITHM OF PRIORITY
3D SCHEDULING ALGORITHM
4A IMPLEMENTATION OF SEQUENTIAL
FILE ALLOCATION STRATEGY
4B IMPLEMENTATION OF INDEXED FILE
ALLOCATION STRATEGY
4C IMPLEMENTATION OF LINKED FILE
ALLOCATION STRATEGY
5
IMPLEMENTATION OF SEMAPHORES CONTENTS
6A IMPLEMENTATION OF FILE
ORGANIZATION TECHNIQUES –SINGLE
6B LEVEL DIRECTORY
IMPLEMENTATION OF FILE
ORGANIZATION TECHNIQUES –TWO
LEVEL DIRECTORY
7
IMPLEMENT BANKERS ALGORITHM
FOR DEAD LOCK AVOIDANCE
8 IMPLEMENT AN ALGORITHM FOR Ex. No. 1 BASICS OF UNIX
DEAD LOCK DETECTION
9A IMPLEMENTATION OF PAGE COMMANDS
REPLACEMENT ALGORITHMS -FIFO
9B IMPLEMENTATION OF PAGE OBJECTIVE:
REPLACEMENT ALGORITHMS –LRU Acquire operating system skills through UNIX Shell Commands.
9C IMPLEMENTATION OF PAGE
REPLACEMENT ALGORITHMS –LFU OUTCOME:
10 IMPLEMENTATION OF INTERPROCESS
COMMUNICATION
11 IMPLEMENTATION OF PAGING
3
Students can understand how to work with UNIX shell commands. PRE-REQUEST: Shell- Basics.
SAMPLE PROGRAM TITLE:
Study of basic shell commands.
UNIX SHELL:
A UNIX shell is a command-line interpreter that provides a user interface for the Unix
operating system . Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one
or more such commands.
Cd - Finally, typing just cd alone will move you into the home directory.
Output: $ cd example
[student@server example]$
View directory (ls)
List the content of a current directory.
Syntax: $ls [-options] name
Options, a - to list all directory entries
d - to list name of directories
l - to list files in long form
t - to list files sorted by name
Example: $ls –l - list each of the files in the current directory and the files permissions, the size of the file, date of the last modification, and
the file name or directory.
Output: [student@server example]$ls –l
Copy directory contents (cp) 5
Syntax: $pwd
Example: $pwd
/home/student/example
Create and view file (cat)
Allows look, modifying or combining a file.
Syntax: cat > filename */create file/*
cat filename */view file/*
Example: $cat > file1
Hai 6
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) 7
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 8
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 9
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
10
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: 11
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 12
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). 14
The administrator can change the default settings for creating user. The information’s are stored in the “/etc/default/useradd” file.
Syntax: $useradd [option] name
Usermod:
The usermod command is used to modify a user account.
It makes the changes in the system files relating to the user accounts, such as the etc/paswd, /etc/shadow and user/group files.
Syntax: $usermod [options] user
Userdel:
This command used to remove the user account.
The userdel command does not allow the user to remove a if the user is currently logged in.
At that time the administrator may need to kill all the process of the user in order to remove the user immediately.
Syntax: $userdel [option] user
Groupadd:
This command is used to create new groups on the Linux system
A group contains more number of users
The group creation is makes a new entry in the /etc/group file.
Syntax: $groupadd [options] group
Groupmod: 15
The groupmod command is used to modify the attributes of a group and write the changes to the system files.
Syntax: $groupmod [options] group
Groupdel:
This command used to remove the group account from the system.
It makes the necessary changes in the system files, such as the /etc/group file, but it does not change the group ID of the files owned by that group.
The administrator needs to manually change the group ID for the files whose group owner the user have deleted.
Syntax: $groupdel group
CONCLUSION:
Thus the study and execution of basic shell commands has been completed successfully.
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 16
#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.
17
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
RESULT :
Thus the shell program to perform all arithmetic operations was written and executed successfully.
19
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.
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.
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
24
RESULT :
Thus the shell program to perform all the arithmetic operations using case statements was written and executed successfully.
25
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.
27
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
29
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
30
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.
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.
32
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;
33
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
34
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.
35
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;
36
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
38
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,
39
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");
40
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{
2 3 6
3 6 9
1 9 11
2 11 14
3 14 17
3 17 18
Average Waiting Time: 20.000000
Average TurnAround Time:26.000000
RESULT:
The program for implementation of round robin scheduling has been executed and shown the
results as above.
Ex.No.3.d. IMPLEMENTATION OF PRIORITY SCHEDULING
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
42
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;
43
}}}
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
44
===============================
PSSID BT WT TT
===============================
1 5 0 5
2 7 5 12
3 8 12 20
AVGWT AVGTT
5 12
45
RESULT
The program for priority scheduling has been executed and shown the results as above.
ALGORITHM:
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]);
47
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.
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();
49
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);
52
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
53
RESULT
The program to implement Linked File Allocation method has been executed and shown the results as above.
ALGORITHM:
#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:
55
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");
57
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:-
58
Result
59
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();
60
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");
61
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);
62
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}
output:-
63
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);
64
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++)
66
{
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
67
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!!
68
Result
Thus the program to implement banker’s algorithm for Dead Lock Avoidance was executed
Step 6: If it is possible then the system is in safe state. Step 7: Else system is not in safety state.
Step 8: If the new request comes then check that the system is in safety. Step 9: Or not if we allow the request.
Step 10: Stop the program.
Step 11: End
PROGRAM:
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algorithm ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t"); scanf("%d",&n);
printf("Enter the no of resource instances\t"); scanf("%d",&r);
printf("Enter the Max Matrix\n"); for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
70
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n"); for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n"); for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t"); for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1); for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
71
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0; int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j]; finish[i]=1;
flag=1;
}
72
//printf("\nP%d",i); if(finish[i]==1)
{
i=n;
}}}}}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n"); for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}
}
73
OUTPUT:
RESULT
Thus the program to implement banker’s algorithm for Dead Lock Detection was executed and the output was verified.
74
Students become expertise to write a C coding to implement FIFO page replacement algorithm.
ALGORITHM:
#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 .............................................");
75
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();
}
77
RESULT
Thus the program to implement Page Replacement algorithms –FIFO was executed and the output was verified.
Program
#include<dos.h>
#include<stdio.h>
78
#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;
79
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++;
80
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n\n total page faults=%d",pfc);
getch();
}
81
RESULT
Thus the program to implement Page Replacement algorithms –LRU was executed and the output was verified
} clrscr();
printf("\n PAGE REPLACEMENT TECHNIQUE ::LFU ALGORITHM \n");
printf("\n .......................................... \n");
printf("F-Page Fault \t H-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=5)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
(ru[j])++;
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]=1;
flg2=1;
break;
}}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>=ru[j])
83
{
if(ru[min]>ru[j])
min=j;
else
{
for(k=0;k<i;k++)
{
if(prs[k]==fp[min])
break;
if(prs[k]==fp[j])
{
min=j;
break;
}}}}}
fp[min]=prs[i];
ru[min]=1;
}}
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(%d)",fp[j],ru[j]);
}
y++;
gotoxy(x,y);
printf("-----");
y++;
gotoxy(x,y);
printf("%c",ps);
}
84
RESULT
Thus the program to implement Page Replacement algorithms –LFU 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)
86
{
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:-
87
88
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.
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++)
89
{
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++)
{
90
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;
}
}
91
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]
92
Result:
Thus the c program for implementing paging technique of memory management using first fit was executed and the output was verified.
OUTCOME:
Students become expertise to write a C coding to implement threading applications.
ALGORITHM:
Step 1: Start the program.
Step 2: Create number of threads.
Step 3: Counter is maintained for the user. Step 4: Enter the logs of the jobs to start. Step 5: Stop the program.
PROGRAM:
94
#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.
95
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();
96
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);
}
97
Output:
Step 6: After the exiting of the child process parent process will get exit. Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
void parent();
void child();
main()
{
int r;
r=fork();
{
printf("fork is empty");
}
if(r>0)
parent(r);
else
child(r);
}
void child()
{
int i;
printf("\nchild starts executing"); for(i=0;i<5;i++)
{
printf("\n child %d is executing %d time",getpid(),i);
sleep(i);
}
printf("child %d exit \n",getpid());
}
void parent(child_pid)
{
printf("\n Parent %d watiting for child %d to start the executing", getpid(),child_pid); printf("\n");
waitpid(child_pid,NULL,0);
99
OUTPUT:
[com39@localhost ~]$ cd os
[com39@localhost os]$ gcc ex1a.c
[com39@localhost os]$ ./a.out
fork is empty
Parent 1769 waiting for child 1770 to start the executing fork is empty
child starts executing
child 1770 is executing 0 time
child 1770 is executing 1 time
child 1770 is executing 2 time
child 1770 is executing 3 time
child 1770 is executing 4 time child 1770 exit Parent 1769 exit
[com39@localhost os]
Result:
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:
100
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:
101
[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]$
102
Result:
Thus the C program to simulate UNIX system calls opendir, readdir, closedir and exit has been executed successfully.
Step 4: Else if pid = 0, execlp() function will invoked. Step 5: Else print child process complete.
Step 6: Stop the program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
int pid;
pid=fork();
if(pid<0)
{
fprintf(stderr,"Fork failed \n");
exit(-1);
}
else if(pid==0)
{
execlp("/bin/ls","ls",NULL);
}
else
{
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:
104
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++)
105
{
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();
}
106
OUTPUT:
OUTPUT
File No File Size Block No Block Size Fragment
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]);
}
108
OUTPUT:
OUTPUT
File No File Size Block No Block Size Fragment
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:
Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0, bf[max],ff[max]. Step 3: Get the number of blocks,files,size of the blocks using
for loop.
Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check lowest>temp,if so assign ff[i]=j,highest=temp Step 6: Assign frag[i]=lowest, bf[ff[i]]=1,lowest=10000 Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment. Step 9: Stop the program.
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]);
}
111
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:
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
112
OUTPUT
File No File Size Block No Block Size Fragment
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
INPU
T Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTP
UT
Tracks traversed Difference between tracks
55 45
58 3
60 2
70 10
18 52
90 72
150 60
160 10
184 24
INPU
T Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT
Tracks traversed Difference between tracks
150 50
160 10
184 24
90 94
70 20
60 10
58 2
55 3
18 37
for(i=
0;i<=n
+2;i+
+)
if(t[i]
==h);
j=i;break;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j]; j++;
p++;
}
atr[p]=t[j]; p++;
i=0;
119
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();
}
121
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.