operating systems manage: processes memory file systems

54
Operating systems manage: Processes Memory File systems

Upload: abel-stephens

Post on 05-Jan-2016

233 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Operating systems manage: Processes Memory File systems

Operating systems manage: Processes Memory File systems

Page 2: Operating systems manage: Processes Memory File systems

Operating systems manage: Processes Memory Storage I/O subsystem

Page 3: Operating systems manage: Processes Memory File systems

Process Management

A process is a program in execution. It is a unit of work within the system. Program is a passive entity, process is an active entity.

Operating system controls execution of user and system processes.

Process needs resources to accomplish its task CPU, memory, I/O, files Initialization data

Process termination requires reclaim of any reusable resources

Page 4: Operating systems manage: Processes Memory File systems

Process Management Activities

The operating system is responsible for the following activities in connection with process management:

Creating and deleting both user and system processes

Suspending and resuming processes (context switching)

Providing mechanisms for process synchronization

Providing mechanisms for process communication

Page 5: Operating systems manage: Processes Memory File systems

Memory Management

Memory management activities Keeping track of which parts of memory are

currently being used and by whom Deciding which processes (or parts thereof) and data

to move into and out of memory Allocating and deallocating memory space as needed

Page 6: Operating systems manage: Processes Memory File systems

Storage Management

OS provides uniform, logical view of information storage

Abstracts physical properties to logical storage unit - file

Page 7: Operating systems manage: Processes Memory File systems

Storage Management File-System management

Files usually organized into directories Access control on most systems to determine who

can access what OS activities include

Creating and deleting files and directories Primitives to manipulate files and dirs Mapping files onto secondary storage Backup files onto stable (non-volatile) storage media

Page 8: Operating systems manage: Processes Memory File systems

Disk Management

OS activities Free-space management Disk-space allocation Disk scheduling

Some storage need not be fast Tertiary storage includes optical storage, magnetic tape

(often used for back-ups).

Page 9: Operating systems manage: Processes Memory File systems

I/O Management

One purpose of OS is to hide peculiarities of hardware devices from the user

I/O subsystem responsible for Memory management of I/O including buffering

(storing data temporarily while it is being transferred), caching (storing parts of data in faster storage for performance), spooling (intercepting concurrent requests for device such as printer and ensuring sequential order, i.e., no interleaving of files).

General device-driver interface Drivers for specific hardware devices

Page 10: Operating systems manage: Processes Memory File systems

OS as Execution Environment

Can also view the operating system as providing an environment for the execution of programs.

Provides services for user as well as user (and system) applications.

Page 11: Operating systems manage: Processes Memory File systems

Services for User

User interface - Almost all operating systems have a user interface (UI)

Varies between Command-Line Interface (CLI), Graphics User Interface (GUI), Batch

Program execution - The system must be able to load a program into memory and to run that program, end execution, either normally or abnormally (indicating error)

Page 12: Operating systems manage: Processes Memory File systems

Operating System Services

I/O operations - A running program may require I/O, which may involve a file or an I/O device.

File-system manipulation - The file system is of particular interest. Obviously, programs need to read and write files and directories, create and delete them, search them, list file Information, permission management.

Communications – Processes may exchange information, on the same computer or between computers over a network

Communications may be via shared memory or through message passing (packets moved by the OS)

Page 13: Operating systems manage: Processes Memory File systems

Operating System Services

Error detection – OS needs to be constantly aware of possible errors

May occur in the CPU and memory hardware, in I/O devices, in user program

For each type of error, OS should take the appropriate action to ensure correct and consistent computing

Debugging facilities can greatly enhance the user’s and programmer’s abilities to efficiently use the system

Page 14: Operating systems manage: Processes Memory File systems

Other Services

Another set of OS functions exists for ensuring the efficient operation of the system itself via resource sharing

Resource allocation - When multiple users or multiple jobs running concurrently, resources must be allocated to each of them

Many types of resources - CPU cycles, main memory, file storage, and I/O devices.

Accounting - To keep track of which users use how much and what kinds of computer resources

Page 15: Operating systems manage: Processes Memory File systems

Operating System Services (Cont.)

Protection and security - The owners of information stored in a multi-user or networked computer system may want to control use of that information, concurrent processes should not interfere with each other

Protection involves ensuring that all access to system resources is controlled

E.g., base-limit registers for memory protection. Access lists on files.

Security of the system from outsiders. E.g., requiring user name and password.

Page 16: Operating systems manage: Processes Memory File systems

User Operating System Interface - CLI

CLI allows direct command entry Sometimes implemented in kernel, sometimes by

systems program Sometimes multiple flavors implemented – shells Primarily fetches a command from user and

executes it Commands may be implemented in shell Implemented through “system calls”.

Looks for program of that name. If found, executes it. If not, returns an error message.

Page 17: Operating systems manage: Processes Memory File systems

