Introduction to Operating Systems and Processes

CS 105
  
       
  
OS and Processes
 
Intro to Operating Systems
 
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
i
s
 
a
 
p
i
e
c
e
 
o
f
 
s
o
f
t
w
a
r
e
 
t
h
a
t
 
m
a
n
a
g
e
s
a
 
c
o
m
p
u
t
e
r
'
s
 
r
e
s
o
u
r
c
e
s
 
f
o
r
 
i
t
s
 
u
s
e
r
s
 
a
n
d
 
t
h
e
i
r
 
a
p
p
l
i
c
a
t
i
o
n
s
Examples: OSX, Windows, Ubuntu, iOS, Android, Chrome OS
c
o
r
e
 
O
S
 
f
u
n
c
t
i
o
n
a
l
i
t
y
 
i
s
 
i
m
p
l
e
m
e
n
t
e
d
 
b
y
 
t
h
e
 
O
S
 
k
e
r
n
e
l
 
 
resource allocation
isolation
communication
access control
 
multiprocessing
virtual memory
reliable networking
virtual machines
 
user interface
file I/O
device management
process control
Operating System Goals
R
e
l
i
a
b
i
l
i
t
y
:
 
t
h
e
y
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
s
h
o
u
l
d
 
d
o
 
w
h
a
t
 
y
o
u
 
w
a
n
t
A
v
a
i
l
a
b
i
l
i
t
y
:
 
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
s
h
o
u
l
d
 
r
e
s
p
o
n
d
 
t
o
 
u
s
e
r
 
i
n
p
u
t
S
e
c
u
r
i
t
y
:
 
t
h
e
 
s
y
s
t
e
m
 
s
h
o
u
l
d
 
n
o
t
 
b
e
 
(
e
a
s
i
l
y
)
 
c
o
r
r
u
p
t
e
d
 
b
y
 
a
n
a
t
t
a
c
k
e
r
P
o
r
t
a
b
i
l
i
t
y
:
 
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
s
h
o
u
l
d
 
b
e
 
e
a
s
y
 
t
o
 
m
o
v
e
 
t
o
n
e
w
 
h
a
r
d
w
a
r
e
 
p
l
a
t
f
o
r
m
s
P
e
r
f
o
r
m
a
n
c
e
:
 
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
 
s
h
o
u
l
d
 
i
m
p
o
s
e
 
m
i
n
i
m
a
l
o
v
e
r
h
e
a
d
,
 
t
h
e
 
U
I
 
s
h
o
u
l
d
 
b
e
 
r
e
s
p
o
n
s
i
v
e
A
d
o
p
t
i
o
n
:
 
p
e
o
p
l
e
 
s
h
o
u
l
d
 
u
s
e
 
t
h
e
 
o
p
e
r
a
t
i
n
g
 
s
y
s
t
e
m
Exercise 1: Operating Systems
What is an example of an operating system as:
a)
referee
b)
illusionist
c)
glue
Try to be specific with your examples
Processes
A
 
p
r
o
g
r
a
m
 
i
s
 
a
 
f
i
l
e
 
c
o
n
t
a
i
n
i
n
g
 
c
o
d
e
 
+
 
d
a
t
a
 
t
h
a
t
 
d
e
s
c
r
i
b
e
s
a
 
c
o
m
p
u
t
a
t
i
o
n
A
 
p
r
o
c
e
s
s
 
i
s
 
a
n
 
i
n
s
t
a
n
c
e
 
o
f
 
a
 
r
u
n
n
i
n
g
 
p
r
o
g
r
a
m
.
One of the most profound ideas in computer science
Not the same as “program” or “processor”
Multiprocessing
Computer runs many processes simultaneously
Running program “top” on Mac
System has 123 processes, 5 of which are active
Identified by Process ID (PID)
Multiprocessing: The Illusion
Process provides each program with two key abstractions:
L
o
g
i
c
a
l
 
c
o
n
t
r
o
l
 
f
l
o
w
Each program seems to have exclusive use of the CPU
P
r
o
v
i
d
e
d
 
b
y
 
k
e
r
n
e
l
 
m
e
c
h
a
n
i
s
m
 
c
a
l
l
e
d
 
c
o
n
t
e
x
t
 
s
w
i
t
c
h
i
n
g
P
r
i
v
a
t
e
 
a
d
d
r
e
s
s
 
