SlideShare a Scribd company logo
System Software for Program Execution:
 User-level View of Operating Systems

      COMP 229 (Section PP) Week 6

          Prof. Richard Zanibbi
          Concordia University
           February 13, 2006
Last Week...
               Supports Programming,
                Efficient System Use
System Software for Program Execution (pp. 1-18)
  Systems and application software (again)
  Resource Abstraction                Applies System Software
  Resource Sharing                          to a Problem


Operating System Strategies (pp. 18-39)                            including
  A brief history of computing and operating systems                 BIOS
       (batch, timesharing, personal, SCC’s, embedded, networks)
  The modern OS as integration/evolution of previous systems


Goals:
  1.    Learn the function and operation of operating systems
  2.    Learn how to exploit OS design during programming (for
        program execution)               “Hacker notes”                        2
Resource Abstractions
  Files
       Represent data on storage devices (e.g. disk)
       Represent data structured as a list


  Abstract Machines
       Represent resources for executing programs
       Program running on abstract machine: a “process”

  Good Abstractions for Programmers:
       Easy to understand
       Provide necessary control (not “too abstract”)
COMP 229-PP (Week 6, Feb. 8)                              3
Resource Sharing
Resource Sharing
  Time-multiplexed: share time on resource (“take turns”, e.g. processor)
  Space-multiplexed: share space on resource (e.g. memory)

Transparent Resource Sharing
  Sharing “invisible” to programmer (done by OS)
  e.g. simulating multiple abstract machines (multiprogramming)

Explicit Resource Sharing
  Programmer requests resource sharing
  e.g. sharing memory between programs

Resource Isolation
  OS permits one process (abstract machine) access to a resource at a time

Security and Consistency Issues
  Prevent problematic or malicious access to resources
                                                                             4
Example

     (Multiprogramming “best case”
      performance, Fig. 1.8)




COMP 229-PP (Week 6, Feb. 8)             5
Operating System Strategies
     OS Strategy
          General characteristics of the programmer’s abstract machine

     Early Days (approx. pre-1960)
          One program at a time, basically no OS

     Strategies
        1. Batch systems: executed non-interactively, multiprogramming
        2. Timesharing systems: multiple users interacting with the system;
           extended multiprogramming, security
        3. Personal Computers, and Workstations: single-user machines;
           multiprogrammed, minimize wait time rather than max. hardware
           use (many had no file system (!); provided by CP/M, MS-DOS)
        4. Embedded systems: guaranteed response times (“real time”)
        5. Small, Communicating Computers (SCC’s): new resource
           management, power management, device management
        6. Network Technology: handle resources, information on networks

COMP 229-PP (Week 6, Feb. 8)                                                  6
This week...
User-Level View of Operating Systems
The Programmer’s Abstract Machine (pp. 42-47)
  Sequential Computation (“single thread”)
  Multithreaded Computation

Process Resources (pp. 47-52)
  esp. files (under UNIX, Windows)

More on Processes and Threads (pp. 52-58)

Writing Concurrent Programs (pp. 58-72)
  Executing Computations
  Executing Commands in UNIX and Windows


Objects (pp. 72-74)
                                                7
The Programmer’s
Abstract Machine
    (pp. 42-52)
Sequential Computation
Algorithm
  A process or set of rules for calculation or problem-solving, esp. with a
   computer (CA Oxford Dictionary)

Sequential algorithms
  Describe a sequence of operations
  Fundamental abstraction for computation

Examples
  Sorting and Search Algorithms
  (e.g. Vol.3,“The Art of Computer Programming” (Donald Knuth))

Algorithms May be Defined Using
  – Natural Language (e.g. English or French)
  – Pseudocode (“informal” operation sequence)
  – Mathematics
     • may not provide an explicit operation sequence
  – As a Source Program in a Programming Language (e.g. C/C++, Java)
     • explicit sequence of operations (unambiguous)                          9
System Calls
     Binary Program
          – Machine-readable translation of source program (e.g.
            “load module”, or executable program (.exe) )
          – Represents explicit machine instructions used to
            implement an algorithm (e.g. op. sequence)


     System Calls in Binary Programs
          – Procedure calls to the system call interface invoke OS
            features (e.g. stop a program, read data from a device)
          – System calls allow the programmer to take advantage
            of resource abstractions and sharing built into the OS

COMP 229-PP (Week 6, Feb. 8)                                          10
What is POSIX?

     Portable Operating System Interface
          Developed to standardize UNIX system call
           interfaces
          POSIX.1: published 1988
          Now used by a large number of UNIXes,
           including Linux
              • Linus Torvalds, Univ. Helsinki, Finland, first
                announced Linux on the Minix newsgroup in 1991



COMP 229-PP (Week 6, Feb. 8)                                     11
Sequential Computation and
            Multiprogramming
Running a Program
  1.   Translate source to binary program (and possibly link)
  2.   Provide program input data, parameters
  3.   Direct OS to start executing instructions from the transfer address
       (“main entry point”) (includes OS calling program loader)
  4.   Program halts when last statement is executed or system call for halt
       (exit()) is made

Execution Engine (part of a process)
  Used by OS to implement multiprogramming
  Status of abstract machine running the process
  Copy of the runtime stack for the process

Runtime Stack
  Used to implement “scopes” in languages (among other things)
  Contains local variables, return addresses, etc.

