Exam 1 Review Flashcards

1
Q

What is a Virtual Machine (VM)?

A

• A software system that allows an operating system to run as an
application in user-space
• Allows us to run multiple operating systems on a single computer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why Do We Use Virtual Machines?

A
  • Running Windows/Linux on the same machine
  • Sandboxing for testing
  • Cloud computing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Advantages So Far with Online IDEs

A
  • Online IDEs are great for learning how to code
  • Provides a standardized coding environment
  • Little configuration / installation required – it just works!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why VM-Based Development Now?

A

Development in a more realistic environment
• Running on your local computer
• Logging into a virtual machine that runs Linux C similar to how system
administrators log into a server machine or cloud VM
• Build an application running on an actual operating system that you installed!
• Write C programs that interact with your operating systems using system calls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an Operating System?

A

Operating systems provide an interface between hardware and user
programs, and makes hardware usable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

OS Functions

A

Extended machine providing abstraction of the hardware
• Hides the messy details which must be performed

It is a resource manager
• Time on CPU is shared among multiple users/programs
• Space in memory and on disks is shared among multiple users/programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s a Process?

A

A “program in execution”
• With associated (data and execution) context
• Process execution must progress in sequential fashion

NOT the same as “program” or “application”
• A given program may be running 0, 1, or >1 times

Each instance of a program is a separate process
• With its own address space and context

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why Run Processes?

A

An operating system executes a variety of programs
• Batch systems – job
• Time-shared systems: user programs or tasks

Many processes can run concurrently
• Singer-user system: several programs (word processor, browser, email)
running at the same time
• OS internal programmed activities (ex: memory management)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Process Creation in a Nutshell

A

• Parent processes create child processes, which in turn create other
processes, forming a tree of processes
• Generally processes are identified and managed via a process
identifier (PID)

Resource sharing
• Parent and children have separate virtual address space
• Parent and child shared some resources (e.g. open file descriptors, semaphores)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Process Hierarchy

A

Init process
SSH server
User shell
Firefox/Emax

Init process
System processes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

First System Call: fork()

A

Both parent and child run concurrently after fork()

Linux does copy-on-write to reduce fork() overhead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Second System Call: exec()

A
  • exec() loads in a new binary for execution

* PC, SP, and memory (stack, heap, data) are all reset to run new program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Third and Fourth System Calls: wait() and exit()

A

exit(status) – executed by a child process when it wants to terminate
• Makes status (an integer) available to parent
• Zero exit status means command exited without errors
• Non-zero exit status indicates errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Process Termination

A

Graceful termination via exit

Non-graceful termination when process is killed
• kill(pid, sig) – sends signal sig to process with process-id pid
• Ex: SIGKILL signal to terminate target process immediately
• Can be done from C code or from Shell
• A process can kill another process only if both processes belong to the
same user and if the “killer process” is owned by a super user

When a process terminates, resouces are reclaimed by system:
• PCB, memory, files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Process’s ”Context”?

A

Contents of the PCB

Execution Context
• Stack pointer
• Program counter
• Compute register values
• Segment register values
• Status (running, blocked, ready)

Memory Context
• Pointer to page table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Context Switch: P1 to P2 (running)

A

Steps:

  1. P1 stops running
  2. CPU - kernel mode
  3. OS starts running
  4. OS copies CPU register values to P1’s PCB
  5. OS scheduler decides P2 should run next
  6. OS loads P2’s PCB values into CPU
  7. OS - user mode
  8. P2 starts running

Context Switching has Overhead
• Transition from user to kernel mode
• Copying contents to and from PCBs
• Potential memory and CPU cache misses when running another
process
• A well-designed OS should:
• Balance carefully fairness and context-switching overheads
• Minimize time spent in OS by running efficiently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Life of a Process

A

Process Created
Ready
Running
Terminated/Blocked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

When Does Context Switching Occur?

