Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar.

Similar presentations


Presentation on theme: "Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar."— Presentation transcript:

1 Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar

2 Teaching Assistant Name : Ohyoung Jang Office Hour : 2:30pm ~ 4:30pm, Mon Wed Location : 338E IST E-mail : oyj5007@cse.psu.eduoyj5007@cse.psu.edu 2/22

3 Contents Sample Program Compiling in Unix How to use gdb Makefile 3/22

4 Sample Program > #include 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; } > #include 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

5 Contents Sample Program Compiling in Unix How to use gdb Makefile 5/22

6 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

7 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

8 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

9 Compiling in Unix Easier example –Compile and Link in a command 9/22

10 Contents Sample Program Compiling in Unix How to use gdb Makefile 10/22

11 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 ” 11/22

12 How to use gdb gdb console Output of the program Program terminated. But process infos remains. Then type “bt” or “backtrace” 12/22

13 How to use gdb Print variable Error. Current frame is 0 Call stack with frame number Change from to 1 Print variable The bug is found 13/22

14 How to use gdb Another GDB commands –help [command] –b(reak) : set breakpoint b : [condition] b [condition] –Ex) b src1.c:15 if i == 10 –Ex) b compute_intsum –info b(reak) : list breakpoints –delete 14/22

15 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 16/22

17 Contents Sample Program Compiling in Unix How to use gdb Makefile 17/22

18 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

19 Makefile Usage –To build $make [build] –To clean $make clean –To rebuild $make rebuild > CC = gcc CFLAGS = -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

20 Makefile > CC = gcc CFLAGS = -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 : \t.. 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 : \t..

21 Makefile 21/22

22 Reference [1]http://www.gnu.org/software/make/ manual/make.html 22/22

23 Last class Interrupts, traps, signals Introduction to processes –Address space –Process life-cycle

24 Tutorial on gdb

25 Project 1

26 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

27 CPU switch among processes

28 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?

29 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

30 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

31 Ready Waiting Running Disk Lock

32 Ready Waiting Running Disk Lock Timer interrupt

33 Ready Waiting Running Disk Lock

34 Ready Waiting Running Disk Lock I/O call

35 Ready Waiting Running Disk Lock OS (scheduler) Lets pick the second process in the ready queue

36 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 1Process 2 Time ExcpIntr Kernel control paths One KCP

37 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 1Process 2 Time ExcpIntr Kernel control paths One KCP

38 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?

39 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.

40 Id=2000 State=ready PCB of parent RAM OS Processes Parent’s memory Process calls fork Id=2001 1. PCB with new id 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

41 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!

42 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, …

43 Process Switch Suspend the current process and resume a previously suspended process –Also called context switch or task switch

44 Process Switch Process 0 Process 1 A1 A2 B1 B2 B3 B4 Context_switch() { } Involuntary/Voluntary

45 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.

46 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 }

47 Overheads of Process Switch Direct –The time spent switching context Indirect –Cache pollution –TLB flush


Download ppt "Operating Systems CMPSC 473 Processes August 31, 2010 - Lecture 3 Instructor: Bhuvan Urgaonkar."

Similar presentations


Ads by Google