Cs3461 Operating System Lab Manual-1-4
Cs3461 Operating System Lab Manual-1-4
0 0 3 1.5
COURSE OBJECTIVES:
LIST OF EXPERIMENTS:
3. Process Management using System Calls : Fork, Exit, Getpid, Wait, Close
15. Write C programs for the implementation of various disk scheduling algorithms
16. Install any guest operating system like Linux using VMware.
TOTAL: 45 PERIODS
EX. NO: 1 ) (CASE STUDY): Perform a case study by installing and exploring varioustypes
of operating systems on a physical or logical (virtual) machine. (Linux Installation).
Instructions to Install Ubuntu Linux 12.04 (LTS) along with Windows.
This is highly recommended that you should take backup of your entire data before start
with the installation process.
shot.
2. Find the Boot option in the setup utility. Its location depends on your BIOS.
Select the Boot option from the menu, you can now see the options Hard Drive, CD-ROM
Drive, Removable Devices Disk etc.
3. Change the boot sequence setting so that the CD-ROM is first. See the list of “Item Specific
Help” in right side of the window and find keys which is used to toggle to change the boot
sequence.
In a few minutes installation wizard will be started. Select your language and click the
"Install Ubuntu" button to continue...
Optionally, you can choose to download updates while installing and/or install third party
software, such as MP3 support. Be aware, though, that if you select those options, the entire
installation process will be longer!
Since we are going to create partitions manually, select Something else, then click Continue.
Keep in mind that even if you do not want to create partitions manually, it is better to select the same
option as indicated here. This would insure that the installer will not overwrite yourWindows , which
will destroy your data. The assumption here is that sdb will be used just for Ubuntu 12.04, and that
there are no valuable data on it.
Where are you? Select your location and Click the "Continue" button.
Keyboard layout
Select your keyboard layout and UK (English) and Click on “Continue” button.
It will take approximately 10-12 minutes (depending on computer's speed), a pop-up window
will appear, notifying you that the installation is complete, and you'll need to restart thecomputer in
order to use the newly installed Ubuntu operating system. Click the "Restart Now" button.
Please remove the CD and press the "Enter" key to reboot. The computer will be restarted. In
a few seconds, you should see Windows 7′s boot menu with two entires listed – Windows 7 and
Ubuntu 12.04 (LTS).
Then you may choose to boot into Windows 7 or Ubuntu 12.04 using the UP/Down arrow key.
Please select Ubuntu 12.04 (LTS) and press Enter to boot the machine in Ubuntu 12.04
Linux.
Here you can see the users on the machine, Click on the user name and enter the
passwordand press Enter key to login.
ALGORITHM :
#!/bin/bash
echo "enter the a vale"
read a
echo "enter b value"
read b
c=`expr $a + $b`
echo "sum:"$c
c=`expr $a - $b`
echo "sub:"$c
c=`expr $a \* $b`
echo "mul:"$c
c=`expr $a / $b`
echo "div:"$c
OUTPUT:
RESULT:
Thus the program was executed successfully
EX. NO: 2 ) ii NUMBER CHECKIG USIG SHELL PROGRAM
AIM: To write a shell program to check whether the number is odd or even.
ALGORITHM :
Step 1 : Include the necessary header files. Step 2 : get the input
Step 3 : perform the division by 2. Step 4 : print the result.
Step 5 :stop the execution.
PROGRAM CODING:
#!/bin/bash
num="1 2 3 4 5 6 7 8"
for n in $num
do
q=`expr $n % 2`
if [ $q -eq 0 ]
then
echo "even no"
continue
fi
echo "odd no"
done
OUTPUT:
[2mecse25@rhes3linux ~]$ sh pg2.sh
odd no
even no
odd no
even no
odd no
even no
odd no
even no
RESULT:
Thus the program was executed successfully
EX. NO: 2 ) iii MULTIPLICATION TABLE USIG SHELL PROGRAM
AIM: To write a shell program to check whether the number is odd or even.
ALGORITHM ::
Step 1 : Include the necessary header files.
Step 2 : get the input
Step 3 : perform the multiplication .
Step 4 : print the result.
Step 5 :stop the execution.
PROGRAM CODING:
#!/bin/bash
echo " which table you want"read n
for((i=1;i<10;i++))do
echo $i "*" $n "=" `expr $i \* $n`
done
OUTPUT:
RESULT:
Thus the program was executed successfully
EX. NO: 2 ) iv USING WHILE LOOP IN SHELL PROGRAM
AIM: To write a shell program to print the number from 1 to 10 using while loop.
ALGORITHM :
Step 1 : Include the necessary header files.
Step 2 : Define the buffer size as 1024.
Step 3 : Get the file name which has been created already.
Step 4 : Open the file in read mode.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
Step 7 : Close the file.
PROGRAM CODING:
#!/bin/bash
a=1
while [ $a -lt 11 ]
do
echo "$a"
a=`expr $a + 1`
done
OUTPUT:
[2mecse25@rhes3linux ~]$ sh pg2.sh 1
2
3
4
5
6
7
8
9
10
RESULT:
Thus the program was executed successfully.
EX. NO: 2 ) vi SIMPLE FUNCTION IN SHELL PROGRAMING
ALGORITHM :
Step 1 : Include the necessary header files.
Step 2 : Define the buffer size as 1024.
Step 3 : Get the file name which has been created already.
Step 4 : Open the file in read mode.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
Step 7 : Close the file.
PROGRAM CODING:
#!/bin/bash
add()
{
c=`expr $1 + $2`
echo "addition = $c"
}
add 5 10
OUTPUT:
[2mecse25@rhes3linux ~]$ sh pg2.sh
addition = 15
RESULT:
Thus the program was executed successfully
EX. NO: 2 ) vii SWITCH STATEMENT IN SHELL PROGRAMING
AIM: To write a shell program to add a two number using function .
ALGORITHM :
Step 1 : Include the necessary header files.
Step 2 : Define the buffer size as 1024.
Step 3 : Get the file name which has been created already.
Step 4 : Open the file in read mode.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
Step 7 : Close the file.
PROGRAM CODING
#!/bin/bash
ch='y'
while [ $ch = 'y ' ]
do
echo "enter your choice"
echo "1 no of user loged on"
echo "2 print calender"
echo "3 print date"
read d
case $d in
1) who | wc -l;;
2) cal 20;;
3) date;;
*) break;; esac
echo "do you wish to continue (y/n)"
read ch
OUTPUT:
[2mecse25@rhes3linux ~]$ sh case2.sh
enter your choice
1 no of user loged on
2 print calender
3 print date
Thu Apr 4 11:18:20 IST 2013
do you wish to continue (y/n) n
RESULT:
Thus the program was executed successfully
EX. NO: 3 ) a)PROCESS MANAGEMENT USING SYSTEM
CALLS:FORK,EXIT,GETPID,WAIT,CLOSE
AIM: To write the program for system calls of unix Operating
Systems(Opendir,Readdir,closedir,etc..)
ALGORITHM :
Step 1: Start the program.
Step 2: Declare the variables pid and child id.
Step 3: Get the child id value using system call fork().
Step 4: If child id value is greater than zero then print as “i am in the parent process”.
Step 5: If child id! = 0 then using getpid() system call get the process id.
Step 6: Print “i am in the parent process” and print the process id.
Step 7: If child id! = 0 then using getppid() system call get the parent process id.
Step 8: Print “i am in the parent process” and print the parent process id.
Step 9: Else If child id value is less than zero then print as “i am in the child process”.
Step 10: If child id! = 0 then using getpid() system call get the process id.
Step 11: Print “i am in the child process” and print the process id.
Step 12: If child id! = 0 then using getppid() system call get the parent process id.
Step 13: Print “i am in the child process” and print the parent process id.
Step 14: Stop the program.
PROGRAM CODING
#include<stdio.h>
#include<dirent.h>
struct dirent*dptr;
int main(int argc,char *argv[])
{
char buff[100];
DIR *dirp;
printf("\n\n Enter the Directory name");
scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("the given directory does not exist");
exit(1);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}
OUTPUT:
ALGORITHM :
Step 1 : Declare the variables pid , parent pid , child id and grand chil id.
Step 2 : Get the child id value using system call fork().
Step 3 : If child id value is less than zero then print as “error at fork() child”.
Step 4 : If child id !=0 then using getpid() system call get the process id.
Step 5 : Print “I am parent” and print the process id.
Step 6 : Get the grand child id value using system call fork().
Step 7 : If the grand child id value is less than zero then print as “error at fork() grandchild”.
Step 8 : If the grand child id !=0 then using getpid system call get the process id.
Step 9 : Assign the value of pid to my pid.
Step 10 : Print “I am child” and print the value of my pid.
Step 11 : Get my parent pid value using system call getppid().
Step 12 : Print “My parent‟s process id” and its value.
Step 13 : Else print “I am the grand child”.
Step 14 : Get the grand child‟s process id using getpid() and print it as “my process id”.
Step 15 : Get the grand child‟s parent process id using getppid() and print it as “my parent‟sprocess id
PROGRAM CODING
#include<stdio.h>
#include<unistd.h>
main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf("error in process creation\n");
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf("\n the parent process ID is:%d\n",pid1);
}
else
{
pid2=getpid();
printf("\n the child process ID is:%d\n",pid2);
}
}
OUTPUT:
the child process ID is:5394
RESULT:
Thus the program was executed successfully
EX. NO: 4) implement the various CPU Scheduling Algorithms
AIM: To write a c program to simulate the CPU scheduling algorithm First Come First
Serve (FCFS)
ALGORITHM :
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process name and the burst time
Step 4: Setthe waiting of the first process as ‗0„and its burst time as its turnaround time
Step 5: for each process in the Ready Q calculate a).Waiting time (n) = waiting time (n-1) + Burst
time (n-1)b). Turnaround time (n)= waiting time(n)+Burst time(n)
Step 6: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process
PROGRAM CODING
// First Come First Serve ( FCFS Program in C)
#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n); for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time –%f",
wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}
INPUT:
Enter the number of processes --3
Enter Burst Time for Process 0 --24
Enter Burst Time for Process 1 --3
Enter Burst Time for Process 2 --3
OUTPUT:
P BURS WAITIN TURNAROUN
R T GTIME DTIME
O TIME
C
E
SS
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000
RESULT:
Thus the program was executed successfully
AIM: To write a program to stimulate the CPU scheduling algorithm Shortest job first (Non-
Preemption)
ALGORITHM :
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest
to highest burst time.
Step 5: Set the waiting time of the first process as ‗0„ and its turnaround time as its burst
time.Step 6: Sort the processes names based on their Burt time
Step 7: For each process in the ready queue, calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number ofprocess
Step 9: Stop the process
PROGRAM CODING
#include<stdio.h
>
#include<conio.h
>main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++) for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i]; bt[i]=bt[k]; bt[k]=temp;
temp=p[i]; p[i]=p[k]; p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i]; wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}
INPUT:
OUTPUT:
RESULT:
Thus the program was executed successfully
C. ROUND ROBIN:
ALGORITHM :
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where No. of time slice for process (n)
=burst time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
a) Waiting time for process (n) = waiting time of process(n-1)+ burst timeof process(n-1 ) + the
time difference in getting the CPU fromprocess(n-1)
b) Turnaround time for process(n) = waiting time of process(n) + burst time of process(n)+ thetime
difference in getting CPU from process(n).
Step 7: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number ofprocess
Step 8: Stop the process
PROGRAM CODING
#include<stdio.h
>main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0]; for(i=1;i<n;i++)
if(max<bu[i]) max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is --%f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();
}
INPUT:
OUTPUT:
RESULT:
Thus the program was executed successfully