Fig. 1.2: “Classic” processes (e.g. UNIX)                                      12
Multiprocessing vs. Multithreading
     “Classic” Process
          Sequential program “run” alone on its own abstract machine

     Multiprocessing
          Concurrent execution of processes
          Processes contain separate resource abstractions, binary program copies

     Multithreading
          Thread: another name for “execution engine”
              • “Classic” processes have a single thread
          Multithreading: multiple threads within a single process
          Threads within a process share resources, including binary program

     Example: Fig. 2.6 (Multithreaded process, as found in Windows
      NT/XP, Linux with POSIX threads extension (2.2+))
     Example: Fig 2.3 (“accountant example”)
COMP 229-PP (Week 6, Feb. 8)                                                        13
Implementing Threads
     Example: Java
          Java Virtual Machine (JVM) supports threads
          Thread base class supports threads
          Create subclass of Thread (e.g. MyThread): instances
           define a new thread

     Executing Threads
          – If multiple processors are available, threads may be
            executed in parallel
          – Otherwise (usually) threads share the processor
            (through time-multiplexing)

COMP 229-PP (Week 6, Feb. 8)                                       14
Process Resources

    (pp. 47-52)
OS Management of
   Resources Used by Processes
Resource Allocation
  Threads access resources through system calls
  (insures resource isolation)
  1. Threads must request a resource before use
  2. After request, thread is suspended until resource is allocated to it
     •   while suspended, other threads/processes allocated processor time
  3. After a resource is allocated to a thread, all threads in its
     associated process may share access to the resource (the
     process “owns” the resource)

Memory and Processors
  Traditionally distinguished from other resources
  – Processes/threads (implicitly) request processor when ready
  – When user process is started, loader requests memory from the
     OS to run the program (e.g. user login -> shell -> interaction)
                                                                             16
Resource Descriptors
          – Data structures used to represent physical and
            abstract resources managed by the OS
          – Indicates if resource is available
          – Includes record of which processes are
            currently blocked waiting for the resource

          e.g. file descriptors, process descriptors



COMP 229-PP (Week 6, Feb. 8)                                 17
Files and File Descriptors
     Files
          –   Named, linear stream of bytes stored on a device
          –   Common (default) method for representing data storage
          –   Forms the basis of many other device abstractions
          –   OS maintains record of which files are currently open

     File Lock
          OS preventing processes other than currently allocated from accessing a
           file (e.g. for writing)

     File Descriptor
        OS’s internal representation of file attributes (for file resources)
              • e.g. Indicates if file is locked, or read-only
          UNIX: a non-negative integer (file descriptor (inode) number)
          Windows: “HANDLE”: reference to internal data structure

COMP 229-PP (Week 6, Feb. 8)                                                        18
(Internal) OS Representation of
            Byte Stream Files
                                 Name: Test     (ASCII)
                               Byte     Value
                File Pointer
On file open:                   0     0100 0001 A
(default) pointer set to 0      1     0100 0010 B
Read/Write K bytes:             2     0100 0011 C
Advance pointer by K
                                3     0100 0100 D
Setting File Pointer:           4     0100 0101 E
lseek() (POSIX)
SetFilePointer() (Win)          5     0100 0110 F
No “Type” for Bytes:            6     0100 0111 G
                                                          19
Treated as “raw” bytes
Example: POSIX File System Calls
 System Call                          Effect
 open()        Open file for read or write. OS creates internal
               representation, optionally locks the file. Returns
               a file descriptor (integer), or -1 (error)
 close()       Close file, releasing associated locks and
               resources (e.g. internal representation)
 read()        Read bytes into a buffer. Normally blocks
               (suspends) a process until completion. Returns #
               bytes read, or -1 (error)
 write()       Write bytes from a buffer. Normally blocks
               (suspends) a process until completion.
               Returns # bytes written, or -1 (error)
 lseek()       Set the file pointer location

 fcntl()       (“File Control”): various options to set file
                                                                    20
               attributes (locks, thread blocking, ....)
Rough Equivalences
  for POSIX and Win32 API
 POSIX             Win32
open()    CreateFile()/ OpenFile()
close()   CloseHandle()
read()    ReadFile()
write()   WriteFile()
lseek()   SetFilePointer()



                                     21
Examples

     UNIX file system calls, Fig. 2.4
     Windows file system calls, Fig. 2.5




COMP 229-PP (Week 6, Feb. 8)               22
Using File to Represent Other Resources:
        Devices and Pipes in UNIX
Other Resource Abstractions
  e.g. keyboard, display, memory, processors
  – The more similar these abstractions are, the easier they are for
    programmers to use

UNIX Devices
  Devices are represented as files in the directory tree
  – Devices: located in the /dev directory (e.g. /dev/tty)
  – Devices have open(), close(), read(), write(),
    seek(), fcntl() commands implemented in their drivers
    (similar to file interface)

UNIX Pipes
  Abstract resource allowing processes to communicate by chaining
    outputs to inputs
       e.g. > cat schedule.txt | grep deadline
  – Inputs/outputs are represented using standard files for each
    process (standard input and standard output)                       23
More on Processes and Threads
          (pp. 52-74)
Heavyweight vs. Lightweight Threads

     Heavyweight (“Classic”) Process
          Processes which may contain exactly one thread
           (execution engine)


     Lightweight Process (= “Thread”)
          – A thread (execution engine) within a multithreaded
            process
          – “Light” because each thread does not require the
            overhead of a separate process
     Illustration/Example: Fig. 2.6