s
p
a
c
e
Each program seems to have exclusive use of main memory.
P
r
o
v
i
d
e
d
 
b
y
 
k
e
r
n
e
l
 
m
e
c
h
a
n
i
s
m
 
c
a
l
l
e
d
 
v
i
r
t
u
a
l
 
m
e
m
o
r
y
C
P
U
Registers
M
e
m
o
r
y
Stack
Heap
Code
Data
C
P
U
Registers
M
e
m
o
r
y
Stack
Heap
Code
Data
C
P
U
Registers
M
e
m
o
r
y
Stack
Heap
Code
Data
Multiprocessing: The (Traditional) Reality
Single processor executes multiple processes concurrently
Process executions interleaved (multitasking)
Register values for nonexecuting processes saved in memory
Address spaces managed by virtual memory system
C
P
U
Registers
M
e
m
o
r
y
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
Process Control Block (PCB)
To implement a context switch, OS maintains a PCB for
each process containing:
process table, which contains information about the process (id,
user, privilege level, arguments, status)\
location of executable on disk
file table
register values (general-purpose registers, float registers, pc,
eflags…)
memory state
scheduling information
      
... and more! 
Context Switching
P
r
o
c
e
s
s
e
s
 
a
r
e
 
m
a
n
a
g
e
d
 
b
y
 
a
 
s
h
a
r
e
d
 
c
h
u
n
k
 
o
f
 
m
e
m
o
r
y
-
r
e
s
i
d
e
n
t
 
k
e
r
n
e
l
 
c
o
d
e
Important: the kernel code is not a separate process, but rather
code and data structures that the OS uses to manage all processes
C
o
n
t
r
o
l
 
f
l
o
w
 
p
a
s
s
e
s
 
f
r
o
m
 
o
n
e
 
p
r
o
c
e
s
s
 
t
o
 
a
n
o
t
h
e
r
 
v
i
a
 
a
c
o
n
t
e
x
t
 
s
w
i
t
c
h
Process A
Process B
user code
kernel code
user code
kernel code
user code
context switch
context switch
Time
M
e
m
o
r
y
Multiprocessing: The (Traditional) Reality
C
P
U
Registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
1.
Save current registers to memory (in PCB)
M
e
m
o
r
y
Multiprocessing: The (Traditional) Reality
C
P
U
Registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
1.
Save current registers to memory (in PCB)
2.
Schedule next process for execution
M
e
m
o
r
y
Multiprocessing: The (Traditional) Reality
C
P
U
Registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
1.
Save current registers to memory (in PCB)
2.
Schedule next process for execution
3.
Load saved registers and switch address space
Multiprocessing: The (Modern) Reality
Multicore processors
Multiple CPUs on single chip
Share main memory (and some of the caches)
Each can execute a separate process
Scheduling of processors onto cores done by kernel
C
P
U
Registers
M
e
m
o
r
y
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
Stack
Heap
Code
Data
Saved
registers
C
P
U
Registers
Interrupts (Asynchronous Exceptions)
 
Caused by events external to the processor
Indicated by setting the processor’s 
interrupt pin
Handler returns to “next” instruction
 
Examples:
Timer interrupt
Every few ms, an external timer chip triggers an interrupt
Used by the kernel to take back control from user programs
 I/O interrupt from external device
Hitting Ctrl-C at the keyboard
Arrival of a packet from a network
Arrival of data from a disk
Exceptions
A
n
 
e
x
c
e
p
t
i
o
n
 
i
s
 
a
 
t
r
a
n
s
f
e
r
 
o
f
 
c
o
n
t
r
o
l
 
t
o
 
t
h
e
 
O
S
 
k
e
r
n
e
l
 
i
n
r
e
s
p
o
n
s
e
 
t
o
 
s
o
m
e
 
e
v
e
n
t
 
 
(
i
.
e
.
,
 
c
h
a
n
g
e
 
i
n
 
p
r
o
c
e
s
s
o
r
 
s
t
a
t
e
)
Kernel is the memory-resident part of the OS
Examples of events: timer interrupt, Divide by 0, page fault, I/O
request completes, typing Ctrl-C
User code
Kernel code
 
Exception
 
Exception processing
by 
exception handler
 
 Return to I_current
Return to I_next
Abort
 
Event
I_current
 
