jawaharlal nehru engineering college · submission related to whatever lab work has been completed...

26
1 Jawaharlal Nehru Engineering College Laboratory Manual Operating System For Third Year Students CSE Dept: Computer Science & Engineering (NBA Accredited) Author JNEC, Aurangabad

Upload: others

Post on 25-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

1

Jawaharlal Nehru Engineering College

Laboratory Manual

Operating System

For

Third Year Students CSE Dept: Computer Science & Engineering (NBA Accredited)

Author JNEC, Aurangabad

Page 2: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

2

FOREWORD

It is my great pleasure to present this laboratory manual for Third year

engineering students for the subject of Operating System.

As a student, many of you may be wondering with some of the questions in your

mind regarding the subject and exactly what has been tried is to answer through this

manual.

As you may be aware that MGM has already been awarded with ISO 9001:2000

certification and it is our endure to technically equip our students taking the advantage of

the procedural aspects of ISO 9001:2000 Certification.

Faculty members are also advised that covering these aspects in initial stage itself,

will greatly relived them in future as much of the load will be taken care by the

enthusiasm energies of the students once they are conceptually clear.

Dr. S.D.Deshmukh

Principal

Page 3: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

3

LABORATORY MANUAL CONTENTS

This manual is intended for the Third year students of Computer Science and

Engineering in the subject of Operating System. This manual typically contains

practical/Lab Sessions related Operating System covering various aspects related the

subject to enhanced understanding.

Students are advised to thoroughly go through this manual rather than only topics

mentioned in the syllabus as practical aspects are the key to understanding and

conceptual visualization of theoretical aspects covered in the books.

Good Luck for your Enjoyable Laboratory Sessions.

Prof. D.S.Deshpande Mrs. A.B.Husain

HOD, CSE Lecturer, CSE Dept.

Page 4: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

4

DOs and DON’Ts in Laboratory:

1. Make entry in the Log Book as soon as you enter the Laboratory.

2. All the students should sit according to their roll numbers starting from their left to

right.

3. All the students are supposed to enter the terminal number in the log book.

4. Do not change the terminal on which you are working.

5. All the students are expected to get at least the algorithm of the program/concept

to be implemented.

6. Strictly observe the instructions given by the teacher/Lab Instructor.

Instruction for Laboratory Teachers:

1. Submission related to whatever lab work has been completed should be done during

the next lab session. The immediate arrangements for printouts related to submission on

the day of practical assignments.

2. Students should be taught for taking the printouts under the observation of lab teacher.

3. The promptness of submission should be encouraged by way of marking and

evaluation patterns that will benefit the sincere students.

Page 5: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

5

SUBJECT INDEX

1. Simulation of Copy command.

2. Simulation of MORE command.

3. Implementation of First Come First Serve algorithm for Processor scheduling.

4. Implementation of Shortest Job First, a priority algorithm for Processor scheduling.

5. Implementation of Round Robin scheduling, a priority algorithm for Processor

scheduling.

6. Study of deadlock and Banker’s Algorithm.

7. Program for determining physical memory of a disk.

8. Case study of Windows -NT

9. Case study of LINUX

Page 6: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

6

1. Simulation of Copy command

Exercise No1: (2 Hours) – 1 Practical

Aim: - Simulation of Copy command i.e. to copy the contents of one file to another

file.

In MS-DOS copy command copies one or more files to another location.

COPY [/A | /B] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination] [/A | /B]] [/V] [/Y

| /-Y]

source Specifies the file or files to be copied.

/A Indicates an ASCII text file.

/B Indicates a binary file.

destination Specifies the directory and/or filename for the new file(s).

/V Verifies that new files are written correctly.

/Y Suppresses prompting to confirm you want to overwrite an existing

destination file.

/-Y Causes prompting to confirm you want to overwrite an existing destination

file.

By using Command Line Arguments, accept the source filename and destination

filename. These will be passed as an argument to the program and source file will be read

copied to the destination file.

Input: - Program-Name source-file destination-file

Output: - Source file is copied to destination file.

Page 7: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

7

Procedure:

1) Create two file pointers.

2) Read source and destination file names.

3) Open files using file handling commands.

4) Using a while loop read characters and transfer them to the destination file.

5) Close the files.

6) Print a message for confirmation.

Conclusion: - Hence, by using command line arguments we are simulating the copy

command.

Page 8: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

8

2. Simulation of MORE command

