SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
Operating Systems
         CMPSCI 377
     Processes & Threads
                   Emery Berger
University of Massachusetts Amherst




UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Outline
    Processes


    Threads


    Basic synchronization


    Bake-off





      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   2
Processes vs. Threads…
    Both useful for parallel programming &


    concurrency
        Hide latency
    

        Maximize CPU utilization
    

        Handle multiple, asynchronous events
    

    But: different programming styles,


    performance characteristics, and more




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   3
Processes

    Process:


    execution context
    (PC, registers) +
    address space,
    files, etc.
    Basic unit of


    execution



      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   4
Process API
    UNIX:


        fork() – create copy of current process
    

              Different return value
          

              Copy-on-write
          


        exec() – replace process w/ executable
    



    Windows:


        CreateProcess (…)
    

              10 arguments
          




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   5
Translation Lookaside Buffer
    TLB: fast, fully

    associative memory
        Stores page
    
        numbers (key),
        frame (value) in
        which they are
        stored
    Copy-on-write:

    protect pages, copy
    on first write

             UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   6
Processes Example




  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   7
Exercise
    Print out fib(0) … fib(100), in parallel


        Main loop should fork off children
    

        (returns pid of child)
          int pid;
          pid = fork();
          if (pid == 0) {
            // I’m child process
          } else {
            // I’m parent process
          }

        Wait for children (waitpid(pid,0,0))
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   8
Exercise
// Print fib(0) ... fib(100) in parallel.
int main()
{
  int cids[101]; // Saved child pids.
  for (int i = 0; i < 101; i++) { // Fork off a bunch of processes to run
    fib(i).
    int cid = fork();
    if (cid == 0) { // I am the child.
      cout << quot;Fib(quot; << i << quot;) = quot; << fib(i) << endl;
      return 0; // Now the child is done, so exit (IMPORTANT!).
    } else { // I am the parent. Store the child’s pid somewhere.
      cids[i] = cid;
    }
  }
  // Wait for all the children.
  for (int i = 0; i < 101; i++) {
    waitpid (cids[i], 0, 0); // Wait for the ith child.
  }
  return 0;
}




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   9
Communication
    Processes:


        Input = state before fork()
    

        Output = return value
    

              argument to exit()
          


    But: how can processes communicate


    during execution?




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   10
IPC
    signals


        Processes can send & receive ints associated
    

        with particular signal numbers
             Process must set up signal handlers
         


        Best for rare events (SIGSEGV)
    

             Not terribly useful for parallel or concurrent
         

             programming
    pipes


        Communication channels – easy & fast
    

        Just like UNIX command line
    

             ls | wc -l
         


        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   11
Pipe example
int main() {
  int pfds[2];
  pipe(pfds);
  if (!fork()) {
    close(1);     /* close normal stdout */
    dup(pfds[1]); /* make stdout same as pfds[1] */
    close(pfds[0]); /* we don't need this */
    execlp(quot;lsquot;, quot;lsquot;, NULL);
  } else {
    close(0);     /* close normal stdin */
    dup(pfds[0]); /* make stdin same as pfds[0] */
    close(pfds[1]); /* we don't need this */
    execlp(quot;wcquot;, quot;wcquot;, quot;-lquot;, NULL);
  }
}



       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   12
IPC, continued
    sockets


    - Explicit message passing
    + Can distribute processes anywhere
    mmap (common and aws0m hack)


        All processes map same file into fixed
    
        memory location
        Objects in region shared across processes
    

        Use process-level synchronization
    

             Much more expensive than threads…
         




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   13
Threads
    Processes -


    everything in
    distinct address
    space

    Threads – same


    address space
    (& files, sockets,
    etc.)


      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   14
Threads API
    UNIX (POSIX):


        pthread_create() – start separate
    

        thread executing function
        pthread_join() – wait for thread to
    

        complete

    Windows:


        CreateThread (…)
    

              only 6 arguments!
          




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   15
Threads example
#include <pthread.h>
void * run (void * d) {
  int q = ((int) d);
  int v = 0;
  for (int i = 0; i < q; i++) {
    v = v + expensiveComputation(i);
  }
  return (void *) v;
}
main() {
  pthread_t t1, t2;
  int r1, r2;
  pthread_create (&t1, run, 100);
  pthread_create (&t2, run, 100);
  pthread_wait (&t1, (void *) &r1);
  pthread_wait (&t2, (void *) &r2);
  printf (“r1 = %d, r2 = %dn”, r1, r2);
}

     UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   16