User Operating System Interface - GUI

User-friendly desktop metaphor interface Usually mouse, keyboard, and monitor Icons represent files, programs, actions, etc Various mouse buttons over objects in the interface

cause various actions (provide information, options, execute function, open directory (known as a folder)

Invented at Xerox PARC

Page 18: Operating systems manage: Processes Memory File systems

System Calls

Interface between executing program and OS defined by set of system calls OS provides.

System call causes a TRAP to switch from user to kernel mode and starts execution at interrupt vector location for TRAP instruction.

Operating system looks at requested operation and any parameters passed by the application.

Dispatches the correct system call handler through a table of pointers to system call handlers.

Handler completes and (may) return to user code at the next instruction. OS may schedule another process to execute.

Page 19: Operating systems manage: Processes Memory File systems

Transition from User to Kernel Mode

Page 20: Operating systems manage: Processes Memory File systems

System Calls

Mostly accessed by programs via a high-level Application Program Interface (API) rather than direct system call use

Three most common APIs are Win32 API for Windows, POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM)

Why use APIs rather than system calls?

Page 21: Operating systems manage: Processes Memory File systems

System Calls

Why use APIs rather than system calls? Portability: Code should run on any system that supports

the same API.

Page 22: Operating systems manage: Processes Memory File systems

System Calls

Why use APIs rather than system calls? Portability: Code should run on any system that supports

the same API.

Ease of use. Some system calls are quite complex involving, for example,

assembly code.

Page 23: Operating systems manage: Processes Memory File systems

System Call Implementation

High-level languages provide system-call interface.

Run-time libraries added by the compiler. Program makes an API call. Trapped by the run-time library. RTL places number of requested system call in

correct register. Places parameters in appropriate locations. Issues TRAP.

Page 24: Operating systems manage: Processes Memory File systems

System Call Implementation

The standard I/O library in C (C++) is another high-level API.

Examples: fopen, printf, scanf, cin, cout ……………

These are functions available to the program, but they are not system calls.

Rather, they are replaced (at compile time) with calls to user-level libraries.

In C, these libraries are loaded into the application’s address space via the #include directive.

Actual system call made in the library.

Page 25: Operating systems manage: Processes Memory File systems

Standard C Library Example

C program invoking printf() library call, which calls write() system call.

Library handles details of making system call (e.g., where to put parameters, system call id, etc. )

Page 26: Operating systems manage: Processes Memory File systems

System Call Parameter Passing

Often, more information is required than simply identity of desired system call

Exact type and amount of information vary according to OS and call

Three general methods used to pass parameters to the OS

Simplest: pass the parameters in registers.

Page 27: Operating systems manage: Processes Memory File systems

System Call Parameter Passing

Often, more information is required than simply identity of desired system call

Exact type and amount of information vary according to OS and call

Three general methods used to pass parameters to the OS

Simplest: pass the parameters in registers In some cases, may be more parameters than registers

Page 28: Operating systems manage: Processes Memory File systems

System Call Parameter Passing

Parameters stored in a block, or table, in memory, and address of block passed as a parameter in a register

This approach taken by Linux and Solaris

Parameters placed, or pushed, onto the stack by the program and popped off the stack by the operating system

Approach taken by Unix.

Block and stack methods do not limit the number or length of parameters being passed

Page 29: Operating systems manage: Processes Memory File systems

Parameter Passing via Table

Page 30: Operating systems manage: Processes Memory File systems

#include <stdio.h>

{ while (1)

{ int j, k = 0 ;

char buf[1024] ;

k++ ;

printf(“Hello”) ;

j = open(“my_file”, “w”) ;

read(j, buf, 1024) ;

close(j) ;

printf(“ALL DONE\n “) ;

}

}

Page 31: Operating systems manage: Processes Memory File systems

#include <stdio.h>

/* This is a demo program that does nothing. It was written on 9/13/05 at 10:31 PM. It was written to show the basics of good documentation*/

{ int j ; // declaring and integer called j. It can hold any value //between -2^32 – 1 to 2^32.

char buf[1024] ; //this buffer holds 1K chars. Any characters in //the ASCII character set can be placed in this //buffer.

while(1) //Creating a loop that will run for a really long time.

{

printf(“Hello”) ; //printing Hello to the screen

j = open(“my_file”, “w”) ; // opening a file called “my_file”

read(j, buf, 1024) ; //reading from “my_file” into buffer buf. I sure //hope they are characters that can be placed //into the character buffer buf.

close(j) ;

printf(“ALL DONE\n “) ;

}

}

Page 32: Operating systems manage: Processes Memory File systems

System Calls for Process Management

Process Creation: fork() system call. Creates an exact duplicate of the calling process including all

variables, file descriptors, registers …….. fork returns the process ID of child to the parent (pid), and returns

a zero to child. After completion, two independent processes executing