I_next
0
1
2
...
n-1
Exception Tables
Each type of event has a
unique exception number k
k = index into exception table
(a.k.a. interrupt vector)
Handler k is called each time
exception k occurs
Exception
Table
Code for  
exception handler 0
Code for 
exception handler 1
Code for
exception handler 2
Code for 
exception handler n-1
...
Exception 
numbers
Synchronous Exceptions
 
Caused by events that occur as a result of executing an
instruction:
T
r
a
p
s
Intentional
E
x
a
m
p
l
e
s
:
 
s
y
s
t
e
m
 
c
a
l
l
s
,
 
b
r
e
a
k
p
o
i
n
t
 
t
r
a
p
s
,
 
s
p
e
c
i
a
l
 
i
n
s
t
r
u
c
t
i
o
n
s
Returns control to “next” instruction
F
a
u
l
t
s
Unintentional but possibly recoverable
Examples: page faults (recoverable), protection faults (unrecoverable),
floating point exceptions
Either re-executes faulting (“current”) instruction or aborts
A
b
o
r
t
s
Unintentional and unrecoverable
Examples: illegal instruction, parity error, machine check
Aborts current program
Exercise 2: Context Switching
1)
Explain the steps that an operating system goes
through when the CPU receives an interrupt.
2)
A hardware designer argues that there are now enough
on-chip transistors to build a CPU with 1024 integer
registers and 512 floating point registers. As a result,
the compiler should almost never need to store anything
on the stack. As a new operating systems expert, give
your opinion of this design.
Process Life Cycle
I
n
i
t
R
u
n
n
a
b
l
e
R
u
n
n
i
n
g
T
e
r
m
i
n
a
t
e
d
S
t
o
p
p
e
d
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
fork
 is interesting (and often confusing) because
it is called 
once
 
but returns 
twice
fork
 Example
int
 
main
()
{
    
pid_t
 
pid
;
    
int
 
x
 = 1;
    pid = Fork(); 
    
if
 (pid == 0) {  
/* Child */
        printf(
"child : x=%d\n"
, ++x); 
 
return 0;
    }
    
/* Parent */
    printf(
"parent: x=%d\n"
, --x); 
    return 0;
}
fork.c
 
Call once, return twice
Duplicate but separate
address 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
execve:
 Loading and Running Programs
 
int execve(char *filename, char *argv[], char *envp[])
Loads and runs in the current process:
Executable  file 
filename
Can be object file or script file beginning with 
#!interpreter
(e.g., 
#!/bin/bash
)
…with argument list 
argv
By convention 
argv[0]==filename
…and  environment variable 
list
 envp
“name=value” strings (e.g., 
USER=droh
)
getenv, putenv, printenv
Overwrites code, data, and stack
Retains PID, open files and signal context
Called 
once
 and 
never 
returns
…except if there is an error
Linux Process Hierarchy
24
24
L
o
g
i
n
 
s
h
e
l
l
C
h
i
l
d
C
h
i
l
d
G
r
a
n
d
c
h
i
l
d
G
r
a
n
d
c
h
i
l
d
[0]
D
a
e
m
o
n
e
.
g
.
 
h
t
t
p
d
init [1]
L
o
g
i
n
 
s
h
e
l
l
C
h
i
l
d
Note: you can view the
hierarchy using the Linux
pstree
 command
pstree
 on 
pom-itb-cs2
25
25
[ebac2018@pom-itb-cs2 ~]$ pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        …
        ├─attacklab-repor
        ├─attacklab-reque
        ├─attacklab-resul
        ├─attacklab.pl
        ├─crond
        ├─cupsd
        ├─sshd─┬─sshd───sshd───bash───pstree
        │      └─28*[sshd───sshd───sftp-server]
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
 
        └─xdg-permission-───2*[{xdg-permission-}]
Process Life Cycle
I
n
i
t
R
u
n
n
a
b
l
e
R
u
n
n
i
n
g
T
e
r
m
i
n
a
t
e
d
S
t
o
p
p
e
d
fork
 Example
int
 
main
()
{
    
pid_t
 
pid
;
    
int
 
x
 = 1;
    pid = Fork(); 
    
if
 (pid == 0) {  
/* Child */
        printf(
"child : x=%d\n"
, ++x); 
 
return 0;
    }
    
/* Parent */
    printf(
"parent: x=%d\n"
, --x); 
    return 0;
}
fork.c
Call once, return twice
Duplicate but separate
address 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
Concurrent execution
Can’t predict execution
order of parent and child
Modeling 
fork
 with Process Graphs
 
