程序代写CS代考 data structure Processes Overview – cscodehelp代写

Processes Overview
CSci4061: Introduction to Operating Systems

September 19, 2021
Computer Science & Engineering, University of Minnesota
1

Team forming
• Due today
• What should I do if I cannot find a team member?
• Check if someone is looking for team members on Canvas • Ask for members on Canvas
• Submit your team list; we will try to coordinate if possible
2

Last lecture
• Elements in C code
• Components of a C program
• Compiling and building programs • Executing and debugging
• Common problems
• How to avoid them?
3

More about last lecture
• Order of Makefile dependencies
:

• Makefile decides the default target if not specified • Order of the tuples is not significant
• Cons with dynamic library
• Dynamic libs use more memory • Compatibility issues
4

This lecture
• Process overview • Process in Unix • Special processes
5

Processes Overview

What is a process?
6

What is a process?
• A process is an instance of a program in execution while a program is static code and data
• A program can have multiple processes in an OS
• Enables multitasking
• Unit of execution/work/task
• Provides boundary of protection: resource isolation
6

What resources are associated with a process?
• An image of the executable
• Allocated memory
• OS descriptors of resources
• Security attributes, e.g., permissions • Process states
7

OS virtualization
• A common, important, and general concept/technology in OS • Goal: to provide each program a copy of the resources
• We have limited physical resources: CPU, memory, etc.
• Resource usage is dynamic and diverse, and we cannot build a dedicated
program for each computer
8

OS virtualization
• A common, important, and general concept/technology in OS • Goal: to provide each program a copy of the resources
• We have limited physical resources: CPU, memory, etc.
• Resource usage is dynamic and diverse, and we cannot build a dedicated
program for each computer
• Solution 1: free up previous programs before running a program?
• No good—no sharing at all
• Solution 2: create illusion of exclusive resources for each program
• Good—enabling sharing
• Resources are virtualized
• Accesses are allowed through predefined operations (abstraction)
“process” is the OS abstraction for execution
8

From a program to a process: What are actually happening
OS kernel:
1. OS creates a virtual address space, e.g., of size 264
2. OS creates memory regions in the address space
• Stack, heap, ,data, text (code), etc.
• Also sub-spaces: kernel space and user space
3. OS loads the program’s code and data, and the loader (ld.so) to user space
4. OS uses data structures, such as Process Control Block (PCB), to manifest
processes
Loader (user space):
5. The loader loads libraries into the address space 6. Control goes to entry point (main())
9

Memory layout of a process (232)
Fig from Prof.
10

How an OS manages processes
• In an OS kernel, processes are represented by a data structure called a Process Control Block (PCB).
• This structure logically contains all the necessary data to represent a process’s current state of execution.
• Process identification data
• Each process has a unique pid
• Process state data • Process control data
11

What’s in a PCB?
• Reference to binary code (program)
• Program counter (pointing to the current instruction) • Data
• Other registers like stack pointer
• Memory limits for variables
• OS resources
• Process ID
• File descriptors, locks, signals, sockets, etc.
12

Layout of a PCB
• OS maintains a map: pid–>PCB. • Where is PCB stored?
13

Process states
• New: Being created
• Running: Executing instructions on the CPU • Blocked: Waiting for an event
• Ready: Waiting to be assigned a CPU
• Done: Finished executing
Process/task scheduling in next lecture.
14

Processes In Unix

Unix process lifecycle
• Process is created with fork system call
• Parent clones itself
• Each process has a parent—imagine a process tree in Unix
• Note: In Linux, fork is no longer a syscall; it internally calls the clone syscall
• Process executes a program with exec system call • Loads program from a file
• Executes the code
• Process exits
• Parent might wait for the child to finish
15

Creating a new process with fork
• Creates a new (child) process whose context is the same as the calling process.
• All memory and OS resources are logically duplicated.
• All processes are part of a tree made up of parents and children which is
rooted with the init process.
16

Discussion
Why does Unix use fork (i.e., clone) followed by exec (i.e., replace) to create a new process, instead of creating one from scratch?
17

Using fork for process creation
• Historical reason: Easy for implementation
• The concept and code for fork had existed; just some minor updates to process table
• A note on fork efficiency
• fork is efficient
• Most Unix implementations simply make the child process reference the
memory of the parent to save time • copy-on-write
• The memory is only truly duplicated when the child performs a write to the memory
• The “replace” is mostly creating virtual memory mapping
• A downside: May have some side channels that leak information
• Maybe easier for management with the tree structure
18