Exercise
    Print out fib(0) … fib(100), in parallel


        Main loop should spawn children as threads
    

          int tid;
          pid = pthread_create(&tid, function,
           argument);
        Wait for children
    

          pthread_join(tid,&result)




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   17
Exercise




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   18
Communication
    In threads, everything shared except:


    stacks, registers & thread-specific data
        Old way:
    

             pthread_setspecific
         

             pthread_getspecific
         


        New way: __thread
    

             static __thread int x;
         

             Easier in Java…
         




    Updates of shared state must be

    synchronized
        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   19
Bake-off
    Processes or threads?


        Performance
    

        Flexibility / Ease-of-use
    

        Robustness
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   20
Scheduling




  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   21
Context Switch Cost
    Threads – much cheaper


        Stash registers, PC (“IP”), stack pointer
    

    Processes – above plus


        Process context
    

        TLB shootdown
    



Process switches more expensive, or
    require long quanta



        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   22
Flexibility / Ease-of-use
    Processes – more flexible


    + Easy to spawn remotely
         + ssh foo.cs.umass.edu “ls -l”
    + Can communicate via sockets = can be
      distributed across cluster / Internet
    - Requires explicit communication or risky
      hackery

    Threads


      Communicate through memory – must be on
    
      same machine
    - Require thread-safe code
        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   23
Robustness
    Processes – far more robust


        Processes isolated from other processes
    

              Process dies – no effect
          


        Apache 1.x
    



    Threads:


        If one thread crashes (e.g., derefs NULL),
    
        whole process terminates
        Then there’s the stack size problem
    

        Apache 2.x…
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   24
The End




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   25

Contenu connexe

Tendances

OS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switchOS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switchDaniel Ben-Zvi
 
Chapter 14 Computer Architecture
Chapter 14 Computer ArchitectureChapter 14 Computer Architecture
Chapter 14 Computer ArchitectureGiulianoRanauro
 
Process creation and termination In Operating System
Process creation and termination In Operating SystemProcess creation and termination In Operating System
Process creation and termination In Operating SystemFarhan Aslam
 
Operating Systems: Memory Management
Operating Systems: Memory ManagementOperating Systems: Memory Management
Operating Systems: Memory ManagementDamian T. Gordon
 
Kcd226 Sistem Operasi Lecture06
Kcd226 Sistem Operasi Lecture06Kcd226 Sistem Operasi Lecture06
Kcd226 Sistem Operasi Lecture06Cahyo Darujati
 
OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)Muhammad Osama
 
Operating Systems: Processor Management
Operating Systems: Processor ManagementOperating Systems: Processor Management
Operating Systems: Processor ManagementDamian T. Gordon
 
gcma: guaranteed contiguous memory allocator
gcma:  guaranteed contiguous memory allocatorgcma:  guaranteed contiguous memory allocator
gcma: guaranteed contiguous memory allocatorSeongJae Park
 
Basic concept of process
Basic concept of processBasic concept of process
Basic concept of processNabin Dahal
 
Module 3-cpu-scheduling
Module 3-cpu-schedulingModule 3-cpu-scheduling
Module 3-cpu-schedulingHesham Elmasry
 
Operating Systems - Memory Management
Operating Systems - Memory ManagementOperating Systems - Memory Management
Operating Systems - Memory ManagementDamian T. Gordon
 
Linux tuning for PostgreSQL at Secon 2015
Linux tuning for PostgreSQL at Secon 2015Linux tuning for PostgreSQL at Secon 2015
Linux tuning for PostgreSQL at Secon 2015Alexey Lesovsky
 
Troubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationTroubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationAlexey Lesovsky
 
Operating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - ProcessesOperating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - ProcessesPeter Tröger
 

Tendances (17)

process creation OS
process creation OSprocess creation OS
process creation OS
 
Chapter 3 - Processes
Chapter 3 - ProcessesChapter 3 - Processes
Chapter 3 - Processes
 
OS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switchOS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switch
 
Process
ProcessProcess
Process
 
