SlideShare une entreprise Scribd logo
1  sur  44
Télécharger pour lire hors ligne
Operating Systems
      CMPSCI 377
  Concurrency Patterns
            Emery Berger
University of Massachusetts Amherst




  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Finishing Up From Last Time
    Avoiding deadlock: is this ok?


    lock (a);                             lock (b);
    lock (b);                             lock (a);
    unlock (b);                           unlock (a);
    unlock (a);                           unlock (b);




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   2
Finishing Up From Last Time
    Not ok – may deadlock.


    lock (a);                             lock (b);
    lock (b);                             lock (a);
    unlock (b);                           unlock (a);
    unlock (a);                           unlock (b);


    Solution: impose canonical order (acyclic)


    lock (a);                             lock (a);
    lock (b);                             lock (b);
    unlock (b);                           unlock (b);
    unlock (a);                           unlock (a);




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   3
Motivating Example: Web Server
                                                                        web
                                                                       server
    Client (browser)


        Requests HTML, images
    
                                                                                    not found
    Server


        Caches requests
    
                                              http://server/Easter-bunny/
        Sends to client
                                                   200x100/75.jpg




                                                  client
             UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Possible Implementation
while (true) {
  wait for connection;
  read from socket & parse URL;
  look up URL contents in cache;
  if (!in cache) {
    fetch from disk / execute CGI;
    put in cache;
  }
  send data to client;
}

   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   5
Possible Implementation
while (true) {
  wait for connection; // net
  read from socket & parse URL; // cpu
  look up URL contents in cache; // cpu
  if (!in cache) {
    fetch from disk / execute CGI;//disk
    put in cache; // cpu
  }
  send data to client; // net
}

   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   6
Problem: Concurrency
                                                                      web
    Sequential fine until:
                                                                    server
        More clients
    

        Bigger server
    

             Multicores, multiprocessors
         


    Goals:


        Hide latency of net & disk I/O
    

             Don’t keep clients waiting
         


        Improve throughput
    

             Serve up more pages
         




                                                         clients
               UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Building Concurrent Apps
    Patterns / Architectures


        Thread pools
    

        Producer-consumer
    

        “Bag of tasks”
    

        Worker threads (work stealing)
    

    Goals:


        Minimize latency
    

        Maximize parallelism
    

        Keep progs. simple to program & maintain
    



        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   8
Thread Pools
    Thread invocation & destruction relatively


    expensive
    Instead: use pool of threads


        When new task arrives, get thread from pool
    

        to work on it; block if pool empty
        Faster with many tasks
    

        Limits max threads
    

        ( ThreadPoolExecutor class in Java)
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   9
Producer-Consumer
    Can get pipeline parallelism:


        One thread (producer) does work
    

              E.g., I/O
          


        and hands it off to other thread (consumer)
    




producer                                                           consumer




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   10
Producer-Consumer
    Can get pipeline parallelism:


        One thread (producer) does work
    

              E.g., I/O
          


        and hands it off to other thread (consumer)
    




producer                                                           consumer




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   11
Producer-Consumer
    Can get pipeline parallelism:


        One thread (producer) does work
    

              E.g., I/O
          


        and hands it off to other thread (consumer)
    




producer                                                           consumer




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   12
Producer-Consumer
    Can get pipeline parallelism:


        One thread (producer) does work
    

              E.g., I/O
          


        and hands it off to other thread (consumer)
    




producer                                                           consumer




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   13
Producer-Consumer
    Can get pipeline parallelism:


        One thread (producer) does work
    

              E.g., I/O
          


        and hands it off to other thread (consumer)
    




producer                                                           consumer


               LinkedBlockingQueue
        Blocks on put() if full, poll() if empty


        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   14
Producer-Consumer Web Server
              Use 2 threads: producer & consumer
          

                  queue.put(x) and x = queue.poll();
              

while (true) {                                           while (true) {
  wait for connection;                                     do something…
  read from socket & parse URL;                            queue.put (x);
  look up URL contents in cache;                         }
  if (!in cache) {
    fetch from disk / execute CGI;
    put in cache;
  }
                                                         while (true) {
  send data to client;
                                                           x = queue.poll();
}
                                                           do something…
                                                         }




                  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   15
