100% found this document useful (2 votes)
78 views

Sysenv

The document discusses the operating system (OS) environment and various OS abstractions including processes, files, and objects. It describes the classic process model where a process contains code, data, and required resources. Modern systems use threads as lightweight processes that share resources within a process. The UNIX process model separates process memory into text, data, and stack sections and uses the kernel and system calls to manage resources and request services.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT or read online on Scribd
100% found this document useful (2 votes)
78 views

Sysenv

The document discusses the operating system (OS) environment and various OS abstractions including processes, files, and objects. It describes the classic process model where a process contains code, data, and required resources. Modern systems use threads as lightweight processes that share resources within a process. The UNIX process model separates process memory into text, data, and stack sections and uses the kernel and system calls to manage resources and request services.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT or read online on Scribd
You are on page 1/ 31

The OS Environment

Fred Kuhns
[email protected]
Applied Research Laboratory,
Department of Computer Science and Engineering,
Washington University in St. Louis

Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Model and Core Abstractions
• Basic model
– computer used to access, transform and store information.
• OS provides abstractions to facilitate the information
processing task and its conceptualization.
• Common abstractions in modern systems:
– Process – basic unit of computation
– File – basic unit of information storage
• Less common abstractions (in an OS): Objects
– Objects – basic unit of computation
– performs computation on internal data in response to messages,
may also send messages to other objects.
• Using resources with the process model
– a process must request resources from the OS
– a process must suspend operation until resource is allocated
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 2
Processes and Resources
An application consists of one or more processes, a set of resources and state
Resources
Processes
File F0
Process P0
File F1
Process P1
... ...
File FM
Process PN
memory Ri
...
CPU
display Rj

Operating System

other
CPU DRAM DISKS NET other
other
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 3
Traditional UNIX Model
execution
environment application
libraries
user
kernel System call interface

Other IPC
Process
Resources control scheduler
File subsystem
subsystem
memory

hardware

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 4


Files
• A container for data
– typically persistent storage
– also non-persistent such as memory based files
• Sequential file: named linear stream of records
• Typical operations: open, close, read, write, lseek, ioctl
• UNIX and MS Windows attempt to use a common, file
based interface.

Process code: open(“myfile.txt”, ...) Process File Table


Name: myfile.txt
OS File Table OS Descriptor: 2453, ...
OS Descriptor: 2453, ...
Current byte offset

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 5


File Example
#include <fcntl.h>
#include <unistd.h>
// you should: check for errors and read man pages for syntax and
// requirements
int main (int argc, char **argv) {
int ifd, ofd, n;
char buf[MAXFNAME]
struct Env_t env;
parseargs(&env, argc, argv);

ifd = open(env.ifname, O_RDONLY);


ofd = open(env.ofname, O_WRONLY);

while ((n = read (ifd, (void *)buf, MAXFNAME)) > 0) {


write(ofd, (void *)buf, n);
}

close (ifd);
close (ofd);
exit(0);
}
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 6
Computations, Processes and Threads
• Common model: A computation is the sequential execution of a set of
instructions implementing some algorithm and applied to a specific
data set.
• A program results from the encoding of an algorithm using a
programming language (like C or C++).
– program source is compiled to produce instructions native to the target
platform, i.e. the binary.
• A programmer requests resources from the operating using the
exported OS interface, aka system call interface.
• Classical definition of a process is a program in execution and
embodies all resources and state of a running program (CPU,
memory, files,...)
• Modern systems separate program execution context from other
process resources. A process may have multiple execution contexts
for a given process. Thread is an execution context.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 7


Classic Process Model
Process Pk

Resource R0
Code Data
Resource R1
Context
Resource RN

Abstract Machine Environment (OS)

• Process consists of
– object code (program) to be executed
– data which the process works on
– Required resources
– Execution status, used by the OS to manage process

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 8


Classic Process Example
traces for P1, P2 and P3 Process P1
Resource R0
Code Data
P1 P2 P3 Context
Resource R1
Resource RN

Abstract Machine Environment (OS)

Process P2
Resource R0
Code Data
Resource R1
Context
Resource RN

Abstract Machine Environment (OS)

