+ All Categories
Home > Documents > Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Date post: 21-Dec-2015
Category:
View: 219 times
Download: 4 times
Share this document with a friend
48
Chapter 2: Using the Operating System
Transcript
Page 1: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Chapter 2: Using the Operating System

Page 2: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

The Airplane Pilot’s Abstract Machine

Page 3: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Basic Abstractions

ProgramProgram ResultResult

ProgramProgram ResultResult

ProgramProgram ResultResult

… …

Idea

Idea

Idea

Page 4: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Basic Abstractions(2)

ProgramProgram ResultResult

AbstractMachine

AbstractMachine

ProgramProgram ResultResult

ProgramProgram ResultResult

AbstractMachine

PhysicalMachine

… …

Idea

Idea

Idea

Page 5: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Abstract Machine Entities

• Process: A sequential program in execution

• Resource: Any abstract resource that a process can request, and which may can cause the process to be blocked if the resource is unavailable.

• File: A special case of a resource. A linearly-addressed sequence of bytes. “A byte stream.”

Page 6: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Algorithms, Programs, and Processes

Data

FilesFiles

Files

OtherResources

AlgorithmAlgorithm

Idea

SourceProgram

SourceProgram

BinaryProgram

Execution Engine

Process

StackStatus

Page 7: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Classic Process

• OS implements {abstract machine} – one per task

• Multiprogramming enables N programs to be space-muxed in executable memory, and time-muxed across the physical machine processor.

• Result: Have an environment in which there can be multiple programs in execution concurrently*, each as a processes

* Concurrently: Programs appear to execute simultaneously

Page 8: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Process Abstraction

Hardware

DataData

ProcessProcess

Sta

ck

Processor

ExecutableMemory

ProgramProgram

Operating SystemOperating System

Page 9: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Processes Sharing a Program

Shared Program Text

P1

P2

P3

P1 P2 P3

FilesFiles

FilesFiles

FilesFiles

Page 10: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Multithreaded Accountant

Purchase Orders

Invoice

Invoice

(a) Separate Processes

Purchase Orders

InvoiceFirst Accountant

Second Accountant

Accountant & Clone

(b) Double Threaded Process

Page 11: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Modern Process & Thread

• Divide classic process:– Process is an infrastructure in which execution

takes place – address space + resources– Thread is a program in execution within a

process context – each thread has its own stack

DataData

ProcessProcess

Sta

ck

ProgramProgram

Operating SystemOperating System

ThreadThread

Thread

Sta

ck

Sta

ck

Page 12: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

A Process with Multiple Threads

Data

FilesFiles

Files

OtherResources

BinaryProgram

Process

StackStatus

StackStatus

StackStatus

Thread (Execution Engine)

Page 13: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

More on Processes• Abstraction of processor resource

– Programmer sees an abstract machine environment with spectrum of resources and a set of resource addresses (most of the addresses are memory addresses)

– User perspective is that its program is the only one in execution

– OS perspective is that it runs one program with its resources for a while, then switches to a different process (context switching)

• OS maintains– A process descriptor data structure to implement the

process abstraction• Identity, owner, things it owns/accesses, etc.• Tangible element of a process

– Resource descriptors for each resource

Page 14: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Address Space• Process must be able to reference every

resource in its abstract machine

• Assign each unit of resource an address– Most addresses are for memory locations– Abstract device registers– Mechanisms to manipulate resources

• Addresses used by one process are inaccessible to other processes

• Say that each process has its own address space

Page 15: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Shared Address Space

• Classic processes sharing program shared address space support

• Thread model simplifies the problem– All threads in a process implicitly use that process’s

address space , but no “unrelated threads” have access to the address space

– Now trivial for threads to share a program and data• If you want sharing, encode your work as threads in a process

• If you do not want sharing, place threads in separate processes

Page 16: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Process & Address Space

Address Space

CodeResources

Resources

Abstract Machine Environment

Stack

DataResources

Page 17: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Creating a Process

• Here is the classic model for creating processes:

FORK(label): Create another process in the same addressspace beginning execution at instruction labelQUIT(): Terminate the process.JOIN(count): disableInterrupts(); count--; if(count > 0) QUIT(); enableInterrupts();

