Synchronous vs Asynchronous Programming
Synchronous: Program executed line by line. Wait until previous line is finished before continuing to the next line.
Asynchronous: Asynchronous programming allows something like diskRead to return immediately and continue execution of later code.
Idea from Unit 2A
– We would like to do other things while waiting for an IO event
– To do so, we need independent streams of execution in a program:
– one requests data, then does something else instead of waiting – while another gets the data ready and stores it in memory
Virtual Process (Thread)
– a single stream of synchronous execution of a program – can be stopped and started
– co-exists with other threads sharing the same CPU
– each thread has its own stack
Idea behind threads:
1) Each thread has its own sequential flow of execution (so to a programmer it is easy to read like synchronous programming)
2) Threads can be stopped and started to get the benefits of asynchronous programming
What do you expect the output of the program shown below? You can describe in words
What do you expect the output of the program shown below?
It will execute the procedures in order
It will print 1000 Is followed by 1000 Os.
IOIOIO…..
What do you expect the output of the program shown below?
Use this program for the following questions
Table
Chair
Blanket IOIOIO……Done
Thread Implementation
Blocking:
– explanation: pauses execution
– what happens to thread: put on blocked queue
– what happens to the physical processor: context switch
Data structures needed:
– thread
– waiter queue: enqueue: add item to back of queue
dequeue: remove the item at the front
Basic operation required:
block, unblock, yield
1. What is the output for this program
IOIOIOIO…..Done
2. Change the order of the calls to create_threads, what is the output
OIOIOIO….Done
Starts with Os why – because pong is first in the ready queue
3. Remove the calls to join
Done
Main thread doesn’t wait for the created threads to run, it terminates after Done and ping and pong are never run.
4. Remove both calls to yield
1000 Is 1000 Os Done
Just like the sequential version, thread running ping finishes before thread running pong begins.
5.
6.
main ping pong
7.
Remove both calls to yield and join terminates after Done and ping and pong are never run. Done
Main thread doesn’t wait for the created threads to run, it
Remove both calls to yield, and one of the joins. Is the result dependent on which join is removed?
1000 Is 1000 Os Done
just like sequential version
once join is called, it lets first thread go and if no yield each thread will take turns finishing…when ping is done next thread to run is pong. Why doesn’t order matter, because of the ready queue, piing or pong has been waiting longer than main.
*R N N
Join on pong ->
block until pong done
*R(Done) done
*R (“III…”)
done
*R(“OO…”) done
Remove pong’s yield and ping’s join
IOOOOOOOOOO…I Done
ping doesn’t finish executing, ping yields to pong, pong does not yield so it finishes next
thread to run is ping who yields (one iteration),
next thread to run is main –it waits for pong (done) it does not wait for ping (not done)
main
ping pong
8.
Main
Ping Pong
9.
*R
N N
Join on pong ->
block until pong done
unblock *R(“I”)
*R(Done) done yield
*R (“I”)
yield
*R(“OO…”)
done
Remove pong’s yield and pong’s join
ping yields to pong, pong does not yield so it finishes,
next thread to run is tping who yields,
main is still blocked, so the only item in the ready queue is tping and so it runs until it returns and then main in unblocked
*R
N N
Join on ping ->
block until ping done
unblock *R(Done) done
*R (“I”)
yield
*R(“III….”) with yields
*R(“OO…”) done
Change the yield in ping to a block
Program stalls, because ping is never unblocked and the main thread is waiting for it (via join) we just wait indefinitely.