A
 
p
r
o
c
e
s
s
 
g
r
a
p
h
 
i
s
 
a
 
u
s
e
f
u
l
 
t
o
o
l
 
f
o
r
 
c
a
p
t
u
r
i
n
g
 
t
h
e
 
p
a
r
t
i
a
l
o
r
d
e
r
i
n
g
 
o
f
 
s
t
a
t
e
m
e
n
t
s
 
i
n
 
a
 
c
o
n
c
u
r
r
e
n
t
 
p
r
o
g
r
a
m
:
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 inedges
 
Any topological sort of the graph corresponds to a
feasible total ordering.
Total ordering of vertices where all edges point from left to right
Process Graph Example
int
 
main
()
{
    
pid_t
 
pid
;
    
int
 
x
 = 1;
    pid = Fork(); 
    
if
 (pid == 0) {  
/* Child */
        printf(
"child : x=%d\n"
, ++x); 
 
return 0;
    }
    
/* Parent */
    printf(
"parent: x=%d\n"
, --x); 
    return 0;
}
fork.c
Interpreting Process Graphs
Original graph:
Relabeled graph:
fork
 Example: Two consecutive 
fork
s
void
 
fork1
()
{
    printf(
"L0\n"
);
    fork();
    printf(
"L1\n"
);
    fork();
    printf(
"Bye\n"
);
}
 
Which of these outputs are feasible?
 
L0
L1
Bye
Bye
L1
Bye
Bye
 
L0
Bye
L1
Bye
L1
Bye
Bye
Exercise 3: Forks and Scheduling
For each of the following programs, draw the process
graph and then determine which of the possible outputs
are feasible
void
 
fork2
(){
    printf(
"L0\n"
);
    
if
 (fork() != 0) {
        printf(
"L1\n"
);
        
if
 (fork() != 0) {
            printf(
"L2\n"
);
 
}
    }
    printf(
"Bye\n"
);
}
void
 
fork3
(){
    printf(
"L0\n"
);
    
if
 (fork() == 0) {
        printf(
"L1\n"
);
        
if
 (fork() == 0) {
            printf(
"L2\n"
);
        }
    }
    printf(
"Bye\n"
);
}
 
L0
L1
Bye
Bye
L2
Bye
 
L0
Bye
L1
Bye
Bye
L2
 
L0
Bye
L1
L2
Bye
Bye
 
L0
Bye
L1
Bye
Bye
L2
Exercise 3a
void
 
fork2
()
{
    printf(
"L0\n"
);
    
if
 (fork() != 0) {
        printf(
"L1\n"
);
        
if
 (fork() != 0) {
            printf(
"L2\n"
);
 
}
    }
    printf(
"Bye\n"
);
}
 
L0
L1
Bye
Bye
L2
Bye
 
L0
Bye
L1
Bye
Bye
L2
 
Which of these outputs are feasible?
Exercise 3b
void
 
fork3
()
{
    printf(
"L0\n"
);
    
if
 (fork() == 0) {
        printf(
"L1\n"
);
        
if
 (fork() == 0) {
            printf(
"L2\n"
);
        }
    }
    printf(
"Bye\n"
);
}
 
L0
Bye
L1
L2
Bye
Bye
 
L0
Bye
L1
Bye
Bye
L2
 
Which of these outputs are feasible?
Process Life Cycle
I
n
i
t
R
u
n
n
a
b
l
e
R
u
n
n
i
n
g
T
e
r
m
i
n
a
t
e
d
S
t
o
p
p
e
d
Reaping Children
36
36
Reaping
Performed by parent on terminated child (using 
wait
 or 
waitpid
)
Parent is given exit status information
Kernel then deletes zombie child process
i
n
t
 
w
a
i
t
(
i
n
t
 
*
c
h
i
l
d
_
s
t
a
t
u
s
)
Suspends current process until one of its children terminates
Return value is the 
pid
 of the child process that terminated
I
f
 
c
h
i
l
d
_
s
t
a
t
u
s
 
!
=
 
N
U
L
L
,
 
t
h
e
n
 
t
h
e
 
i
n
t
e
g
e
r
 
i
t
 
p
o
i
n
t
s
 
t
o
 
w
i
l
l
 
b
e
 
s
e
t
t
o
 
 
a
 