COMP 229-PP (Week 6, Feb. 8)                                     25
Thread Representation (Data)
                       in Detail
     Runtime Stack
          Data private to the thread (e.g. local variables)


     Status
          OS data structure: all properties unique to the thread
          – program counter value
          – whether thread is blocked waiting for a resource
          – which resource is being waited for
          – etc.


COMP 229-PP (Week 6, Feb. 8)                                       26
The Move Towards Threads
     Lightweight Processes (Threads)
          Became popular in late 1990’s (esp. w. Java)

     Motivation: Sharing Resources
          – Server managing shared file systems
          – Windowing systems using threads to represent
            individual windows within the physical display

     Example: Fig. 2.7 (window threads)
COMP 229-PP (Week 6, Feb. 8)                                 27
Thread Libraries vs. True Threading
     Thread Libraries (Early 1990’s)
          Used to run threads within heavyweight (“classic”)
           processes (e.g. Mach C thread library)
          OS still implemented classic processes only
          Problem: if one “thread” blocks, then all threads block


       “True” Threading
          OS implements and manages threads independently of
             one another
          If a thread in a multithreaded process blocks, the other
             threads can still execute
COMP 229-PP (Week 6, Feb. 8)                                         28
Creating Processes and Threads
     Initial Process
          – The first process started when machine is turned on
          – Usually the bootstrap loader loading the OS into
            memory


     Creating New Processes and/or Threads
          Done using system calls (i.e. another resource request)
          – If threads are available, separate system calls are used
            for creating threads and creating processes


COMP 229-PP (Week 6, Feb. 8)                                           29
(Initial Process, after turning computer on)

                                Bootstrap Loader
Child Process of                  (Loads OS)                           Parent Process of



                                      OS (UNIX)
                                                                        Sibling Processes

            Login Shell 1            Login Shell 2           Login Shell 3



                                     Bash Shell


                  Email Program                      Web Browser
COMP 229-PP (Week 6, Feb. 8)                                                                30
FORK(), JOIN(), and QUIT()
             System Calls
 (Conway, 1963; Dennis, Van Horne 1966)
Purpose
  Define cooperating sequential processes executing on shared data
  Like threads, processes can share resources (e.g. binary program copy)

FORK(label)
  – Creates a child process that begins execution at “label”
  – Parent (calling) process continues execution after child is created
  – Parent, child are executed concurrently

QUIT()
  Terminates and deallocates (removes) the process issuing the call

JOIN(count)
  – Used to merge processes
  – count is an integer variable shared by a parent and its child processes
  – Locks processor until system call is finished (prevents inconsistency)

                                                                              31
Effect of JOIN(count)


     /* Decrement shared variable */
          count = count – 1;
     /* QUIT unless this is the last process */
          if (count != 0) QUIT();


                     (example provided pp. 56-57)


COMP 229-PP (Week 6, Feb. 8)                        32
Creating “Classic” (Heavyweight)
        Processes (~1970)
Classic Process
 Single-threaded
 Separate resource representation per process
 Each has a separate, private address space


Address Space
 – Physical resources (mainly memory) that can be
   referenced by the execution engine (single thread)
 – Contains stack, status, data, and program (“text”)
 – A memory protection mechanism: prevents writing
   over memory allocated to other processes
                                                        33
Creating “Classic” (Heavyweight) Processes
       Using fork() in POSIX/UNIX
     fork()
          Creates new process
          Child is given copy of current address space
          Parent and child address spaces are separate
              • e.g. changing a variable in the child process does
                not change the variable’s value for the parent


     Process Communication In UNIX
          Only permitted through open files
COMP 229-PP (Week 6, Feb. 8)                                         34
Example: Creating a new process using
                fork()
     1. fork() instruction is executed, causing
        a system call
     2. OS copies data (including address
        space) from the original process to
        the child process
     3. The child process starts execution at
        the instruction following the fork()
        instruction

COMP 229-PP (Week 6, Feb. 8)                      35
Creating Modern and (Lightweight)
           Processes
Creating Modern Process
  – Created by system call from a thread
  – Child created with separate address space, resources
  – A base thread must be created to execute the process
      e.g. CreateProcess() in Windows API

Threads Creating Child Threads within a Process
  – Again, using a system call from a thread
  – Similar to Conway FORK(): child thread runs within the
    process containing the parent thread
  – Child thread has stack and status separate from parent
      e.g. CreateThread() in Windows API
                                                             36
Example: Creating a New Thread
             in a Modern Process
     1. CreateThread(...) is executed
              •     NOTE: unlike fork(), CreateThread() has many
                    parameters
     2. OS creates thread data within the
        process address space (i.e. within the
        same process)
     3. OS starts execution of child thread at
        the indicated starting point

COMP 229-PP (Week 6, Feb. 8)                                       37
Writing Concurrent Programs

         (pp. 58-72)
Multiple Single-Threaded Processes:
               the UNIX Model
     Unix Process Behaviour Defined By
          Text segment (program instructions)
          Data segment (static program data)
          Stack segment (run-time stack: local variables)
          *located in the address space

     UNIX Executable (Binary) Programs
          Compiled/Linked programs define the text, data, and stack
           segments (“a.out”)


     Example: Class UNIX processes (Fig. 2.10)
          Note that the address space contains the Status, Stack Segment,
          Text Segment, and Data Segment
COMP 229-PP (Week 6, Feb. 8)                                                39
Process Descriptors and
             Context Switching
Process Descriptor
 OS Data Structure representing process attributes, incl.:
   • PID: Process Identifier (a reference to the process descriptor)
         – UNIX: PID is integer index for OS-internal table of process descr’s
   •   User that created the process
   •   Parent, Sibling, and Child processes
   •   Resources held by process (e.g. file descriptor references)
   •   Address space for the process
   •   Threads within process
   •   State and Stack Location (in memory) (for classic process)


