operating systems cmpsc 473 processes august 31, 2010 - lecture 3 instructor: bhuvan urgaonkar
TRANSCRIPT
![Page 1: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/1.jpg)
Operating SystemsCMPSC 473
ProcessesAugust 31, 2010 - Lecture 3
Instructor: Bhuvan Urgaonkar
![Page 2: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/2.jpg)
Teaching Assistant
• Name : Ohyoung Jang
• Office Hour : 2:30pm ~ 4:30pm, Mon Wed
• Location : 338E IST
• E-mail : [email protected]/22
![Page 3: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/3.jpg)
Contents
• Sample Program• Compiling in Unix• How to use gdb• Makefile
3/22
![Page 4: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/4.jpg)
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
![Page 5: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/5.jpg)
Contents
• Sample Program• Compiling in Unix• How to use gdb• Makefile
5/22
![Page 6: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/6.jpg)
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
![Page 7: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/7.jpg)
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
![Page 8: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/8.jpg)
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
![Page 9: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/9.jpg)
Compiling in Unix
• Easier example– Compile and Link in a command
9/22
![Page 10: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/10.jpg)
Contents
• Sample Program• Compiling in Unix• How to use gdb• Makefile
10/22
![Page 11: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/11.jpg)
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
![Page 12: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/12.jpg)
How to use gdb
gdb console
Output of the program
Program terminated. But process infos remains. Then type “bt” or “backtrace” 12/22
![Page 13: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/13.jpg)
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
![Page 14: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/14.jpg)
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
![Page 15: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/15.jpg)
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
![Page 16: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/16.jpg)
16/22
![Page 17: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/17.jpg)
Contents
• Sample Program• Compiling in Unix• How to use gdb• Makefile
17/22
![Page 18: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/18.jpg)
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
![Page 19: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/19.jpg)
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
![Page 20: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/20.jpg)
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>
..
![Page 21: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/21.jpg)
Makefile
21/22
![Page 22: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/22.jpg)
Reference
• [1]http://www.gnu.org/software/make/manual/make.html
22/22
![Page 23: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/23.jpg)
Last class• Interrupts, traps, signals• Introduction to processes
– Address space– Process life-cycle
![Page 24: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/24.jpg)
Tutorial on gdb
![Page 25: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/25.jpg)
Project 1
![Page 26: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/26.jpg)
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
![Page 27: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/27.jpg)
CPU switch among processes
![Page 28: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/28.jpg)
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?
![Page 29: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/29.jpg)
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
![Page 30: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/30.jpg)
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
![Page 31: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/31.jpg)
Ready
Waiting
Running
Disk
Lock
![Page 32: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/32.jpg)
Ready
Waiting
Running
Disk
Lock
Timer interrupt
![Page 33: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/33.jpg)
Ready
Waiting
Running
Disk
Lock
![Page 34: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/34.jpg)
Ready
Waiting
Running
Disk
Lock
I/O call
![Page 35: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/35.jpg)
Ready
Waiting
Running
Disk
Lock
OS (scheduler)
Lets pick the secondprocess in the readyqueue
![Page 36: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/36.jpg)
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
![Page 37: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/37.jpg)
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
![Page 38: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/38.jpg)
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?
![Page 39: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/39.jpg)
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 .
![Page 40: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/40.jpg)
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
![Page 41: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/41.jpg)
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!
![Page 42: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/42.jpg)
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, …
![Page 43: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/43.jpg)
Process Switch• Suspend the current process and resume a previously suspended process– Also called context switch or task switch
![Page 44: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/44.jpg)
Process Switch
Process 0 Process 1
A1A2
B1B2
B3
B4
Context_switch() {
}
Involuntary/Voluntary
![Page 45: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/45.jpg)
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.
![Page 46: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/46.jpg)
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}
![Page 47: Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar](https://reader035.vdocuments.net/reader035/viewer/2022062422/56649f205503460f94c385f8/html5/thumbnails/47.jpg)
Overheads of Process Switch
• Direct– The time spent switching context
• Indirect– Cache pollution– TLB flush