Producer-Consumer Web Server
          Pair of threads – one reads, one writes
      


while (true) {                                 while (true) {
  wait for connection;                           URL = queue.poll();
  read from socket & parse URL;                  look up URL contents in cache;
  queue.put (URL);                               if (!in cache) {
}                                                  fetch from disk / execute CGI;
                                                   put in cache;
                                                 }
                                                 send data to client;
                                               }




            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science    16
Producer-Consumer Web Server
          More parallelism –
      

          optimizes common case (cache hit)
while (true) {                                 while (true) {
  wait for connection;                           URL = queue1.poll();
  read from socket & parse URL;                  look up URL contents in cache;
  queue1.put (URL);                              if (!in cache) {
                                          1
}                                                  queue2.put (URL); return;
                                                 }
                                                 send data to client;
                                               }

                                                                           2
                                               while (true) {
                                                 URL = queue2.poll();
                                                 fetch from disk / execute CGI;
                                                 put in cache;
                                                 send data to client;
                                               }
            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   17
When to Use Producer-Consumer
    Works well for pairs of threads


        Best if producer & consumer are symmetric
    

              Proceed roughly at same rate
          


        Order of operations matters
    

    Not as good for


        Many threads
    

        Order doesn’t matter
    

        Different rates of progress
    




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   18
Producer-Consumer Web Server
          Have to be careful to balance load across
      

          threads
while (true) {                                 while (true) {
  wait for connection;                           URL = queue1.poll();
  read from socket & parse URL;                  look up URL contents in cache;
  queue1.put (URL);                              if (!in cache) {
                                          1
}                                                  queue2.put (URL);
                                                 }
                                                 send data to client;
                                               }

                                                                           2
                                               while (true) {
                                                 URL = queue2.poll();
                                                 fetch from disk / execute CGI;
                                                 put in cache;
                                                 send data to client;
                                               }
            UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   19
Bag of Tasks
    Collection of mostly independent tasks





    worker               worker                   worker                  worker




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        20
Bag of Tasks
    Collection of mostly independent tasks





    worker               worker                   worker                  worker




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        21
Bag of Tasks
    Collection of mostly independent tasks





    worker               worker                   worker                  worker




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        22
Bag of Tasks
    Collection of mostly independent tasks





    worker               worker                   worker                  worker




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        23
Bag of Tasks
    Collection of mostly independent tasks





    worker               worker                   worker                  worker




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        24
Bag of Tasks
    Collection of mostly independent tasks





    worker               worker                   worker                  worker




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        25
Bag of Tasks
        Collection of mostly independent tasks
    




addWork




        worker               worker                   worker                  worker



        Bag could also be LinkedBlockingQueue
    

        (put, poll)
           UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        26
Bag of Tasks Web Server
    Re-structure this into bag of tasks:


        addWork & worker threads
    

        t = bag.poll() or bag.put(t)
    



          while (true) {
            wait for connection;
            read from socket & parse URL;
            look up URL contents in cache;
            if (!in cache) {
              fetch from disk / execute CGI;
              put in cache;
            }
            send data to client;
          }


        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   27
Bag of Tasks Web Server
          Re-structure this into bag of tasks:
      

               addWork & worker
           

               t = bag.poll() or bag.put(t)
           


addWork:                        Worker:
while (true) {                  while (true) {
  wait for connection;            URL = bag.poll();
  bag.put (URL);                  look up URL contents in cache;
}                                 if (!in cache) {
                                    fetch from disk / execute CGI;
                                    put in cache;
                                  }
                                  send data to client;
                                }



               UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   28
Bag of Tasks Web Server
             Re-structure this into bag of tasks:
         

                  t = bag.poll() or bag.put(t)
              