Chapter 14 Computer Architecture
Chapter 14 Computer ArchitectureChapter 14 Computer Architecture
Chapter 14 Computer Architecture
 
Process creation and termination In Operating System
Process creation and termination In Operating SystemProcess creation and termination In Operating System
Process creation and termination In Operating System
 
Operating Systems: Memory Management
Operating Systems: Memory ManagementOperating Systems: Memory Management
Operating Systems: Memory Management
 
Kcd226 Sistem Operasi Lecture06
Kcd226 Sistem Operasi Lecture06Kcd226 Sistem Operasi Lecture06
Kcd226 Sistem Operasi Lecture06
 
OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)
 
Operating Systems: Processor Management
Operating Systems: Processor ManagementOperating Systems: Processor Management
Operating Systems: Processor Management
 
gcma: guaranteed contiguous memory allocator
gcma:  guaranteed contiguous memory allocatorgcma:  guaranteed contiguous memory allocator
gcma: guaranteed contiguous memory allocator
 
Basic concept of process
Basic concept of processBasic concept of process
Basic concept of process
 
Module 3-cpu-scheduling
Module 3-cpu-schedulingModule 3-cpu-scheduling
Module 3-cpu-scheduling
 
Operating Systems - Memory Management
Operating Systems - Memory ManagementOperating Systems - Memory Management
Operating Systems - Memory Management
 
Linux tuning for PostgreSQL at Secon 2015
Linux tuning for PostgreSQL at Secon 2015Linux tuning for PostgreSQL at Secon 2015
Linux tuning for PostgreSQL at Secon 2015
 
Troubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming ReplicationTroubleshooting PostgreSQL Streaming Replication
Troubleshooting PostgreSQL Streaming Replication
 
Operating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - ProcessesOperating Systems 1 (6/12) - Processes
Operating Systems 1 (6/12) - Processes
 

En vedette

Course 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life CycleCourse 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life CycleAhmed El-Arabawy
 
Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals Ahmed El-Arabawy
 
What Is An Antivirus Software?
What Is An Antivirus Software?What Is An Antivirus Software?
What Is An Antivirus Software?culltdueet65
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to DebuggersSaumil Shah
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory ManagementAkmal Cikmat
 
Types of operating system
Types of operating systemTypes of operating system
Types of operating systemMohammad Alam
 
Unix operating system
Unix operating systemUnix operating system
Unix operating systemABhay Panchal
 
Device Drivers
Device DriversDevice Drivers
Device DriversSuhas S R
 
Multimedia authoring tools
Multimedia authoring toolsMultimedia authoring tools
Multimedia authoring toolsOnline
 
OS Process and Thread Concepts
OS Process and Thread ConceptsOS Process and Thread Concepts
OS Process and Thread Conceptssgpraju
 
Operating system overview concepts ppt
Operating system overview concepts pptOperating system overview concepts ppt
Operating system overview concepts pptRajendraPrasad Alladi
 

En vedette (20)

Course 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life CycleCourse 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life Cycle
 
Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals
 
Unix signals
Unix signalsUnix signals
Unix signals
 
Os Concepts
Os ConceptsOs Concepts
Os Concepts
 
What Is An Antivirus Software?
What Is An Antivirus Software?What Is An Antivirus Software?
What Is An Antivirus Software?
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to Debuggers
 
Unix slideshare
Unix slideshareUnix slideshare
Unix slideshare
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Linux v/s Windows
Linux v/s WindowsLinux v/s Windows
Linux v/s Windows
 
Operating System-Memory Management
Operating System-Memory ManagementOperating System-Memory Management
Operating System-Memory Management
 
Types of operating system
Types of operating systemTypes of operating system
Types of operating system
 
File management
File managementFile management
File management
 
File Management
File ManagementFile Management
File Management
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
 
Device Drivers
Device DriversDevice Drivers
Device Drivers
 
Multimedia authoring tools
Multimedia authoring toolsMultimedia authoring tools
Multimedia authoring tools
 
Memory management
Memory managementMemory management
Memory management
 
OS Process and Thread Concepts
OS Process and Thread ConceptsOS Process and Thread Concepts
OS Process and Thread Concepts
 
Presentation on operating system
 Presentation on operating system Presentation on operating system
Presentation on operating system
 
