Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar.
-
Upload
rosa-bradley -
Category
Documents
-
view
216 -
download
0
Transcript of Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar.
Operating SystemsCMPSC 473
ProcessesAugust 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 : [email protected]/22
Contents
• Sample Program• Compiling in Unix• How to use gdb• Makefile
3/22
Sample Program<< 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;
for (i = 1; i <= n; i++) { sum += i; }
return sum;}
<< main.c >>#include <stdio.h>#include <string.h>
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);
strcpy(name1, name2); print_hello(name1);
return 0;}
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
Print variableError.Current frame is 0
Call stackwith frame number
Change from to 1
Print variable
The bug is found13/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 = gccCFLAGS = -O0 -g
OBJS = main.o \ src1.o
TARGET=test
build: $(OBJS) gcc -o $(TARGET) $(OBJS)
rebuild: clean build
clean: rm -rf $(OBJS) rm -rf $(TARGET)
Variables
Rules
19/22
Makefile<< Makefile >>
CC = gccCFLAGS = -O0 -g
OBJS = main.o \ src1.o
TARGET=test
build: $(OBJS) gcc -o $(TARGET) $(OBJS)
rebuild: clean build
clean: rm -rf $(OBJS) rm -rf $(TARGET)
Define Rules<target name>:<prerequisite rules>\t<command1>\t<command2>
..
Predefined Rules%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
Automatic Variables%< : The name of the first prerequisite%@ : The name of the target
20/22
Define Rules<target name>:<prerequisite rules>\t<command1>\t<command2>
..
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• Process id (PID)• Program counter• CPU registers• CPU scheduling information
• Memory-management information
• Accounting information• I/O status information
Process state
. . .
Process id
Program counter
CPU registers
Memory limits
List of open files
today
later
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
• Can PCBs be swapped out?– Depends on the OS design .. so sometimes YES
Process id
Program Counter
…
Other registers
Process state
Ptr to linked list
Main Memory (RAM)
OS
Processes
Ready
Waiting
Running
Disk
Lock
Ready
Waiting
Running
Disk
Lock
Timer interrupt
Ready
Waiting
Running
Disk
Lock
Ready
Waiting
Running
Disk
Lock
I/O call
Ready
Waiting
Running
Disk
Lock
OS (scheduler)
Lets pick the secondprocess in the readyqueue
Modern Kernels are
Re-entrant
• 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
Process 1
USER MODE
KERNEL MODE
Process 1 Process 2
Time
Excp Intr
Intr
Intr
Kernel control paths
One KCP
Re-entrant Kernels
• Note: Not showing scheduler invocations
• Why re-entrancy?– Improves throughput of devices controllers that raise
interrupts– Allows priorities among interrupts
Process 1
USER MODE
KERNEL MODE
Process 1 Process 2
Time
Excp Intr
Intr
Intr
Kernel control paths
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 .
Id=2000State=ready
PCB of parent
RAM
OS
ProcessesParent’s memory
Processcalls fork
Id=2001 1. PCB with newid created
2. Memory allocated for child
Initialized by copying over from the parent
Child’s memory
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
5. Child added to ready queue, all set to go now!
State=ready
PCB of child
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 Process 1
A1A2
B1B2
B3
B4
Context_switch() {
}
Involuntary/Voluntary
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, … // save regs on its stack PCB[curr].SP = SP // save stack pointer PCB[curr].PT = PT // save ptr(s) to address space next = schedule() // 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