v
a
l
u
e
 
t
h
a
t
 
i
n
d
i
c
a
t
e
s
 
r
e
a
s
o
n
 
t
h
e
 
c
h
i
l
d
 
t
e
r
m
i
n
a
t
e
d
 
a
n
d
 
t
h
e
 
e
x
i
t
s
t
a
t
u
s
:
Checked using macros defined in 
wait.h
WIFEXITED
, 
WEXITSTATIS
, 
WIFSIGNALED
, 
WTERMSIG
,
WIFSTOPPED
, 
WSTOPSIG
, 
WIFCONTINUED
See textbook for details
wait
 Example
37
37
void
 
fork6
() {
    
int
 
child_status
;
    
if
 (fork() == 0) {
        printf(
"HC: hello from child\n"
);
 
    exit(0);
    } 
else
 {
        printf(
"HP: hello from parent\n"
);
        wait(&child_status);
        printf(
"CT: child has terminated\n"
);
    }
    printf(
"Bye\n"
);
}
Feasible output:
HC
HP
CT
Bye
Infeasible output:
HP
CT
Bye
HC
Reaping Children
38
38
What if parent doesn’t reap?
If any parent terminates without reaping a child, then the orphaned
child will be reaped by 
init
 process (pid == 1)
So, only need explicit reaping in long-running processes
e.g., shells and servers
Process Life Cycle
I
n
i
t
R
u
n
n
a
b
l
e
R
u
n
n
i
n
g
T
e
r
m
i
n
a
t
e
d
S
t
o
p
p
e
d
Terminating Processes
 
 
Process becomes terminated for one of three reasons:
Returning from the 
main
 routine
Calling the 
exit
 function
Receiving a signal whose default action is to terminate
 
void exit(int status)
T
e
r
m
i
n
a
t
e
s
 
w
i
t
h
 
a
n
 
e
x
i
t
 
s
t
a
t
u
s
 
o
f
 
s
t
a
t
u
s
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.
 
Non-terminating Child
41
41
void
 
fork4
()
{
    
if
 (fork() == 0) {
        
/* Child */
        printf(
"Running Child, PID = %d\n"
,
               getpid());
        
while
 (1)
            ; 
/* Infinite loop */
    } 
else
 {
        printf(
"Terminating Parent, PID = %d\n"
,
               getpid());
        exit(0);
    }
}
pid_t getpid(void)
Returns PID of current process
pid_t getppid(void)
Returns PID of parent process
void
 
fork4
()
{
    
if
 (fork() == 0) {
        
/* Child */
        printf(
"Running Child, PID = %d\n"
,
               getpid());
        
while
 (1)
            ; 
/* Infinite loop */
    } 
else
 {
        printf(
"Terminating Parent, PID = %d\n"
,
               getpid());
        exit(0);
    }
}
linux> 
./forks 4
Terminating Parent, PID = 6675
Running Child, PID = 6676
linux> 
ps
  PID TTY          TIME CMD
 6585 ttyp9    00:00:00 tcsh
 6676 ttyp9    00:00:06 forks
 6677 ttyp9    00:00:00 ps
linux>
 kill 6676
linux>
 ps
  PID TTY          TIME CMD
 6585 ttyp9    00:00:00 tcsh
 6678 ttyp9    00:00:00 ps
 
Child process still active even though
parent has terminated
 
Must kill child explicitly, or else will
keep running indefinitely
Non-terminating Parent
43
43
 
When process terminates, it still consumes system resources
Examples: Exit status, various OS tables
Called a “zombie”
Living corpse, half alive and half dead
void
 
fork5
() {
    
if
 (fork() == 0) {
        
/* Child */
        printf(
"Terminating Child, PID = %d\n"
, getpid());
        exit(0);
    } 
else
 {
        printf(
"Running Parent, PID = %d\n"
, getpid());
        
while
 (1)
            ; 
/* Infinite loop */
    }
}
void
 
fork5
() {
    
if
 (fork() == 0) {
        
/* Child */
        printf(
"Terminating Child, PID = %d\n"
, getpid());
        exit(0);
    } 
else
 {
        printf(
"Running Parent, PID = %d\n"
, getpid());
        
while
 (1)
            ; 
/* Infinite loop */
    }
}
44
44
linux> 
./forks 5 &
[1] 6639
Running Parent, PID = 6639
Terminating Child, PID = 6640
linux> 
ps
  PID TTY          TIME CMD
 6585 ttyp9    00:00:00 tcsh
 6639 ttyp9    00:00:03 forks
 6640 ttyp9    00:00:00 forks <defunct>
 6641 ttyp9    00:00:00 ps
