Ch_10_UNIX

download Ch_10_UNIX

of 48

Transcript of Ch_10_UNIX

  • 8/4/2019 Ch_10_UNIX

    1/48

    Ceng 334 - Operating Systems 10-1

    Chapter 10 : Case Study - UNIX

    History of unix

    Overview of unix

    Processes in unix

    Memory management in unix Input/output in unix

    The unix file system

    Security in unix

    Note: This case study covers only UNIX. Please read

    chapter 10 of the text book forLINUX.

  • 8/4/2019 Ch_10_UNIX

    2/48

    Ceng 334 - Operating Systems 10-2

    History of UNIX

    Originated from the MULTICS (Multiplexed

    Information and Computing Service)

    operating system by M.I.T, Bell Labs and GE

    There are two main versions:

    AT&T System V Release 4 (SVR4)

    Originally developed by AT&T, nowSCO

    BSD (Berkeley Software Distribution)

  • 8/4/2019 Ch_10_UNIX

    3/48

    Ceng 334 - Operating Systems 10-3

    Overview of UNIX

    Supports various architectures Structure varies

    Supports preemptive multitasking

    Multiuser environment - generally secure Supports multithreaded applications

    Protection/Security is high on modern versions

    Supports symmetric multiprocessing Highly scalable/portable to various systems

    Many types/flavours of UNIX exist

  • 8/4/2019 Ch_10_UNIX

    4/48

    Ceng 334 - Operating Systems 10-4

    UNIX Layers

    The layers of a UNIX system.

    User

    Interface

  • 8/4/2019 Ch_10_UNIX

    5/48

    Ceng 334 - Operating Systems 10-5

    UNIX Utility Programs

    A few of the more common UNIX utility programs required by POSIX

  • 8/4/2019 Ch_10_UNIX

    6/48

    Ceng 334 - Operating Systems 10-6

    UNIX Kernel (1)

    Approximate structure of generic UNIX kernel

  • 8/4/2019 Ch_10_UNIX

    7/48

    Ceng 334 - Operating Systems 10-7

    UNIX Kernel (2)

    Bottom layer

    Device drivers for character and block devices

    Process dispatcher which stops the current

    process, saves its state and starts the appropriate

    driver when an interrupt occurs

  • 8/4/2019 Ch_10_UNIX

    8/48

    Ceng 334 - Operating Systems 10-8

    Process Creation in UNIX - fork

    Process creation in UNIX.

  • 8/4/2019 Ch_10_UNIX

    9/48

    Ceng 334 - Operating Systems 10-9

    POSIX Shell

    A highly simplified shell

  • 8/4/2019 Ch_10_UNIX

    10/48

    Ceng 334 - Operating Systems 10-10

    The ls Command

    Steps in executing the command ls type to the shell

  • 8/4/2019 Ch_10_UNIX

    11/48

    Ceng 334 - Operating Systems 10-11

    Some UNIX Process Concepts

    Daemons (background processes)

    Cron daemon which wakes up once a minute to

    check scheduled events (eg., disk backup)

    Pipes - syncronized channels between

    processes to pass byte streams

    Signalssoftware interrupts used for

    interprocess communication. Choices:

    catch, ignore, or kill process

  • 8/4/2019 Ch_10_UNIX

    12/48

    Ceng 334 - Operating Systems 10-12

    Signals Required By POSIX

    The signals required by POSIX.

  • 8/4/2019 Ch_10_UNIX

    13/48

    Ceng 334 - Operating Systems 10-13

    System Calls for Process Management

    s is an error code

    pid is a process ID

    residual is the remaining time from the previous alarm

  • 8/4/2019 Ch_10_UNIX

    14/48

    Ceng 334 - Operating Systems 10-14

    Thread Calls in POSIX

    The principal POSIX thread calls.

  • 8/4/2019 Ch_10_UNIX

    15/48

    Ceng 334 - Operating Systems 10-15

    Thread Calls in POSIX

    Threads were not in the first versions of UNIX

    There are many thread packages in use which arestandardized in POSIX

    Thread calls are the same for user-space or kernel-space

    In kernel-space implementation calls are systemcalls

    In user-space implementation calls are to a run-time library

  • 8/4/2019 Ch_10_UNIX

    16/48

    Ceng 334 - Operating Systems 10-16

    Thread Communication - mutexes

    Threads use locks calledmutexes for short-time

    locking a resource (say a shared buffer)

    A mutex must be firstcreated(and finallydestroyed Mutual exclusion is implemented by locking a

    mutex before accessing a resource and unlock it

    when they are done (like binary semaphores which

    is 0 or 1, a mutex is either locked or unlocked)

  • 8/4/2019 Ch_10_UNIX

    17/48

    Ceng 334 - Operating Systems 10-17

    Thread Communicationcondition

    variables

    For long-term synchronization (such as waiting for

    a tape to become free)condition variables are

    used Condition variables have to becreatedfirst and

    laterdestroyedlike mutexes

    A condition variable is used by having one thread

    wait on it, and another threadsignalit. If no

    thread is waiting when a signal is sent, the signal

    is lost

  • 8/4/2019 Ch_10_UNIX

    18/48

    Ceng 334 - Operating Systems 10-18

    UNIX Scheduler (1)

    The UNIX scheduler is based on a multilevel queue structure

    (highest priority queue first, round-robin in each queue)

    In this scheme, a process which was blocked and waiting for an

    event joins the appropriate queue when blocking is over (a

    process whose disk I/O is finished joins,say, queue4)

  • 8/4/2019 Ch_10_UNIX

    19/48

    Ceng 334 - Operating Systems 10-19

    UNIX Scheduler (2)

    Once a second the priority of all processes are recalculatedto avoid starvation using

    priority = CPU_usage + nice + base

    CPU_usage, represents the average number of clock ticks

    per second that the process has had during the past fewseconds

    Nice is a value between20 to 20 (default 0). Nice systemcall can be used to set this value 0-20

    Base is a system parameter in UNIX source code The scheduler forces CPU bound (on positive queues) get

    any service that is left over when all I/O bound andinteractive processes are blocked

  • 8/4/2019 Ch_10_UNIX

    20/48

    Ceng 334 - Operating Systems 10-20

    Booting UNIX (1)

    The first sector of the boot disk (master bootrecord) is read in and executed

    This sector loads theboot program

    Boot reads root directory, loadskerneland startsits execution

    Kernel reads the rest of the operating system(main C-code section)

    C code does some initialization, allocates systemdata structures, loads device drivers andhandcrafts the first process,process 0

  • 8/4/2019 Ch_10_UNIX

    21/48

    Ceng 334 - Operating Systems 10-21

    Booting UNIX (2)

    The sequences of processes used to boot some systems

    cp

  • 8/4/2019 Ch_10_UNIX

    22/48

    Ceng 334 - Operating Systems 10-22

    Handling Memory

    Each process has three segments (shown as one segment in the figure, but ifhardware supports they can be separate):

    Text : executable code (which is shared in the figure)

    Data : variables, strings, arrays etc.

    initialized datavariables which must be initialized to some value when programstarts

    Uninitialized data (BSS)not initialized but has value 0 as default

    Stack

    Text is fixed in length, data and stack can grow and shrink

    Process BProcess A

  • 8/4/2019 Ch_10_UNIX

    23/48

    Ceng 334 - Operating Systems 10-23

    Paging in UNIX (1)

    Prior to 3BSD, UNIX systems used swapping

    (if memory is full, swap processes to disk)

    To run a process, all that is needed is the userstructure andpage table. The pages of the

    text, data and stack segments are brought in

    on demand

  • 8/4/2019 Ch_10_UNIX

    24/48

    Ceng 334 - Operating Systems 10-24

    Paging in UNIX (2)Core Map

    Main memory: kernel, core map, pages

    Core map has an entry for each page and contains

    information about the contents of the page frames

    Page on Disk

    Process

    table entry

  • 8/4/2019 Ch_10_UNIX

    25/48

    Ceng 334 - Operating Systems 10-25

    Page Replacement Algorithm (1)

    The page replacement algorithm is executedby thepage daemon (process 2)

    Page daemon wakes up every 250 msec and

    transfers pages to disk if the amount of freememory is less than the system parameter

    lotsfree (typically set to of memory)

    Page daemon uses thetwo-handed clockalgorithm

  • 8/4/2019 Ch_10_UNIX

    26/48

    Ceng 334 - Operating Systems 10-26

    Basic Clock Algorithm (1)

    The pointer (hand) points to the oldest page

    When a page fault occurs,

    if theR bit of the pointed page is 0 (page not referred), this page isevicted (the new page replaces this page - written to disk first if itis dirty)

    if theR bit is 1 (page accessed),R bit is cleared and the hand isadvanced to the next page

  • 8/4/2019 Ch_10_UNIX

    27/48

    Ceng 334 - Operating Systems 10-27

    Two-handed Clock Algorithm (2)

    Page daemon has to do two passes withone core mappointer. Pass 1 clears allR bits, second pass removespages (R bits are set between pass 1 and 2)

    Page daemon maintains two pointers into the coremap to speed up the process (one pass instead of two)for large memories

    When page daemon runs, it first clears theR bit at thefront hand, and then checks theR bit at thebackhand, after which it advances both hands

    Each time the page daemon runs, the hands rotate lessthan a full revolution, the amount depending on thenumber of pages needed to reach lotsfree

  • 8/4/2019 Ch_10_UNIX

    28/48

    Ceng 334 - Operating Systems 10-28

    I/O in UNIX

    All I/O devices are integrated into the file

    system asspecial files

    These special files are accessed likeordinary files (ie., file operations such as

    read, write, open are the same for special

    files

  • 8/4/2019 Ch_10_UNIX

    29/48

    Ceng 334 - Operating Systems 10-29

    Networking in UNIX (1)

    Sockets are used to establish a connection

    between network nodes

  • 8/4/2019 Ch_10_UNIX

    30/48

    Ceng 334 - Operating Systems 10-30

    Networking in UNIX (2) Sockets are created and destroyed dynamically

    Creating a socket returns a file descriptor, which isneeded for establishing a connection, reading data,writing data, and releasing the connection

    One party makes a listen call on a local socket,which creates a buffer and blocks until data arrive

    The other party makes aconnect call giving asparameters the file descriptor of the local socket and

    the address of a remote socket (a sockets has anaddress in the network like the internet)

    Once a connection is established, a socket functionslike apipe

  • 8/4/2019 Ch_10_UNIX

    31/48

    Ceng 334 - Operating Systems 10-31

    UNIX I/O (1)

    When a user accesses a special file, the file systemdetermines the major and minor device numbers andwhether it is a block or character special file

    Major device number is used to index into eitherbdevswarray for block special orcdevsw for character special files

    These structures contain pointers to the procedures to openthe device, read, write etc., Some of the fields of a typical

    cdevsw table are shown below

  • 8/4/2019 Ch_10_UNIX

    32/48

    Ceng 334 - Operating Systems 10-32

    UNIX I/O (2)

    The UNIX I/O system in BSD

    C-list

  • 8/4/2019 Ch_10_UNIX

    33/48

    Ceng 334 - Operating Systems 10-33

    UNIX I/O (3)

    For block special files (eg., disks) the blocks arecached in abuffer cache

    The buffer cache works for both reads and writes

    Usually dirty (modified) blocks are written to thedisk in every 30 seconds

    For character special devices, data is buffered in achain ofC-lists. A C-list block is 64 characterslong, plus a count and a pointer to the next block(BSD method of character buffering)

  • 8/4/2019 Ch_10_UNIX

    34/48

    Ceng 334 - Operating Systems 10-34

    The UNIX File System (1)

    Some important directories found in most

    UNIX systems

  • 8/4/2019 Ch_10_UNIX

    35/48

    Ceng 334 - Operating Systems 10-35

    The UNIX File System (2)

    Before linking.

    After linking.

    (a) Before linking. (b) After linking

  • 8/4/2019 Ch_10_UNIX

    36/48

    Ceng 334 - Operating Systems 10-36

    The UNIX File System (3)

    Separate file systems

    After mounting

    (a) Before mounting (b) After Mounting

  • 8/4/2019 Ch_10_UNIX

    37/48

    Ceng 334 - Operating Systems 10-37

    Locking Files in UNIX (1)

    Accessing a file by several processes need somecritical section management

    This is done by locks

    A lock is defined by a file name, the starting byteand the number of bytes

    When placing a lock, the process specifies to

    Block: when the existing block is removed, the process is

    unblocked and the lock is placed

    Not to block: the system call returns with a status code

    telling whether the lock succeeded or not

  • 8/4/2019 Ch_10_UNIX

    38/48

    Ceng 334 - Operating Systems 10-38

    Locking Files (2)

    (a) File with one lock

    (b) Addition of a second lock

    (c) A third lock

  • 8/4/2019 Ch_10_UNIX

    39/48

    Ceng 334 - Operating Systems 10-39

    System Calls for File Management

    s is an error code (-1 if an error has occured) fd is a file descriptor (a positive number: 0 standard input)

    position is a file offset

  • 8/4/2019 Ch_10_UNIX

    40/48

    Ceng 334 - Operating Systems 10-40

    The statSystem Call

    Fields returned by the stat system call

  • 8/4/2019 Ch_10_UNIX

    41/48

    Ceng 334 - Operating Systems 10-41

    System Calls for Directory Management

    s is an error code

    dir identifies a directory stream

    dirent is a directory entry

  • 8/4/2019 Ch_10_UNIX

    42/48

    Ceng 334 - Operating Systems 10-42

    UNIX File System (1)

    Disk layout in classical UNIX systems

    Block 0 is the boot block

    Block 1 is thesuperblock which contains information

    about the layout of the file system, including thenumber of i-nodes, number of disk blocks, and startof the list of free disk blocks

  • 8/4/2019 Ch_10_UNIX

    43/48

    Ceng 334 - Operating Systems 10-43

    UNIX File System (2)

    Directory entry fields.

    Structure of the i-node in System V

  • 8/4/2019 Ch_10_UNIX

    44/48

    Ceng 334 - Operating Systems 10-44

    UNIX File System (3)

    The relation between the file descriptor table, the open file description

    File descriptor table is

    indexed by thefd

    parameter and has oneentry for each file

  • 8/4/2019 Ch_10_UNIX

    45/48

    Ceng 334 - Operating Systems 10-45

    UNIX File System (4)

    A BSD directory with three files.The same directoryafter the file voluminous has been removed

    File name can be 255 characters long The first 4 fields are fixed length

  • 8/4/2019 Ch_10_UNIX

    46/48

    Ceng 334 - Operating Systems 10-46

    Security in UNIX (1)

    Each UNIX user has a UID (User ID). AUID is an integer between 0 and 65536.Files, processes and other resources are

    marked with the UID of their owner The user with UID 0 is thesuperuser

    Users can be organized in groups, which are

    also numbered with 16-bit GIDs (GroupID)

  • 8/4/2019 Ch_10_UNIX

    47/48

    Ceng 334 - Operating Systems 10-47

    Security in UNIX (2)

    Some examples of file protection modes

  • 8/4/2019 Ch_10_UNIX

    48/48

    System Calls for File Protection

    s is an error code uid and gid are the UID and GID, respectively