Page 18: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Example

procA() { while(TRUE) { <compute section A1>; update(x); <compute section A2>; retrieve(y); }}

procB() { while(TRUE) { retrieve(x); <compute section B1>; update(y); <compute section B2>; }}

Process AProcess A Process BProcess B

x

y

Page 19: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

UNIX Processes

UNIX Kernel

DataSegment

FilesFiles

Files

OtherResources

TextSegment

StackSegment

Status

Process

Page 20: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

UNIX Processes

• Each process has its own address space– Subdivided into text, data, & stack segment– a.out file describes the address space

• OS kernel creates descriptor to manage process

• Process identifier (PID): User handle for the process (descriptor)

• Try “ps” and “ps -aux” (read man page)

Page 21: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Creating/Destroying Processes

• UNIX fork() creates a process– Creates a new address space– Copies text, data, & stack into new adress space– Provides child with access to open files

• UNIX wait() allows a parent to wait for a child to terminate

Page 22: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Creating a UNIX Process

int pidValue; ...pidValue = fork(); /* Creates a child process */if(pidValue == 0) { /* pidValue is 0 for child, nonzero for parent */ /* The child executes this code concurrently with parent */ childsPlay(…); /* A procedure linked into a.out */ exit(0);}/* The parent executes this code concurrently with child */parentsWork(..);wait(…); ...

Page 23: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Child Executes aDifferent Program

int pid; .../* Set up the argv array for the child */ .../* Create the child */if((pid = fork()) == 0) { /* The child executes its own absolute program */ execve(childProgram.out, argv, 0); /* Only return from an execve call if it fails */ printf(“Error in the exec … terminating the child …”); exit(0);} ...wait(…); /* Parent waits for child to terminate */ ...

Page 24: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Example: Parent

#include <sys/wait.h>

#define NULL 0

int main (void){ if (fork() == 0){ /* This is the child process */ execve("child",NULL,NULL); exit(0); /* Should never get here, terminate */ }/* Parent code here */ printf("Process[%d]: Parent in execution ...\n", getpid()); sleep(2); if(wait(NULL) > 0) /* Child terminating */ printf("Process[%d]: Parent detects terminating child \n", getpid()); printf("Process[%d]: Parent terminating ...\n", getpid());}

Page 25: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Example: Child

int main (void){/* The child process's new program This program replaces the parent's program */

printf("Process[%d]: child in execution ...\n", getpid()); sleep(1); printf("Process[%d]: child terminating ...\n", getpid());}

***sys_wait in another word file

Page 26: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Threads -- The NT Model

Data

FilesFiles

Files

OtherResources

BinaryProgram

Process

StackStatus

StackStatus

StackStatus

Thread (Execution Engine)

Page 27: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Resources

• Anything that a process requests from an OS– Available allocated– Not available process is blocked

• Examples– Files– Primary memory address space (“virtual memory”)– Actual primary memory (“physical memory”)– Devices (e.g., window, mouse, kbd, serial port, …)– Network port– … many others …

Page 28: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Files

• Data must be read into (and out of) the machine – I/O devices

• Storage devices provide persistent copy• Need an abstraction to make I/O simple –

the file• A file is a linearly-addressed sequence of

bytes– From/to an input device– Including a storage device

Page 29: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

The File Abstraction

Hardware

DataData

ProcessProcess

Sta

ck

Processor

ExecutableMemory

ProgramProgram

Operating SystemOperating System

StorageDevice

FileFile

FileDescriptor

FileDescriptor

Page 30: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

UNIX Files

open Specifies file name to be usedclose Release file descriptorread Input a block of informationwrite Output a block of informationlseek Position file for read/writeioctl Device-specific operations

• UNIX and NT try to make every resource (except CPU and RAM) look like a file

• Then can use a common interface:

Page 31: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

UNIX File Example#include <stdio.h>#include <fcntl.h>int main() { int inFile, outFile; char *inFileName = “in_test”; char *outFileName = “out_test”; int len; char c;

