computer system programming

Upload: onurcan

Post on 03-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 computer system programming

    1/46

    Ch1:Intro to Unix

    1

  • 8/12/2019 computer system programming

    2/46

    Unix (officially trademarked as UNIX, sometimes

    also written as Unix) is a multitasking, multi-

    user computer operating system originallydeveloped in 1969 by a group of AT&T employees

    at Bell Labs.

    The Unix operating system was first developed

    in assembly language, but by 1973 had been

    almost entirely recoded in C.

    Among all variants of Unix, Linux (1991) is themost widely used, powering everything from huge

    data centers to desktop systems to mobile phones

    to embedded devices such as routers.2

  • 8/12/2019 computer system programming

    3/46

    Unix shell is a command-line interpreter or shellthat provides a traditional user interface for theUnix operating system and for Unix-like systems.

    Users direct the operation of the computer byentering commands as text for a command lineinterpreter to execute or by creating text scriptsof one or more such commands.

    The program is called a "shell" because it hidesthe details of the underlying operating systembehind the shell's interface.

    The shell manages the technical details of the

    operating system kernel interface, which is thelowest-level, or 'inner-most' component of anoperating system.

    3

  • 8/12/2019 computer system programming

    4/46

  • 8/12/2019 computer system programming

    5/46

    5

  • 8/12/2019 computer system programming

    6/46

    System call is how a program requests a

    service from an operating system's kernel. This

    may include hardware related services (fore.g. accessing the Hard Disk), creating and

    executing new processes, and communicating

    with kernel services(like scheduling). System

    calls provide the interface between a process

    and the operating system.

    6

  • 8/12/2019 computer system programming

    7/46

    Systems Programming

    Systems programming is an area of computer

    engineering that deals with the developmentof complex program systems and their

    components.

    Unix Standards

    ANSI C

    a language standard

    includes standard C library

    7

  • 8/12/2019 computer system programming

    8/46

    8

  • 8/12/2019 computer system programming

    9/46

    Basic modules of UNIX

    User program

    Library

    Shell

    User level

    OS command

    Library

    System call interface

    Kernel

    level

    Process management

    &memory management

    File

    Subsystem

    Character block

    I/O I/O

    Devicedrivers

    Hardware control

    Hardware

    Hardware

    level

    9

  • 8/12/2019 computer system programming

    10/46

    In computing, a device driver is a computer program allowinghigher-level computer programs to interact with a hardware device.

    A driver typically communicates with the device throughthe computer bus to which the hardware connects Drivers are

    hardware-dependent and operating-system-specific. A device driver simplifies programming by acting as translator

    between a hardware device and the applications or operatingsystems that use it. Programmers can write the higher-levelapplication code independently of whatever specific hardwaredevice.

    There are device drivers for printers, displays, CD-ROM readers,diskette drives, and so on. When you buy an operating system,many device drivers are built into the product. However, if you laterbuy a new type of device that the operating system didn'tanticipate, you'll have to install the new device driver. A devicedriver essentially converts the more general input/outputinstructions of the operating system to messages that the devicetype can understand.

    10

  • 8/12/2019 computer system programming

    11/46

    An abstract view of UNIX architecture

    hardware

    Text editor Command-line interpreter (shell)

    VI sh pico

    a.out

    open( ), read( ), close( )

    write()

    fork(

    )

    UNIX kernel

    who

    cc

    Application

    programs

    C compiler

    System

    calls

    11

    UNIX and its basic ser ices

  • 8/12/2019 computer system programming

    12/46

    UNIX and its basic services

    User Processes System Processes

    System callinterface

    Interface todevices

    U U U S S

    Kernel

    LAN

    Devices

    Modem

    12

    B i S i

  • 8/12/2019 computer system programming

    13/46

    Basic ServicesSystem initializationProcess management

    Memory managementFile system managementInput/output managementCommunication facilities

    Program interface (system calls)UNIX - overview:

    UNIX is a Multitasking, Multi-user OS, with a Virtual

    Memory, Programmable Shell and Networking capability.

    UNIX has access controls and securityUNIX environment is organized into three layers kernel -

    programs - shell

    UNIX has a hierarchical, universal file system

    UNIX - set of processes 13

  • 8/12/2019 computer system programming

    14/46

    Multitasking Capability of UNIXProcess:

    A single program in execution (e.g. running a Cprogram, reading I/O, sending an email are all kinds ofprocesses)Each process in UNIX has a unique Process ID (PID)

    Created by fork() systems callsProcess termination (return from main() or callingexit())

    Multiprocess application: a process wants to execute

    another programProcess Synchronization: wait() system call is used

    Process communication: local and remote IPCmechanisms

    14

  • 8/12/2019 computer system programming

    15/46

    Multitasking: is the ability to execute severalprocesses simultaneously.

    Processes are not really executed at the sametime, but the operating system executes a pieceof one process and then switches to another (thisis called task switching).

    This switching happens so fast (10x per secondsor more) that it looks like the computer is doingseveral things at a time.

    At any given time, only one process is running. For a certain period of time, all the processes

    have made progress.

    15

  • 8/12/2019 computer system programming

    16/46

    Process (task)

    1

    Process (task)

    2

    Process (task)

    3

    Context

    switching

    Time

    16

  • 8/12/2019 computer system programming

    17/46

    Multiuser capability of UNIX

    UNIX

    hostLocal Area Network (LAN)

    UNIX

    is here

    No UNIX

    here

    (Windows

    OS)Users workstations (PCs)

    UNIX

    Shell commands

    Shell

    Each host runs a number of independent processes that executes in different protection domains (WSs).

    17

  • 8/12/2019 computer system programming

    18/46

    Networking capability of UNIX

    Electronic mail (e-mail)

    File transfer (FTP) service

    World wide web (WWW) service Remote login (through Telnet)

    Archie: is a tool for indexing FTP archives, allowingpeople to find specific files.

    Gopher: is a TCP/IP application layer protocol designedfor distributing, searching, and retrieving documentsover the Internet. Gopher protocol was a predecessorof (and later, an alternative to) the World Wide Web.The protocol imposes a much stronger hierarchy on

    information stored on it. Its text menu interface is well-suited to computing environments that rely heavily onremote text-oriented computer terminals, which werestill common at the time of its creation in 1991.

    18

  • 8/12/2019 computer system programming

    19/46

    A typical session in UNIX

    Login: your identificator

    Password: your password

    %command 1

    %

    %logout

    A few UNIX commands

    ls or ls -l to list the current directory

    cd to change a directory

    rm to delete a file

    mkdir to create a directory rmdir to remove a directory

    more to show a text file

    19

  • 8/12/2019 computer system programming

    20/46

    Standard Files on a UNIX System:

    /bin - (binary files) contains executable

    programs, UNIX commands

    /dev - special files that represent the physical

    devices

    /lib - libraries of programs used byprogrammers

    /sys - system source files (system managers)

    /tmp - temporary files

    20

  • 8/12/2019 computer system programming

    21/46

    Commands:

    The basic form of any Unix command is:

    command_name options argument(s) Most descriptions for commands such as

    those given in the On-line Manualuse a muchmore precise syntax. For example:

    cp [-iprR] filename ... directory UNIX on-line help manual

    Section

    Contents 1 User commands (Shell commands)

    2 OS services (system calls)

    3 Library functions

    4 Devices networks interfaces21

  • 8/12/2019 computer system programming

    22/46

    5 System file formats

    6 Demo programs

    7 Miscellaneous (ASCII, etc )

    8 System maintenance commands

    Getting Specific Information

    Once you know the names of the man page or pages youneed, you can display them on the screen by giving the mancommand:

    man [section] If you omit the section number, the man program searches

    through each section in turn until it finds the named manpage.

    Most manual sections have an ``intro'' man page, describing

    that section in general. So to find out more about libraryfunctions (section 3 of the man pages), type

    %man 2 intro

    %man 3 intro

    %man 2 fork

    %man 3 sin22

  • 8/12/2019 computer system programming

    23/46

    Interrupts are a commonly used technique

    for computer multitasking, especially in real-

    time computing. Such a system is said to beinterrupt-driven. An act of interrupting is

    referred to as an interrupt request (IRQ). The

    part of a program that deals with the interrupt

    is referred to as an interrupt service routine

    (ISR) or interrupt handler.

    23

  • 8/12/2019 computer system programming

    24/46

    Interruption handling in UNIX (simplified)

    Interruptsignal

    Save theinterruption

    NoNew priority > currentpriority?

    Yes

    Create a new context,set current priority

  • 8/12/2019 computer system programming

    25/46

    UNIX kernel

    Basic services:

    System initialization (bootstrap facility).

    Process management (creation, control, andtermination of process).

    Memory management (using a virtualmemory).

    File system management.

    I/O management

    Communication facilities (inter-processcommunication, networks).

    Program interface (system calls).25

  • 8/12/2019 computer system programming

    26/46

    Kernel software

    Machine dependent Machine independent

    System initialization

    Interrupt handling

    Memory management

    I/O device drivers

    Etc.

    System call interface (!)

    Management of processes

    Scheduling system

    Etc.

    26

  • 8/12/2019 computer system programming

    27/46

    The UNIX Operating SystemBasic structure of the UNIX operating system is divided

    into three parts:

    kernelschedules programsmanages data/file access and storageenforces security mechanismsperforms all hardware access

    shellpresents each user with a promptinterprets commands types by a userexecutes user commandssupports a custom environment for each user

    utilitiesfile management (rm, cat, ls, rmdir, mkdir)user management (passwd, chmod, chgrp)process management (kill, ps)printing (lp, troff, pr)

    program development tools 27

  • 8/12/2019 computer system programming

    28/46

    Ch2: Preparing a C program and using

    command line parameters in UNIX

    28

  • 8/12/2019 computer system programming

    29/46

    How to prepare a C program in UNIX

    Case A: Your program consists of one source file

    After starting a text editor (for example PICO), create and save a

    source text file (for example myprog.c).

    Compile and link the program. The result is executable file (for

    example, myprog).

    Start your program.

    %pico

    %cc -o myprog myprog.c

    %myprog or

    %myprog param1,param2.

    29

  • 8/12/2019 computer system programming

    30/46

    Case B: Your program consists of a few separate sourcefiles (for example, module1.c, module2.c, module3.c)

    1. Create in a text editor, and save each source file.

    2. Compile separately each source file. The result isobject files (module1.o, module2.o, module3.o)

    3. Link the object files. The result is executable file.

    %cc -c module1.c

    %cc -c module2.c%cc -c module3.c

    %cc -o myprog module1.o module2.o module3.o

    %cc -o myprog module1.c module2.c module3.c

    30

  • 8/12/2019 computer system programming

    31/46

    31

  • 8/12/2019 computer system programming

    32/46

    32

  • 8/12/2019 computer system programming

    33/46

    33

  • 8/12/2019 computer system programming

    34/46

    34

  • 8/12/2019 computer system programming

    35/46

    35

  • 8/12/2019 computer system programming

    36/46

    36

  • 8/12/2019 computer system programming

    37/46

    37

  • 8/12/2019 computer system programming

    38/46

    If you execute as dayit will print usage: day .

    38

  • 8/12/2019 computer system programming

    39/46

    39

  • 8/12/2019 computer system programming

    40/46

    40

  • 8/12/2019 computer system programming

    41/46

    41

  • 8/12/2019 computer system programming

    42/46

    42

  • 8/12/2019 computer system programming

    43/46

    43

  • 8/12/2019 computer system programming

    44/46

    44

    l f

  • 8/12/2019 computer system programming

    45/46

    To clarify array pointers,

    45

  • 8/12/2019 computer system programming

    46/46