The fork function
pid_t fork(void)
• Returns ‘pid_t’; type: ‘int’ • When successful:
• Returns 0 to new child process
• Return the child process ID to parent.
• Upon failure, returns -1 without creating child process.
19

Fork Example in C
// Create n child processes.
for (int i=0; i < n; ++i) // If this is the child process, break out. if (fork() == 0) break; 20 Sharing between parent and child What are shared? • Variables • Open files What are not shared? • Locks, pid, signals, CPU time, etc. 21 Executing a new program—the exec* call • Deletes the process’s current execution environment (stack, heap, etc.) • Begins executing a new program by loading and initializing it. • Some OS resources (like open files) are retained 22 An exec example if (fork() == 0) { execv("/some/path", (char*)null); printf("Will this message be printed out? Why?"); } Would you see the message? 23 An exec example if (fork() == 0) { execv("/some/path", (char*)null); printf("Will this message be printed out? Why?"); } Would you see the message? It depends: Any code written after an exec call in the original program will only be run if exec fails. 23 Exec* variants • Different parameters • Produce different execution environments. execl, execlp, execle, execv, execvp, execvpe Man page located here: http://man7.org/linux/man-pages/man3/exec.3.html 24 Waiting for another process to terminate—the wait call • A call to wait() blocks the calling process until one of its child processes exits or a signal is received • After child process terminates, parent continues its execution after wait system call instruction // Waits until the next child terminates pid_t wait(int* status) // Waits specifically for the specified process finish pid_t waitpid(pid_t pid, int* status, int options) 25 Wait example int* status; if (fork() == 0) execv("/some/path", (char*)NULL); else wait(status); // Wait for child to finish. Notes • Parent processes can wait indefinitely • Calling wait without a child process will just set the parent process’s errno to ECHILD and return immediately. 26 A C example The following code will fork and execute the same process 10 times sequentially for (int i=0; i < 10; ++i) { if (fork() == 0) { execv("some/path", (char*)NULL); } else { wait(); } } Q: How could we change this code to run them all simultaneously? 27 Process termination—normal process exit • Processes typically end when a call is made to exit(int) • exit(0): done without errors (convention) • exit(int): non-zero int indicates specific errors • Upon exit, OS reclaims all reserved resources and flushes print buffers • Status code is passed to waiting processes 28 Common abnormal process exits • Calls to abort() • Uncaught Exceptions • Kill signal received (Ctrl+C) • Segmentation faults • Core dump for debugging 29 Special processes Orphan processes Remember the process tree? Except the root (init) process, others all have a parent What if a parent process exits before its child finishes? • The child becomes an orphan process • Handling: The default init process becomes the parent • This is called re-parenting • Child is eventually reaped 30 Zombie processes • System does not completely reap a child until its parent has waited on it • Zombie process: a process that has completed execution (via the exit system call) but still has an entry in the process table • E.g., terminated process whose parent failed to call wait • In fact, a child process always first becomes a zombie before being removed from the resource table 31 Unix Daemons • Indefinitely running processes • Generally not user-facing • Names typically end in d • Responsible for specific OS services • Web, print, or SSH servers • E.g. httpd, inetd, launchd, systemd 32 Come back to the C example The following code will fork and execute the same process 10 times sequentially for (int i=0; i < 10; ++i) { if (fork() == 0) { execv("some/path", (char*)NULL); } else { wait(); } } Q: How could we change this code to run them all simultaneously? • wait(NULL): block parent process until any of its children has finished • waitpid(pid, status): wait for process to change state 33 Summary • Processes • Abstraction of programs for OS virtualization • Creating them (fork) • Executing new programs (exec) • Waiting for a process (wait) • Termination • Special processes 34 Next lecture • Process states • Reading: Robbins 3.2-3.6, 3.8-3.9 35 References [1] http://www-users.cselabs.umn.edu/classes/Spring- 2018/csci4061/notes/processes.pdf [2] http://www-users.cselabs.umn.edu/classes/Fall- 2018/csci4061/notes/w2_2_post.pdf 36

Leave a Reply

Your email address will not be published. Required fields are marked *