CS代写 COMP1411: Introduction to Computer Systems

PowerPoint Presentation

Lecture 08
Concurrency

Copyright By PowCoder代写 加微信 powcoder

COMP1411: Introduction to Computer Systems
Dr. Xianjin XIA
Department of Computing,
The Hong Kong Polytechnic University
Spring 2022
These slides are only intended to use internally. Do not publish it anywhere without permission.
Acknowledgement: These slides are based on the textbook (Computer Systems: A Programmer’s Perspective) and its slides.

Keywords and main message
CONCURRENCY

Understanding the concept of “process”
Understanding the concept of “concurrency”
Learn to do simple concurrent programming
Forking child processes
Synchronization between parent and child processes
Using signals

Programs so far

When a program runs
It totally occupies the CPU
It consumes memory
It will not stop until the last instruction is finished
The program now runs in an isolated world

USERS: I want to run multiple programs at the same time

COMPUTER DESIGNERS: I want the hardware to be used more efficiently

waste of CPU time
waste of CPU time

Concurrency
The concept of concurrency
There are more than one programs, before one running program finishes, another program has stared execution
Multiple programs use the CPU alternatively
The operating system offers the capability to shift between programs

Parallel execution on multiple CPU cores

Concurrent execution on a single CPU core

Definition: A process is an instance of a running program
Not the same as “program” or “processor”
We can create multiple processes of a “program”
E.g., open multiple text editors at the same time
A process provides the following abstractions:
Logical control flow
Each program seems to have exclusive use of the CPU
Provided by kernel mechanism called context switching
Private address space
Each program seems to have exclusive use of main memory
Provided by kernel mechanism called virtual memory

Multi-processing

Multi-processing

Saving registers for P1

Multi-processing

Loading registers for P2

Multi-processing

Saving registers for P2

Multi-processing

Multi-processing

Loading registers for P1

Multi-processing
Typical work flow of switching among processes
Start process A
Save the context of A (registers) to A’s memory region
Load the context of process B
Start to execute process B
Issues not covered so far
How each switching is triggered? (interrupts, will explain soon)
Who is managing the switching? (an operating system)
When process A pauses, there are B, C, D, …, which one should I switch to? (process scheduling, to learn in OS courses)

Multiprocessing Example

Running program “top” on Mac
System has 123 processes, 5 of which are active
Identified by Process ID (PID)

Multiprocessing: The Illusion
Computer runs many processes simultaneously
Applications for one or more users
Web browsers, email clients, editors, …
Background tasks
Monitoring network & I/O devices

Multiprocessing: The (Traditional) Reality
Single processor executes multiple processes concurrently
Process executions interleaved (multitasking)
Address spaces managed by virtual memory system (in our next lecture)
Register values for nonexecuting processes saved in memory

Saved registers
Saved registers
Saved registers

Multiprocessing: The (Traditional) Reality
[Step 1] Save current registers in memory

Saved registers
Saved registers
Saved registers

Multiprocessing: The (Traditional) Reality
[Step 2] Schedule next process for execution

Saved registers
Saved registers
Saved registers

Multiprocessing: The (Traditional) Reality
[Step 3] Load saved registers and switch address space (context switch)

Saved registers
Saved registers
Saved registers

Multiprocessing: The (Modern) Reality
Multicore processors
Multiple CPUs on single chip
Share main memory (and some caches)
Each can execute a separate process
Scheduling of processors onto cores done by kernel

Saved registers
Saved registers
Saved registers

Context Switching
Processes are managed by a shared chunk of memory-resident OS code called the kernel
Note: the kernel is not a separate process
Control flow passes from one process to another via a context switch

kernel code
kernel code

context switch

context switch

Write a concurrent program
Write a program: a process A is created and started with the program; when A runs, A creates a new process B, running the same program as A, and concurrently with A
A: parent process; B: child process

Creating and Terminating Processes
From a programmer’s perspective, a process is in one of three states

Process is either executing, or waiting to be executed and will eventually be scheduled (i.e., chosen to execute) by the kernel
Process execution is suspended and will not be scheduled until further notice
Terminated
Process is stopped permanently

Creating Processes
Parent process creates a new running child process by calling fork
int fork(void)
Returns 0 to the child process, child’s PID to parent process
Child is almost identical to parent:
Child get an identical (but separate) copy of the parent’s virtual address space.
Child gets identical copies of the parent’s open file descriptors
Child has a different PID than the parent
Note: fork is called once but returns twice

