CS考试辅导

Recitation 14: Exam Review – Signals
Instructor: TA(s)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Copyright By PowCoder代写 加微信 powcoder

⬛ Proxylab
⬛ Final Exam
⬛ TA Applications ⬛ Signals
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

⬛ Proxylab is due Thursday (or late by Friday) ▪ No submissions will be accepted after Friday!
▪ Submit something, even if doesn’t pass everything
⬛ Worth almost a letter grade
⬛ Submit early
▪ Autolab may compile / run differently if you have undefined behavior or race conditions
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Final Exam Logistics
⬛ Online but in-person
⬛ 213/513 Fall 2021 Final Exam Review
▪ Time: 12:00 pm – 3:00 pm on December 4th
▪ Location: TBD
⬛ Monitor Piazza for more information about the final exam
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

So you wanna TA for 213?
⬛ What qualifications are we looking for?
○ Decent class performance, but also critical thinking skills
○ Like computer systems + want to help others like systems!
○ Have a reasonable ability to gauge your schedule +
responsibilities
○ Leadership potential! Take initiative, we love to see it 😌
○ Ability to tell students:
■ “Did you write your heap checker?”
■ “Run backtrace for me”
Apply at https://www.ugrad.cs.cmu.edu/ta/S21/
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signals and Handling Reminders
⬛ Signals can happen at any time
▪ Control when through blocking signals
⬛ Signals also communicate that events have occurred ▪ What event(s) correspond to each signal?
⬛ Write separate routines for receiving (i.e., signals) ▪ What can you do / not do in a signal handler?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signal Blocking
⬛ We need to block and unblock signals. Which sequence?
pid_t pid; sigset_t mysigs, prev;
sigemptyset(&mysigs);
sigaddset(&mysigs, SIGCHLD);
sigaddset(&mysigs, SIGINT);
// need to block signals. what to use?
// A. sigprocmask(SIG_BLOCK, &mysigs, &prev);
// B. sigprocmask(SIG_SETMASK, &mysigs, &prev);
if ((pid = fork()) == 0) {
// need to unblock signals. what to use?
/* A. sigprocmask(SIG_BLOCK, &mysigs, &prev);
* B. sigprocmask(SIG_UNBLOCK, &mysigs, &prev);
* C. sigprocmask(SIG_SETMASK, &prev, NULL);
* D. sigprocmask(SIG_BLOCK, &prev, NULL);
* E. sigprocmask(SIG_SETMASK, &mysigs, &prev);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signal Blocking
⬛ We need to block and unblock signals. Which sequence?
pid_t pid; sigset_t mysigs, prev;
sigemptyset(&mysigs);
sigaddset(&mysigs, SIGCHLD);
sigaddset(&mysigs, SIGINT);
// need to block signals. what to use?
// A. sigprocmask(SIG_BLOCK, &mysigs, &prev);
// B. sigprocmask(SIG_SETMASK, &mysigs, &prev);
if ((pid = fork()) == 0) {
// need to unblock signals. what to use?
/* A. sigprocmask(SIG_BLOCK, &mysigs, &prev);
* B. sigprocmask(SIG_UNBLOCK, &mysigs, &prev);
* C. sigprocmask(SIG_SETMASK, &prev, NULL);
* D. sigprocmask(SIG_BLOCK, &prev, NULL);
* E. sigprocmask(SIG_SETMASK, &mysigs, &prev);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signal Blocking
⬛ We need to block and unblock signals. Which sequence?
pid_t pid; sigset_t mysigs, prev;
sigemptyset(&mysigs);
sigaddset(&mysigs, SIGCHLD);
sigaddset(&mysigs, SIGINT);
// need to block signals. what to use?
// A. sigprocmask(SIG_BLOCK, &mysigs, &prev);
// B. sigprocmask(SIG_SETMASK, &mysigs, &prev);
if ((pid = fork()) == 0) {
// need to unblock signals. what to use?
/* A. sigprocmask(SIG_BLOCK, &mysigs, &prev);
* B. sigprocmask(SIG_UNBLOCK, &mysigs, &prev);
* C. sigprocmask(SIG_SETMASK, &prev, NULL);
* D. sigprocmask(SIG_BLOCK, &prev, NULL);
* E. sigprocmask(SIG_SETMASK, &mysigs, &prev);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signal Blocking cont.
⬛ Someone implemented the wrong choices. Which signals are now blocked?
pid_t pid; sigset_t mysigs, prev;
sigemptyset(&mysigs);
sigaddset(&mysigs, SIGCHLD);
sigaddset(&mysigs, SIGINT);
sigprocmask(SIG_SETMASK, &mysigs, &prev);
// What is blocked?
if ((pid = fork()) == 0) {
sigprocmask(SIG_BLOCK, &prev, NULL);
// What is blocked?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signal Queuing
⬛ How many times is the handler invoked? void handler(int sig)
sigset_t mysigs, prev;
signal(SIGUSR1, handler);
sigemptyset(&mysigs);
sigaddset(&mysigs, SIGUSR1);
sigprocmask(SIG_BLOCK, &mysigs, &prev);
kill(getpid(), SIGUSR1);
kill(getpid(), SIGUSR1);
sigprocmask(SIG_SETMASK, &prev, NULL);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signal Delivery
⬛ What can be printed?
⬛ When is a blocked signal delivered?
sigset_t mysigs, prev;
sigemptyset(&mysigs);
sigaddset(&mysigs, SIGINT);
sigprocmask(SIG_BLOCK, &mysigs, &prev);
pid_t pid = fork();
if (pid > 0) {
kill(pid, SIGINT);
sigprocmask(SIG_SETMASK, &prev, NULL);
printf(“A”);
kill(getppid(),SIGINT);
sigprocmask(SIG_SETMASK, &prev, NULL);
printf(“B”);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Signal Delivery
⬛ Child calls kill(parent, SIGUSR{1,2}) between 2-4 times. What sequence of kills may print 1?
Can you guarantee printing 2?
What is the range of values printed?
int counter = 0;
void handler (int sig) {
atomically {counter++;}
int main(int argc, char** argv) {
signal(SIGUSR1, handler);
signal(SIGUSR2, handler);
int parent = getpid(); int child = fork();
if (child == 0) {
/* insert code here */
exit(0); }
sleep(1); waitpid(child, NULL, 0);
printf(“Received %d USR{1,2} signals\n”, counter);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
See http://www.cs.cmu.edu/~213/oldexams/final-s07.pdf 13

Signal Delivery
⬛ Suppose the program is currently inside the signal handler, which signals are blocked?
⬛ Is this handler safe?
int counter = 0;
void handler (int sig)
counter++; }
int main(int argc, char** argv)
signal(SIGUSR1, handler);
signal(SIGUSR2, handler);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Final Exam Q&A
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Appendix: Thread Synchronization
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Appendix: Thread Synchronization (Contd.)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com