Software Systems Spring 2008 For today, you should have: 1) prepared for a quiz 2) done the next LBoS puzzle 3) read from Tanenbaum 4) read from the Cow book Outline: 0) Announcement: in-class exams Feb 25 and Apr 7. 1) quiz 2) LBoS 3) reading questions from last time 4) processes 5) address spaces For next time: 1) reading questions below 2) next semaphore puzzle 3) read Cow Book chapter 5 and do programming exercise 5-2 LBoS ---- Rendezvous problem: Three solutions on page 15: good, ok, bad. 1) Download and install Swampy from allendowney.com/swampy 2) Navigate into the Swampy directory 3) wget http://wb/ss/code/rendez.py 4) python Sync.py rendez.py 5) use Sync to simulate the execution of the program 6) modify the code to make the OK solution and run it 7) modify the code to make the bad solution and run it The process abstraction ----------------------- What is a process? 1) a program while it is running 2) a program together with its data and hardware state If you have 3 windows running emacs, there are three processes running the same program. At a Linux prompt type ps aux to see a list of processes, and man ps to read about the details. To see which processes are using the most CPU, type top (type m to sort by memory use) (and then q to exit) Multiprogramming/timesharing ---------------------------- Of all the programs "running" on your computer, only one (at most) is actually executing instructions. The others are suspended. The OS has the ability to 1) interrupt a running process, 2) save its state 3) resume a suspended process. This is called a "context switch", and it happens 10-1000 times a second. 1) If the number of processes that need CPU time is relatively small, and 2) they all get a reasonable share of the CPU, then 3) the system behaves as if the processes ran in parallel! A CPU model ----------- To run a program, you have to (somehow) get a program into memory, and load the address of the beginning of the program into a special register called the PC (program counter). Then: 1) send the address in the PC to the memory module 2) get an instruction back and put it in the IR (instruction register) 3) decode the instruction and execute it, which may involve a) reading values from registers or memory b) performing arithmetic computations c) writing results to registers or memory d) changing the PC 4) increment the PC and go to (1) Why is booting called "booting"? As the CPU runs, it generates a sequence of memory reads and writes. Some of these accesses are intercepted by the caches, but this optimization is invisible to the running program. The address space ----------------- A process is a program PLUS data and hardware state. How is the data organized? Let's do an experiment. wget http://wb/ss/code/address_space.tgz tar -xzf address_space.tgz cd address_space make aspace ./aspace Read it, run it, and draw a picture of where the various variables and objects are in memory. #include #include // needed for malloc typedef struct { int num, den; } Rational; int global; void recurse (int n) { int a[4000]; Rational *rv = (Rational *) malloc (1000 * sizeof (Rational)); printf ("Address of a is 0x%.8x\n", &a[0]); printf ("Address of rv is 0x%.8x\n", rv); if (n > 0) recurse (n-1); } int main () { int x = 5; Rational *r1 = (Rational *) malloc (sizeof (Rational)); printf ("Address of x is 0x%.8x\n", &x); printf ("Address of global is 0x%.8x\n", &global); printf ("Address of main is 0x%.8x\n", main); printf ("Address of r1 is 0x%.8x\n", r1); recurse (1); return 0; } Makefile -------- CFLAGS = -g aspace: aspace.c gcc ${CFLAGS} -o aspace aspace.c sleep: sleep.c gcc ${CFLAGS} -o sleep sleep.c clean: rm *~ *.o aspace sleep Reading ------- Tanenbaum Chapter 2, pages 71-80 1) What is the difference between a program and a process? 2) If a program contains two consecutive statements, A and B, how much real time will elapse between the execution of A and the execution of B? 3) What's a daemon? 4) What is an address space? (you might have to consult the index) 5) How do processes end? 6) In UNIX, what is the first process to execute, and how does it get loaded? 7) What states can a process be in, and what events cause a process to transition from one state to another? 8) What are the entries in the process table? What information is stored in an entry? 9) What is an interrupt? What is the interrupt vector? 10) When an interrupt occurs, how does the hardware state of the running process get saved?