addWork: while (true){
  wait for connection;
  bag.put (URL);
}                                                                                        worker
                                                          worker: while (true) {
    addWork                                                 URL = bag.poll();
                                                            look up URL contents in cache;
                                                            if (!in cache) {
                                                              fetch from disk / execute CGI;
                                                              put in cache;
             worker                 worker                  }
                                                            send data to client;
                                                          }


                  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science            29
Bag of Tasks vs. Prod/Consumer
    Exploits more parallelism


    Even with coarse-grained threads


        Don’t have to break up tasks too finely
    

    Easy to change or add new functionality




    But: one major performance problem…





        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   30
What’s the Problem?



addWork




      worker                worker                   worker                  worker




          UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        31
What’s the Problem?
        Contention – single lock on structure
    

              Bottleneck to scalability
          




addWork




        worker                  worker                   worker                  worker




              UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science        32
Work Queues
    Each thread has own work queue (deque)


        No single point of contention
    




executor               executor                executor               executor


    Threads now generic “executors”


        Tasks (balls): blue = parse, yellow = connect…
    

        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science     33
Work Queues
    Each thread has own work queue (deque)


        No single point of contention
    




executor               executor                executor               executor




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science     34
Work Queues
    Each thread has own work queue (deque)


        No single point of contention
    




executor               executor                executor               executor




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science     35
Work Queues
    Each thread has own work queue (deque)


        No single point of contention
    




executor               executor                executor               executor




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science     36
Work Queues
    Each thread has own work queue


        No single point of contention
    




executor               executor                executor               executor


    Now what?




        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science     37
Work Stealing
    When thread runs out of work,


    steal work from random other thread




    worker              worker                 worker                  worker




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science     38
Work Stealing
    When thread runs out of work,


    steal work from top of random deque




    worker              worker                 worker                  worker

    Optimal load balancing algorithm




       UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science     39
Work Stealing Web Server
    Re-structure:


    readURL, lookUp, addToCache, output
        myQueue.put(new readURL (url))
    


          while (true) {
            wait for connection;
            read from socket & parse URL;
            look up URL contents in cache;
            if (!in cache) {
              fetch from disk / execute CGI;
              put in cache;
            }
            send data to client;
          }



        UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   40
Work Stealing Web Server
           Re-structure:
       

           readURL, lookUp, addToCache, output
                myQueue.put(new readURL (url))
            


readURL(url) {
  wait for connection;
  read from socket & parse URL;
  myQueue.put (new lookUp (URL));
}




                UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   41
Work Stealing Web Server
           Re-structure:
       

           readURL, lookUp, addToCache, output
                myQueue.put(new readURL (url))
            


readURL(url) {                                    lookUp(url) {
  wait for connection;                             look up URL contents in cache;
  read from socket & parse URL;                    if (!in cache) {
  myQueue.put (new lookUp (URL));                     myQueue.put (new addToCache (URL));
}                                                   } else {
                                                      myQueue.put (new output(contents));
                                                    }
   addToCache(URL) {
                                                  }
     fetch from disk / execute CGI;
     put in cache;
     myQueue.put (new
      output(contents));
   }
                UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science    42
Work Stealing
    Works great for heterogeneous tasks


        Convert addWork and worker into units of
    

        work (different colors)
    Flexible: can easily re-define tasks


        Coarse, fine-grained, anything in-between
    

    Automatic load balancing


    Separates thread logic from functionality




    Popular model for structuring servers



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




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   44

Contenu connexe

Tendances

Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9myrajendra
 
Multiprocessor Systems
Multiprocessor SystemsMultiprocessor Systems
Multiprocessor Systemsvampugani
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Mukesh Chinta
 
Operating System - Monitors (Presentation)
Operating System - Monitors (Presentation)Operating System - Monitors (Presentation)
Operating System - Monitors (Presentation)Experts Desk
 
Process in operating system
Process in operating systemProcess in operating system
Process in operating systemChetan Mahawar
 
6 multiprogramming & time sharing
6 multiprogramming & time sharing6 multiprogramming & time sharing
6 multiprogramming & time sharingmyrajendra
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.pptLuis Goldster
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating SystemJanki Shah
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structuresMukesh Chinta
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Distributed operating system
Distributed operating systemDistributed operating system
Distributed operating systemPrankit Mishra
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionBipul Chandra Kar
 
