Docsity

Program Swapping and Basic I/O in Unix: An Overview, Study notes of Computer Science

Partial preview of the text

Download Program Swapping and Basic I/O in Unix: An Overview and more Study notes Computer Science in PDF only on Docsity!

Chapter 14 and 15

Program Swapping &

Introduction to Basic I/

• Rupesh Jain

• Mikel Rodriguez

Outline

1. Introduction

2. Understanding the Sched Procedure

3. Walkthrough of the program swapping code

4. Four procedure manipulating the text array

5. Basic I/O under Unix

6. The file “Buff.h”

7. The file “Conf.c”

8. The Swap procedure

9. Race Conditions

10.Summary

11.References

What is Program Swapping?

Processes can be selectively swapped out and swap in to share the

limited resource of main physical memory among several process

It is also called roll in and roll out

UNIX like other timesharing and multiprogramming system uses

Program swapping

Process Swap Area

DISK

Main Memory

Process

Main Memory

Swap Area

Swap Out

Swap In

Handle by direct call (2024 ) on the procedure xswap (4368)

Handle by direct call (2034 ) on the procedure swap (5196)

Process 0

Process …

Process …

Process 0

Sched

Sched (1940)

Sched spends most of its time waiting in one of the following situation

A. (runout)

  • None of the processes which are swapped out is ready to run
  • The situation can be changed by a call to “wakeup” or to “xswap”

called by “newproc”.

B. (runin)

•There is atleast one process swapped out and ready to run but it

hasn’t been out more than 3 seconds and none of the process

present in main memory is inactive or has been more than 2 seconds.

•The situation can be changed by a call to “sleep”

Find the process ready to run,select the longest one

A search made for the process which is ready to run and has been swapped out for the longest time

Sched Procedure

There is no such process, situation A holds

main memory area for to hold data segment if text segment needs to be present also text segment the size needs to be increased

If it has adequate size text and data then goto found2(2031)

If the process is waiting for event of low precedence &which is not locked and state is swait or sstop but not ssleep then goto swap out

If image to be swapped in has been <3 sec ,B holds

Search for the process which is loaded but not locked whose state is srun or ssleep (waiting for high precedence) and been in mem for longest time

Process swapped out is < sec ,situation B holds Swap out using xswap and process image is flagged as not loaded

Read the text seg into mm Swap( addr within swap area, mm addr, size, direction indicator)

Release disk swap area to the available list,record mm addr,set the sload reset the accumulated time indicator

An Introduction to

Basic I/O under Unix

• “buf.h”

• “conf.h”

• “conf.c”

There are three files whose contents are essential to UNIX

input/output:

The File “buf.h”

This file declares two structures:

buff devtab

“struct buf”

This structure is buffer header and serves as a buffer control block

Position on free list Major+minor device name transfer count

block # on device returned after I/O words not transferred

Status flags

The buf structure may be divided into three

sections:

“struct buf” (continued)

Flags

List pointer

i/o parameters

The File “conf.c”

“Swbuf” controls swapping input and output

Swbuf in “bio.c”

Swbuf Continued (An Example)

The code for swap displays some of the problems of race conditions when several processes are running together

A

Process “A” initiates a swapping operation

A

A

Flags==B_BUSY | B_PHYS | rdflg

Swbuf Continued (An Example)

A

B

Process “B” initiates a swapping operation

Flags==B_BUSY | B_PHYS | rdflg

B

Swbuf Continued (An Example)

A

B

C

Wake up

Wake up

What happens next depends on the order in which A and B are reactivated

Flags== B_BUSY | B_PHYS | rdflg | B_DONE

Sets Flags to

Swbuf Continued (An Example)

Case 1: A goes first:

•“B_DONE” is set (so no more sleeping is needed) (^) •“B_WANTED” is reset (so there is no wakeup)

Process “A” finishes up and sets: Flags==B_PHYS | rdflg | B_DONE

A A

A

B

Process B can now finish

Swbuf Continued (An Example)

Case 2: B goes first:

•Finds “B_BUSY” •Turns “B_WANTED” on

Goes to sleep again leaving: Flags==B_BUSY | B_PHYS | rdflg | B_DONE | B_WANTED

Process “A” starts again: it finds B_WANTED on •Process A calls “wakeup”

  • Process “B” wakes up and finishes

Summary

  • “Sched” procedure makes the decision of swap in and swap out
  • Four procedures manipulate the text array structure
    1. xswap
    2. xccdec
    3. xalloc
    4. xfree

•The “buff” structure in “buf.h” is buffer header and serves as a buffer control block. •“Swbuf” (an instance of the buf structure) controls the process of swapping input and output.