A
  • (1) Currently running process makes a system call and is blocked
  • Process is put on the blocked queue
  • Scheduler picks up another process in ready queue to run
  • (2) Currently running process terminates
  • Scheduler picks up another process in ready queue to run
  • (3) Hardware or software interrupt happens
  • OS handles the interrupt and blocked processes may become ready
  • Scheduler may choose to continue running current process of pick another process to run
  • (4) Current process used up its current “time slice”
  • Scheduler picks up another process in ready queue to run
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Typical Scheduler Heuristics

A

Each process runs for a fixed time slice (typically 100 msec)
• Response times vs throughput tradeoffs

Some processes have higher priority over others:
• Interactive applications that block frequently because of system calls
• User-defined priority (using “nice” command in Linux or task manager in
Windows)
• System daemons
• User has a higher priority over other users using the computer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

State Transitions for fork and exec

A

fork()
• Clones the child process
• Both parent and child become ready
immediately

exec()
• Open file, load (or map) contents to
memory, reset context
• Process goes to ready state as soon
as this happens
• Might block if opening or loading
the file takes a while
21
Q

Quick Primer on I/O System Calls

A

Input and output (I/O) – any interactions between computer and external devices, such as keyboard, mouse, disks, printers, and monitors.

In Linux, all input and output (I/O) is done by writing to files as sequences of bytes

Each I/O device is uniquely identified by a file descriptor:
• STDIN – keyboard
• STDOUT – display on screen
• Other file descriptors: open files, network sockets

22
Q

Example I/O System Calls

A

Functionally equivalent calls
scanf(“%s”, buf)
fscanf(STDIN_FILENO, “%s”, buf)
read(STDIN_FILENO, buf, …)

System calls documented in section 2 and 3 of Man pages
• Type “man 2 read” or “man 3 fread” for examples

read() attempts to read up to count bytes from file descriptor fd
into the buffer starting at buf.

   On files that support seeking, the read operation commences at
   the file offset, and the file offset is incremented by the number
   of bytes read.  If the file offset is at or past the end of file,
   no bytes are read, and read() returns zero.

   ssize_t read(int fd, void *buf, size_t count);

The function fread() reads nmemb items of data, each size bytes
long, from the stream pointed to by stream, storing them at the
location given by ptr.

   The function fwrite() writes nmemb items of data, each size bytes
   long, to the stream pointed to by stream, obtaining them from the
   location given by ptr.
      size_t fread(void *restrict ptr, size_t size, size_t nmemb,
                    FILE *restrict stream);
       size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb,
                    FILE *restrict stream);
23
Q

State Transitions for I/O System Calls

A

read()
• If error, put process in ready state
• If input is available, put process in ready state
• If no input is available, put process in blocked state

write()
• If error, put process in ready state
• If I/O channel is not available, put process in blocked state, otherwise put process in ready state

24
Q

Invoking a System Call

A

The code for the “function” is in the OS
• Operates in kernel mode
• Different address space
• Context switching required!

Limited number of entry points (“functions”)
• A user process can’t just jump to anywhere it likes in the OS

A system call might not return right away
• Process’ state might change to blocked as a result of the system call’s
actions
• OS might decide to schedule a different program

25
Q

TRAP Instructions

A

Step 1: CPU mode
changes from user to
kernel (supervisor)

Step 2: OS pops off register values
storing program’s current execution
context and stores in the PC

Step 3: OS pops off syscall# printf
and parameters

Step 4: Calls appropriate function in
kernel via dispatcher

Step 5: Push return value for printf
onto stack

Step 6: OS scheduler decides which
process to run next (could be P1 or
another process)

Step 7: OS sets mode back to user
from kernel mode

Step 8: Reload registers from PCB
of process to run

Step 9: Run next instruction after
system call

26
Q

System Calls are More Expensive

A

Going between user and kernel mode is more expensive
• Have to save current process state
• Have to restart state when done
• More expensive than a function call

