ECS150 SQ20
March 27, 2020
Lecture Notes 3 The Programming Interface
• Process Management – How do we create/stop/resume/wait for a process?
• Input/Output – How do processes communicate with outside world?
• Thread Management – How do we create/stop/resume/wait for threads?
• Memory Management – How do processes ask for more or give back allocated memory?
• File Systems and Storage – How is the user’s data stored persistently?
• Networking and Distributed Systems – How to processes communicate between systems?
• Graphics and Window Management – How does a process control pixels on screen?
• Authentication and Security – What permissions does a process or user have?
Process Management
• Shell – A process control system
• Parent Process – Creator of process
• Child Process – Process being created
• Process Tree – A tree structure of all processes with connections between Parent/Children
init pid=1
login pid=22
bash pid=55
sshd pid=32
tcsch pid=57
cat pid=101
grep pid=102
• Process Creation
• Process Identifier (PID) – The unique identifier for a process
• fork(), exec() – Typical combination of system calls to create a process (*nix OSes)
• CreateProcess() – Windows system call to create a process
• Steps for fork in kernel
1. Create and initialized the process control block (PCB) in the kernel
2. Create a new address space
This content is protected and may not be shared, uploaded, or distributed. Lecture Notes 3
1 of 3
ECS150 SQ20 3.
4.
1. Load the program prog into the current address space
2. Copy arguments args into memory in the address space
3. Initialize the hardware context to start execution at “start”
• Process Termination
• exit(), kill() – Typical system calls to terminate a process either normally with exit or
forced/signaled through kill (*nix OSes)
• TerminateProcess() – Windws system call to terminate a process
• Cascading Termination – Initiated at root of process tree to terminate all processes
• Zombie Process – Child process that has terminated, but parent has not called wait
• Orphan Process – Child process whose parent has terminated
• wait() – Waits for a child process to terminate
• kill(), signal() – Sends a signal or upcall to process
Input/Output
• Uniformity – Use the same set of system calls for all types open, close, read, and write
• Open before use – Device must be opened before it is used
• File Descriptor – A handle to a I/O device (commonly a file hence the name)
• Byte-oriented – All access is byte-oriented even block devices
• Kernel-buffered reads – Stream data is buffered and returned to process as requested
• Kernel-buffered writes – Stream data is buffered by kernel and output later
• Explicit close – Specifically notifies that the process is done with the device
• Pipe – Kernel buffer between two file descriptors
• Replace file descriptor – dup2() can make copy of file descriptor
• Wait for multiple reads – select() (or poll()) can be used to wait for multiple file
descriptors
• Common *NIX I/O system calls
• open() – Opens a file or device
• pipe() – Creates a pipe with a read end and a write end
• dup2() – Duplicates a file descriptor
• read() – Reads data in from the device specified by the file descriptor
• write() – Writes data to the device specified by the file descriptor
• select() – Waits for a device from set of devices to enter certain stat or states (data
ready, write possible, error)
• poll() – Similar to select, being suggest for use over select
• close() – Closes the use of the device for the process with that file descriptor
This content is protected and may not be shared, uploaded, or distributed. Lecture Notes 3
2 of 3
March 27, 2020
Initialize the address space with a copy of the entire contents of the address space of the parent
Inherit the execution context of the parent (e.g., any open files)
Inform the scheduler that the new process is ready to run
5.
• Steps for exec
ECS150 SQ20 March 27, 2020
Implementing a Shell
• stdin – File descriptor that should be open for reading at beginning of process
• stdout – File descriptor that should be open for writing at beginning of process
• stderr – Similar to stdout, but for outputting errors
• Program can be a file of commands – Essentially a script
• Program can send its output to a file – Redirection of stdout to a file will allow this
• Program can read its input from a file – Redirection of a file to stdin will allow this
• Output of one program can be input of another – Using a pipe and redirection will allow
this
• Producer Consumer Communication – One produces output that is consumed as input by
another
Interprocess Communication
• Producer Consumer – One way communication that producer writes and consumer reads
• Client-Server – Two way communication between processes, client requests service from
server
• File System – Communication can occur if a file is written to then read some time later
Operating System Structure
• Monolithic Kernel – Simple structure with single monolithic simple piece of code
• Layered Approach – Layers builds upon lower layers only
• Hardware Abstraction Layer – Portable interface to machine specific operations
• Microkernel – Nonessential functionality removed from kernel
• Provides Message passing between services • Modules – Loadable kernel modules
• Dynamically load additional functionality into kernel
• User-level device drivers – Have device driver in user space for protection against
crashes
• Virtual machine device drivers – Allows for legacy code to run inside guest OS
• Driver sandboxing – Provide drivers with own restricted execution environment
• Hybrid Systems – Most OSes are mixes of multiple types
This content is protected and may not be shared, uploaded, or distributed. Lecture Notes 3
3 of 3