Docstoc

Concurrency Concurrency versus Parallelism

Document Sample
Concurrency Concurrency versus Parallelism Powered By Docstoc
					                                                                                             Concurrency versus Parallelism
                                                                                 • Up to now programs have been sequential, i.e. each
                                                                                   program executes a single sequence of instructions
                                                                                 • Programs can do several things at once, with two main
                                                                                   different aims:
                          Chapter R.6                                            • Parallelism: the program can be split across multiple
                          Concurrency                                              processors, which cooperate in getting the work done
                                                                                 • Pure parallelism does not increase expressive power,
                                                                                   just speed (but beware bottlenecks and overheads)
                                 Ian Holyer
                                                                                 • Concurrency: the program can interleave operations from
                                                                                   the user's point of view, increasing expressiveness
                                                                                 • Concurrency can be implemented on a single processor
                                                                                   with processes or threads
Ian Holyer               COMS21101 Software Engineering   Chapter R.6 Slide 1   Ian Holyer           COMS21101 Software Engineering               Chapter R.6 Slide 2




              Applications of concurrency                                                                Processes
 • Hiding network latency, eg in Web browser:                                    • Processes are the traditional form of concurrency in C
      – display the text of a page before the pictures                             and on Unix, and more recently on Windows/MacOS
      – display a 2nd page while the 1st is loading                              • A process is a running program (e.g. "emacs is a
      – make use of multiple network connections                                   program;                                      processes")
                                                                                   program; my computer is running 10 emacs processes")
 • Hiding processing delay:                                                      • Processes allow multiple users to share a single
      – give the user something to look at while doing a lot of                    computer, or allow a computer to act as a server
        processing (e.g. splash screen, progress bar, ...)
                                                                                 • Each user interacts with the computer via a separate
      – give illusion of rapid command execution; allow 2nd                        shell process, and can run further processes
        command to be queued or executed while 1st is executing
                                                                                 • A single program can be made up of several processes
 • User interaction with GUIs:                                                     (e.g. netscape)
      – good user interaction is almost impossible without threads
                                                                                 • Processes are not good in large numbers, or if they are
      – double buffering of images and animations                                  small, or if they need to cooperate closely
Ian Holyer               COMS21101 Software Engineering   Chapter R.6 Slide 3   Ian Holyer           COMS21101 Software Engineering               Chapter R.6 Slide 4




                      C/Unix processes                                                                         Fork
 • C has some standard library procedures (ANSI):                                • A fork creates a new process which is identical to the
                                                                                   original, except that the data and stack are copies and
    Appendix B of Kernighan & Ritchie                                              the return value from the fork() call is different
 • The POSIX library provides a reasonably standard set
   of Unix-like operating system library procedures                                                       data                   42       stack
 • Most POSIX functions are also available for C on PCs:                                     code                                     return from fork
    http://www.whatis.com/posix.htm                                                    shared code
                                                                                                          data                   0        stack
 • The functions include two families:
                                                                                                       copied data                      copied stack
        fork()           create a new process
                                                                                 • fork() returns the id of the new child process to the
             exec()              replace the program code                           parent program, and 0 to the new child process

Ian Holyer               COMS21101 Software Engineering   Chapter R.6 Slide 5   Ian Holyer           COMS21101 Software Engineering               Chapter R.6 Slide 6
                                    Exec                                                                                Fork-and-exec
 • An exec() call discards the current code and loads new                                  •   Often fork() and exec() are used together
   code from a file; it is like a large scale jump                                         •   Example: a shell reads in a command and runs it
 • Naturally, the data is initialised from the new file, and                               •   How? it forks a child process, and the child immediately
   the stack is initialised to be empty                                                        execs the command program
                                                                                                       code                    data                 42    stack
              code               data                       stack
                                                                                                   shell code
                                                                                                                               data                 0     stack
              code        data
                                                             fresh stack
             new code    new data                                                                      code             data
                                                                                                 command code

Ian Holyer                 COMS21101 Software Engineering           Chapter R.6 Slide 7   Ian Holyer                    COMS21101 Software Engineering            Chapter R.6 Slide 8




                            Time Slicing                                                                           Process Switching
 • How does the operating system run several processes?                                    • When the scheduler's timer interrupts a process, its state
   Answer: time slicing                                                                      has to be saved (mostly registers)
 • The OS contains a scheduler which makes use of a                                                processor                42 24
   hardware timer to interrupt execution every so often
 • When a process is interrupted, the OS saves its state                                           proc          code                 data                stack
   ready to continue it later, and starts up another process
 • Processor time is shared between processes, by giving                                   • The processor contains pointer registers specifying the
   each a slice of time in turn                                                              memory segments, program pointer etc
 • Users get the illusion of smooth parallel execution of the                              • It also contains data registers with intermediate values
   processes on n separate processors                                                      • The OS has a process descriptor structure where it can
 • Each virtual processor runs at 1/n speed
                                    1/n                                                      dump all these when the process is switched out
 • Processes can have different priorities (nice in Unix)                                  • It also contains the priority of the process etc
Ian Holyer                 COMS21101 Software Engineering           Chapter R.6 Slide 9   Ian Holyer                    COMS21101 Software Engineering            Chapter R.6 Slide 10




                     Cost of process switching                                                                                 Threads
 • A process switch involves                                                               •   A thread is a lightweight process
   – a system call                                                                         •   Threads cooperate in running a single program
   – dumping the old process registers                                                     •   Threads share program code, and data
                                                                                           •   Each thread has its own stack
      – checking priority and scheduling details (large scale)
                                                                                                          code                    data                   thread 1's stack
      – loading the new process registers
                                                                                                                                                         thread 2's stack
 • Processes do not share data, so there is also an overhead                               • Lightweight means
   in clearing and reloading caches                                                                                                  thread 3's stack
                                                                                              – low cost thread-switching, i.e.
 • Process switching is not suitable for large numbers of                                     – no system calls (well, not necessarily)
   small threads
                                                                                              – register dumping is minimal
                                                                                              – scheduling is kept really simple
Ian Holyer                 COMS21101 Software Engineering       Chapter R.6 Slide 11      Ian Holyer                    COMS21101 Software Engineering            Chapter R.6 Slide 12
              OS v Language Threads                                                                   Pros and Cons
 •   OS threads:                                                              • Features of OS threads:
 •   Unix provides support for lightweight threads                               – Library routines provide all the services you need
 •   See man threads for details                                                 – Threads look like processes to the programmer
 •   There are Solaris and Posix threads
                                                                                 – Proper scheduling and shared I/O are provided
 •   Use Posix ones for better portability
 •   Java used to call these native threads                                      – Slower and horribly platform dependent
                                                                              • Features of language threads:
 •   Language threads:                                                           – No system calls or timer interrupts needed
 •   The run time system for the language supports threads                       – Controllable fast sheduling
 •   The compiler knows and produces suitable code                               – Platform independent thread behaviour
 •   Java used to call these green threads, but always uses
     native threads now                                                          – Often poorly implemented, e.g. no true time slicing
Ian Holyer           COMS21101 Software Engineering   Chapter R.6 Slide 13   Ian Holyer               COMS21101 Software Engineering    Chapter R.6 Slide 14




                       Sharing I/O                                                          Implementing shared I/O
 • Multiple threads run as a single process                                   • A non-blocking getchar() might be:
 • This means that if one thread makes a blocking I/O call                         getchar()
   (e.g. getchar), the whole process suspends                                      { if character available, return it;
 • This is wrong, because other threads should be allowed                             record this thread as suspended;
   to continue unaffected                                                             if thread available, wake it and return;
                                                                                      call select(), or equivalent, which
 • So, threads must not use blocking I/O, they must use                                  puts the whole process to sleep,
   only non-blocking I/O
                                                                                         to be woken by the next I/O operation
 • Non-blocking I/O means that if a call fails to read a                           }
   character from the terminal (say), it returns immediately                  • Also we need to arrange to periodically:
   and tells you so, rather than suspending the process and
   returning the character later                                                   check the threads suspended on I/O
                                                                                   if the I/O op can continue, wake the thread
 • This is sometimes called asynchronous I/O
Ian Holyer           COMS21101 Software Engineering   Chapter R.6 Slide 15   Ian Holyer               COMS21101 Software Engineering    Chapter R.6 Slide 16




                      Sharing data                                                             Problems with Sharing
• If updatable data is shared, there is a serious problem                     • We have seen one problem with inconsistent data
     class Time                                                               • General problems with sharing data include:
     { int hours, minutes;
                                                                                   – inconsistent data: a group of related changes forming a critical
        void tick ()
        { if (minutes == 59)                                                         section is interrupted part way through
            { mins = 0;                                                            – race conditions: threads running at varying rates need to be
               hours++;                                                              synchronised, e.g. producer-consumer flow control
            } else minutes++;
                                                                                   – deadlock: all threads get stuck because each is waiting for
        ...
                                                                                     something that is locked by one of the others
• If the tick call is interrupted, the time is 59 minutes out                      – starvation: a thread is ready to run, but never gets a chance
                mins = 0;                                                            because the others repeatedly lock it out
                /* interrupt */
                hours++;
Ian Holyer           COMS21101 Software Engineering   Chapter R.6 Slide 17   Ian Holyer               COMS21101 Software Engineering    Chapter R.6 Slide 18
                Sharing data safely
 • Every object which contains updatable data, and which
   may be accessed by ≥2 threads, needs protecting
 • Java uses monitors; a monitor is an object whose
   methods are restricted to one-thread-at-a-time
      synchronised void tick () { ... }
 • The tick() call may still be interrupted, but other
   threads will be locked out until it is resumed
 • Problem: programs becomes littered with thread related
   safety code (see String.java)
 • Problem: how do you know your program is correct?
   subtle and unrepeatable bugs are everywhere
 • Solution: nobody knows (yet)
Ian Holyer         COMS21101 Software Engineering   Chapter R.6 Slide 19

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:3/29/2011
language:English
pages:4