csc369 – tutorial 4

15
CSC369 – TUTORIAL 4 TA: Trevor Brown Slides: http://www.cs.utoronto.ca/~tabrown/csc369/week4.ppt

Upload: gram

Post on 22-Jan-2016

58 views

Category:

Documents


0 download

DESCRIPTION

CSC369 – tutorial 4. TA: Trevor Brown Slides: http://www.cs.utoronto.ca/~tabrown/csc369/week4.ppt. UNIX-like Signals. The data structure. In Unix, the signal number represents a bit position in a 32-bit flag in the thread struct Allows a thread to be sent multiple types of signals - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC369 – tutorial 4

CSC369 – TUTORIAL 4TA: Trevor BrownSlides: http://www.cs.utoronto.ca/~tabrown/csc369/week4.ppt

Page 2: CSC369 – tutorial 4

UNIX-LIKE SIGNALS

Page 3: CSC369 – tutorial 4

UN

IX-LIKE SIGN

ALSThe data structure

In Unix, the signal number represents a bit position in a 32-bit 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.

Page 4: CSC369 – tutorial 4

UN

IX-LIKE SIGN

ALSAn alternative design?

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.

Page 5: CSC369 – tutorial 4

UN

IX-LIKE SIGN

ALSThe algorithm – how does it work?

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

Page 6: CSC369 – tutorial 4

UN

IX-LIKE SIGN

ALSSome examples of signals

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

Page 7: CSC369 – tutorial 4

UN

IX-LIKE SIGN

ALSFunny cases and design decisions

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?

Page 8: CSC369 – tutorial 4

ASSIGNMENT 2

Page 9: CSC369 – tutorial 4

ASSIGN

MEN

T 2What are you doing in A2?

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

Page 10: CSC369 – tutorial 4

ASSIGN

MEN

T 2Old slides 1/5

Page 11: CSC369 – tutorial 4

ASSIGN

MEN

T 2Old slides 2/5

Page 12: CSC369 – tutorial 4

ASSIGN

MEN

T 2Old slides 3/5

Page 13: CSC369 – tutorial 4

ASSIGN

MEN

T 2Old slides 4/5

Page 14: CSC369 – tutorial 4

ASSIGN

MEN

T 2Old slides 5/5

Page 15: CSC369 – tutorial 4

ASSIGN

MEN

T 2Some tips for A2

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