Communication in client server system.pptx
Communication in client server system.pptxCommunication in client server system.pptx
Communication in client server system.pptxMSivani
 
System call (Fork +Exec)
System call (Fork +Exec)System call (Fork +Exec)
System call (Fork +Exec)Amit Ghosh
 

Tendances (20)

Os Question Bank
Os Question BankOs Question Bank
Os Question Bank
 
Multiprocessing operating systems
Multiprocessing operating systemsMultiprocessing operating systems
Multiprocessing operating systems
 
Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9
 
Multiprocessor Systems
Multiprocessor SystemsMultiprocessor Systems
Multiprocessor Systems
 
Message passing in Distributed Computing Systems
Message passing in Distributed Computing SystemsMessage passing in Distributed Computing Systems
Message passing in Distributed Computing Systems
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
 
Distributed and clustered systems
Distributed and clustered systemsDistributed and clustered systems
Distributed and clustered systems
 
Operating System - Monitors (Presentation)
Operating System - Monitors (Presentation)Operating System - Monitors (Presentation)
Operating System - Monitors (Presentation)
 
Process in operating system
Process in operating systemProcess in operating system
Process in operating system
 
6 multiprogramming & time sharing
6 multiprogramming & time sharing6 multiprogramming & time sharing
6 multiprogramming & time sharing
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.ppt
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating System
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Distributed operating system
Distributed operating systemDistributed operating system
Distributed operating system
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Communication in client server system.pptx
Communication in client server system.pptxCommunication in client server system.pptx
Communication in client server system.pptx
 
System call (Fork +Exec)
System call (Fork +Exec)System call (Fork +Exec)
System call (Fork +Exec)
 

Similaire à Operating Systems - Concurrency

Operating Systems - Queuing Systems
Operating Systems - Queuing SystemsOperating Systems - Queuing Systems
Operating Systems - Queuing SystemsEmery Berger
 
Processes and Threads
Processes and ThreadsProcesses and Threads
Processes and ThreadsEmery Berger
 
Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++Emery Berger
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationEmery Berger
 
Operating Systems - Networks
Operating Systems - NetworksOperating Systems - Networks
Operating Systems - NetworksEmery Berger
 
Operating Systems - Introduction
Operating Systems - IntroductionOperating Systems - Introduction
Operating Systems - IntroductionEmery Berger
 
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
 
Lightweight Grids With Terracotta
Lightweight Grids With TerracottaLightweight Grids With Terracotta
Lightweight Grids With TerracottaPT.JUG
 
MM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey Pavlenko
MM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey PavlenkoMM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey Pavlenko
MM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey PavlenkoAMD Developer Central
 
Ten practical ways to improve front-end performance
Ten practical ways to improve front-end performanceTen practical ways to improve front-end performance
Ten practical ways to improve front-end performanceAndrew Rota
 
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016Zabbix
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the wayOleg Podsechin
 
Running your Java EE 6 Applications in the Cloud
Running your Java EE 6 Applications in the CloudRunning your Java EE 6 Applications in the Cloud
Running your Java EE 6 Applications in the CloudArun Gupta
 
Running your Java EE 6 applications in the Cloud (FISL 12)
Running your Java EE 6 applications in the Cloud (FISL 12)Running your Java EE 6 applications in the Cloud (FISL 12)
Running your Java EE 6 applications in the Cloud (FISL 12)Arun Gupta
 
JFokus 2011 - Running your Java EE 6 apps in the Cloud
JFokus 2011 - Running your Java EE 6 apps in the CloudJFokus 2011 - Running your Java EE 6 apps in the Cloud
JFokus 2011 - Running your Java EE 6 apps in the CloudArun Gupta
 
Running your Java EE 6 applications in the Cloud
Running your Java EE 6 applications in the CloudRunning your Java EE 6 applications in the Cloud
Running your Java EE 6 applications in the CloudArun Gupta
 

