The data structure

advertisement
CSC369 – TUTORIAL 4
TA: Trevor Brown
Slides: http://www.cs.utoronto.ca/~tabrown/csc369/week4.ppt
UNIX-LIKE SIGNALS
 In Unix, the signal number represents a bit position in a 32bit flag in the thread struct
 Allows a thread to be sent multiple types of signals
 Positives?
 minimal memory space, no dynamic memory allocation needed
 just setting a bit is fast…
 Negatives?
 Signals can be lost. How?
 … if two of the same type are sent before the first is handled.
UNIX-LIKE SIGNALS
The data structure
 Create signal queue for each thread
 linked list of structs containing a field that identifies the signal type
 Get reliable signal delivery, but…
 Downsides:
 more memory
 more time to allocate a struct and queue it up
 What should you do?
 You are free to design your own mechanism, but
 For the signals we are supporting, losing a repeat is not a big deal.
 Most people should prefer the simpler design.
UNIX-LIKE SIGNALS
An alternative design?
 How do we send signals?
 Using the sys_kill() system call
 The signaler (the thread that call sys_kill()):
 Set a bit in the target thread
 Error check and return
 What kinds of things do we do to error check?
 Returning: copyin/out
 The signaled thread:
 Checks its signal flag before returning to user space
 Handles any signals that are marked
UNIX-LIKE SIGNALS
The algorithm – how does it work?
 SIGKILL
 Order a thread to roll over and die
 SIGSTOP
 Order a thread to go to sleep
 Sleeping beauty style: must wait for prince SIGCONT to wake
 SIGCONT
 Wake up a thread put to sleep with SIGSTOP
UNIX-LIKE SIGNALS
Some examples of signals
 Funny cases are design decisions
 They should appear in your design document!
 What else should appear in your design doc.?
 Any time you choose among several algorithms, why?
 How does a thread wait for SIGCONT after getting SIGSTOP?
 One way: use a condition variable
 Recall, a condition variable supports operations:
 wait() (suspend the invoking process and release the lock)
 signal() (resume exactly one suspended process)
 broadcast() (resumes all suspended processes)
 We said a signaled thread checked and handles signals
before returning to user space…
 What if a thread receives a signal in kernel space?
 Should a thread immediately die if it receives SIGKILL?
UNIX-LIKE SIGNALS
Funny cases and design decisions
ASSIGNMENT 2
In a nut-shell:
 Check out the new code (“…/a2” instead of “…/a1”)
 Implementing parts of the PID system
 noting that pid.c is a monitor which protects access to the PID array!
 Implementing missing synchronization functions




thread_join
thread_detach
thread_exit
thread_fork
 These must work together!
 Implementing missing system calls
 getpid
 waitpid
 kill
ASSIGNMENT 2
What are you doing in A2?
ASSIGNMENT 2
Old slides 1/5
ASSIGNMENT 2
Old slides 2/5
ASSIGNMENT 2
Old slides 3/5
ASSIGNMENT 2
Old slides 4/5
ASSIGNMENT 2
Old slides 5/5
 Look at the new code in kern/thread
 Search functionality in an IDE (e.g., NetBeans) makes
browsing code (and finding functionality) much easier
 Be very thorough. If one person implements, the other
should code-read and test.
 Read the man pages so you understand the parameters
received and errors returned.




Reminders:
pid.c serves as a monitor to an important data structure
getpid and waitpid are user level tools
thread_join and thread_detach are kernel level tools
 http://www.cdf.toronto.edu/~csc369h/fall/assignments/a2/a2.shtml
ASSIGNMENT 2
Some tips for A2
Download