1 of 20

ECS 189e

Sam King

Processes, threads, and event-driven programming: general concepts

2 of 20

Administrative

  • HW4 due on Saturday
  • Quiz 3 is out
  • Sprint planning due today — please pay close attention to what we’re asking for and see the course website for details
  • Project proposals due today
  • Please double check your groups in canvas and let me know if there are any issues

We created your groups so all project assignments are submitted as a group

3 of 20

Administrative

Last time: API calls

This time: The history of threads and event driven programming

Next time: Concurrency in iOS

Next next time: Synchronization in iOS

4 of 20

Threads and concurrency

  • Motivation
    • OSes getting complex
    • Multiple users, programs, I/O devices, etc.
    • How to manage this complexity?

  • Decompose or separate hard problems into simpler ones

5 of 20

  • Processes decompose mix of activities running on a processor into several parallel tasks (columns)

  • Each job can work independently of the others
  • For any area of OS, ask:
    • What interface does the hardware provide?
    • What interface does the OS provide?

Job 1

Job 2

Job 3

6 of 20

What’s in a process?

  • Definition of a process
    • (informal) a program in execution. A running piece of code along with all the things the program can read/write
      • Note: process != program
    • (formal) one of more threads in their own address space
  • Play analogy

7 of 20

  • Thread
    • Sequence of executing instructions from a program (i.e., the running computation)
    • Active
    • Play analogy: Actors (threads) reading from a script (the program)

  • Address space
    • All the data in the process uses as it runs
    • Passive (acted upon by the thread)
    • Play analogy: all the object on the stage in a play

8 of 20

Multiple threads

  • Can have several threads in a single address space
    • Play analogy: several actors on a single set. Sometimes interact (e.g., dance together), sometimes do independent tasks

  • Private state for a thread vs. global state shared between threads

9 of 20

A little bit of history

  • Computer systems circa 2000 were uniprocessor, I/O bound
  • Web servers were the research problem of the day

10 of 20

General flow for handling a web server request

handleWebRequest() {

socket = newClientConnection(serverSock)

request = readHTTPRequest(socket)

object = accessDatabase(request)

response = accessFilesystem(request)

sendResponse(socket, response)

}

11 of 20

  • Handle one request at a time
    • Easy to program, slow
      • No overlapping disk requests with computation or with network receive

Request 1

Start disk I/O req 1

Disk I/O ret req 1

Reply to req 1

Request 2

Process req 1

Process req 2

12 of 20

  • Event-driven with async I/O
    • Need to keep track of outstanding requests

Request 1

Proc req 1

Start I/O 1a

Request 2

Proc req 2

Start I/O 2a

Request 3

Proc req 3

Start I/O 3a

Disk I/O 1a ret

Reply req 1

13 of 20

activeFds.add(serverSock)

handleWebRequest() {

activeFd = select(activeFds)

// giant state machine to track pending

// requests (yuck)

switch (activeFd) {

case serverSock:

// new request

case isHttpRequest(activeFd):

// read request, store state when done

case isDatabaseFd(activeFd):

// handle response from database

}

14 of 20

Web server using threads

  • Each thread handles one request

Request 1

Proc req 1

Start I/O 1a

Request 2

Proc req 2

Start I/O 2a

Request 3

Proc req 3

Start I/O 3a

Reply req 1

I/O 1a ret

15 of 20

handleWebRequest() {

while (true) {

socket = newClientConnection(serverSock)

createNewThread(webRequestThread, socket)

}

}

webRequestThread(socket) {

request = readHTTPRequest(socket)

object = accessDatabase(request)

response = accessFilesystem(request)

sendResponse(socket, response)

}

16 of 20

Is Swift on iOS multi-threaded or event driven?

17 of 20

Sketch out from a high level what your iOS Swift-based web server would look like

18 of 20

A Swift based server

Socket.acceptRequest() { request in

Request.lookupData(request) { data in

Request.fetchTemplate(data) { template in

request.respond(data, template)

}

}

}

19 of 20

Web server programming model advantages and disadvantages

  • Advantages of thread example?

  • Advantages of event-driven example?

  • Advantages of closure-based example?

20 of 20

Benefits and uses of threads

  • Thread system in operating system manages the sharing of the single CPU among several threads
    • Applications get a simpler programming interface

  • Typical domains that use multiple threads
    • Physical control
      • Slow component?
    • Window system (1 thread per window)

    • Network server

    • Parallel programming (for using multiple CPUs)