linux>
 kill 6639
[1]    Terminated
linux> 
ps
  PID TTY          TIME CMD
 6585 ttyp9    00:00:00 tcsh
 6642 ttyp9    00:00:00 ps
 
ps
 shows child process as
“defunct” (i.e., a zombie)
 
Killing parent allows child to be
reaped by 
init
Exercise 4: Feedback
1.
Rate how well you think this recorded lecture worked
1.
Better than an in-person class
2.
About as well as an in-person class
3.
Less well than an in-person class, but you still learned something
4.
Total waste of time, you didn't learn anything
2.
How much time did you spend on this video (including
exercises)?
3.
Do you have any particular questions you’d like me to
address in this week’s problem session?
4.
Do you have any other comments or feedback?
Slide Note
Embed
Share

In this informative content, we delve into the fundamental concepts of operating systems (OS) and processes. Operating systems are essential software that manage a computer's resources for users and applications. We explore the core functionalities of an OS, such as resource allocation, isolation, communication, access control, multiprocessing, virtual memory, reliable networking, virtual machines, user interfaces, file I/O, device management, and process control. Additionally, we touch upon the goals of an operating system, including reliability, availability, security, portability, performance, and adoption. The content also covers the basics of processes, multiprocessing, and the key abstractions provided by the OS to each program.

  • Operating Systems
  • Processes
  • Multiprocessing
  • Resource Management
  • Computer Science

