1 / 13

Cooperating Threads

Learn about different thread types, atomic operations, possible execution orders, and the impact of cooperating threads on deterministic computation in operating systems. Explore shared resources, speedup benefits, and reasons for allowing cooperating threads.

sotelo
Download Presentation

Cooperating Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cooperating Threads Andy Wang Operating Systems COP 4610 / CGF 5765

  2. Independent Threads • No states shared with other threads • Deterministic computation • Output depends on input • Reproducible • Output does not depend on the order and timing of other threads • Scheduling order does not matter • e.g., gif2jpeg

  3. Cooperating Threads • Shared states • Nondeterministic • Nonreproducible • e.g., elevator controllers • Example: 2 threads sharing the same display Thread A Thread B cout << “A”; cout << “1”; cout << “B”; cout << “2”; cout << “C”; cout << “3”; • You may get “A12BC3”

  4. So, Why Allow Cooperating Threads?

  5. So, Why Allow Cooperating Threads? • Shared resources • e.g., a single processor • Speedup • Occurs when threads use different resources at different times • Modularity • An application can be decomposed into threads

  6. Some Concurrent Programs • If threads work on separate data, scheduling does not matter Thread A Thread B x = 1; y = 2;

  7. Some Concurrent Programs • If threads share data, the final values are not as obvious Thread A Thread B x = 1; y = 2; x = y + 1; y = y * 2; • What are the indivisible operations?

  8. Atomic Operations • An atomic operation always runs to completion; it’s all or nothing • e.g., memory loads and stores on most machines • Many operations are not atomic • Double precision floating point store

  9. Suppose… • Each C statement is atomic • Let’s revisit the example…

  10. x = 1 y = 2 x = y + 1 y = 2 x = 1 y = y * 2 x = 1 y = 2 x = y + 1 y = y * 2 x = y + 1 y = y * 2 y = y * 2 x = y + 1 All Possible Execution Orders Thread A Thread B x = 1; y = 2; x = y + 1; y = y * 2; A decision tree

  11. All Possible Execution Orders Thread A Thread B x = 1; y = 2; x = y + 1; y = y * 2; (x = ?, y = ?) (x = ?, y = 2) (x = 1, y = ?) x = 1 y = 2 (x = 1, y = 2) (x = ?, y = ?) (x = ?, y = 4) x = y + 1 y = 2 x = 1 y = y * 2 (x = ?, y = 2) (x = 3, y = 2) (x = 1, y = 4) (x = 1, y = 4) x = 1 y = 2 x = y + 1 y = y * 2 x = y + 1 y = y * 2 y = y * 2 x = y + 1 (x = ?, y = 4) (x = 3, y = 4) (x = 5, y = 4) (x = 5, y = 4)

  12. Another Example • Assume each C statement is atomic • Both threads are in the same address space Thread A Thread B j = 0; j = 0; while (j < 10) { while (j > -10) { ++j; --j; } } cout << “A wins”; cout << “B wins”;

  13. So… • Who wins? • Can the computation go on forever? • Race conditions occur when threads share data, and their results depend on the timing of their executions…

More Related