Exercise No 2: (2 Hours) – 1 Practical

Aim: - Simulation of More command i.e to display the contents of the file on the

screen page wise.

Output: - The contents of the file will be printed page wise on the screen.

Procedure: -

1) Open the file that is to be viewed.

2) Two types of counter variables are used, one used for counting the total no. of

lines & other to keep watch on the condition that the no. of lines become 25.

3) Reset the value by printing M or More.

Press any key to continue.

4) Finally total no. of lines are printed.

Conclusion: - Hence, we can simulate the more command.

Page 9: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

9

3. Implementation of First Come First Serve algorithm for

Processor scheduling.

Exercise No 3: (2 Hours) – 1 Practical

Aim: - Implementation of First Come First Serve algorithm for Processor

scheduling.

This code implements the First come First Served Scheduling Algorithm. It gets the process

name, burst time for each process. Calculates and displays the average turnaround time and

waiting time. Then it will display the Gantt chart.

Input: Process Burst Time

P0 24

P1 03

P2 02

Output: The Gantt chart is:

P0 P1 P2

0 24 27 29

The average waiting time is = (0+24+27) / 3 = 17 msecs.

Procedure:

1) Accept values for processes and burst time.

2) Display in a table format

3) Compare the burst time and accordingly make a Gantt chart.

4) Also, calculate the average waiting time.

Advantages:

• simple

Page 10: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

10

• easy to understand

• first come first served

Disadvantages:

• This scheduling method is nonpreemptive, 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

Conclusion: - Hence, we have studied the First Come First Served Scheduling Algorithm.

Page 11: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

11

4. Implementation of Shortest Job first, a priority algorithm

for Processor scheduling.

Exercise No 4: ( 2 Hours) – 1 Practical

Aim: - Implementation of Shortest Job first, a priority algorithm for Processor

scheduling.

Shortest-Job-First (SJF) is a non-preemptive algorithm in which waiting job (or

process) with the smallest estimated run-time-to-completion is run next. In other words,

when CPU is available, it is assigned to the process that has smallest next CPU burst.

Input: Process Burst Time

P0 24

P1 03

P2 02

Output: The Gantt chart is:

P2 P1 P0

0 2 5 29

The average waiting time is = (0+2+5) / 3

=2.3333 msecs

Procedure:

1) Accept values for processes and burst time.

2) Display in a table format

3) Compare the burst time and accordingly make a Gantt chart.

4) Also, calculate the average waiting time.

Advantage:

Page 12: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

12

• Waiting time is minimal.

Disadvantage:

• The real difficulty with the SJF algorithm is knowing the length of the next CPU

request.

Conclusion: - Hence, we have studied the Shortest Job First Scheduling Algorithm.

Page 13: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

13

5. Implementation of Round Robin scheduling, a priority

algorithm for Processor scheduling.

Exercise No 5: (2 Hours) – 1 Practical

Aim: - Implementation of Round Robin scheduling, a priority algorithm for

Processor scheduling.

Round-robin scheduling algorithm is one of the simplest scheduling algorithms. It is

designed especially for time-sharing systems. The ready queue is treated as a circular

queue. The algorithm assigns a time-slice (also called time quantum) to each process in

the ready queue in order, handling all processes without priority. A maximum of one time

slice is allocated at once. If the remaining request is less than a time slice, only the

remaining request time is allocated. Round robin scheduling is both simple and easy to

implement. It is also starvation-free.

Input: Process Burst Time

P0 20

P1 13

P2 02

Enter time quantum: 5

Output:

P0 P1 P2 P0 P1 P0 P1 P0

0 5 10 12 17 22 27 30 35

The waiting time for P0 is = 0+7+5+3 = 15

The waiting time for P1 is = 5+7+5 = 17

The waiting time for P2 is = 10

The average waiting time is = (15+17+10) / 3

= 42/3

Page 14: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

14

= 14 msecs.

Advantage-

• Every process gets the CPU and thus there is no starvation.

Disadvantage-

• Average waiting time is often quite long.

Procedure:

1) Ask the user for the number of processes.

2) Accept burst time for all processes.

3) Accept the time quantum desired by the user.

4) Calculate the average waiting time.

5) Also draw the Gantt chart for the same.

Conclusion: - Hence, we see that Round Robin Scheduling Algorithm is very fair and no

starvation occurs.

Page 15: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

15

6. Study of deadlock and Banker’s Algorithm

Exercise No 6: (2 Hours) – 1 Practical