Uploaded on Sep 14, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. OS and Processes CS 105

  2. Intro to Operating Systems the operating system is a piece of software that manages a computer's resources for its users and their applications Examples: OSX, Windows, Ubuntu, iOS, Android, Chrome OS core OS functionality is implemented by the OS kernel resource allocation isolation communication access control multiprocessing virtual memory reliable networking virtual machines user interface file I/O device management process control

  3. Operating System Goals Reliability: they operating system should do what you want Availability: the operating system should respond to user input Security: the system should not be (easily) corrupted by an attacker Portability: the operating system should be easy to move to new hardware platforms Performance: the operating system should impose minimal overhead, the UI should be responsive Adoption: people should use the operating system

  4. Exercise 1: Operating Systems What is an example of an operating system as: a) referee b) illusionist c) glue Try to be specific with your examples

  5. Processes A program is a file containing code + data that describes a computation A process is an instance of a running program. One of the most profound ideas in computer science Not the same as program or processor Memory Stack Heap Data Code CPU Registers

  6. Multiprocessing Computer runs many processes simultaneously Running program top on Mac System has 123 processes, 5 of which are active Identified by Process ID (PID)

  7. Multiprocessing: The Illusion Memory Memory Memory Stack Heap Data Stack Heap Data Stack Heap Data Code Code Code CPU CPU CPU Registers Registers Registers Process provides each program with two key 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

  8. Multiprocessing: The (Traditional) Reality Memory Stack Heap Data Stack Heap Data Stack Heap Data Code Saved registers Code Saved registers Code Saved registers CPU Registers Single processor executes multiple processes concurrently Process executions interleaved (multitasking) Register values for nonexecuting processes saved in memory Address spaces managed by virtual memory system

  9. Process Control Block (PCB) To implement a context switch, OS maintains a PCB for each process containing: process table, which contains information about the process (id, user, privilege level, arguments, status)\ location of executable on disk file table register values (general-purpose registers, float registers, pc, eflags ) memory state scheduling information ... and more!

  10. Context Switching Processes are managed by a shared chunk of memory- resident kernel code Important: the kernel code is not a separate process, but rather code and data structures that the OS uses to manage all processes Control flow passes from one process to another via a context switch Process A Process B user code context switch kernel code Time user code context switch kernel code user code

  11. Multiprocessing: The (Traditional) Reality Memory Stack Heap Data Stack Heap Data Stack Heap Data Code Saved registers Code Saved registers Code Saved registers CPU Registers 1. Save current registers to memory (in PCB)

  12. Multiprocessing: The (Traditional) Reality Memory Stack Heap Data Stack Heap Data Stack Heap Data Code Saved registers Code Saved registers Code Saved registers CPU Registers 1. Save current registers to memory (in PCB) 2. Schedule next process for execution

  13. Multiprocessing: The (Traditional) Reality Memory Stack Heap Data Stack Heap Data Stack Heap Data Code Saved registers Code Saved registers Code Saved registers CPU Registers 1. Save current registers to memory (in PCB) 2. Schedule next process for execution 3. Load saved registers and switch address space

  14. Multiprocessing: The (Modern) Reality Memory Stack Heap Data Stack Heap Data Stack Heap Data Code Saved registers Code Saved registers Code Saved registers CPU CPU Registers Registers Multicore processors Multiple CPUs on single chip Share main memory (and some of the caches) Each can execute a separate process Scheduling of processors onto cores done by kernel

  15. Interrupts (Asynchronous Exceptions) Caused by events external to the processor Indicated by setting the processor s interrupt pin Handler returns to next instruction Examples: Timer interrupt Every few ms, an external timer chip triggers an interrupt Used by the kernel to take back control from user programs I/O interrupt from external device Hitting Ctrl-C at the keyboard Arrival of a packet from a network Arrival of data from a disk

  16. Exceptions An exception is a transfer of control to the OS kernel in response to some event (i.e., change in processor state) Kernel is the memory-resident part of the OS Examples of events: timer interrupt, Divide by 0, page fault, I/O request completes, typing Ctrl-C User code Kernel code Exception Event I_current I_next Exception processing by exception handler Return to I_current Return to I_next Abort

  17. Exception Tables Each type of event has a unique exception number k Exception numbers Code for exception handler 0 k = index into exception table (a.k.a. interrupt vector) Exception Table Code for exception handler 1 0 1 2 Handler k is called each time exception k occurs Code for exception handler 2 ... n-1 ... Code for exception handler n-1

  18. Synchronous Exceptions Caused by events that occur as a result of executing an instruction: Traps Intentional Examples: system calls, breakpoint traps, special instructions Returns control to next instruction Faults Unintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions Either re-executes faulting ( current ) instruction or aborts Aborts Unintentional and unrecoverable Examples: illegal instruction, parity error, machine check Aborts current program

  19. Exercise 2: Context Switching 1) Explain the steps that an operating system goes through when the CPU receives an interrupt. 2) A hardware designer argues that there are now enough on-chip transistors to build a CPU with 1024 integer registers and 512 floating point registers. As a result, the compiler should almost never need to store anything on the stack. As a new operating systems expert, give your opinion of this design.

  20. Process Life Cycle Init Terminated fork Runnable Running Stopped

  21. 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 fork is interesting (and often confusing) because it is called once but returns twice

  22. fork Example Call once, return twice int main() { pid_t pid; int x = 1; Duplicate but separate address space x has a value of 1 when fork returns in parent and child Subsequent changes to x are independent pid = Fork(); if (pid == 0) { /* Child */ printf("child : x=%d\n", ++x); return 0; } /* Parent */ printf("parent: x=%d\n", --x); return 0; } Shared open files stdout is the same in both parent and child fork.c

  23. execve: Loading and Running Programs int execve(char *filename, char *argv[], char *envp[]) Loads and runs in the current process: Executable file filename Can be object file or script file beginning with #!interpreter (e.g., #!/bin/bash) with argument list argv By convention argv[0]==filename and environment variable list envp name=value strings (e.g., USER=droh) getenv, putenv, printenv Overwrites code, data, and stack Retains PID, open files and signal context Called once and never returns except if there is an error

  24. 24 Linux Process Hierarchy [0] init [1] Daemon e.g. httpd Login shell Login shell Child Child Child Note: you can view the hierarchy using the Linux pstree command Grandchild Grandchild

  25. 25 pstree on pom-itb-cs2 [ebac2018@pom-itb-cs2 ~]$ pstree systemd NetworkManager 2*[{NetworkManager}] attacklab-repor attacklab-reque attacklab-resul attacklab.pl crond cupsd sshd sshd sshd bash pstree 28*[sshd sshd sftp-server] systemd-journal systemd-logind systemd-udevd xdg-permission- 2*[{xdg-permission-}]

  26. Process Life Cycle Init Terminated interrupt, yield fork Runnable Running scheduled Stopped

  27. fork Example Call once, return twice int main() { pid_t pid; int x = 1; Duplicate but separate address space x has a value of 1 when fork returns in parent and child Subsequent changes to x are independent pid = Fork(); if (pid == 0) { /* Child */ printf("child : x=%d\n", ++x); return 0; } /* Parent */ printf("parent: x=%d\n", --x); return 0; } Shared open files stdout is the same in both parent and child fork.c Concurrent execution Can t predict execution order of parent and child

  28. Modeling fork with Process Graphs A process graphis a useful 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 inedges Any topological sort of the graph corresponds to a feasible total ordering. Total ordering of vertices where all edges point from left to right

  29. Process Graph Example int main() { pid_t pid; int x = 1; x=2 2 printf x=0 0 Child pid = Fork(); if (pid == 0) { /* Child */ printf("child : x=%d\n", ++x); return 0; } x=1 Parent main fork printf /* Parent */ printf("parent: x=%d\n", --x); return 0; } fork.c

  30. Interpreting Process Graphs Original graph: x=2 2 printf x=0 0 Child x=1 Parent main fork printf Feasible total ordering: Relabeled graph: e a b e c a b c Infeasible total ordering: a e b c

  31. fork Example: Two consecutive forks Bye printf Bye void fork1() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n"); } L1 printf fork printf Bye printf Bye L1 L0 printf fork printf printf fork L0 L1 Bye Bye L1 Bye Bye L0 Bye L1 Bye L1 Bye Bye Which of these outputs are feasible?

  32. Exercise 3: Forks and Scheduling For each of the following programs, draw the process graph and then determine which of the possible outputs are feasible void fork2(){ printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); void fork3(){ printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); } } printf("Bye\n"); } } } printf("Bye\n"); } L0 L1 Bye Bye L2 Bye L0 Bye L1 Bye Bye L2 L0 Bye L1 Bye Bye L2 L0 Bye L1 L2 Bye Bye

  33. Exercise 3a void fork2() { printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); } } printf("Bye\n"); } Bye Bye printf printf L2 L0 L1 Bye printf printf fork printf printf fork Which of these outputs are feasible? L0 L1 Bye Bye L2 Bye L0 Bye L1 Bye Bye L2

  34. Exercise 3b void fork3() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); } } printf("Bye\n"); } L2 Bye printf Bye printf L1 print f Bye fork printf L0 printf printf fork Which of these outputs are feasible? L0 Bye L1 L2 Bye Bye L0 Bye L1 Bye Bye L2

  35. Process Life Cycle Init Terminated interrupt, yield fork Runnable Running scheduled process or I/O completion wait, I/O operation Stopped

  36. 36 Reaping Children Reaping Performed by parent on terminated child (using wait or waitpid) Parent is given exit status information Kernel then deletes zombie child process int wait(int *child_status) Suspends current process until one of its children terminates Return value is the pid of the child process that terminated If child_status!= NULL, then the integer it points to will be set to a value that indicates reason the child terminated and the exit status: Checked using macros defined in wait.h WIFEXITED, WEXITSTATIS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, WSTOPSIG, WIFCONTINUED See textbook for details

  37. 37 wait Example void fork6() { int child_status; HC exit printf if (fork() == 0) { printf("HC: hello from child\n"); exit(0); } else { printf("HP: hello from parent\n"); wait(&child_status); printf("CT: child has terminated\n"); } printf("Bye\n"); } CT Bye HP printf wait printf fork Feasible output: HC HP CT Bye Infeasible output: HP CT Bye HC

  38. 38 Reaping Children What if parent doesn t reap? If any parent terminates without reaping a child, then the orphaned child will be reaped by init process (pid == 1) So, only need explicit reaping in long-running processes e.g., shells and servers

  39. Process Life Cycle Init Terminated interrupt, yield fork return from main, exit, terminated Runnable Running scheduled process or I/O completion wait, I/O operation Stopped

  40. Terminating Processes Process becomes terminated for one of three reasons: Returning from the main routine Calling the exit function Receiving a signal whose default action is to terminate 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.

  41. Exercise 4: Feedback 1. Rate how well you think this recorded lecture worked 1. Better than an in-person class 2. About as well as an in-person class 3. Less well than an in-person class, but you still learned something 4. Total waste of time, you didn't learn anything 2. How much time did you spend on this video (including exercises)? 3. Do you have any particular questions you d like me to address in this week s problem session? 4. Do you have any other comments or feedback?

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#