TTIT61: Process Programming and Operating Systems Sergiu Rafiliu

advertisement
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
Download