Overview TTIT61: Process Programming and Operating Systems Introduction to C Introduction to Pintos Laboratory Assignment 1 Sergiu Rafiliu serra@ida.liu.se phone: 282281, room: B 329:228 serra@ida.liu.se -2- Lab schedule 1. Introduction to Pintos, Laboratory Assignment 1 : Presentation 3 lab sessions for Lab 00, 0 and 1 deadline* : 2008-jan-23 2. Laboratory Assignment 2 : Presentation 6 lab sessions for Lab 2 deadline* : 2008-feb-15 3. Laboratory Assignment 3,4 : Presentation 6 lab sessions for Lab 2 deadline* : 2008-mar-04 4. Questions & Answers * TTIT61 Lab lesson Lab Rules 1. Every member of the group participate equally and every member should be able to answer questions related to labs 2. Before doing the lab answer on “preparatory questions” 3. Understand first, then implement 4. Try to find ”bugs” yourself at first 5. No copying of source code from any sources, no cheating This deadlines are not mandatory, they only offer a guideline of how you should schedule your time serra@ida.liu.se -3- TTIT61 Lab lesson serra@ida.liu.se -4- Tips Read documentation and source code Pintos Documentation (html) : http://www.scs.stanford.edu/07aucs140/pintos/pintos.html#SEC_Contents (pdf) : http://www.ida.liu.se/~TTIT61/tutorials/pintos.pdf Use a debugger (gdb / ddd) http://www.gnu.org/manual/ddd/ Use code browsing tools (emacs, grep, eclipse) Use different test cases Check the homepage regularly serra@ida.liu.se -5- TTIT61 Lab lesson TTIT61 Lab lesson Introduction to C Pointers Passage of arguments to functions Guidelines for modifying code serra@ida.liu.se -6- TTIT61 Lab lesson Pointers - Default A pointer is a numeric variable and must be declared and initialized before it can be used. data_type *pointer_variable_name; int location; /* declare a pointer variable, m of type int */ int *m; /* assign the address of variable location to variable m, so pointer m is pointing to variable location */ m = &location; /* the actual data assigned to variable location */ location = 200; serra@ida.liu.se TTIT61 Lab lesson -7- Pointers - Arrays char char char char *a = ’’slightly less boring string’’; *b = ’’foo bar’’; *ptr; c = ‘A’; *ptr = c; ptr = &c; *ptr = ‘B’; ptr = c; b = a; ptr = a; ptr++; (*ptr)++; ++(*ptr); *(ptr++); *(++ptr); c = a[5]; c = *(a+5); c = 5[a]; /* /* /* /* /* /* /* /* /* /* /* /* /* /* error */ correct */ c = ‘B’ */ error */ error */ correct, ptr points to a[0] */ returns a[0] increments to a[1] */ returns a[1], increments a[1] value to ‘m’ */ increments a[1] value to ‘n’ and returns it */ returns a[1], and increments to a[2] */ increments to a[3] and returns it */ c = ‘t’ */ c = ‘t’ */ correct/incorrect ? c = ? */ serra@ida.liu.se Pointers - Structures struct a { ... int i; ... }; struct b { ... struct a vr; struct a *ptr; int j; ... }; va.i = 5; vb.var.i = 6; ptrstr = &vb; vb.ptr = &va; vb.ptr->i = 7; (*vb.ptr).i = 6; ptrstr->ptr->i = 3; (*(*ptrstr).ptr).i = 5; (*ptrstr->ptr).i--; va.i = ptrstr->vr.i++; TTIT61 Lab lesson -8- Passage of arguments to functions Two types of argument passage : /* /* /* /* /* /* va.i = 7 */ va.i = 6 */ va.i = 3 */ va.i = 5 */ va.i = 4 */ va.i = 6, vb.var1.i = 7*/ Through value Through address struct b vb; struct b *ptrstr; struct a va; serra@ida.liu.se TTIT61 Lab lesson -9- serra@ida.liu.se Passage of arguments to functions TTIT61 Lab lesson -10- Passage of arguments to functions val(a) <- val(X) Through value 0000 a …. 5 int func (int a) { a = a+1; return a; } . . . int x, y; x =5; y = func(x); printf(’’%d’’, x); serra@ida.liu.se X 5 a <- x …. ffff 0000 a …. X int func (int *a) { (*a) = (*a)+1; return (*a); } . . . int x, y; x =5; y = func(&x); printf(’’%d’’, x); /* y = 6 */ /* x = 5 */ -11- Through address TTIT61 Lab lesson serra@ida.liu.se x 5 …. ffff /* y = 6 */ /* x = 6 */ -12- TTIT61 Lab lesson Guidelines for Modifying the Code Laboratory Sessions Marking changes SU rooms Solaris C Tools: emacs(or another editor), make(gmake), ddd, man Mark your changes in the source code serra@ida.liu.se -13- TTIT61 Lab lesson /* -----------------------------------------------07-01-24: your_name: changed ... end of changes -----------------------------------------------*/ serra@ida.liu.se -14- Guidelines for Modifying the Code Guidelines for Modifying the Code Function header File header /* ========================================== TTIT61: Lab 1: solution.cpp -----------------------------------------------Group XYZ: -----------------------------------------------This module extends Nachos to be able to work with semaphores. -----------------------------------------------07-02-01: XYZ: created functin F 07-02-12: XYZ: semSignal: changed signal type ========================================= /* -----------------------------------------------int readInput(int file) -----------------------------------------------This function reads continuously from the file and returns the number of bytes read -----------------------------------------------Parameters: file: handle of the file to read from ------------------------------------------------ */ */ serra@ida.liu.se -15- TTIT61 Lab lesson serra@ida.liu.se Introduction to Pintos Descendent form Nachos (“Not Another Completely Heuristic Operating System”) Free simulation environment to study an operating system (QEMU) Runs as a single Unix process (real operating systems run on bare machines) Simulates low-level facilities (including interrupts, virtual memory and interrupt-driven device I/O) Implemented in C (both kernel and user programs) See homepage material and links Browse the source code !!!!!!! serra@ida.liu.se TTIT61 Lab lesson -17- TTIT61 Lab lesson -16- TTIT61 Lab lesson General Description of Labs Lab 00: “Introduction to C Programming” Checks your ability to complete the labs Lab 0: “Introduction and installation” Introductory lab, where you need to setup your program environment and try out debugging Lab 1: “Timers, threads, interrupts and synchronization” First “real” lab Timers Threads and interrupts Synchronization mechanisms serra@ida.liu.se -18- TTIT61 Lab lesson General Description of Labs Lab 2: “System calls” System calls Single user program Memory issues Console Lab 3: “Execution, termination and synchronization of user programs” Handling program arguments Execution of several user programs Termination of a user program Synchronization of shared data structures Wait system call serra@ida.liu.se -19- TTIT61 Lab lesson General Description of Labs Lab 4: “File system” Synchronization of read-write operations serra@ida.liu.se -20- TTIT61 Lab lesson Lab 00 http://www.ida.liu.se/~TTIT61/labs/laboration00.sv.shtml Meant to test your C coding skills. If you are not able to complete the lab, then you should focus on C before you proceed further or even consider to take the course next year. No formal pass/fail or other regulations though… Lab 0 http://www.ida.liu.se/~TTIT61/labs/laboration0.sv.shtml Install, run and debug Pintos Tasks : Set up the program environment Tasks : Debug a small program “debugthis.c” with DDD after compiling it with GCC Create a linked list (will be helpful later!) module initadd ~TDDB68/labs/modules/pintos module add ~TDDB68/labs/modules/pintos Copy Pintos to your directory gzip -cd ~TDDB68/labs/pintos_ida.tar.gz | tar xvf - \ Learn how to browse the source code serra@ida.liu.se -21- TTIT61 Lab lesson serra@ida.liu.se -22- TTIT61 Lab lesson Lab 0 Tasks : Tasks : Compile and build Pintos Debugging Note that only breakpoints can be inserted and only “Continue” command of DDD works since Pintos is already running. Other commands would terminate Pintos! Test if Pintos works cd build pintos --qemu -- run alarm-multiple -23- (from build) pintos --qemu --gdb -- run testname ddd --gdb --debugger pintos-gdb kernel.o& cd pintos/src/threads gmake serra@ida.liu.se Lab 0 TTIT61 Lab lesson serra@ida.liu.se -24- TTIT61 Lab lesson Lab 1 http://www.ida.liu.se/~TTIT61/labs/laboration1.sv.shtml Timers Threads and interrupts Synchronization mechanisms serra@ida.liu.se -25- TTIT61 Lab lesson Lab1 - Timers Used to keep track of time A device sends an external interrupt to the processor at fixed intervals of time The processor executes a piece of code that updates a counter every time such an interrupt appears. serra@ida.liu.se Lab 1 – Threads and Interrupts -26- TTIT61 Lab lesson Lab 1 – Threads and Interrupts CPU Threads Process: a user program in execution Thread (Pintos): the kernel correspondent of a user process •Internal Thread 1 Thread 2 Thread 3 OS KERNEL Interrupts Process 1 Process 2 Process 3 serra@ida.liu.se -27- Kernel thread TTIT61 Lab lesson Lab 1 – Synchronization Mechanisms locked A shared resource is a resource used by several threads, it can be a collection of datas or a piece of cod. locked Only one thread is allowed to use the Shared resource at a given time, no context resource switching with other threads using the same resource are allowed. serra@ida.liu.se -29- The kernel thread is called to make the context switch between the two threads Test Testprograms programs Manny threads … one CPU? TTIT61 Lab lesson serra@ida.liu.se •External -28- TTIT61 Lab lesson Lab 1 – Synchronization and Mutual Exclusion Mutual exclusion achieved by disabling / enabling interrupts Synchronization mechanisms : Semaphores Locks Condition variables serra@ida.liu.se -30- TTIT61 Lab lesson Lab 1 – Semaphores Semaphore: Init : sema_init (constructor) P : sema_down (decrement semaphore’s counter, blocking caller if 0) V : sema_up (increment semaphore’s counter, releasing one thread if any are blocked) A semaphore is an non-negative integer variable, accessible only by the operations P and V (Claim, and Release) Wait(S): while S <= 0 do no-op; S := S -1 ; Lab 1 – Locks A binary semaphore Checks that only the thread that acquired the lock can release it Init : lock_init Acquire : lock_acquire (acquire lock, blocking caller if the lock is acquired ) Release : lock_release (release the lock, unblocking one thread waiting for the lock) Signal(S): S := S + 1 ; serra@ida.liu.se -31- TTIT61 Lab lesson Lab 1 – Condition Variables (Wait) Wait : cond_wait -32- TTIT61 Lab lesson Lab 1 – Condition Variables (Signal & Broadcast) Signal : cond_signal Release the lock Add the thread to a queue of threads waiting on this condition Go to sleep Acquire the lock serra@ida.liu.se serra@ida.liu.se -33- TTIT61 Lab lesson Take one thread from the list Run it (scheduler->ReadyToRun(that thread)) Broadcast : cond_broadcast Wake up all the threads and let them compete for the lock serra@ida.liu.se -34- Lab 1::PartA Task : TTIT61 Lab lesson Lab 1::PartA If the thread calls timer_sleep() then its execution is suspended for (at least) ticks ticks If the system is idle, then the thread should be awaken after exactly ticks ticks Do not preempt other processes Re-implement timer_sleep(int64_t ticks) Understand the source code in Hint 1: Implement a queue for sleeping processes device/timer.c threads/thread.[h|c] device/timer.c Hint 2: Modify other functions or add your own code in timer.c and timer.h files. TIMER_FREQ defines how many ticks there are per second (defined in devices/timer.h) tests/threads/simplethreadtest.c can help you to understand how to create and use threads in Pintos. serra@ida.liu.se -35- TTIT61 Lab lesson serra@ida.liu.se -36- TTIT61 Lab lesson Lab 1::PartB Lab 1::PartA Run tests Task : alarm-single alarm-multiple alarm-simultaneous alarm-zero alarm-negative Write the code for BoundedBuffer data structure. Case 1: buffer is empty Case 2: buffer is full pintos --qemu -- run alarm-simultaneous Understand the source code in serra@ida.liu.se -37- TTIT61 Lab lesson threads/synch.[h|c] threads/synchlist.[h|c] threads/boundedbuffer.[h|c] serra@ida.liu.se Lab 1::PartB::BoundedBuffer An array with a fixed number of available positions to store data Used for communication between threads A thread produces data and puts it in the buffer A thread consumes data by reading data from buffer -38- TTIT61 Lab lesson Lab 1::PartB::BoundedBuffer How does it work? Look at it as a circular buffer Treat the buffer as an array and three indexes, in, out, and count Synchronization! serra@ida.liu.se -39- TTIT61 Lab lesson Lab 1::PartB::BoundedBuffer What happens if a consumer tries to read data from an empty buffer? What happens if a producer tries to put data in a full buffer? Note! The buffer is a shared resource serra@ida.liu.se -41- TTIT61 Lab lesson serra@ida.liu.se -40- TTIT61 Lab lesson