Context Switching
 Refers to switching the active process and/or thread
 May involve swapping a process/thread out of memory
  and storing it on a device (e.g. disk), among other ops.                       40
UNIX Process Commands
     “ps” command
          – Lists process identifiers (PID) for the user
          – “ps –aux”: lists all processes in the system


     int fork()
          – Returns PID of child process to the parent
          – Returns 0 to child process
          – Child given copy of text, data, stack segments, access
            to all open file resources
          – Parent process descriptor also copied
          – The next process run may be the child, parent, or
            some other process
COMP 229-PP (Week 6, Feb. 8)                                         41
fork() Examples
  Example 1: What do the parent and child output?
       theChild = fork()
       printf(“My PID is %dn”,theChild)


  Example 2: Directing child process execution
       childPID = fork()
       if (theChild == 0) {
           /* child executes here */
           codeForTheChild();
           exit(0);   // Important!
       }
       /* Parent executes here */
       ...

COMP 229-PP (Week 6, Feb. 8)                        42
Changing Program Data Dynamically:
            execve()
int execve(char *path,char *argv[],char *envp[]);
                 program     arguments        environment
                                               variables
Effect of execve()
  – Replaces text, data, and stack areas using the program
    loader
  – After loading, stack is cleared, variables initialized
  – Program is started (call does not return)

System Calls to Wait for Child Process to Stop:
  wait(): block until any child process terminates
  waitpid(P): block until child process with PID P terminates
                                                                43
Multiple Modern Processes
           (e.g. Windows)
Process Creation (CreateProcess())
 Flexible: many more parameters than in fork() (10!)
 Base process can start anywhere in program code
 Returns two resource descriptor references:
   • one for the new process
   • one for the new base thread


Thread Creation (CreateThread())
 Again, flexible, this time with 6 parameters
 A thread may create a sibling thread

  (see text pp. 66-67 for function signatures, discussion)
                                                             44
Executing Commands in
                   UNIX and Windows Shells
     Fatal Errors
          Unrecoverable process errors
              • e.g. trying to write outside a processes’ address space


     System Shells
          Users needs to be able to execute any program,
           however buggy, but without crashing the shell


     Solution:
          Create a child process to execute each command
              • e.g. using fork() or CreateProcess()
COMP 229-PP (Week 6, Feb. 8)                                              45
Objects

(pp. 72-73)
Objects: Another Abstraction
Objects
  – Originally used to represent independent “computation units” in
    simulations; similar to a small process (e.g. workflow simulation)
  – Have private data and operations, along with message passing
    operations (e.g. get/set operations)
  – Can be used to represent a coordinated execution of distributed
    computational units (as was common in simulations)

Classes
  – Simula 67: introduces notion of classes to define object
    behaviour (similar to programs for threads/processes)
  – First widespread application: interfaces (e.g. InterViews, 1989)
  – Java: OS calls into the Java Virtual Machine (JVM), which runs
    on top of the native OS (a simulation in an abstract machine!)
  – OS may be designed to support efficient object implementations
  – Early attempts at “pure” OO operating systems: efficiency issues
                                                                         47
Windows NT/2000/XP
                          Kernel and Objects
     Primitive OS Components
          Defined as objects
          Implemented as C functions: no language inheritance


     Threads
          – Creating a thread generates a kernel Dispatcher
            Object, which then has additional status fields specific
            to threads added (“pseudo inheritence”)
          – This approach allows Kernel operations to be applied
            uniformly to a broad class of abstract data types

COMP 229-PP (Week 6, Feb. 8)                                           48
Summary
• Classic Processes (e.g. in “Old” UNIX)
• Multithreaded Processes (e.g. in Linux,
  Windows)
• Creating Processes and Threads
• Resource Descriptors (file and process)
• Resource Management
• File (e.g. open()) and Process (e.g. fork(),
  CreateThread()) System Calls
• Overview of Writing Concurrent Programs
• Objects and Operating Systems                  49
Next Week...

     Operating Systems Organization
          pages 89-110


     Process Management
          pages 197-206




COMP 229-PP (Week 6, Feb. 8)                  50

More Related Content

PDF
Processes in unix
PPTX
Unix operating system basics
PPT
Process and Threads in Linux - PPT
PPTX
Unix Administration
PDF
Linux kernel architecture
PDF
Ubuntu OS Presentation
PPTX
Unix operating system
PPTX
Unix Operating System
Processes in unix
Unix operating system basics
Process and Threads in Linux - PPT
Unix Administration
Linux kernel architecture
Ubuntu OS Presentation
Unix operating system
Unix Operating System

What's hot (19)

ODP
Linux Internals - Kernel/Core
PPT
PPTX
Introduction to Unix
PDF
Linux kernel architecture
PDF
Linux kernel Architecture and Properties
PDF
Know thyubuntu
 
PPT
Ch1 linux basics
PPT
Linux architecture
PPTX
Chapter 1: Introduction to Unix / Linux Kernel
DOCX
Case study
PPTX
Linux kernel
PDF
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
PPT
Unix Internals OS Architecture
PPTX
Process management in linux
PDF
Architecture Of The Linux Kernel
PPT
Intro to linux systems administration
ODP
Linux internal
PPT
OSCh20
PDF
File systems for Embedded Linux
Linux Internals - Kernel/Core
Introduction to Unix
Linux kernel architecture
Linux kernel Architecture and Properties
Know thyubuntu
 