Terminating Processes
Process becomes terminated for one of three reasons:
Receiving a signal whose default action is to terminate
Returning from the main routine
Calling the exit function
void exit(int status)
Terminates with an exit status of status
Convention: normal return status is 0, nonzero on error
Another way to explicitly set the exit status is to return an integer value from the main routine
exit is called once but never returns

fork Example: fork.c
int main()
pid_t pid;
int x = 1;

pid = Fork();
if (pid == 0) { /* Child */
printf(“child : x=%d\n”, ++x);

/* Parent */
printf(“parent: x=%d\n”, –x);
linux> ./fork
parent: x=0
child : x=2
Call once, return twice
Concurrent execution
Can’t predict execution order of parent and child
Duplicate but separate memory space
x has a value of 1 when fork returns in parent and child
Subsequent changes to x are independent
Shared open files
stdout is the same in both parent and child

Write a concurrent program

pid == 1789

Parent Process

pid == 1789

Parent Process

Child Process

pid == 1789

Parent Process

Child Process

Modeling fork with Process Graphs
A process graph is a tool for capturing the partial ordering of statements in a concurrent program
Each vertex is the execution of a statement
a  b means a happens before b
Edges can be labeled with current value of variables
printf vertices can be labeled with output
Each graph begins with a vertex with no in-edges
child: x=2

parent: x=0

Process Graph Example
int main()
pid_t pid;
int x = 1;

pid = Fork();
if (pid == 0) { /* Child */
printf(“child : x=%d\n”, ++x);

/* Parent */
printf(“parent: x=%d\n”, –x);
child: x=2

parent: x=0

fork Example: Two consecutive forks
void fork2()
printf(“L0\n”);
printf(“L1\n”);
printf(“Bye\n”);

Feasible output:
Infeasible output:
Possible to get this output
Not possible to get this output.

Synchronizing parent with child
The wait()function
int wait(int *child_status)
Suspend current process until one of its children terminates
Return value is the pid of the child process that terminates
child_status: If child_status != NULL, the integer it points to will be set to a value that indicates the reason why the child terminates
Use macros to extract the information given by child_status

MACROS Indication
WIFEXITED(status) Returns true if the child terminated normally, via a call to exit or a return.
WEXITSTATUS(status) Returns the exit status of a normally terminated child. This status is only defined if WIFEXITED() returned true.
WIFSIGNALED(status) Returns true if the child process terminated because of a signal that was not caught.
WTERMSIG(status) Returns the number of the signal that caused the child process to terminate. This status is only defined if WIFSIGNALED() returned true.
WIFSTOPPED(status) Returns true if the child that caused the return is currently stopped.
WSTOPSIG(status) Returns the number of the signal that caused the child to stop. This status is only defined if WIFSTOPPED() returned true.
WIFCONTINUED(status) Returns true if the child process was restarted by receipt of a SIGCONT signal.

Synchronizing parent with child

Is the following outputs valid?

Synchronizing parent with child

An OS mechanism to allow one process interrupt another process.
A signal is a small message that notifies a process that an event of some type has occurred in the system
A signal receiver can respond according to the signal occurred

Sending a signal
With command, e.g., kill
Let the child process enter an infinite loop
Use the command “kill” to terminate the child process

Sending a signal
Using kill() function
Child process calls pause(), waiting to be killed
Parent process waits for 2 seconds, and calls kill() to kill the child process

Receiving a signal
We let the process to listen to signal SIGINT
SIGINT = Ctrl-C
The process will wait by calling pause()
When the user press Ctrl-C, a signal handler will be executed

Interrupts, that make signals possible
A CPU provides hardware signals, called interrupts, to represent that something happens
For example, pressing Ctrl+C will generate an interrupt

Programs will be developed to handle different interrupts, called interrupt handlers

At the end of each instruction cycle, the CPU will check if any interrupt has come
Fetch  decode  execute  check interrupt

If an interrupt occurs, the corresponding interrupt handler will be called to perform specified functionality

Steps to process a signal
In modern computer systems, interrupts will be received and managed by the operating system
Using signal() to register interrupt handler
OS will transfer the control to application process to execute interrupt handler

Interrupt occurs
Interrupt -> Signal
Signal processing
Post actions
Program continues

Process: the running instance of a program
Concurrent execution of multiple processes
Simple concurrent programming
Explicitly creating multiple processes
Synchronization between parent and child processes
Explicitly sending a signal from process A to process B
Explicitly receiving a signal inside a process

/docProps/thumbnail.jpeg

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com