程序代写 CSCI 4061 Introduction to Operating Systems – cscodehelp代写
CSCI 4061 Introduction to Operating Systems
Instructor:
Operating System Structure
Applications
Operating System
Hardware
2
Operating System Structure
Applications
Operating System
Hardware
User Programs Shells, Tools and Utilities
4
Processes, File System, Virtual Memory, Threads, Sockets
CPU, Memory, Disks, Devices
Processes: Outline
Process Definition
Process Structure and States
Process Creation and Execution Process Termination and Waiting
5
1
Multi-programming
Multiple programs executing concurrently
Time-sharing: Fast time-multiplexing between
multiple program executions
Each program execution has some state; needs
resources (CPU, memory, I/O) Questions:
How does the OS identify each execution?
How does the OS manage resources for each
execution?
How does the OS protect and isolate each execution?
6
What is a Process?
A program in execution
Basic unit of work
Enables multiprogramming
Provides a protection boundary
7
Program vs. Process
Program:
Passive entity
Set of instructions A binary file
Process:
Active entity
Executing path of instructions
Live set of resources (CPU cycles, memory, files)
Can multiple processes correspond to the same program?
8
What does a process contain?
Program text: Binary code
Program Counter: Pointer to current instruction
Other registers (e.g., stack pointer)
Data: Memory required for variables, functions Other objects:
File descriptors, signals, locks Accounting information
9
2
Process Memory Layout
Args, environment
Stack
Heap
bss
Data Segment
Program Text
High Address
Low Address
10
Activation Records (function params, local vars, saved registers)
Dynamic Memory
Uninitialized Static Data Initialized Static Data
Binary Code
Multiprogramming
OS multiplexes system resources among multiple processes
Each process is allowed to run on the CPU for a short duration (quantum)
Process has to give up the CPU if:
its time quantum expires
it has to wait for an event (I/O, signal, lock)
Question: What information would the OS need to resume a suspended process?
11
Process Context
Current state of process activity Determined by:
Program counter value
Function call stack
Data values (data segment, bss, and heap) Register values
12
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
13
3
Process Life Cycle
new created
event completion
done terminated
wait for event
quantum expired
ready
running
scheduled blocked
14
Process Identity
Each process has a unique identifier called pid OS keeps track of each process through its pid
15
pid_t getpid();
Returns the pid for the current process
Processes in Unix
Parent-child relation
Each process has a parent
init process: grand-daddy of all processes pid=1, ever-running process
16
pid_t getppid();
Returns pid of parent
17
Process Tree in Unix
1
256
3
4
8
7
4
Process Lifecycle
Process is created
Parent clones itself
Process executes a program Loads program from a file
Executes the code
Process exits
Parent might wait for the child to finish
18
Process Creation: fork
Parent process executes fork
It creates an (almost) identical copy of itself
New (child) process inherits a new copy of the
parent’s whole state and context: Code, data, open files
Program counter, stack
Two clones exist immediately after fork 19
pid_t fork();
Process Creation: fork parent
fork
child
Args, environment Stack
Args, environment Stack
Heap
Data Segment, bss
Program Text
20
Args, environment Stack
Heap
Heap
Data Segment, bss
Data Segment, bss
Program Text
Program Text
Returning from fork
fork() returns twice
Once in parent and once in child
Value returned by fork is different for each process
Returns 0 to child process
Returns pid of child to parent Returns -1 to parent if error
Both processes resume from same point after fork(), but with different return values
21
5
Process Creation: fork
pid_t child_pid;
int x=1;
child_pid=fork();
if (child_pid>0)
/* This is parent process */
printf (“I’m parent process: x=%d
”, x);
else if (child_pid==0)
/* This is child process */
printf (“I’m child process: x=%d
”, x);
22
Process Creation Example 1
pid_t child_pid;
int i, n=3;
for (i=0; i
/* This is parent process */
printf (“I’m parent process: x=%d
”, x);
else if (child_pid==0)
/* This is child process */
{
execl(“/bin/ls”, “ls”, “–l”, NULL);
printf (“I’m child process: x=%d
”, x);
}
28
Normal Process Termination
main function falls off the end
return from main
exit
status value of 0 corresponds to successful completion
OS cleans up all process state
Releases memory, file pointers, registers, etc. Flushes print buffers
29
void exit(int status);
7
Abnormal Process Termination
Call to abort()
Receives a signal it does not catch
Signals are software interrupts
E.g.: Ctrl-C, segmentation fault
OS does not call user-installed exit handlers Core dump may be produced
30
Waiting for a Child: wait
Sometime parent wants to wait for the child to finish execution
Example: “ls –l”
Shell waits until command is executed
The parent suspends execution
wait() returns when a child exits
Returns pid of exited child
status is pointer to child’s exit status
31
pid_t wait(int *status);
Waiting for a Child: wait
parent
fork
child
wait
Time
exit
exec
32
Waiting for a Specific Child: waitpid A process may have many children, but may
want to wait for a specific child
pid: process id of specific child
status: exit status of child
opt: WNOHANG means no waiting, return 0 if
child still running
33
pid_t waitpid(pid_t pid, int *status, int opt);
8
Uses of wait
Synchronization
Allows parent to synchronize its execution with
the child/children
Useful for interactive applications like the shell
Reaping
OS removes a process only when its parent waits
for it
Need to notify the exit status of the process
35
Wait: Some questions
Under what scenarios may a parent not wait for a child?
What might happen if a parent doesn’t wait on a child process?
36
Background Processes
Shell spawns a process but does not wait for it E.g.: “mozilla &”
Here, the parent does not wait for the child parent
fork
child
37
exec
Daemons
Forever running background processes
Similar to shell
Get some input
Do something useful
Print results, log errors if required
Differences from shell:
Each implements specific service May not be interactive
Examples: Web server (httpd), print server (lpd), ssh daemon (sshd)
38
9
Orphans and Zombies
Orphan: Running process whose parent dies before it finishes
Zombie: Terminated process whose parent hasn’t waited on it
System does not remove child completely until parents does a wait
Orphans are adopted by init process init does wait from time to time
Eventually reaped
39
10