Process P3
Resource R0
Code Data
Resource R1
Context
Resource RN

Abstract Machine Environment (OS)


Shared Code (Program)
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 9
Modern Process/Thread Model
Process Pk

Resource R0
Code Data
T1 T1 Resource R1
... context
context Resource RN

Abstract Machine Environment (OS)

• Process: encompasses
– set of threads (computational entities)
– collection of resources
• Thread: Dynamic object representing an execution path and
computational state.
– threads have their own computational state: PC, stack, user registers
and private data
– Remaining resources are shared amongst threads in a process
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 10
UNIX Process Model
Resource 1
Resource N
Text Data
(code) Stack
File 1
Status
File 2

UNIX Kernel (OS)

• Memory model: text, data and stack


• Common file interface for many of the resources
• kernel operates in trusted (i.e. system) mode
• Process requests services from kernel using system calls
• Each process has a unique ID (PID or process descriptor)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 11


UNIX Memory Model
0xffffffff
Kernel stack

Kernel address space

Data
0x7fffffff Kernel Text
stack

Process address space

Data
0x00000000 Text (shared)
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 12
UNIX Process Operations
• Well defined hierarchy: one parent and zero or more
child processes. The init process is at the root of this
tree
• Create new process (i.e. child) using fork or vfork
– create new address space
– copies text, data and stack
– child has access to all parent’s open files
• Different programs may be run during the life of a
process by calling exec
• Processes terminate by calling exit
• Parent may wait for child to terminate, and colledt
exit status, by calling wait

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 13


Interacting with the “System”: Command Interpreters

• User interaction is through a command interpreter –a


system application
• Fundamentally, it simply reads and executes commands
– internal commands: pre-compiled into the program
– external commands: stored in the filesystem as executable
files
• For external command, the shell will spawn a child
process to perform the required function:
– the child overwrites its address space with a new program,
exec
– the parent by default calls wait on the child process, then
returns the exit status to the user.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 14


Simple “Shell”

while (TRUE) { /* repeat forever */


display_prompt( ); /* display prompt */
read_command (command, parameters) /* input from
terminal */

if (fork() != 0) { /* fork child process */


/* Parent code goes there */
waitpid( -1, &status, 0); /* wait for child to exit */
} else {
/* Child code goes here*/
execve (command, parameters, 0); /* execute command */
}
}

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 15


Operating Systems: Approach and funtion
• Common approach
– Resource abstractions (data and “special” devices)
– Resource virtualization to enable process abstraction (i.e.
program instance executing within a virtual environment)
– Resource management to ensure isolation and controlled
sharing.
• Common functions:
– Device management
– Process, thread and resource management
– Memory management
– File management

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 16


Lets Step Back and take another look at OSes

• Issues in OS design
– Performance – should be low overhead
– Exclusive use of resources
• protection and security – sharing, authorizing, authenticating
– correctness – does what it is supposed to do
– maintainability – universal goal
– commercial – they are used by people and organizations
– standards and open systems – conforming to standards
• Common implementation mechanisms
– Protection and processor modes
– Trusted control program – kernel
– Method for processes to request services of the OS – system
calls

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 17


Another Look at a typical UNIX System
execution environment
application
trap libraries
user
System call interface
kernel System Services
File subsystem
dispatcher IPC
Process
Exceptions

Buffer cache control Scheduler


Interrupt

subsystem
char block Memory
Device drivers

hardware
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 18
Policy and Mechanism
• A recurring theme in OS design is ensuring exclusive
use of system resources (real and abstract)
• Administrators and developers define policies to
define resource sharing and isolation
• Mechanisms implement these policies
• Common mechanisms:
– hardware enforced processor modes to control execution of
privileged instructions (such as for I/O or memory)
– Core OS modules are trusted and have access to protected
operations and all resources. This kernel of the OS is
responsible for enforcing policies.
– Well defined and protected mechanism for requesting services
of the OS. There are two approaches: system calls or message
passing.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 19


UNIX: Basic Concepts and Mechanisms

• Two privilege levels, or modes