Restarting a different process can be even more expensive
• Have to load a different address space
• Likely to destroy caches and virtual memory

27
Q

Interrupts

A

System calls are
initiated by user
programs

Interrupts are
the opposite
Hardware
interrupt
External
devices
When interrupts happen:
1. Stop running user
program
2. Trap to kernel
3. Run interrupt handler
4. Go back to user mode
28
Q

Examples of Hardware Interrupts

A

Clock Pulse
• Causes OS to re-invoke the scheduler to run another process
• Example: every 100 msec
• Update system time

Input (from keyboard, network, disk)
• Change that process from blocked to ready
• Re-invoke scheduler

Illegal Instruction
• Send signal to (or terminate) process that issued instruction

29
Q

Interrupts vs System Calls

A

Interrupts are similar to TRAP instructions, except they are triggered by
hardware, not initiated by the currently running process

An interrupt might occur at almost any point in a running process
• The process isn’t expecting it, and so it isn’t saving its parameters, etc.
• “returning” from an interrupt means restarting at the point of last
interruption

System calls have return values but not interrupts

30
Q

Interrupts Steps

A
Step 1: P1 is happily running
Step 2: Interrupt happens.
CPU transitions to kernel mode
Step 3: Interrupt signal is
processed by OS’s Interrupt
Service Routine (ISR)
Step 4: ISR saves P1’s state in PCB
Step 5: ISR identifies right device
driver to call using interrupt ID

Upon completion
• OS calls scheduler to select a process in ready queue to run
• May be the original one, may be something else

OS sets CPU mode bit back to user
• And reloads state registers (PC, etc.) for process selected to run next

Now we’re back in a user space running the process
• Current instruction is whatever it was last doing before context switched

Process gets on with its business
• No need to pop any return value, since process doesn’t know interrupt
occurred

31
Q

Terminology: Signal

A

• Asynchronous software notification to a process of an event
• “Software Interrupt” but can only be initiated by another process, not
necessarily by the OS
• Simplest form of inter-process communication
• Each signal has a symbolic name
• Starts with SIG*
• Defined in signals.h

32
Q

Terminology: Signal Handler

A
  • Used by the process that receives the signal to handle the signal
  • May overwrite default action
33
Q

Are Signal-Handlers Pre-Emptible?

A

It depends!

For the same signal type, signals are usually processed one at a time
per thread.
• E.g. if Ctrl-C is handled, other Ctrl-C signals are blocked on the same
thread.
• Not guaranteed, they may be merged

For different types, they can be preempted
• E.g. a SIGKILL in the middle of a SIGINT handler

To avoid preemption, use signal blocking in handler

34
Q

Advanced Signal Handling

A
  • sigaction(…)
  • Similar to signal(…) but offers more control at the expense of more complexity
  • act
  • Pointer to sigaction struct about action to be taken
  • oact
  • Pointer to previous action associated with signal
int sigaction(int sig, const struct sigaction *restrict act,
struct sigaction *restrict oact);
35
Q

User Level Threads

A
Thread management done
by user-level threads library
entirely in user mode.
As far as the OS is
concerned, it is a “singlethreaded”
process
(old) Examples:
POSIX Pthread
Java Threads
36
Q

Kernel Level Threads

A
For each process, the kernel
has a table with one entry
per thread, for thread’s
registers, state, priority, and
other information
Examples:
Windows XP/2000
Linux
Mac OS X
Kernel is aware of threads
and schedules them
37
Q

Threads: Some Modern Enhancements

A

• No guarantee main thread maps to the top of memory. It is near the
top, but may have other things above it
• Example: virtual dynamic shared object (vDSO)
• Rest of stack can be created at random memory areas, not in a stack
area at the top, for security reasons
• Heap need not be contiguous
• POSIX Pthreads in Linux is now kernel-level and allow for fast context
switching. Read up on Native POSIX thread library (NPTL)
• Modern Java uses kernel level threads for Linux and Windows

