Operating Systems Lab: Implement The Following Using C/C++/JAVA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

OPERATING SYSTEMS LAB

1. Basic UNIX commands Implement the following using Shell Programming.

2. Input number even or odd.

3. Count the number of lines in the input text.

Implement the following using C/C++/JAVA


4. FCFS CPU scheduling algorithm.

5. SJF CPU scheduling algorithm.

6. Round Robin CPU scheduling algorithm.

7. Priority CPU scheduling algorithm.

8. Implement Semaphores.

9. Bankers Algorithm for Dead Lock Avoidance.

10. FIFO Page Replacement Algorithm.

REFERENCE BOOKS:
1. Operating System Principles by Abraham Silberschatz, Peter Galvin, Greg Gagne. Seventh Edition, Wiley
Publication.

2. Understanding the Linux Kernel, Daniel P Bovet and Marco Cesati, 3rd Edition,Reilly, 2005.

3. Unix programming, Stevens, Pearson Education.

4. Shell programming, YashwanthKanetkar.

COMPUTER ORGANIZATION LAB

Digital Logic Design Experiments


1. TTL Characteristics and TTL IC Gates

2. Multiplexers & Decoders

3. Flip-Flops

4. Counters

5. Binary Adders &Subtractors

8085/86AssemblyLanguageProgramming:
1. Addition of two 8-bit numbers.

2. Addition of two 16-bit numbers.

3. Sum of series of 8-bit numbers.

4. Subtraction of two 8-bit numbers.

5. Largest number in an array.

REFERENCE BOOKS:
1. Computer System Architecture: Morris Mano.

2. Advanced Micro Processor and Peripherals - Hall/ A K Ray.

3. Computer Organization and Architecture – William Stallings Sixth Edition, Pearson/PHI.

4. Structured Computer Organization and Design - Andrew S. Tanenbaum, 4th Edition PHI/Pearson.

1. Basic UNIX commands Implement the following using Shell Programming.

Unix Operating Systems


Operating System
Operating System is the set of software programs which acts as an interface between user and the
system. It is software which operates all hardware components. It is program that manages the
resources of the computer.
Unix
Unix is a command user interface operating system. It is set of programs that act as a link between
the computer and the user.
The computer programs that allocate the system resources and coordinate all the details of the
computer's internals is called the operating system or the kernel.
Users communicate with the kernel through a program known as the shell. The shell is a command
line interpreter; it translates commands entered by the user and converts them into a language that is
understood by the kernel.
History
Unix was originally developed in 1969 by a group of AT&T employees Ken Thompson, Dennis
Ritchie, Douglas McIlroy, and Joe Ossanna at Bell Labs.
There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD are a
few examples. Linux is also a flavor of Unix which is freely available.
Several people can use a Unix computer at the same time; hence Unix is called a multiuser system.
A user can also run multiple programs at the same time; hence Unix is a multitasking environment.
Unix Architecture

The concepts that unites all the versions of UNIX are

Kernel: The kernel is the heart of the operating system. It interacts with the hardware and most of
the tasks like memory management, task scheduling and file management.

Shell: The shell is the utility that processes your requests. When you type in a command at your
terminal, the shell interprets the command and calls the program that you want. The shell uses
standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are the most famous shells
which are available with most of the Unix variants.

Commands and Utilities: There are various commands and utilities which you can make use of in
your day to day activities. cp, mv, cat and grep, etc. are few examples of commands and utilities.
There are over 250 standard commands plus numerous others provided through 3rd party software.
All the commands come along with various options.
Files and Directories: All the data of Unix is organized into files. All files are then organized into
directories. These directories are further organized into a tree-like structure called the file system.
UNIX Commands

1. clear
Used to clear the screen
$ clear
2. ls

Used to list the files and directories stored in the current working directory
$ ls
Options:
-l list the more information about the files like size, date and time of creation etc
-a lists the hidden files
-t sorts the files as per the time of creation (earliest first)
3. date
Displays the current date and time
$ date
4. cal
Displays the calendar of the current month and year
$ cal
5. cat
It is used to create a file, append to a file and display files
$ cat filename Used to view the contents of a file
$ cat > filename Used to create a new file.
The file name can contain spaces and special characters
$ cat >> filename Used to append to an existing file
Note: press ‘ctrl+d’ to save the contents to the file
6. wc
Used to get a count of the total number of lines, words, and characters contained in a file
$ wc filename Displays no. of lines, words and total number of bytes in the file.
$ wc –l filename Displays the no. of lines
$ wc -w filename Displays the no. of words
$ wc –c filename Displays the no. of characters
$ wc filename1 filename2 filename3
Displays the information about multiple files.
7. cp
Copy the contents of one file into another
$ cp source_file destination_file
8. mv
Renames the existing file or directory.
$ mv old_file new_file
The mv command will move the existing file completely into the new file. In this case, you
will find only newfile in your current directory.
9. rm
Deletes an existing file
$ rm filename
$ rm -i filename Used for interactive option asking for confirmation
$ rm file1 file2 file3 removes multiple files
10. who
Lists all the users who are currently logged in
$ who
11. who am i
lists the account name associated with the current login.
$ who am i
12 pwd