Ch1 linux basics
Linux architecture
Chapter 1: Introduction to Unix / Linux Kernel
Case study
Linux kernel
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
Unix Internals OS Architecture
Process management in linux
Architecture Of The Linux Kernel
Intro to linux systems administration
Linux internal
OSCh20
File systems for Embedded Linux
Ad

Similar to User level view of os (20)

PDF
Os organization
PPTX
operatinndnd jdj jjrg-system-1(1) (1).pptx
PDF
Process management
PPT
Operating System 2
PPTX
Lecture 3,4 operating systems
PPTX
Lecture 3,4 operating systems
PPT
Chapter 2 - Operating System Structures
PPT
OS - Ch2
 
PPTX
Design Of Operating System_Lecture_OS_2.pptx
PDF
File
PPTX
Chapter 1 Introduction to Operating System Concepts
PDF
Operating Systems PPT 1 (1).pdf
PDF
operating systems hybrid notes for computerscience.pdf
PPT
Ch2
PDF
Engg-0505-IT-Operating-Systems-2nd-year.pdf
PPT
PPT
Chapter 02
PPT
Chapter 02
PPTX
Lec # 1 chapter 2
PPT
Introduction to Operating System
Os organization
operatinndnd jdj jjrg-system-1(1) (1).pptx
Process management
Operating System 2
Lecture 3,4 operating systems
Lecture 3,4 operating systems
Chapter 2 - Operating System Structures
OS - Ch2
 
Design Of Operating System_Lecture_OS_2.pptx
File
Chapter 1 Introduction to Operating System Concepts
Operating Systems PPT 1 (1).pdf
operating systems hybrid notes for computerscience.pdf
Ch2
Engg-0505-IT-Operating-Systems-2nd-year.pdf
Chapter 02
Chapter 02
Lec # 1 chapter 2
Introduction to Operating System
Ad

More from Mohd Arif (20)

PPT
Bootp and dhcp
PPT
Arp and rarp
PPT
User datagram protocol
PPT
Project identification
PPT
Project evalaution techniques
PPT
Presentation
PPT
Pointers in c
PPT
Peer to-peer
PPT
Overview of current communications systems
PPT
Overall 23 11_2007_hdp
PPT
Objectives of budgeting
PPT
Network management
PPT
Networing basics
PPT
Loaders
PPT
Lists
PPT
Iris ngx next generation ip based switching platform
PPT
Ip sec and ssl
PPT
Ip security in i psec
PPT
Intro to comp. hardware
PPT
Heap sort
Bootp and dhcp
Arp and rarp
User datagram protocol
Project identification
Project evalaution techniques
Presentation
Pointers in c
Peer to-peer
Overview of current communications systems
Overall 23 11_2007_hdp
Objectives of budgeting
Network management
Networing basics
Loaders
Lists
Iris ngx next generation ip based switching platform
Ip sec and ssl
Ip security in i psec
Intro to comp. hardware
Heap sort

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
 
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
KodekX | Application Modernization Development
 
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
sap open course for s4hana steps from ECC to s4
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
 
Reach Out and Touch Someone: Haptics and Empathic Computing
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KodekX | Application Modernization Development
 
Unlocking AI with Model Context Protocol (MCP)
Spectral efficient network and resource selection model in 5G networks
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
sap open course for s4hana steps from ECC to s4
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