38
Q

Multithreaded Applications

A

General Rule of Thumb
• A need to share data structures among threads
• No need to for the OS to enforce resource separation
• Threads need to trust each other

Example Applications
• Web Server
• Serves several clients at once
• Serve other clients while waiting for a disk read to complete for one client

Web Browser
• Load different pages simultaneously

39
Q

Web Servers:

Handling Concurrent Requests

A
  • Using multiple threads

* So that only the flow processing a particular request is blocked

40
Q

Parallelism in Multi-Thread Programs

A

• Threads can enable parallelism, e.g. different threads operating on
different parts of a data processing task
• When there are multiple cores, each core can run a different thread
from the same process
• Thread affinity: way to bind threads to cores, so threads do not move
from core to core (moving results in poor cache performance)
• What if there’s only one core and multiple threads, or more threads
than cores?
• Subset of threads run on available cores at a time
• When there is only one core, main benefit of threads is division of labor or
interleaving I/O and CPU

41
Q

Race Conditions vs Synchronization

A

Multiple processes operating on shared data structures, each running
burst of CPU instructions

Race condition:
• Two processes/threads are executing concurrently running CPU bursts on
shared data structures
• Result can depend on the interleaving of the two instruction bursts
• Race conditions cause bugs that are hard to reproduce

Synchronization:
• Process A is blocked waiting for another process B to complete an action
• When process B is done, signal A which blocks and run
• Blocking and not busy waiting

42
Q

What are Pipes?

A
  • Easy way for processes to communicate with each other
  • Think of a pipe as an actual physical pipe
  • A pipe fills up with data (water)
  • One end of the pipe reads (drains)
  • Other end of the pipe writes (fills)
43
Q

Pipe API

A

int pipe (int fd[2])
fd[0] is open for reading
fd[1] is open for writing
Output of fd[1] is input of fd[0]

• For single process, pipe is next to useless. Usually, process that calls
pipe then calls fork
• Reading from a pipe whose write end has been closed returns EOF
• Write to a pipe whose read end has been closed generates a SIGPIPE.
Can be caught or ignored

44
Q

N Stage Pipeline

A

• Call pipe() N-1 times to create N-1 pipes
• Call fork() N times from the parent process
• For each pair of processes, call dup2() to redirect the STDOUT of
process 1 to be the write end of the pipe
• For each pair of processes, call dup2() to redirect the STDIN of
process 2 to be the read end of the pipe

45
Q

Process Group

A

• Process group is a collection of processes established for sending a
signal to an entire group
• Processes in a pipeline form a group
• See setpgid to allow a process to change its process group

46
Q

Goal of Job Control

A

• Job Control allows us to start multiple jobs (groups of processes) from
a single terminal
• Control which jobs can access the terminal (send/receive from
standard in/out) and which jobs runs in the background
• Prevents illegal behavior
• Example: if a background process tries to read from the terminal, a
SIGTTIN signal is delivered to stop the process

47
Q

Synchronous vs Asynchronous Wait

A

If a parent (shell) creates a process group of child processes, how can
the parent know when the child processes are done?

Synchronous Solution
• Busy wait on all child processes – have a loop that waits on each child

Asynchronous Solution
• Useful for background processes
• Parent waits for SIGCHLD (stopped, exited) signals from children
• When a child sends a SIGCHLD, use SIGCHLD handler
• Call waitpid – specifically, the non blocking version
• Based on return value of child, figure out if child is stopped, exited, etc.
Use WIFSTOPPED(status), WIFSIGNALED(status) to check status

48
Q

Problems with Semaphores

A
  • Incorrect use of semaphore operations:
  • signal(mutex) … wait(mutex) results in no mutual exclusion
  • wait(mutex) … wait(mutex) results in a deadlock
  • Omitting:
  • wait(mutex)
  • signal(mutex)
  • Both