Similaire à Operating Systems - Concurrency (20)

Operating Systems - Queuing Systems
Operating Systems - Queuing SystemsOperating Systems - Queuing Systems
Operating Systems - Queuing Systems
 
Processes and Threads
Processes and ThreadsProcesses and Threads
Processes and Threads
 
Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced Synchronization
 
Operating Systems - Networks
Operating Systems - NetworksOperating Systems - Networks
Operating Systems - Networks
 
Operating Systems - Introduction
Operating Systems - IntroductionOperating Systems - Introduction
Operating Systems - Introduction
 
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
 
Lightweight Grids With Terracotta
Lightweight Grids With TerracottaLightweight Grids With Terracotta
Lightweight Grids With Terracotta
 
Time for Comet?
Time for Comet?Time for Comet?
Time for Comet?
 
Conflict Resolution In Kai
Conflict Resolution In KaiConflict Resolution In Kai
Conflict Resolution In Kai
 
GlassFish v3 : En Route Java EE 6
GlassFish v3 : En Route Java EE 6GlassFish v3 : En Route Java EE 6
GlassFish v3 : En Route Java EE 6
 
MM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey Pavlenko
MM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey PavlenkoMM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey Pavlenko
MM-4097, OpenCV-CL, by Harris Gasparakis, Vadim Pisarevsky and Andrey Pavlenko
 
Ten practical ways to improve front-end performance
Ten practical ways to improve front-end performanceTen practical ways to improve front-end performance
Ten practical ways to improve front-end performance
 
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
 
MySQL Proxy tutorial
MySQL Proxy tutorialMySQL Proxy tutorial
MySQL Proxy tutorial
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Running your Java EE 6 Applications in the Cloud
Running your Java EE 6 Applications in the CloudRunning your Java EE 6 Applications in the Cloud
Running your Java EE 6 Applications in the Cloud
 
Running your Java EE 6 applications in the Cloud (FISL 12)
Running your Java EE 6 applications in the Cloud (FISL 12)Running your Java EE 6 applications in the Cloud (FISL 12)
Running your Java EE 6 applications in the Cloud (FISL 12)
 
JFokus 2011 - Running your Java EE 6 apps in the Cloud
JFokus 2011 - Running your Java EE 6 apps in the CloudJFokus 2011 - Running your Java EE 6 apps in the Cloud
JFokus 2011 - Running your Java EE 6 apps in the Cloud
 
Running your Java EE 6 applications in the Cloud
Running your Java EE 6 applications in the CloudRunning your Java EE 6 applications in the Cloud
Running your Java EE 6 applications in the Cloud
 

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 - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingOperating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingEmery 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
 

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 - Distributed Parallel Computing
Operating Systems - Distributed Parallel ComputingOperating Systems - Distributed Parallel Computing
Operating Systems - Distributed Parallel Computing
 
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
 

Dernier

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
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
 
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
 
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
 
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
 

