Review Running a program requires the code & stack segments in - - PowerPoint PPT Presentation

review
SMART_READER_LITE
LIVE PREVIEW

Review Running a program requires the code & stack segments in - - PowerPoint PPT Presentation

Review Running a program requires the code & stack segments in memory. Context = memory address space + stack pointer + instruction pointer A CPU is in the context of a program if its instruction pointer and stack pointer registers


slide-1
SLIDE 1

Review

  • Running a program requires the code & stack segments in memory.
  • Context = memory address space + stack pointer + instruction pointer
  • A CPU is in the context of a program if its instruction pointer and stack

pointer registers point to the code & stack segments of the program.

  • Context-switch means switching the context of a CPU to different

programs by modifying its stack pointer and instruction pointer.

slide-2
SLIDE 2

Big picture of context-switch

  • The initial goal of operating systems is multi-tasking.
  • A naive way of multi-tasking is batch processing.
  • The concept of context-switch enables time-sharing multi-tasking.
  • There are different implementations of context-switch: user-level

threads, kernel-level threads, processes

* Images from https://about.sourcegraph.com/blog/the-ibm-system-360-the-first-modular-general-purpose-computer/

slide-3
SLIDE 3

Implementation comparison

Switching stack pointer? Switching instruction pointer? Switching memory address space? Switching kernel/user mode?

4411 P1

User-level Threads

Yes No No No

Beyond 4411 P1

Kernel-level Threads

Yes Yes No Yes

Processes

Yes Yes Yes Yes

slide-4
SLIDE 4

Switching stack pointer? Switching instruction pointer? Switching memory address space? Switching kernel/user mode?

4411 P1

User-level Threads

Yes No No No

Beyond 4411 P1

Kernel-level Threads

Yes Yes No Yes

Processes

Yes Yes Yes Yes

Implementation comparison

slide-5
SLIDE 5

Switching stack pointer? Switching instruction pointer? Switching memory address space? Switching kernel/user mode?

4411 P1

User-level Threads

Yes No No No

Beyond 4411 P1

Kernel-level Threads

Yes Yes No Yes

Processes

Yes Yes Yes Yes

Implementation comparison

slide-6
SLIDE 6

Switching stack pointer? Switching instruction pointer? Switching memory address space? Switching kernel/user mode?

4411 P1

User-level Threads

Yes No No No

Beyond 4411 P1

Kernel-level Threads

Yes Yes No Yes

Processes

Yes Yes Yes Yes

Implementation comparison

slide-7
SLIDE 7

Context-switch solves the problem

  • f time-sharing multi-tasking.

Processes and threads implement the concept of context-switch.

slide-8
SLIDE 8

Context-switch solves the problem

  • f time-sharing multi-tasking.

Next problem: how do different processes/threads communicate?

Processes and threads implement the concept of context-switch.

slide-9
SLIDE 9

Next problem: how do different processes/threads communicate?

  • For example, say there are 3 threads running my zoom together: one for

user interface, one for microphone and one for camera.

  • When I click the “mute” button, the user interface thread should tell the

microphone thread to stop recording.

  • The camera thread should continuously transfer video data to the user

interface thread.

slide-10
SLIDE 10

Interprocess communication (IPC)

  • The terminology of this problem is IPC which is extensively studied in the
  • perating systems literature. Performance is the key!
  • If IPC has poor performance, the camera thread cannot transfer video

data to the user interface thread in time, leading to poor experience.

  • Note: we will use the general term IPC to represent both communications

among processes and communications among threads.

slide-11
SLIDE 11

Historical representatives

  • IBM 360 is a representative of time-sharing

multi-tasking with context-switch.

  • 1960s
  • AT&T UNIX System V is a representative of

interprocess communication (IPC).

  • 1980s
slide-12
SLIDE 12

UNIX System V IPC

System V IPC is the name given to three interprocess communication mechanisms that are widely available on UNIX systems: message queues, semaphore, and shared memory.

https://man7.org/linux/man-pages/man7/svipc.7.html

what you need to implement in 4411 P1

slide-13
SLIDE 13

Message queue example

User interface thread in zoom Microphone thread in zoom

User-level Kernel-level

Operating Systems Kernel

slide-14
SLIDE 14

User interface thread in zoom Microphone thread in zoom

User-level Kernel-level

System call: I want a message queue!

Message queue example

Operating Systems Kernel

slide-15
SLIDE 15

User interface thread in zoom Microphone thread in zoom

User-level Kernel-level

System call: I want a message queue! Alright! message queue

Message queue example

Operating Systems Kernel

slide-16
SLIDE 16

User interface thread in zoom Microphone thread in zoom

User-level Kernel-level

System call: Please send a “mute” message to the Microphone thread message queue

Message queue example

Operating Systems Kernel

slide-17
SLIDE 17

User interface thread in zoom Microphone thread in zoom

User-level Kernel-level

System call: Please send a “mute” message to the Microphone thread message queue

Message queue example

Alright!

Operating Systems Kernel

slide-18
SLIDE 18

User interface thread in zoom Microphone thread in zoom

User-level Kernel-level

System call: Please send a “mute” message to the Microphone thread message queue

Message queue example

Alright! OK, I’ll stop recording

Operating Systems Kernel