Operating system overview concepts ppt
Operating system overview concepts pptOperating system overview concepts ppt
Operating system overview concepts ppt
 

Similaire à Processes and Threads

Operating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingOperating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingEmery Berger
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationEmery Berger
 
Operating Systems - Introduction
Operating Systems - IntroductionOperating Systems - Introduction
Operating Systems - IntroductionEmery Berger
 
Operating Systems - Concurrency
Operating Systems - ConcurrencyOperating Systems - Concurrency
Operating Systems - ConcurrencyEmery Berger
 
Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++Emery Berger
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with pythonPatrick Vergain
 
Memory Management for High-Performance Applications
Memory Management for High-Performance ApplicationsMemory Management for High-Performance Applications
Memory Management for High-Performance ApplicationsEmery Berger
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded ProgrammingSri Prasanna
 
Intermachine Parallelism
Intermachine ParallelismIntermachine Parallelism
Intermachine ParallelismSri Prasanna
 
6. processes and threads
6. processes and threads6. processes and threads
6. processes and threadsMarian Marinov
 
Operating Systems - Networks
Operating Systems - NetworksOperating Systems - Networks
Operating Systems - NetworksEmery Berger
 
HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...
HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...
HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...AMD Developer Central
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docxwkyra78
 
H2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt DowleH2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt DowleSri Ambati
 
Whatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the processWhatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the processguest3379bd
 
Operating Systems - Queuing Systems
Operating Systems - Queuing SystemsOperating Systems - Queuing Systems
Operating Systems - Queuing SystemsEmery Berger
 
OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionShuya Osaki
 
Tips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software EngineeringTips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software Engineeringjtdudley
 

Similaire à Processes and Threads (20)

Operating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingOperating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel Computing
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced Synchronization
 
Operating Systems - Introduction
Operating Systems - IntroductionOperating Systems - Introduction
Operating Systems - Introduction
 
Operating Systems - Concurrency
Operating Systems - ConcurrencyOperating Systems - Concurrency
Operating Systems - Concurrency
 
Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with python
 
Memory Management for High-Performance Applications
Memory Management for High-Performance ApplicationsMemory Management for High-Performance Applications
Memory Management for High-Performance Applications
 
Os Wilhelm
Os WilhelmOs Wilhelm
Os Wilhelm
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 
Intermachine Parallelism
Intermachine ParallelismIntermachine Parallelism
Intermachine Parallelism
 
6. processes and threads
6. processes and threads6. processes and threads
6. processes and threads
 
Operating Systems - Networks
Operating Systems - NetworksOperating Systems - Networks
Operating Systems - Networks
 
HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...
HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...
HC-4021, Efficient scheduling of OpenMP and OpenCL™ workloads on Accelerated ...
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docx
 
H2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt DowleH2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt Dowle
 
Whatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the processWhatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the process
 
Ndp Slides
Ndp SlidesNdp Slides
Ndp Slides
 
Operating Systems - Queuing Systems
Operating Systems - Queuing SystemsOperating Systems - Queuing Systems
Operating Systems - Queuing Systems
 
OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 Introduction
 
Tips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software EngineeringTips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software Engineering
 

Plus de Emery Berger

Doppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language BarrierDoppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language BarrierEmery Berger
 
Dthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic MultithreadingDthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic MultithreadingEmery Berger
 
Programming with People
Programming with PeopleProgramming with People
Programming with PeopleEmery Berger
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationEmery Berger
 
DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)Emery Berger
 
Operating Systems - Advanced File Systems
Operating Systems - Advanced File SystemsOperating Systems - Advanced File Systems
Operating Systems - Advanced File SystemsEmery Berger
 
Operating Systems - File Systems
Operating Systems - File SystemsOperating Systems - File Systems
Operating Systems - File SystemsEmery Berger
 
Operating Systems - Synchronization
Operating Systems - SynchronizationOperating Systems - Synchronization
Operating Systems - SynchronizationEmery Berger
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and PagingEmery Berger
 
Operating Systems - Virtual Memory
Operating Systems - Virtual MemoryOperating Systems - Virtual Memory
Operating Systems - Virtual MemoryEmery Berger
 
MC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained EnvironmentsMC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained EnvironmentsEmery Berger
 
Vam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory AllocatorVam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory AllocatorEmery Berger
 
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementQuantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementEmery Berger
 