“concurrently”. The parent can choose to wait for the child process to complete

before resuming its execution.

Page 33: Operating systems manage: Processes Memory File systems

Unix fork()

#include <stdio.h>main(int argc, char *argv[]) { int pid, j,k ; j = 10 ; k = 32 ; pid = fork() ; if (pid == 0) /*I am the child*/

{ Do childish things }

else /* I am the parent */wait(NULL) ; /* Block execution until

child terminates*/

}

Page 34: Operating systems manage: Processes Memory File systems

j = 10

k =32

pid = ?

Page 35: Operating systems manage: Processes Memory File systems

j = 10

k =32

pid =

j = 10

k = 32

pid = 0

Set to pid of child.

fork()

Page 36: Operating systems manage: Processes Memory File systems

j = 10

k =32

pid =

j = 10

k = 32

pid = 0

Set to pid of child. Blue if a boy process.

fork()

Page 37: Operating systems manage: Processes Memory File systems

j = 10

k =32

pid =

j = 10

k = 32

pid = 0

Set to pid of child. Pink if girl process.

fork()

Page 38: Operating systems manage: Processes Memory File systems

Processes Tree on a UNIX System

Page 39: Operating systems manage: Processes Memory File systems

System Programs

Provide a convenient environment for program development and execution

Some of them are simply user interfaces to system calls; others are considerably more complex

File management - Create, delete, copy, rename, print, dump, list, and generally manipulate files and directories

Status information Some ask the system for info - date, time, amount of

available memory, disk space, number of users

Page 40: Operating systems manage: Processes Memory File systems

System Programs (cont’d)

File modification Text editors to create and modify files Special commands to search contents of files or

perform transformations of the text Programming-language support - Compilers,

assemblers, debuggers and interpreters sometimes provided

Page 41: Operating systems manage: Processes Memory File systems

System Programs (cont’d)

Program loading and execution- Communications - Provide the mechanism for

creating virtual connections among processes, users, and computer systems

Allow users to send messages to one another’s screens, browse web pages, send electronic-mail messages, log in remotely, transfer files from one machine to another

Page 42: Operating systems manage: Processes Memory File systems

Operating System Design and Implementation

Design and Implementation of OS not “solvable”, but some approaches have proven successful

Internal structure of different Operating Systems can vary widely

Start by defining goals and specifications Affected by choice of hardware, type of system User goals and System goals

User goals – operating system should be convenient to use, easy to learn, reliable, safe, and fast

System goals – operating system should be easy to design, implement, and maintain, as well as flexible, reliable, error-free, and efficient

Page 43: Operating systems manage: Processes Memory File systems

Operating System Design and Implementation (Cont.)

Important principle to separatePolicy: What will be done? Mechanism: How to do it?

Mechanisms determine how to do something, policies decide what will be done

The separation of policy from mechanism is a very important principle, it allows maximum flexibility if policy decisions are to be changed later

Page 44: Operating systems manage: Processes Memory File systems

Simple Structure

MS-DOS – written to provide the most functionality in the least space

Not divided into modules Although MS-DOS has some structure, its interfaces

and levels of functionality are not well separated

Page 45: Operating systems manage: Processes Memory File systems

MS-DOS Layer Structure

Page 46: Operating systems manage: Processes Memory File systems

Layered Approach

The operating system is divided into a number of layers (levels), each built on top of lower layers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the user interface.

With modularity, layers are selected such that each uses functions (operations) and services of only lower-level layers

Page 47: Operating systems manage: Processes Memory File systems

Layered Operating System

Page 48: Operating systems manage: Processes Memory File systems

UNIX

UNIX – limited by hardware functionality, the original UNIX operating system had limited structuring. The UNIX OS consists of two separable parts

Systems programs The kernel

Consists of everything below the system-call interface and above the physical hardware

Provides the file system, CPU scheduling, memory management, and other operating-system functions; a large number of functions for one level

Page 49: Operating systems manage: Processes Memory File systems

UNIX System Structure

Page 50: Operating systems manage: Processes Memory File systems

Microkernel System Structure

Moves as much from the kernel into “user” space Communication takes place between user

modules using message passing Benefits:

Easier to extend a microkernel Easier to port the operating system to new architectures More reliable (less code is running in kernel mode) More secure

Detriments: Performance overhead of user space to kernel space

communication

Page 51: Operating systems manage: Processes Memory File systems

Mac OS X Structure

Page 52: Operating systems manage: Processes Memory File systems

Modules

Most modern operating systems implement kernel modules

Uses object-oriented approach Each core component is separate Each talks to the others over known interfaces Each is loadable as needed within the kernel

Overall, similar to layers but with more flexible

Page 53: Operating systems manage: Processes Memory File systems

Solaris Modular Approach

Page 54: Operating systems manage: Processes Memory File systems

The Java Virtual Machine