slide-19
SLIDE 19

User interface thread in zoom Camera thread in zoom

User-level Kernel-level

Shared memory example

System call: I want to share a piece of memory with the camera thread!

Operating Systems Kernel

slide-20
SLIDE 20

User interface thread in zoom Camera thread in zoom

User-level Kernel-level

Shared memory example

System call: I want to share a piece of memory with the camera thread! shared memory Alright!

Operating Systems Kernel

slide-21
SLIDE 21

User interface thread in zoom Camera thread in zoom

User-level Kernel-level

Shared memory example

shared memory Write video data to the shared memory

Operating Systems Kernel

slide-22
SLIDE 22

User interface thread in zoom Camera thread in zoom

User-level Kernel-level

Operating Systems Kernel

Shared memory example

shared memory Write video data to the shared memory Read video data from the shared memory and render it on screen

slide-23
SLIDE 23

User interface thread in zoom Camera thread in zoom

User-level Kernel-level

Operating Systems Kernel

Shared memory example

shared memory Write video data to the shared memory No need to go through the kernel for this communication! Read video data from the shared memory and render it on screen

slide-24
SLIDE 24

Lesson: shared memory has better performance than message queues because communications get around the kernel.

slide-25
SLIDE 25

The third IPC mechanism: semaphores

System V IPC is the name given to three interprocess communication mechanisms that are widely available on UNIX systems: message queues, semaphore, and shared memory.

https://man7.org/linux/man-pages/man7/svipc.7.html

what you need to implement in 4411 P1

✅ ✅

slide-26
SLIDE 26

The producer-consumer problem

  • There are two types of threads (or processes): producer and consumer.
  • Producer produces some kind of resources (e.g., HTTP web request)

and consumer consume the resources (e.g., process the request).

  • Goal: consumer should only be scheduled when some resource produced

by the producer is available (i.e., has not been consumed).

  • The core of semaphore is a counter of such available resources. If counter

is greater than 0, a consumer thread will be scheduled.

slide-27
SLIDE 27

Producer thread Consumer thread

User-level Kernel-level

Operating Systems Kernel

Producer-consumer example

System call: I want to share a semaphore with the consumer thread! Counter initialized to 0.

slide-28
SLIDE 28

Producer thread Consumer thread

User-level Kernel-level

Operating Systems Kernel

counter = 0 Alright! System call: I want to share a semaphore with the consumer thread! Counter initialized to 0.

Producer-consumer example

slide-29
SLIDE 29

Producer thread Consumer thread

User-level Kernel-level

Operating Systems Kernel

counter = 0 System call: I want to consume a resource please decrement the counter

Producer-consumer example

slide-30
SLIDE 30

Producer thread Consumer thread

User-level Kernel-level

Operating Systems Kernel

counter = 0

Producer-consumer example

No resource available, suspend the thread until further notice

slide-31
SLIDE 31

Producer threads

User-level Kernel-level

Operating Systems Kernel

counter = 0 System call: I have produced a resource and please increment the counter.

Producer-consumer example

Consumer thread (suspended)

slide-32
SLIDE 32

Producer threads

User-level Kernel-level

Operating Systems Kernel

counter = 1 System call: I have produced a resource and please increment the counter. Alright!

Producer-consumer example

Consumer thread (suspended)

slide-33
SLIDE 33

Producer threads

User-level Kernel-level

Operating Systems Kernel

counter = 1

Producer-consumer example

Consumer thread Resource is now available.

slide-34
SLIDE 34

Producer threads

User-level Kernel-level

Operating Systems Kernel

counter = 0

Producer-consumer example

Consumer thread Decrement counter. Put the thread back to the runnable queue.

slide-35
SLIDE 35

Variants of producer-consumer

  • There can be multiple producer threads and consumer threads.
  • Bounded buffer: a producer can only produce if the number of available

resources (the value of the counter) is not greater to a given number.

slide-36
SLIDE 36

Semaphores in P1

struct sema { // counter // queue of threads that are put to sleep // feel free to add other fields that you need }; // initialize a semaphore void sema_init(struct sema *sema, unsigned int count) // produce a resource by incrementing the semaphore void sema_inc(struct sema *sema) // consume a resource by decrementing the semaphore void sema_dec(struct sema *sema)

slide-37
SLIDE 37

Lesson: semaphore is easy to implement, but it is not very useful and

  • ne should try to avoid using it.

Refer to “12 Commandments of Synchronization” by Emin Gün Sirer

slide-38
SLIDE 38

Homework

  • P1: implement semaphores and test your semaphore with

producer-consumer and other synchronization problems.

  • Read the Linux manual of System V IPC: https://

man7.org/linux/man-pages/man7/svipc.7.html

  • Next lecture: introduce the concepts of timer interrupt

and scheduling for P2.

slide-39
SLIDE 39

Concepts in the real-world (Unix/Linux)

  • User-level threads
  • getcontext, setcontext, …
  • Kernel-level threads
  • pthread_create, pthread_join, …
  • Processes
  • fork, …
slide-40
SLIDE 40
  • Message queues
  • msgget, msgsnd, msgrcv, …
  • Semaphores
  • semget, semop, …
  • Shared memory
  • shmget, shmdt, …

Concepts in the real-world (Unix/Linux)