Garbage Collection without Paging
Garbage Collection without PagingGarbage Collection without Paging
Garbage Collection without PagingEmery Berger
 
DieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe LanguagesDieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe LanguagesEmery Berger
 
Exterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High ProbabilityExterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High ProbabilityEmery Berger
 
Operating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory ManagementOperating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory ManagementEmery Berger
 
Operating Systems - Architecture
Operating Systems - ArchitectureOperating Systems - Architecture
Operating Systems - ArchitectureEmery Berger
 
Operating Systems - Garbage Collection
Operating Systems - Garbage CollectionOperating Systems - Garbage Collection
Operating Systems - Garbage CollectionEmery Berger
 
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Emery Berger
 

Plus de Emery Berger (20)

Doppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language BarrierDoppio: Breaking the Browser Language Barrier
Doppio: Breaking the Browser Language Barrier
 
Dthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic MultithreadingDthreads: Efficient Deterministic Multithreading
Dthreads: Efficient Deterministic Multithreading
 
Programming with People
Programming with PeopleProgramming with People
Programming with People
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance Evaluation
 
DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)DieHarder (CCS 2010, WOOT 2011)
DieHarder (CCS 2010, WOOT 2011)
 
Operating Systems - Advanced File Systems
Operating Systems - Advanced File SystemsOperating Systems - Advanced File Systems
Operating Systems - Advanced File Systems
 
Operating Systems - File Systems
Operating Systems - File SystemsOperating Systems - File Systems
Operating Systems - File Systems
 
Operating Systems - Synchronization
Operating Systems - SynchronizationOperating Systems - Synchronization
Operating Systems - Synchronization
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and Paging
 
Operating Systems - Virtual Memory
Operating Systems - Virtual MemoryOperating Systems - Virtual Memory
Operating Systems - Virtual Memory
 
MC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained EnvironmentsMC2: High-Performance Garbage Collection for Memory-Constrained Environments
MC2: High-Performance Garbage Collection for Memory-Constrained Environments
 
Vam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory AllocatorVam: A Locality-Improving Dynamic Memory Allocator
Vam: A Locality-Improving Dynamic Memory Allocator
 
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementQuantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
 
Garbage Collection without Paging
Garbage Collection without PagingGarbage Collection without Paging
Garbage Collection without Paging
 
DieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe LanguagesDieHard: Probabilistic Memory Safety for Unsafe Languages
DieHard: Probabilistic Memory Safety for Unsafe Languages
 
Exterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High ProbabilityExterminator: Automatically Correcting Memory Errors with High Probability
Exterminator: Automatically Correcting Memory Errors with High Probability
 
Operating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory ManagementOperating Systems - Dynamic Memory Management
Operating Systems - Dynamic Memory Management
 
Operating Systems - Architecture
Operating Systems - ArchitectureOperating Systems - Architecture
Operating Systems - Architecture
 
Operating Systems - Garbage Collection
Operating Systems - Garbage CollectionOperating Systems - Garbage Collection
Operating Systems - Garbage Collection
 
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
 