Aim: - Study of deadlock and Banker’s Algorithm

The Banker's Algorithm is a strategy for deadlock prevention. In an operating

system, deadlock is a state in which two or more processes are "stuck" in a circular wait

state. All deadlocked processes are waiting for resources held by other processes.

Because most systems are non-preemptive (that is, will not take resources held by a

process away from it), and employ a hold and wait method for dealing with system

resources (that is, once a process gets a certain resource it will not give it up voluntarily),

deadlock is a dangerous state that can cause poor system performance.

One reason this algorithm is not widely used in the real world is because to use it

the operating system must know the maximum amount of resources that every process is

going to need at all times. Therefore, for example, a just-executed program must declare

up-front that it will be needing no more than, say, 400K of memory. The operating

system would then store the limit of 400K and use it in the deadlock avoidance

calculations.

The Banker's Algorithm seeks to prevent deadlock by becoming involved in the

granting or denying of system resources. Each time that a process needs a particular non-

sharable resource, the request must be approved by the banker.

The banker is a conservative loaner. Every time that a process makes a request of

for a resource to be ``loaned'' the banker takes a careful look at the bank books and

attempts to determine whether or not a deadlock state could possibly arise in the future if

the loan request is approved.

This determination is made by ``pretending'' to grant the request and then looking

at the resulting post-granted request system state. After granting a resource request there

Page 16: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

16

will be an amount of that resource left free in the system, f. Further, there may be other

processes in system. We demanded that each of these other processes state the maximum

amount of all system resources they needed to terminate up-front so, therefore, we know

how much of each resource every process is holding and has claim to.

If the banker has enough free resource to guarantee that even one process can

terminate, it can then take the resource held by that process and add it to the free pool. At

this point the banker can look at the (hopefully) now larger free pool and attempt to

guarantee that another process will terminate by checking whether its claim can be met.

If the banker can guarantee that all jobs in system will terminate, it approves the loan in

question.

If, on the other hand, at any point in the reduction the banker cannot guarantee

any processes will terminate because there is not enough free resource to meet the

smallest claim, a state of deadlock can ensue. This is called an unsafe state. In this case

the loan request in question is denied and the requesting process is usually blocked.

The efficiency of the Banker's algorithm depends greatly on how it is

implemented. For example, if the bank books are kept sorted by process claim size,

adding new process information to the table is O(n) but reducing the table is simplified.

However if the table is kept in no order, adding a new entry is O(1) however reducing the

table is less efficient.

The Banker’s Algorithm is as follows:

STEP 1: initialize

Work := Available;

for i = 1,2,...,n

Finish[i] = false

STEP 2: find i such that both

a. Finish[i] is false

b. Need_i <= Work

if no such i, goto STEP 4

STEP 3:

Work := Work + Allocation_i

Finish[i] = true

Page 17: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

17

goto STEP 2

STEP 4:

if Finish[i] = true for all i, system is in safe state

Procedure:

1) Enter number of processes with their need.

2) Find out whether allocated resources are greater than required resources.

3) If allocated resources are greater than required resources then it in safe state or

else it an unsafe state.

Conclusion: - Thus, we have studied deadlock and the banker’s algorithm.

Page 18: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

18

Page 19: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

19

7. Program for determining physical memory of a disk

Exercise No 8: (2 Hours) – 1 Practical

Aim: - Program for determining physical memory of a disk.

Input: Any drive;

Output: Total clusters

Free Clusters

Sectors per cluster

Bytes per cluster

Memory used:

Memory available:

Procedure:

1) Invoke the dos_getdiskfree() function for current drive.

2) For available bytes take the product of aval_cluster, bytes_per_sector and

sector_per_cluster.

3) For total space get the product of total_cluster, bytes_per_sector and

sector_per_cluster.

Page 20: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

20

8. Case study of Windows -NT

Exercise No 9: (2 Hours) – 1 study practical

Case study of Windows -NT

Architecture of Windows – NT

Administrative tools

Security Mechanism

Advantages of Windows-NT

Page 21: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

21

9. Case study of LINUX

Exercise No 10: (2 Hours) – 1 study practical

• Case study of LINUX

• Architecture of LINUX

• Administrative tools

• Security Mechanism

• Advantages of LINUX

Page 22: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

22

Frequently Asked Questions in OS:

1) Definition of OS.

2) Difference between time sharing and multitasking systems.

3) What is process scheduling?

