Week 11 - Linux Processes Flashcards
Program
Executable code, or source code
- Static
Process
is a running program
- Active
- Can be started by a GUI, command line or by another process
2 types of processes
Foreground process - interacts with user
Background process - runs to completion and report results (to file or window)
Process consists of
- Set of executable instructions
- Region of memory which stores:
- Executable instructions
- Process specific data (values in variables)
- Call Stack
- Operating System resources
- File descriptors
- Security attributes
- Process owner & permissions
- Processor State
- Content of CPU registers
Process ID
When a process runs, Linux keeps track of it
through a process ID (PID)
* When linux boots, the first process created is an initialization process call systemd (or init )
* Systemd is given PID = 1
* Each new process gets next available PID
* If your computer hasn’t been rebooted in a while, the PIDs could be in tens of thousands
* Each process also has a Parent PID
* PID of the process which created it
Process creation
Process can only be created by another process
* Parent Process creates (spawns) one or more child processes
System calls are used to create process
a system call is function call to Linux Kernel
Process creation
Process can only be created by another process
* Parent Process creates (spawns) one or more child processes
* System Call = Function call to Linux Kernel
* To create a process, System call is required
* fork()
* exec()
* wait()
* vfork
Process consists of
Region of memory which includes executable instructions and process specific data
Fork
Duplicates Parent process to create child
Creates new region of memory, and duplicates contents of memory
* E.g Process A creates process B
Wait
- Wait system call is same as fork with one key difference
- The parent goes into a wait state (sleep) until the child terminates
Duplicates parent process to create child
Parent sleeps until child terminates
Exec
Exec system call causes a process to start a new program which replaces the current process.
In other words, creates a new process which replaces the current process
vfork
operates in the same way as fork, with one exception
The parent and the child share the same memory
fork + exec
Typical usage of exec is to pair it with fork
1. Parent calls fork to create a new process with a new PID
* New process is a duplicate of the parent
- The child then calls exec to change the process to execute a different program
What is a computer? CPU
- The processor (central processing unit, CPU)
- Performs the fetch
execute cycle
- Fetch next program instruction from memory
- Decode the instruction into actions
- Command the relevant part(s) of the computer to operate to
execute the instruction - Store the result somewhere (if necessary)
The CPU consists of
Control Unit
* Handles the fetch
execute cycle
* Controls the other components of the computer through signals
* Contains registers (storage locations) for important pieces of
information like the memory location of the next instruction in the
program and the current instruction
- Arithmetic Logic Unit (ALU)
- Digital circuits to perform arithmetic and logic operations
- Data registers to store data being used by the current set of
instructions
What is a computer? CPU
Consider an example:
* We write the instruction A = B * (C + D)
* This is broken into several lesser machine code instructions
* Load C into a data register
* Add D to the data register storing C
* Multiply B to the data register storing C + D
* Store result in A
Explain what happens as process executes
The state of the process changes
* Variables change values
* Portions of program move from swap space to memory
* CPU Registers Change
* Current instruction changes (IP)
* Instruction that the processor will execute next changes (PC)
* Status flags change
As a process executes, state of the process changes
- OS must keep track of state of process
- Does so using
Process Status Word ( - PSW is a collection of most important pieces of info about a process
- Current values of CPU registers
- In other words, PSW store’s a snapshot of the process
Process Management
- OS must also schedule when a process executes
- Scheduling of processes is known as process management
Process management has evolved over the years from single process execution to concurrent processing.
Single Process Execution
Single Tasking
* OS starts a process
* CPU operates on just this process
* Computer thus only run one process at a time
* Process must terminate (or suspended by user) before another
process can be executed.
Single Process Execution
BATCH
- Sequence of processes
- Each process runs one at a time
- OS schedules which processes is executed next
- First in First out
- Priority
- Shortest first
- Processes cannot be interactive (offline)
Batch does not get efficient use from CPU
* E.g. process is running, and needs to read from disk
* Process must wait for slow disk to return data
* When process is waiting, CPU is sitting idle
Concurrent Processing
- Idea is to maximize use of CPU
- Don’t let CPU sit idle
Multi programming
* When a process is performing time
consuming I/O,
process is removed from CPU and replaced with a
different process.
* Looks as if more that one process is executing at the same time
* Replacement of Process is called
Context Switch
Context switch
- Switching of processes on the CPU
- Current Process replaced by next process
- OS must first store the state of the current process
- Process State Word (PSW) Stores state of process
- PSW of current process read from CPU & stored in OS memory
- PSW of new process is loaded from memory & loaded onto CPU
- Once context switch happens, CPU resumes and executes new process
Multi-tasking
- Similar to Multi
Programming (context switch happens) - In Multi
Programming: Context switch occurs when process does slow I/O operation - In Multi
Tasking: Context switch occurs after a set amount of time (also when slow I/O occurs) - CPU has a timer
- Timer set to a threshold ( e.g 10,000)
- Process is executed, timer is reduced at each clock tick
- When timer reaches 0: Context switch occurs and new process
loaded to CPU - In other words: each process takes turns using CPU.
- Each process only has set amount of time on CPU before another
process gets to use CPU.
Multi-processing
- Multiple CPUs (Multi Core)
- OS selects which CPU (Core) will execute a particular processor
- Each individual processor performs multi tasking
Monitoring processes
- You may need to see what a process is doing
- Process could be using too much resources
- Helps understand processes more
top
* Provides ongoing look at the processor activity in real time
* Displays PID, CPU usage, Memory usage, owner etc
* Updates every 3 seconds (default)
vmstat
Displays summary information about OS memory and processes, context switches and block I/O
CPU Can only execute one process at a time
Single tasking
Batch
Multi-programming
Multi-tasking