– user and system mode
– kernel space protected: requires special instruction sequence
to change from user to kernel mode
• Process has "protected" address space - protection
• All process share same kernel space - efficiency
• Per process state (u_area) in kernel - efficiency
• per process kernel stack, practical considerations
– kernel is re-entrant
• system versus process context – virtual property

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 20


Mode, Space and Context

Privileged
mode
user kernel
context
Application System calls
process
(user code) Exceptions

system X Interrupts,
kernel
space not allowed System tasks

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 21


The Kernel – UNIX or other general purpose OS

• Trusted program that runs directly on the hardware


• Loaded at boot time and initializes system,
– creates some initial system processes.
– remains in memory and manages the system
• Resource manager/mediator - process is key
abstraction.
– Time share (time-slice) the CPU,
– coordinate access to peripherals,
– manage virtual memory.
– Synchronization primitives.
• Well defined entry points:
– syscalls, exceptions or interrupts.
• Performs privileged operations.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 22


Entry into the Kernel
• Synchronous - kernel performs work on behalf of the
process:
– System call interface (UNIX API): central component of the
UNIX API
– Hardware exceptions - unusual action of process
• Asynchronous - kernel performs tasks that are
possibly unrelated to current process.
– Hardware interrupts - devices assert hardware interrupt
mechanism to notify kernel of events which require attention
(I/O completion, status change, real-time clock etc)
• System processes - scheduled by OS
– swapper and pagedaemon.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 23


Trap or Interrupt Processing
• Hardware switches to kernel mode, using the
per/process kernel stack.
• HW saves PC, Status word and possibly other state on
kernel stack.
• Assembly routine saves any other information
necessary and dispatches event.
• On completion a return from interrupt (RFI)
instruction is performed.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 24


Interrupt
• Asynchronous event.
– Name three?
• Must be serviced in system context,
– What does this mean?
• Must not block,
– Why?
• What happens to the currently running process?
• Multiple interrupt priority levels (ipl), traditionally 0-7.
• User processes and kernel operate at the lowest ipl
level.
• Higher level Interrupts can preempt lower priority
ones.
• The current ipl level may be set while executing
critical code.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 25
Exception Handling
• Exceptions:
– Synchronous to the currently running process. What are some
examples?
– Must run in the current processes context.
– An Interrupt can occur during the processing of an exception
or trap.
• Software Interrupts:
– Interrupts typically have the highest priority in a system.
– Software interrupts are assigned priorities above that of user
processes but below that of interrupts.
– Software interrupts are typically implemented in software.
– Examples are callout queue processing and network packet
processing.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 26


System Calls
• Interface between running program and the operating system
– Generally written in assembly-language
– Wrapper libraries provide high-level interface
• Three general methods are used to pass parameters:
– Pass parameters in registers.
– Store the parameters in a table in memory.
– Push (store) the parameters onto the stack by the program.
• Trap into kernel dispatch routine which invokes a high-level
system call.
• User privileges are verified and any data is copied into kernel
(copyin()).
• On return, copy out any user data (copyout()), check for an
Asynchronous System Trap (signal, preemption, etc).

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 27


Synchronization Approaches
• Traditional
– Kernel is re-entrant.
– only one thread/processes active at any given time (others are
blocked).
– Nonpreemptive.
– Blocking operations
– Masking interrupts
• Modern
– threaded kernels
– synchronization primitives used in kernel
– locking granularity is issue

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 28


Blocking Operations
• When resource is unavailable (possibly locked), process
sets flag and calls sleep()
• sleep places process on a blocked queue, sets state to
asleep and calls swtch()
• when resource released wakeup() is called
• all sleeping processes are woken and state set to
runnable (placed on the runnable queues).
• When running, process must verify resource is
available. Why?

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 29


Process Scheduling
• Preemptive round-robin scheduling
• fixed time quantum
• priority is adjusted by nice value and usage factor.
• Processes in the kernel are assigned a kernel priority
(sleep priority) which is higher than user priorities.
• Kernel 0-49, user 50-127.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 30


Signals
• Asynchronous events and exceptions
• Signal generation using the kill() system call
• Default operation or user specific handlers
• sets a bit in the pending signals mask in the proc
structure
• All signals are handled before return to normal
processing
• System calls can be restarted

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 31

You might also like