Displays the current working directory


$ pwd
13. mkdir
Used to create a directory
$ mkdir filename creates a directory in current directory
$ mkdir /usr/local/mrn creates a directory ‘mrn’ in /usr/local
$ mkdir mrn1 mrn2 creates mrn1 mrn2 directories in current directory
14. rmdir
Removes an empty directory
$ rmdir directory_name
$ rmdir d1 d2 d3 removes directories d1, d2, d3
15. cd

Change into a directory


$ cd dir_name
$ cd Changes to parent directory
$cd ~ Changes to home directory
16. man
Displays the reference manual file page for a given command
$ man command_name
17. diff
Compares files and shows where they differ
$ diff file1 file2
18. more
Used to view the contents of a file page-wise.
$ more <filename>
19. less
Used to view the contents of a file page-wise. Possible to move in forward as well as
backward direction.
$ less <filename>
20. pg

Shows the contents of the file page-wise


$ pf <filename>
21. echo
Display a line of text/string on standard output or a file
$ echo “Hello Unix” Displays the string Hello Unix
$ x=10; Assigns x with 10
$ echo $x Displays the value in x
22. sort
Sorts the contents of a given file alphabetically
$ sort filename sorts the contents in ascending order
$ sort -r filename sorts the contents in descending order
$ sort -n filename sorts the file numerically
$ sort -f filename sorts the file irrespective of case (case insensitive sorting)
23. grep
Finds a given string in file(s)
$ grep <string> <filename(s)> finds the given string in file
$ grep -v <string> <filename> shows all the lines that don’t match the searched string
$ grep -c <string> <filename> displays only the count of matching lines
$ grep -n <string> <filename> shows the matching line and its number
$ grep -i <string> <filename> shows the matching lines ignoring the case
24. history
Shows all the commands that you have used in the past for the current terminal session
$ history
25. pipe ( | )
Lets you to use two or more commands such that the output of one command serves as input
to the next.
$ cat filename | less Outputs the ‘cat’ command to ‘less’ which shows scroll length of
content at a time.

$ cat filename | grep string


26. head
Displays the specified number of line from the beginning of the file
$ head -n filename displays first n number of lines from given filename
27. tail
Displays the specified number of line from the end of the file

$ tail -n filename displays last n number of lines from given filename

28. df
Gives the report on the total number of free blocks on current disk
$ df

29. bc
Basic calculator which performs arithmetic operations
$ bc
10 + 20

30. nl

Gives lines number for every line in the specified file


$ nl filename
2. Input number even or odd.

clear all
echo "enter a number"
read n
r=$(($n %2))
if [ $r -eq 0 ]
then
echo "EVEN"
else
echo "ODD"
fi
3. Count the number of lines in the input text.

Clear all
echo enter a file name
read file
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l1=`cat $file | wc -l`

echo number of words are $w


echo number of characters are $c
echo number of lines are $l1

4. FCFS CPU scheduling algorithm.

#include<stdio.h>
void main()
{
float bt[30],wt[30],tat[30];
float avg_wt=0.0,avg_tat=0.0;
int c,j,tp;
printf("\nEnter the number of process:");
scanf("%d",&tp);
printf("\n Enter the burst time of the processes\n");
for(c=0;c<tp;c++)
{
printf("process [%d]:",c+1);
scanf("%f",&bt[c]);
}
wt[0]=0;
for(c=1;c<tp;c++)
{
wt[c]=0;
for(j=0;j<c;j++)
{
wt[c]=wt[c]+bt[j];
}
}
printf("\nprocess \t\tburst time\twaiting time\tturnaround time\n");
for(c=0;c<tp;c++)
{
tat[c]=bt[c]+wt[c];
avg_wt=avg_wt+wt[c];
avg_tat=avg_tat+tat[c];
printf("\nprocess [%d] \t\t %.2f \t\t %.2f \t\t .2f",c+1,bt[c],wt[c],tat[c]);
}
avg_wt=avg_wt/tp;
avg_tat=avg_tat/tp;
printf("\nAverage Turnaround time=%.2f \nAverage Wating Time=%.2f\n",avg_tat,avg_wt);
}
//Program to demonstrate RR CPU scheduling
algorithm#include<stdio.h>
void main()
{
float avg_wt,avg_tat;
int i,limit,t=0,x,c=0,tq;
int wt=0,tat=0,bt[10],at[10],temp[10];
printf("\nEnter the number of process:");
scanf("%d",&limit);
x=limit;
printf("\n Enter the burst time of the processes\n");
for(i=0;i<limit;i++)
{
printf("\nEnter details of process[%d]\n",i+1);
printf("\nArrival time:");
scanf("%d",&at[i]);
printf("\nburst time:");
scanf("%d",&bt[i]);
temp[i]=bt[i];
}
printf("\nEnter time quantum:");
scanf("%d",&tq);
printf("\nprocess ID \t\tburst time\tturnaround time\twaitingtime\n");
for(t=0,i=0;x!=0;)
{
if(temp[i]<=tq && temp[i]>0)
{
t=t+temp[i];
temp[i]=0;
c=1;
}
else if(temp[i]>0)
{
temp[i]=temp[i]-tq;
t=t+tq;
}
if(temp[i]==0 && c==1)
{
x--;
printf("\nprocess[%d}\t\t%d\t\t%d\t\t\t%d",i+1,bt[i],t-at[i],t-at[i]-bt[i]);
wt=wt+t-at[i]-bt[i];
tat=tat+t-at[i];
c=0;
}
if(i==limit-1)
{
i=0;
}
else if(at[i+1]<=t)
{
i++;
}
else
{
i=0;
}
}
avg_wt=wt*1.0/limit;
avg_tat=tat*1.0/limit;
printf("\nAverage Turnaround time=%.2f \nAverage Wating Time=%.2f\n",avg_tat,avg_wt);
}