inFile = open(inFileName, O_RDONLY); outFile = open(outFileName, O_WRONLY);/* Loop through the input file */ while ((len = read(inFile, &c, 1)) > 0) write(outFile, &c, 1);/* Close files and quite */ close(inFile); close(outFile);}

Page 32: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Shell Command Line Interpreter

OS

Interactive User

Shell Program

Application& SystemSoftware

OS System Call InterfaceOS System Call Interface

Page 33: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

The Shell Strategy

Shell ProcessShell Process

Processto executecommand

Processto executecommand

% grep first f3

f3

read keyboard fork a process

read file

Page 34: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Bootstrapping

• Computer starts, begins executing a bootstrap program -- initial process

• Loads OS from the disk (or other device)

• Initial process runs OS, creates other processes

Page 35: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Bootstrap Program

• This appendix describes the Bootstrap Program, also known as the ROM Monitor.

• The Bootstrap Program can help you isolate or rule out hardware problems encountered when installing your router.

• A summary of the bootstrap diagnostic tests and command options is provided.

Page 36: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

The ROM program varies in length, but basically performs the following:

• Diagnostics, usually called Power On Self Test (POST)

• Location, loading, and execution of a larger bootstrap program from the bootable drive

• Loading and execution of the kernel by the bootstrap program

Page 37: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

The ROM bootstrap program does the following, though not necessarily in this

order: • Performs POST, including testing memory,

keyboard, and console.

• Polls all addresses where a device can reside, and either runs diagnostics in the ROM or asks the device to run a self-diagnostic and report the results.

• Finds the device that is bootable, usually the first drive.

Page 38: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

• Determines whether that device has a program in its own ROM that needs to be loaded in order to control the device, then loads it.

• Jumps to the now-loaded device control program that lets you load the disk bootstrap program, and loads it. The program resides in the boot block or boot sector of a disk. That sector has become a favorite target area for virus writers, because it's very hard to boot a computer without loading and executing the boot sector.

• Jumps to the beginning of the now-loaded disk bootstrap program and executes it.

Page 39: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

The origin of “Bootstrapping”

• Booting is a shortened version of bootstrapping, which comes from the expression "to lift yourself up by your own bootstraps."

• Oddly enough, that would be impossible, but the expression means "to help yourself."

Page 40: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

A typical Unix boot sequence is:

• Power-on • ROM boot starts and runs POST • ROM boot loads and executes the boot

sector or boot block (commonly called primary boot)

• Boot block executes by loading and executing the secondary boot program, which does a local or network boot

• Unix kernel is loaded from a local disk drive or over the network, then executed

Page 41: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Initializing a UNIX Machine

Serial Port A

Serial Port B

Serial Port C

Serial Port Z

login

login

login

login

gettygetty

/etc/passwd

Page 42: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Objects

• A recent trend is to replace processes by objects

• Objects are autonomous

• Objects communicate with one another using messages

• Popular computing paradigm

• Too early to say how important it will be ...

Page 43: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Interrupt

• CLI

• STI

Page 44: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.
Page 45: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.
Page 46: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Be careful about CLI and STI• Unix-like systems have used the functions cli and

sti to disable and enable interrupts for many years. • In modern Linux systems, however, using them

directly is discouraged. • It is increasingly impossible for any routine to

know whether interrupts are enabled when it is called; thus, simply enabling interrupts with sti before return is a bad practice.

• Your function may be returning to a function that expects interrupts to be still disabled.

Page 47: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Thus, if you must disable interrupts, it is better to use the following calls:

• unsigned long flags;

• save_flags(flags);

• cli();

• /* This code runs with interrupts disabled */ restore_flags(flags);

Page 48: Chapter 2: Using the Operating System. The Airplane Pilot’s Abstract Machine.

Enabling and Disabling Interrupts

• We have already seen the sti and cli functions, which can enable and disable all interrupts. Sometimes, however, it's useful for a driver to enable and disable interrupt reporting for its own IRQ line only. The kernel offers three functions for this purpose, all declared in <asm/irq.h>:

• void disable_irq(int irq); void disable_irq_nosync(int irq);

• void enable_irq(int irq);


Recommended