Dernier

Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Dernier (20)

Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Processes and Threads

  • 1. Operating Systems CMPSCI 377 Processes & Threads Emery Berger University of Massachusetts Amherst UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
  • 2. Outline Processes  Threads  Basic synchronization  Bake-off  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 2
  • 3. Processes vs. Threads… Both useful for parallel programming &  concurrency Hide latency  Maximize CPU utilization  Handle multiple, asynchronous events  But: different programming styles,  performance characteristics, and more UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 3
  • 4. Processes Process:  execution context (PC, registers) + address space, files, etc. Basic unit of  execution UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 4
  • 5. Process API UNIX:  fork() – create copy of current process  Different return value  Copy-on-write  exec() – replace process w/ executable  Windows:  CreateProcess (…)  10 arguments  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 5
  • 6. Translation Lookaside Buffer TLB: fast, fully  associative memory Stores page  numbers (key), frame (value) in which they are stored Copy-on-write:  protect pages, copy on first write UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6
  • 7. Processes Example UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 7
  • 8. Exercise Print out fib(0) … fib(100), in parallel  Main loop should fork off children  (returns pid of child) int pid; pid = fork(); if (pid == 0) { // I’m child process } else { // I’m parent process } Wait for children (waitpid(pid,0,0))  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 8
  • 9. Exercise // Print fib(0) ... fib(100) in parallel. int main() { int cids[101]; // Saved child pids. for (int i = 0; i < 101; i++) { // Fork off a bunch of processes to run fib(i). int cid = fork(); if (cid == 0) { // I am the child. cout << quot;Fib(quot; << i << quot;) = quot; << fib(i) << endl; return 0; // Now the child is done, so exit (IMPORTANT!). } else { // I am the parent. Store the child’s pid somewhere. cids[i] = cid; } } // Wait for all the children. for (int i = 0; i < 101; i++) { waitpid (cids[i], 0, 0); // Wait for the ith child. } return 0; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 9
  • 10. Communication Processes:  Input = state before fork()  Output = return value  argument to exit()  But: how can processes communicate  during execution? UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 10
  • 11. IPC signals  Processes can send & receive ints associated  with particular signal numbers Process must set up signal handlers  Best for rare events (SIGSEGV)  Not terribly useful for parallel or concurrent  programming pipes  Communication channels – easy & fast  Just like UNIX command line  ls | wc -l  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 11
  • 12. Pipe example int main() { int pfds[2]; pipe(pfds); if (!fork()) { close(1); /* close normal stdout */ dup(pfds[1]); /* make stdout same as pfds[1] */ close(pfds[0]); /* we don't need this */ execlp(quot;lsquot;, quot;lsquot;, NULL); } else { close(0); /* close normal stdin */ dup(pfds[0]); /* make stdin same as pfds[0] */ close(pfds[1]); /* we don't need this */ execlp(quot;wcquot;, quot;wcquot;, quot;-lquot;, NULL); } } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 12
  • 13. IPC, continued sockets  - Explicit message passing + Can distribute processes anywhere mmap (common and aws0m hack)  All processes map same file into fixed  memory location Objects in region shared across processes  Use process-level synchronization  Much more expensive than threads…  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 13
  • 14. Threads Processes -  everything in distinct address space Threads – same  address space (& files, sockets, etc.) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 14
  • 15. Threads API UNIX (POSIX):  pthread_create() – start separate  thread executing function pthread_join() – wait for thread to  complete Windows:  CreateThread (…)  only 6 arguments!  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 15
  • 16. Threads example #include <pthread.h> void * run (void * d) { int q = ((int) d); int v = 0; for (int i = 0; i < q; i++) { v = v + expensiveComputation(i); } return (void *) v; } main() { pthread_t t1, t2; int r1, r2; pthread_create (&t1, run, 100); pthread_create (&t2, run, 100); pthread_wait (&t1, (void *) &r1); pthread_wait (&t2, (void *) &r2); printf (“r1 = %d, r2 = %dn”, r1, r2); } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 16
  • 17. Exercise Print out fib(0) … fib(100), in parallel  Main loop should spawn children as threads  int tid; pid = pthread_create(&tid, function, argument); Wait for children  pthread_join(tid,&result) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 17
  • 18. Exercise UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 18
  • 19. Communication In threads, everything shared except:  stacks, registers & thread-specific data Old way:  pthread_setspecific  pthread_getspecific  New way: __thread  static __thread int x;  Easier in Java…  Updates of shared state must be  synchronized UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 19
  • 20. Bake-off Processes or threads?  Performance  Flexibility / Ease-of-use  Robustness  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 20
  • 21. Scheduling UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 21
  • 22. Context Switch Cost Threads – much cheaper  Stash registers, PC (“IP”), stack pointer  Processes – above plus  Process context  TLB shootdown  Process switches more expensive, or require long quanta UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 22
  • 23. Flexibility / Ease-of-use Processes – more flexible  + Easy to spawn remotely + ssh foo.cs.umass.edu “ls -l” + Can communicate via sockets = can be distributed across cluster / Internet - Requires explicit communication or risky hackery Threads  Communicate through memory – must be on  same machine - Require thread-safe code UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 23
  • 24. Robustness Processes – far more robust  Processes isolated from other processes  Process dies – no effect  Apache 1.x  Threads:  If one thread crashes (e.g., derefs NULL),  whole process terminates Then there’s the stack size problem  Apache 2.x…  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 24
  • 25. The End UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 25