4) What are the different scheduling policies?

5) What is the need for memory management?

6) Different memory management techniques.

7) What is paging?

8) What do you mean by base n limit registers?

9) What is segmentation?

10) What are the different page replacement policies?

11) How does interprocess communication occur?

12) What is a race condition?

13) What do you mean by critical section?

14) Explain scheduling.

15) What is contiguous memory allocation?

16) What is a thread?

17) How can these be implemented in kernel space?

18) What is virtual address space?

19) What is virtual memory and physical memory?

20) What are different process states?

21) Explain LIFO, FIFO algorithms?

22) Explain priority scheduling.

23) What are deadlocks?

24) How do you handle deadlocks?

25) Explain the terms

a) Thrashing

b) Compaction

c) Internal/external fragmentation

d) Demand paging

e) Disk scheduling algorithms

f) Disk formatting

g) Directory structure

Page 23: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

23

h) Overlays

i) Process control block

j) Monitors

k) Swapping

l) Encryption

m) Virtual machines

n) Inverted page tables

o) Device driver

Quiz on the subject:

Quiz should be conducted on tips in the laboratory, recent trends and subject knowledge

of the subject. The quiz questions should be formulated such that questions are normally

are from the scope outside of the books. However twisted questions and self formulated

questions by the faculty can be asked but correctness of it is necessarily to be thoroughly

checked before the conduction of the quiz.

Conduction of Viva-Voce Examinations:

Teacher should oral exams of the students with full preparation. Normally, the objective

questions with guess are to be avoided. To make it meaningful, the questions should be

such that depth of the students in the subject is tested Oral examinations are to be

conducted in co-cordial environment amongst the teachers taking the examination.

Teachers taking such examinations should not have ill thoughts about each other and

courtesies should be offered to each other in case of difference of opinion, which should

be critically suppressed in front of the students.

Evaluation and marking system:

Basic honesty in the evaluation and marking system is absolutely essential and in the

process impartial nature of the evaluator is required in the examination system to become

popular amongst the students. It is a wrong approach or concept to award the students by

way of easy marking to get cheap popularity among the students to which they do not

Page 24: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

24

deserve. It is a primary responsibility of the teacher that right students who are really

putting up lot of hard work with right kind of intelligence are correctly awarded.

The marking patterns should be justifiable to the students without any ambiguity and

teacher should see that `students are faced with unjust circumstances.

Page 25: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

25

FCFS:

#include < stdio.h>

#include < conio.h>

struct process

{

int pid;

int burst;

int wait;

};

void main()

{

struct process p[5];

int i, j, wait=0;

float twait;

clrscr();

for(i=1;i<=5;i++)

{

p[i].pid=i;

printf("\nprocess p%d\n",i);

printf("\nenter burst time\n");

scanf("%d",&p[i].burst);

}

printf("\nprocess\t\tburst time\t\twaiting time\n");

for(i=1;i<=5;i++)

{

p[i].pid=i;

p[i].wait=wait;

printf("\nP%d\t\t%d\t\t\t\t %d",p[i].pid, p[i].burst,p[i].wait);

wait = wait + p[i].burst;

}

twait=wait;

printf("\n\naverage waiting time= %f",(twait/5));

getch();

}

Page 26: Jawaharlal Nehru Engineering College · Submission related to whatever lab work has been completed should be done during ... Shortest-Job-First (SJF) is a non-preemptive algorithm

26

SJF

# include < stdio.h>

# include < conio.h>

struct process

{

int pid ;

int burst ;

int wait ;

};

void main()

{

struct process p[5] , temp ;

int i,j,wait=0 ;

float twait;

clrscr();

for(i=0;i<5;i++)

{

p[i].pid = i+1 ;

printf("\nprocess p%d\n",i+1) ;

printf("\nenter burst time\n");

scanf("%d",&p[i].burst);

}

for(i=0;i<5;i++)

{ for(j=0;j<5;j++)

{

if(p[j].burst>p[i].burst)

{

temp=p[i];

p[i]=p[j] ;

p[j]=temp;

}

}

}

printf("\nprocess\t\tburst time\t\twaiting time\n");

for(i=0;i<5;i++)

{

p[i].wait=wait ;

printf("\np%d\t\t%d\t\t\t%d\n",p[i].pid,p[i].burst,p[i].wait);

wait = wait + p[i].burst ;

}

twait=wait;

printf("\n\n average waiting time= %f",(twait/5));

getch();

}