Lecture Systemcalls

Embed Size (px)

Citation preview

  • 7/28/2019 Lecture Systemcalls

    1/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Computer Organization

    ApplicationsOS : Systemsoftware

    user-levelprocesses

    word processor, spreadsheet, web browser, bankingsystem, airline reservation system

    compiler, editor, shells, utilities, libraries

    OS : Kernelkernel-levelprocesses

    process management, device drivers, systemcalls

    Hardware processor, data storage devices, I/O devices

  • 7/28/2019 Lecture Systemcalls

    2/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Operating System

    A collection of system programs which allow theuser to run application software.

    manages processes and resources.

    abstracts the real hardware of the system andpresents the users and applications with a virtual

    machine.

    has utilities for system administration tasks.

  • 7/28/2019 Lecture Systemcalls

    3/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    OS Mechanisms

    Two mechanisms for controlling accessto system resources are

    different modes of execution

    system calls

  • 7/28/2019 Lecture Systemcalls

    4/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Processor Modes

    Modern processors typically can operate in 2 modes:"user mode" and "kernel mode " .

    User mode

    processor executes normal instructions in the user's program.

    Kernel mode

    processor executes both normal and privileged instructions

    Processor can access additional registers and memory address

    space that are accessible only in kernel mode

  • 7/28/2019 Lecture Systemcalls

    5/19

  • 7/28/2019 Lecture Systemcalls

    6/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    System Calls

    User programs are not allowed to accesssystem resources directly. They must ask theOS to do that for them.

    OS provides a set of functions that can be

    called by user programs to request for OS

    services. These functions are called system

    calls

  • 7/28/2019 Lecture Systemcalls

    7/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    System Calls

    System calls run in kernel mode.

    They can be called by executing a special

    instruction (trap or software interrupt) which

    causes processor to switch to the kernel mode andjump to a previously defined location in the

    kernel.

    When the system call finishes, processor returnsto the user program and runs in user mode.

  • 7/28/2019 Lecture Systemcalls

    8/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Differences between library functions andsystem calls

    System calls run in kernel-mode but libraryfunctions run in user-mode and may callsystem calls.

    System calls are not linked to user

    programs.

    There are only a small number of system

    calls. (about 250 in Linux, see

    /usr/include/asm/unistd.h )

  • 7/28/2019 Lecture Systemcalls

    9/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    Muangsin

    Solaris System Calls

    Solaris has about 200 system calls.

    Files and I/O

    Process management

    Interprocess communication

    Others

  • 7/28/2019 Lecture Systemcalls

    10/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    Muangsin

    System Calls: Files & I/O

    open, close open and close a file

    create, unlink create and remove a file

    read, write read and write a file

    lseek move to a specified byte in a file

    chmod change access permission mode

    mkdir, rmdir make and remove a directory

    stat get file status

    ioctl control device

  • 7/28/2019 Lecture Systemcalls

    11/19

  • 7/28/2019 Lecture Systemcalls

    12/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    Muangsin

    System Calls: Interprocess Communication

    kill, sigsend send a signal to a process

    pause suspend process until signal

    sigaction set signal handler

  • 7/28/2019 Lecture Systemcalls

    13/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Programming in UNIX

    Programming in a UNIX system (and modernoperating systems) often relies on two concepts:

    controlled access to system services

    realized by having system calls running in privileged

    mode

    common interface, various implementations

    realized by API

  • 7/28/2019 Lecture Systemcalls

    14/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Programming with System Calls

    High level languages, including C, providefunctions for programmers to use systemcalls just like normal function.

    /usr/include/unistd.h

    man -s 2 intro (Solaris) orman 2 intro

    (Linux)

  • 7/28/2019 Lecture Systemcalls

    15/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    Muangsin

    errno and perror

    When a system call fails, it usually returns -1 and

    sets the global variable errno to a value describingwhat went wrong.

    The function perror() translates this error code

    into human-readable form and prints a system error

    message.

    void perror(const char *s);

    The argument string s is printed first, then the

    system error message. Usually s is a user message

    indicating where the error occurs.

  • 7/28/2019 Lecture Systemcalls

    16/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    API (Application Programming Interface)

    An API is a set of functions provided by an operatingsystem or other system software.

    An application program calls the functions to request the

    services.

    An API clearly defines how to call functions and what

    the results are. (API is specification, not implementation)

    Examples: APIs for file system, graphics user interface,networking, etc.

  • 7/28/2019 Lecture Systemcalls

    17/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Advantages of Using API

    Portability

    User programs that follow the APIs definition are portable.

    An API can provide a common interface for different

    implementations of a service. For example, the UNIX file system API is the same for all

    kinds of devices.

    X windows API has many implementations on different

    machine architectures Using an API allows upgrading system software without

    changing user programs

  • 7/28/2019 Lecture Systemcalls

    18/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Privileged Instructions

    Two modes of execution --> two kinds ofinstructions :

    normal instructions, e.g., add, sub, etc.

    privileged instructions, e.g.,

    read/write reserved registers

    read/write protected memory

  • 7/28/2019 Lecture Systemcalls

    19/19

    Slides for 2110311 Systems Programming, Department of

    Computer Engineering, Chulalongkorn University, by Veera

    1

    Exception Mechanism

    Switching from user mode to kernel mode to performsystem operations and return to user mode

    Exception is caused by hardware interupt (I/O) and

    software interrupt

    Software interrupt or trap

    Some special instructions cause processor to switchbetween user and kernel modes: trap , return_from_trap

    Exception Handler When an exception occurs, an exception handler is

    called. Exception handler code is in kernel's protected memory Exception hander code for software traps are system