Dernier (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
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...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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...
 
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
 
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
 
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
 

Operating Systems - Concurrency

  • 1. Operating Systems CMPSCI 377 Concurrency Patterns Emery Berger University of Massachusetts Amherst UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
  • 2. Finishing Up From Last Time Avoiding deadlock: is this ok?  lock (a); lock (b); lock (b); lock (a); unlock (b); unlock (a); unlock (a); unlock (b); UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 2
  • 3. Finishing Up From Last Time Not ok – may deadlock.  lock (a); lock (b); lock (b); lock (a); unlock (b); unlock (a); unlock (a); unlock (b); Solution: impose canonical order (acyclic)  lock (a); lock (a); lock (b); lock (b); unlock (b); unlock (b); unlock (a); unlock (a); UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 3
  • 4. Motivating Example: Web Server web server Client (browser)  Requests HTML, images  not found Server  Caches requests  http://server/Easter-bunny/ Sends to client  200x100/75.jpg client UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
  • 5. Possible Implementation while (true) { wait for connection; read from socket & parse URL; look up URL contents in cache; if (!in cache) { fetch from disk / execute CGI; put in cache; } send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 5
  • 6. Possible Implementation while (true) { wait for connection; // net read from socket & parse URL; // cpu look up URL contents in cache; // cpu if (!in cache) { fetch from disk / execute CGI;//disk put in cache; // cpu } send data to client; // net } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6
  • 7. Problem: Concurrency web Sequential fine until:  server More clients  Bigger server  Multicores, multiprocessors  Goals:  Hide latency of net & disk I/O  Don’t keep clients waiting  Improve throughput  Serve up more pages  clients UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
  • 8. Building Concurrent Apps Patterns / Architectures  Thread pools  Producer-consumer  “Bag of tasks”  Worker threads (work stealing)  Goals:  Minimize latency  Maximize parallelism  Keep progs. simple to program & maintain  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 8
  • 9. Thread Pools Thread invocation & destruction relatively  expensive Instead: use pool of threads  When new task arrives, get thread from pool  to work on it; block if pool empty Faster with many tasks  Limits max threads  ( ThreadPoolExecutor class in Java)  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 9
  • 10. Producer-Consumer Can get pipeline parallelism:  One thread (producer) does work  E.g., I/O  and hands it off to other thread (consumer)  producer consumer UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 10
  • 11. Producer-Consumer Can get pipeline parallelism:  One thread (producer) does work  E.g., I/O  and hands it off to other thread (consumer)  producer consumer UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 11
  • 12. Producer-Consumer Can get pipeline parallelism:  One thread (producer) does work  E.g., I/O  and hands it off to other thread (consumer)  producer consumer UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 12
  • 13. Producer-Consumer Can get pipeline parallelism:  One thread (producer) does work  E.g., I/O  and hands it off to other thread (consumer)  producer consumer UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 13
  • 14. Producer-Consumer Can get pipeline parallelism:  One thread (producer) does work  E.g., I/O  and hands it off to other thread (consumer)  producer consumer LinkedBlockingQueue Blocks on put() if full, poll() if empty UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 14
  • 15. Producer-Consumer Web Server Use 2 threads: producer & consumer  queue.put(x) and x = queue.poll();  while (true) { while (true) { wait for connection; do something… read from socket & parse URL; queue.put (x); look up URL contents in cache; } if (!in cache) { fetch from disk / execute CGI; put in cache; } while (true) { send data to client; x = queue.poll(); } do something… } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 15
  • 16. Producer-Consumer Web Server Pair of threads – one reads, one writes  while (true) { while (true) { wait for connection; URL = queue.poll(); read from socket & parse URL; look up URL contents in cache; queue.put (URL); if (!in cache) { } fetch from disk / execute CGI; put in cache; } send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 16
  • 17. Producer-Consumer Web Server More parallelism –  optimizes common case (cache hit) while (true) { while (true) { wait for connection; URL = queue1.poll(); read from socket & parse URL; look up URL contents in cache; queue1.put (URL); if (!in cache) { 1 } queue2.put (URL); return; } send data to client; } 2 while (true) { URL = queue2.poll(); fetch from disk / execute CGI; put in cache; send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 17
  • 18. When to Use Producer-Consumer Works well for pairs of threads  Best if producer & consumer are symmetric  Proceed roughly at same rate  Order of operations matters  Not as good for  Many threads  Order doesn’t matter  Different rates of progress  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 18
  • 19. Producer-Consumer Web Server Have to be careful to balance load across  threads while (true) { while (true) { wait for connection; URL = queue1.poll(); read from socket & parse URL; look up URL contents in cache; queue1.put (URL); if (!in cache) { 1 } queue2.put (URL); } send data to client; } 2 while (true) { URL = queue2.poll(); fetch from disk / execute CGI; put in cache; send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 19
  • 20. Bag of Tasks Collection of mostly independent tasks  worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 20
  • 21. Bag of Tasks Collection of mostly independent tasks  worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 21
  • 22. Bag of Tasks Collection of mostly independent tasks  worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 22
  • 23. Bag of Tasks Collection of mostly independent tasks  worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 23
  • 24. Bag of Tasks Collection of mostly independent tasks  worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 24
  • 25. Bag of Tasks Collection of mostly independent tasks  worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 25
  • 26. Bag of Tasks Collection of mostly independent tasks  addWork worker worker worker worker Bag could also be LinkedBlockingQueue  (put, poll) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 26
  • 27. Bag of Tasks Web Server Re-structure this into bag of tasks:  addWork & worker threads  t = bag.poll() or bag.put(t)  while (true) { wait for connection; read from socket & parse URL; look up URL contents in cache; if (!in cache) { fetch from disk / execute CGI; put in cache; } send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 27
  • 28. Bag of Tasks Web Server Re-structure this into bag of tasks:  addWork & worker  t = bag.poll() or bag.put(t)  addWork: Worker: while (true) { while (true) { wait for connection; URL = bag.poll(); bag.put (URL); look up URL contents in cache; } if (!in cache) { fetch from disk / execute CGI; put in cache; } send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 28
  • 29. Bag of Tasks Web Server Re-structure this into bag of tasks:  t = bag.poll() or bag.put(t)  addWork: while (true){ wait for connection; bag.put (URL); } worker worker: while (true) { addWork URL = bag.poll(); look up URL contents in cache; if (!in cache) { fetch from disk / execute CGI; put in cache; worker worker } send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 29
  • 30. Bag of Tasks vs. Prod/Consumer Exploits more parallelism  Even with coarse-grained threads  Don’t have to break up tasks too finely  Easy to change or add new functionality  But: one major performance problem…  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 30
  • 31. What’s the Problem? addWork worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 31
  • 32. What’s the Problem? Contention – single lock on structure  Bottleneck to scalability  addWork worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 32
  • 33. Work Queues Each thread has own work queue (deque)  No single point of contention  executor executor executor executor Threads now generic “executors”  Tasks (balls): blue = parse, yellow = connect…  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 33
  • 34. Work Queues Each thread has own work queue (deque)  No single point of contention  executor executor executor executor UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 34
  • 35. Work Queues Each thread has own work queue (deque)  No single point of contention  executor executor executor executor UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 35
  • 36. Work Queues Each thread has own work queue (deque)  No single point of contention  executor executor executor executor UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 36
  • 37. Work Queues Each thread has own work queue  No single point of contention  executor executor executor executor Now what?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 37
  • 38. Work Stealing When thread runs out of work,  steal work from random other thread worker worker worker worker UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 38
  • 39. Work Stealing When thread runs out of work,  steal work from top of random deque worker worker worker worker Optimal load balancing algorithm  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 39
  • 40. Work Stealing Web Server Re-structure:  readURL, lookUp, addToCache, output myQueue.put(new readURL (url))  while (true) { wait for connection; read from socket & parse URL; look up URL contents in cache; if (!in cache) { fetch from disk / execute CGI; put in cache; } send data to client; } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 40
  • 41. Work Stealing Web Server Re-structure:  readURL, lookUp, addToCache, output myQueue.put(new readURL (url))  readURL(url) { wait for connection; read from socket & parse URL; myQueue.put (new lookUp (URL)); } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 41
  • 42. Work Stealing Web Server Re-structure:  readURL, lookUp, addToCache, output myQueue.put(new readURL (url))  readURL(url) { lookUp(url) { wait for connection; look up URL contents in cache; read from socket & parse URL; if (!in cache) { myQueue.put (new lookUp (URL)); myQueue.put (new addToCache (URL)); } } else { myQueue.put (new output(contents)); } addToCache(URL) { } fetch from disk / execute CGI; put in cache; myQueue.put (new output(contents)); } UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 42
  • 43. Work Stealing Works great for heterogeneous tasks  Convert addWork and worker into units of  work (different colors) Flexible: can easily re-define tasks  Coarse, fine-grained, anything in-between  Automatic load balancing  Separates thread logic from functionality  Popular model for structuring servers  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 43
  • 44. The End UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 44