Lecture 3

advertisement
Operating Systems
CMPSC 473
Processes
August 31, 2010 - Lecture 3
Instructor: Bhuvan Urgaonkar
Teaching Assistant
• Name : Ohyoung Jang
• Office Hour : 2:30pm ~ 4:30pm, Mon Wed
• Location : 338E IST
• E-mail : oyj5007@cse.psu.edu
2/22
Contents
•
•
•
•
Sample Program
Compiling in Unix
How to use gdb
Makefile
3/22
<< main.c >>
#include <stdio.h>
#include <string.h>
Sample Program
void print_hello(char* name);
int compute_intsum(int);
int main(int argc, char** argv)
{
int sum;
int n = 100;
char* name1 = "Ohyoung";
char* name2 = "Another long name";
sum = compute_intsum(100);
printf("sum from 1 to %d is %d\n", n, sum);
print_hello(name1);
}
<< src1.c >>
#include <stdio.h>
void print_hello(char* name)
{
printf("Hello %s.\n", name);
}
int compute_intsum(int n)
{
int i;
int sum = 0;
strcpy(name1, name2);
print_hello(name1);
for (i = 1; i <= n; i++)
{
sum += i;
}
return 0;
return sum;
}
4/22
Contents
•
•
•
•
Sample Program
Compiling in Unix
How to use gdb
Makefile
5/22
Compiling in Unix
• Use gcc for c, or g++ for c++ to
compile, link and generate executable
• “-c” option for compile
– If omitted, gcc/g++ links object files and make
executable file
• “-g” option for insert debug infos
– To use gdb, this option is required.
man(1) gcc
6/22
Compiling in Unix
• Use gcc for c, or g++ for c++
– Options
• “-Ox” option for optimization level
– 0 : no-optimization (required for gdb)
– 1-5 : optimization level. Higher number -> more opt
• “-I” for set include directories
• “-L” for setting library directories
• “-o” for setting output file
– Infile
• src or object files
7/22
Compiling in Unix
• Example
– Compile source files
– Link all object files to make an executable
Compile main.c
with debug info
with no-optimization
output file is main.o
Make executable
with main.o and src1.o
8/22
Compiling in Unix
• Easier example
– Compile and Link in a command
9/22
Contents
•
•
•
•
Sample Program
Compiling in Unix
How to use gdb
Makefile
10/22
How to use gdb
• You may face error before you use gdb
– You may expect “Another long name”
instead of segmantatino fault
Program is terminated
due to a trap
Just type “gdb <executable>”
11/22
How to use gdb
gdb console
Output of the program
Program terminated. But process infos remains.
Then type “bt” or “backtrace”
12/22
How to use gdb
Call stack
with frame number
Print variable
Error.
Current frame is 0
Change from to 1
Print variable
The bug is found
13/22
How to use gdb
• Another GDB commands
– help [command]
– b(reak) : set breakpoint
• b <filename>:<lineno> [condition]
• b <function_name> [condition]
– Ex) b src1.c:15 if i == 10
– Ex) b compute_intsum
– info b(reak) : list breakpoints
– delete <breakpoint n>
14/22
How to use gdb
• Another GDB commands
– r(un) [argument]
• Start program
– s(tep)
• Step program until it reaches a different src line
– n(ext)
• Like “step” command, as long as subroutine calls
do not happen
– c(ontinue)
• Continue program until breakpoints or signals
15/22
16/22
Contents
•
•
•
•
Sample Program
Compiling in Unix
How to use gdb
Makefile
17/22
Makefile
The “make” utility automatically determines which
pieces of a large program need to be recompiled, and
issues commands to recompile them.[1]
To use “make” utility, we need “Makefile” file.
Makefile is consisted of variables and rules
18/22
Makefile
• Usage
– To build
• $make [build]
– To clean
• $make clean
– To rebuild
• $make rebuild
<< Makefile >>
CC = gcc
CFLAGS = -O0 -g
OBJS = main.o \
src1.o
Variables
TARGET=test
build: $(OBJS)
gcc -o $(TARGET) $(OBJS)
rebuild: clean build
Rules
clean:
rm -rf $(OBJS)
rm -rf $(TARGET)
19/22
Makefile
<< Makefile >>
Define Rules
CC = gcc
CFLAGS = -O0 -g
<target name>:<prerequisite rules>
\t<command1>
\t<command2>
OBJS = main.o \
src1.o
..
TARGET=test
build: $(OBJS)
gcc -o $(TARGET) $(OBJS)
Predefined Rules
%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
rebuild: clean build
clean:
rm -rf $(OBJS)
rm -rf $(TARGET)
Automatic Variables
%< : The name of the first prerequisite
%@ : The name of the target
20/22
Makefile
21/22
Reference
• [1]http://www.gnu.org/software/make/
manual/make.html
22/22
Last class
• Interrupts, traps, signals
• Introduction to processes
– Address space
– Process life-cycle
Tutorial on gdb
Project 1
Process Control Block
(PCB)
Process state
today
Process id
Program counter
CPU registers
Memory limits
List of open files
...
later
•
•
•
•
•
•
Process state
Process id (PID)
Program counter
CPU registers
CPU scheduling information
Memory-management
information
• Accounting information
• I/O status information
CPU switch among processes
Enumerating # Possible
CPU Multiplexing
Between Processes
• Consider two processes P1 and P2
– P1: n instructions
– P2: m instructions
– No jump instrictions => No loops
• How many unique executions are possible?
Data structs associated
with PCBs
• Queue of ready (runnable) processes
– Scheduler picks from these
• Queues of waiting (blocked) processes
– Sepearate queues for difference devices
• Sometimes PCB of exited process kept in memory
– Pop quiz: Why?
• All PCBs in a part of RAM reserved by the kernel
for itself and inaccessible to processes
– This part of RAM initialized during boot-up
Data Structure #1: PCB
Main Memory (RAM)
Process id
Program Counter
Other registers
Process state
OS
Processes
Ptr to linked list
…
• Can PCBs be swapped out?
– Depends on the OS design .. so sometimes YES
Running
Ready
Lock
Waiting
Disk
Timer interrupt
Running
Ready
Lock
Waiting
Disk
Running
Ready
Lock
Waiting
Disk
I/O call
Running
Ready
Lock
Waiting
Disk
Lets pick the second
process in the ready
queue
Running
OS (scheduler)
Ready
Lock
Waiting
Disk
Modern Kernels are
Re-entrant
Process 1
Process 1
Process 2
USER MODE
KERNEL MODE
Excp
Intr
Intr
One KCP
Kernel control paths
Time
Intr
•
Note: Not showing scheduler invocations
•
A re-entrant kernel is able to suspend the current running process even if it is in
the Kernel Mode
– Several processes may be in Kernel Mode at the same time
•
Usually one or more “kernel mode stacks” used when in kernel mode
– Kept in kernel’s address space
Re-entrant Kernels
Process 1
Process 1
Process 2
USER MODE
KERNEL MODE
Excp
Intr
Intr
Kernel control paths
Time
Intr
•
Note: Not showing scheduler invocations
• Why re-entrancy?
– Improves throughput of devices controllers that raise interrupts
– Allows priorities among interrupts
One KCP
Relationships among
processes
• Several relatives of P recorded in its PCB
– real_parent
• Process that called fork to create P
• Or init (process 1)
– parent
• Usually real_parent
– Kernel signals this parent process when child exits
• Or process that issues ptrace () to monitor P
• Pop quiz: If you run a background process and exit the shell, who is the
parent of the process?
– children
– siblings
• Why maintain these?
Creating Processes
• fork ()
• Take 1: Used in older kernels
– Create a copy of the entire address space of the
parent
– Create a new PCB for the new process
– Update parent, children, sibling pointers
– Place the new process in the ready queue
• S.L.O.W.
RAM
OS
Id=2000
State=ready
PCB of parent
Id=2001
State=ready
PCB of child
1. PCB with new
id created
2. Memory allocated for child
Process
calls fork
Initialized by copying over
from the parent
3. If parent had called wait,
it is moved to a waiting queue
4. If child had called exec,
its memory overwritten
with new code & data
Processes
Parent’s memory
Child’s memory
5. Child added to ready queue,
all set to go now!
Creating Processes
• fork ()
• Problems with Take 1
– Child rarely needs to read/modify ALL the
resources inherited from the parent
– Often, it immediately issues an execve() rendering
all the effort that went into copying the address
space useless!
Creating Processes: COW
• fork ()
• What modern kernels do to avoid this waste of
precious CPU time
– Use Copy-On-Write
– Basic idea: Postpone work
till the last minute
– Sounds familiar?
Think assignments, quizzes, …
Process Switch
• Suspend the current process and resume a
previously suspended process
– Also called context switch or task switch
Process Switch
Process 0
Involuntary/Voluntary
Process 1
Context_switch() {
A1
A2
B1
B2
}
B3
B4
Process Switch
• What does the kernel need to save when suspending a
process?
– Hint: The entire address space is already saved (either in memory or
on swap space). What else would the process need when it has to be
resumed?
– CPU registers
• This is called the hardware context of the process
• Execution context, PC, pointers to elements within address
space, page table, etc.
Context_switch() {
push R0, R1, …
PCB[curr].SP = SP
PCB[curr].PT = PT
next = schedule()
// save regs on its stack
// save stack pointer
// save ptr(s) to address space
// find next process to run
PT = PCB[next].PT
SP = PCB[next].SP
pop Rn, … R0
return
}
// NOTE: Ctrl returns to another process
Overheads of Process Switch
• Direct
– The time spent switching context
• Indirect
– Cache pollution
– TLB flush
Download