5. SJF CPU scheduling algorithm.

#include<stdio.h>
void main()
{
int temp,i,j,limit,sum=0,position;
float avg_wt,avg_tat;
int bt[20],p[20],wt[20],tat[20];
printf("\nEnter total number of processes:");
scanf("%d",&limit);
for(i=0;i<limit;i++)
{
printf("Enter the burst time for process [%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<limit;i++)
{
position=i;
for(j=i+1;j<limit;j++)
{
if(bt[j]<bt[position])
{
position=j;
}
}
temp=bt[i];
bt[i]=bt[position];
bt[position]=temp;
temp=p[i];
p[i]=p[position];
p[position]=temp;
}
wt[0]=0;
for(i=1;i<limit;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]=wt[i]+bt[j];
}
sum=sum+wt[i];
}
avg_wt=(float)sum/limit;
sum=0;
printf("\nprocess \t\tburst time\twaiting time\tturnaround time\n");
for(i=0;i<limit;i++)
{
tat[i]=bt[i]+wt[i];
sum=sum+tat[i];
printf("\np[%d] \t\t %d \t\t %d \t\t %d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)sum/limit;
printf("\nAverage Turnaround time=%.2f \nAverage Wating Time=%.2f\n",avg_tat,avg_wt);
}

6. Round Robin CPU scheduling algorithm.

#include<stdio.h>
void main()
{
float avg_wt,avg_tat;
int i,limit,t=0,x,c=0,tq;
int wt=0,tat=0,bt[10],at[10],temp[10];
printf("\nEnter the number of process:");
scanf("%d",&limit);
x=limit;
printf("\n Enter the burst time of the processes\n");
for(i=0;i<limit;i++)
{
printf("\nEnter details of process[%d]\n",i+1);
printf("\nArrival time:");
scanf("%d",&at[i]);
printf("\nburst time:");
scanf("%d",&bt[i]);
temp[i]=bt[i];
}
printf("\nEnter time quantum:");
scanf("%d",&tq);
printf("\nprocess ID \t\tburst time\tturnaround time\twaitingtime\n");
for(t=0,i=0;x!=0;)
{
if(temp[i]<=tq && temp[i]>0)
{
t=t+temp[i];
temp[i]=0;
c=1;
}
else if(temp[i]>0)
{
temp[i]=temp[i]-tq;
t=t+tq;
}
if(temp[i]==0 && c==1)
{
x--;
printf("\nprocess[%d}\t\t%d\t\t%d\t\t\t%d",i+1,bt[i],t-at[i],t-at[i]-bt[i]);
wt=wt+t-at[i]-bt[i];
tat=tat+t-at[i];
c=0;
}
if(i==limit-1)
{
i=0;
}
else if(at[i+1]<=t)
{
i++;
}
else
{
i=0;
}
}
avg_wt=wt*1.0/limit;
avg_tat=tat*1.0/limit;
printf("\nAverage Turnaround time=%.2f \nAverage Wating Time=%.2f\n",avg_tat,avg_wt);
}

7. Priority CPU scheduling algorithm.

#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
}

8. Implement Semaphores.

9. Bankers Algorithm for Dead Lock Avoidance.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");

10. FIFO Page Replacement Algorithm.

#include<stdio.h>
void main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\nENter no. of pages:");
scanf("%d",&n);
printf("\nEnter the page number:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nEnter the number of frames:");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]=-1;
j=0;
printf("\tRef String \t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if(avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page fault is %d",count);
}
COMPUTER ORGANIZATION LAB

Digital Logic Design Experiments

1. TTL Characteristics and TTL IC Gates


2. Multiplexers & Decoders

You might also like