User level view of os

  • 1. System Software for Program Execution: User-level View of Operating Systems COMP 229 (Section PP) Week 6 Prof. Richard Zanibbi Concordia University February 13, 2006
  • 2. Last Week... Supports Programming, Efficient System Use System Software for Program Execution (pp. 1-18) Systems and application software (again) Resource Abstraction Applies System Software Resource Sharing to a Problem Operating System Strategies (pp. 18-39) including A brief history of computing and operating systems BIOS (batch, timesharing, personal, SCC’s, embedded, networks) The modern OS as integration/evolution of previous systems Goals: 1. Learn the function and operation of operating systems 2. Learn how to exploit OS design during programming (for program execution) “Hacker notes” 2
  • 3. Resource Abstractions Files Represent data on storage devices (e.g. disk) Represent data structured as a list Abstract Machines Represent resources for executing programs Program running on abstract machine: a “process” Good Abstractions for Programmers: Easy to understand Provide necessary control (not “too abstract”) COMP 229-PP (Week 6, Feb. 8) 3
  • 4. Resource Sharing Resource Sharing Time-multiplexed: share time on resource (“take turns”, e.g. processor) Space-multiplexed: share space on resource (e.g. memory) Transparent Resource Sharing Sharing “invisible” to programmer (done by OS) e.g. simulating multiple abstract machines (multiprogramming) Explicit Resource Sharing Programmer requests resource sharing e.g. sharing memory between programs Resource Isolation OS permits one process (abstract machine) access to a resource at a time Security and Consistency Issues Prevent problematic or malicious access to resources 4
  • 5. Example (Multiprogramming “best case” performance, Fig. 1.8) COMP 229-PP (Week 6, Feb. 8) 5
  • 6. Operating System Strategies OS Strategy General characteristics of the programmer’s abstract machine Early Days (approx. pre-1960) One program at a time, basically no OS Strategies 1. Batch systems: executed non-interactively, multiprogramming 2. Timesharing systems: multiple users interacting with the system; extended multiprogramming, security 3. Personal Computers, and Workstations: single-user machines; multiprogrammed, minimize wait time rather than max. hardware use (many had no file system (!); provided by CP/M, MS-DOS) 4. Embedded systems: guaranteed response times (“real time”) 5. Small, Communicating Computers (SCC’s): new resource management, power management, device management 6. Network Technology: handle resources, information on networks COMP 229-PP (Week 6, Feb. 8) 6
  • 7. This week... User-Level View of Operating Systems The Programmer’s Abstract Machine (pp. 42-47) Sequential Computation (“single thread”) Multithreaded Computation Process Resources (pp. 47-52) esp. files (under UNIX, Windows) More on Processes and Threads (pp. 52-58) Writing Concurrent Programs (pp. 58-72) Executing Computations Executing Commands in UNIX and Windows Objects (pp. 72-74) 7
  • 9. Sequential Computation Algorithm A process or set of rules for calculation or problem-solving, esp. with a computer (CA Oxford Dictionary) Sequential algorithms Describe a sequence of operations Fundamental abstraction for computation Examples Sorting and Search Algorithms (e.g. Vol.3,“The Art of Computer Programming” (Donald Knuth)) Algorithms May be Defined Using – Natural Language (e.g. English or French) – Pseudocode (“informal” operation sequence) – Mathematics • may not provide an explicit operation sequence – As a Source Program in a Programming Language (e.g. C/C++, Java) • explicit sequence of operations (unambiguous) 9
  • 10. System Calls Binary Program – Machine-readable translation of source program (e.g. “load module”, or executable program (.exe) ) – Represents explicit machine instructions used to implement an algorithm (e.g. op. sequence) System Calls in Binary Programs – Procedure calls to the system call interface invoke OS features (e.g. stop a program, read data from a device) – System calls allow the programmer to take advantage of resource abstractions and sharing built into the OS COMP 229-PP (Week 6, Feb. 8) 10
  • 11. What is POSIX? Portable Operating System Interface Developed to standardize UNIX system call interfaces POSIX.1: published 1988 Now used by a large number of UNIXes, including Linux • Linus Torvalds, Univ. Helsinki, Finland, first announced Linux on the Minix newsgroup in 1991 COMP 229-PP (Week 6, Feb. 8) 11
  • 12. Sequential Computation and Multiprogramming Running a Program 1. Translate source to binary program (and possibly link) 2. Provide program input data, parameters 3. Direct OS to start executing instructions from the transfer address (“main entry point”) (includes OS calling program loader) 4. Program halts when last statement is executed or system call for halt (exit()) is made Execution Engine (part of a process) Used by OS to implement multiprogramming Status of abstract machine running the process Copy of the runtime stack for the process Runtime Stack Used to implement “scopes” in languages (among other things) Contains local variables, return addresses, etc. Fig. 1.2: “Classic” processes (e.g. UNIX) 12
  • 13. Multiprocessing vs. Multithreading “Classic” Process Sequential program “run” alone on its own abstract machine Multiprocessing Concurrent execution of processes Processes contain separate resource abstractions, binary program copies Multithreading Thread: another name for “execution engine” • “Classic” processes have a single thread Multithreading: multiple threads within a single process Threads within a process share resources, including binary program Example: Fig. 2.6 (Multithreaded process, as found in Windows NT/XP, Linux with POSIX threads extension (2.2+)) Example: Fig 2.3 (“accountant example”) COMP 229-PP (Week 6, Feb. 8) 13
  • 14. Implementing Threads Example: Java Java Virtual Machine (JVM) supports threads Thread base class supports threads Create subclass of Thread (e.g. MyThread): instances define a new thread Executing Threads – If multiple processors are available, threads may be executed in parallel – Otherwise (usually) threads share the processor (through time-multiplexing) COMP 229-PP (Week 6, Feb. 8) 14
  • 15. Process Resources (pp. 47-52)
  • 16. OS Management of Resources Used by Processes Resource Allocation Threads access resources through system calls (insures resource isolation) 1. Threads must request a resource before use 2. After request, thread is suspended until resource is allocated to it • while suspended, other threads/processes allocated processor time 3. After a resource is allocated to a thread, all threads in its associated process may share access to the resource (the process “owns” the resource) Memory and Processors Traditionally distinguished from other resources – Processes/threads (implicitly) request processor when ready – When user process is started, loader requests memory from the OS to run the program (e.g. user login -> shell -> interaction) 16
  • 17. Resource Descriptors – Data structures used to represent physical and abstract resources managed by the OS – Indicates if resource is available – Includes record of which processes are currently blocked waiting for the resource e.g. file descriptors, process descriptors COMP 229-PP (Week 6, Feb. 8) 17
  • 18. Files and File Descriptors Files – Named, linear stream of bytes stored on a device – Common (default) method for representing data storage – Forms the basis of many other device abstractions – OS maintains record of which files are currently open File Lock OS preventing processes other than currently allocated from accessing a file (e.g. for writing) File Descriptor OS’s internal representation of file attributes (for file resources) • e.g. Indicates if file is locked, or read-only UNIX: a non-negative integer (file descriptor (inode) number) Windows: “HANDLE”: reference to internal data structure COMP 229-PP (Week 6, Feb. 8) 18
  • 19. (Internal) OS Representation of Byte Stream Files Name: Test (ASCII) Byte Value File Pointer On file open: 0 0100 0001 A (default) pointer set to 0 1 0100 0010 B Read/Write K bytes: 2 0100 0011 C Advance pointer by K 3 0100 0100 D Setting File Pointer: 4 0100 0101 E lseek() (POSIX) SetFilePointer() (Win) 5 0100 0110 F No “Type” for Bytes: 6 0100 0111 G 19 Treated as “raw” bytes
  • 20. Example: POSIX File System Calls System Call Effect open() Open file for read or write. OS creates internal representation, optionally locks the file. Returns a file descriptor (integer), or -1 (error) close() Close file, releasing associated locks and resources (e.g. internal representation) read() Read bytes into a buffer. Normally blocks (suspends) a process until completion. Returns # bytes read, or -1 (error) write() Write bytes from a buffer. Normally blocks (suspends) a process until completion. Returns # bytes written, or -1 (error) lseek() Set the file pointer location fcntl() (“File Control”): various options to set file 20 attributes (locks, thread blocking, ....)
  • 21. Rough Equivalences for POSIX and Win32 API POSIX Win32 open() CreateFile()/ OpenFile() close() CloseHandle() read() ReadFile() write() WriteFile() lseek() SetFilePointer() 21
  • 22. Examples UNIX file system calls, Fig. 2.4 Windows file system calls, Fig. 2.5 COMP 229-PP (Week 6, Feb. 8) 22
  • 23. Using File to Represent Other Resources: Devices and Pipes in UNIX Other Resource Abstractions e.g. keyboard, display, memory, processors – The more similar these abstractions are, the easier they are for programmers to use UNIX Devices Devices are represented as files in the directory tree – Devices: located in the /dev directory (e.g. /dev/tty) – Devices have open(), close(), read(), write(), seek(), fcntl() commands implemented in their drivers (similar to file interface) UNIX Pipes Abstract resource allowing processes to communicate by chaining outputs to inputs e.g. > cat schedule.txt | grep deadline – Inputs/outputs are represented using standard files for each process (standard input and standard output) 23
  • 24. More on Processes and Threads (pp. 52-74)
  • 25. Heavyweight vs. Lightweight Threads Heavyweight (“Classic”) Process Processes which may contain exactly one thread (execution engine) Lightweight Process (= “Thread”) – A thread (execution engine) within a multithreaded process – “Light” because each thread does not require the overhead of a separate process Illustration/Example: Fig. 2.6 COMP 229-PP (Week 6, Feb. 8) 25
  • 26. Thread Representation (Data) in Detail Runtime Stack Data private to the thread (e.g. local variables) Status OS data structure: all properties unique to the thread – program counter value – whether thread is blocked waiting for a resource – which resource is being waited for – etc. COMP 229-PP (Week 6, Feb. 8) 26
  • 27. The Move Towards Threads Lightweight Processes (Threads) Became popular in late 1990’s (esp. w. Java) Motivation: Sharing Resources – Server managing shared file systems – Windowing systems using threads to represent individual windows within the physical display Example: Fig. 2.7 (window threads) COMP 229-PP (Week 6, Feb. 8) 27
  • 28. Thread Libraries vs. True Threading Thread Libraries (Early 1990’s) Used to run threads within heavyweight (“classic”) processes (e.g. Mach C thread library) OS still implemented classic processes only Problem: if one “thread” blocks, then all threads block “True” Threading OS implements and manages threads independently of one another If a thread in a multithreaded process blocks, the other threads can still execute COMP 229-PP (Week 6, Feb. 8) 28
  • 29. Creating Processes and Threads Initial Process – The first process started when machine is turned on – Usually the bootstrap loader loading the OS into memory Creating New Processes and/or Threads Done using system calls (i.e. another resource request) – If threads are available, separate system calls are used for creating threads and creating processes COMP 229-PP (Week 6, Feb. 8) 29
  • 30. (Initial Process, after turning computer on) Bootstrap Loader Child Process of (Loads OS) Parent Process of OS (UNIX) Sibling Processes Login Shell 1 Login Shell 2 Login Shell 3 Bash Shell Email Program Web Browser COMP 229-PP (Week 6, Feb. 8) 30
  • 31. FORK(), JOIN(), and QUIT() System Calls (Conway, 1963; Dennis, Van Horne 1966) Purpose Define cooperating sequential processes executing on shared data Like threads, processes can share resources (e.g. binary program copy) FORK(label) – Creates a child process that begins execution at “label” – Parent (calling) process continues execution after child is created – Parent, child are executed concurrently QUIT() Terminates and deallocates (removes) the process issuing the call JOIN(count) – Used to merge processes – count is an integer variable shared by a parent and its child processes – Locks processor until system call is finished (prevents inconsistency) 31
  • 32. Effect of JOIN(count) /* Decrement shared variable */ count = count – 1; /* QUIT unless this is the last process */ if (count != 0) QUIT(); (example provided pp. 56-57) COMP 229-PP (Week 6, Feb. 8) 32
  • 33. Creating “Classic” (Heavyweight) Processes (~1970) Classic Process Single-threaded Separate resource representation per process Each has a separate, private address space Address Space – Physical resources (mainly memory) that can be referenced by the execution engine (single thread) – Contains stack, status, data, and program (“text”) – A memory protection mechanism: prevents writing over memory allocated to other processes 33
  • 34. Creating “Classic” (Heavyweight) Processes Using fork() in POSIX/UNIX fork() Creates new process Child is given copy of current address space Parent and child address spaces are separate • e.g. changing a variable in the child process does not change the variable’s value for the parent Process Communication In UNIX Only permitted through open files COMP 229-PP (Week 6, Feb. 8) 34
  • 35. Example: Creating a new process using fork() 1. fork() instruction is executed, causing a system call 2. OS copies data (including address space) from the original process to the child process 3. The child process starts execution at the instruction following the fork() instruction COMP 229-PP (Week 6, Feb. 8) 35
  • 36. Creating Modern and (Lightweight) Processes Creating Modern Process – Created by system call from a thread – Child created with separate address space, resources – A base thread must be created to execute the process e.g. CreateProcess() in Windows API Threads Creating Child Threads within a Process – Again, using a system call from a thread – Similar to Conway FORK(): child thread runs within the process containing the parent thread – Child thread has stack and status separate from parent e.g. CreateThread() in Windows API 36
  • 37. Example: Creating a New Thread in a Modern Process 1. CreateThread(...) is executed • NOTE: unlike fork(), CreateThread() has many parameters 2. OS creates thread data within the process address space (i.e. within the same process) 3. OS starts execution of child thread at the indicated starting point COMP 229-PP (Week 6, Feb. 8) 37
  • 39. Multiple Single-Threaded Processes: the UNIX Model Unix Process Behaviour Defined By Text segment (program instructions) Data segment (static program data) Stack segment (run-time stack: local variables) *located in the address space UNIX Executable (Binary) Programs Compiled/Linked programs define the text, data, and stack segments (“a.out”) Example: Class UNIX processes (Fig. 2.10) Note that the address space contains the Status, Stack Segment, Text Segment, and Data Segment COMP 229-PP (Week 6, Feb. 8) 39
  • 40. Process Descriptors and Context Switching Process Descriptor OS Data Structure representing process attributes, incl.: • PID: Process Identifier (a reference to the process descriptor) – UNIX: PID is integer index for OS-internal table of process descr’s • User that created the process • Parent, Sibling, and Child processes • Resources held by process (e.g. file descriptor references) • Address space for the process • Threads within process • State and Stack Location (in memory) (for classic process) Context Switching Refers to switching the active process and/or thread May involve swapping a process/thread out of memory and storing it on a device (e.g. disk), among other ops. 40
  • 41. UNIX Process Commands “ps” command – Lists process identifiers (PID) for the user – “ps –aux”: lists all processes in the system int fork() – Returns PID of child process to the parent – Returns 0 to child process – Child given copy of text, data, stack segments, access to all open file resources – Parent process descriptor also copied – The next process run may be the child, parent, or some other process COMP 229-PP (Week 6, Feb. 8) 41
  • 42. fork() Examples Example 1: What do the parent and child output? theChild = fork() printf(“My PID is %dn”,theChild) Example 2: Directing child process execution childPID = fork() if (theChild == 0) { /* child executes here */ codeForTheChild(); exit(0); // Important! } /* Parent executes here */ ... COMP 229-PP (Week 6, Feb. 8) 42
  • 43. Changing Program Data Dynamically: execve() int execve(char *path,char *argv[],char *envp[]); program arguments environment variables Effect of execve() – Replaces text, data, and stack areas using the program loader – After loading, stack is cleared, variables initialized – Program is started (call does not return) System Calls to Wait for Child Process to Stop: wait(): block until any child process terminates waitpid(P): block until child process with PID P terminates 43
  • 44. Multiple Modern Processes (e.g. Windows) Process Creation (CreateProcess()) Flexible: many more parameters than in fork() (10!) Base process can start anywhere in program code Returns two resource descriptor references: • one for the new process • one for the new base thread Thread Creation (CreateThread()) Again, flexible, this time with 6 parameters A thread may create a sibling thread (see text pp. 66-67 for function signatures, discussion) 44
  • 45. Executing Commands in UNIX and Windows Shells Fatal Errors Unrecoverable process errors • e.g. trying to write outside a processes’ address space System Shells Users needs to be able to execute any program, however buggy, but without crashing the shell Solution: Create a child process to execute each command • e.g. using fork() or CreateProcess() COMP 229-PP (Week 6, Feb. 8) 45
  • 47. Objects: Another Abstraction Objects – Originally used to represent independent “computation units” in simulations; similar to a small process (e.g. workflow simulation) – Have private data and operations, along with message passing operations (e.g. get/set operations) – Can be used to represent a coordinated execution of distributed computational units (as was common in simulations) Classes – Simula 67: introduces notion of classes to define object behaviour (similar to programs for threads/processes) – First widespread application: interfaces (e.g. InterViews, 1989) – Java: OS calls into the Java Virtual Machine (JVM), which runs on top of the native OS (a simulation in an abstract machine!) – OS may be designed to support efficient object implementations – Early attempts at “pure” OO operating systems: efficiency issues 47
  • 48. Windows NT/2000/XP Kernel and Objects Primitive OS Components Defined as objects Implemented as C functions: no language inheritance Threads – Creating a thread generates a kernel Dispatcher Object, which then has additional status fields specific to threads added (“pseudo inheritence”) – This approach allows Kernel operations to be applied uniformly to a broad class of abstract data types COMP 229-PP (Week 6, Feb. 8) 48
  • 49. Summary • Classic Processes (e.g. in “Old” UNIX) • Multithreaded Processes (e.g. in Linux, Windows) • Creating Processes and Threads • Resource Descriptors (file and process) • Resource Management • File (e.g. open()) and Process (e.g. fork(), CreateThread()) System Calls • Overview of Writing Concurrent Programs • Objects and Operating Systems 49
  • 50. Next Week... Operating Systems Organization pages 89-110 Process Management pages 197-206 COMP 229-PP (Week 6, Feb. 8) 50