160 likes | 166 Views
CS 241 Section Week #10 (04/01/10) . Topics This Section. MP5 Overview Signals. MP5 Overview. MP5 looks at a modern programming paradigm that was popularized by Google called “MapReduce”.
E N D
Topics This Section • MP5 Overview • Signals
MP5 Overview • MP5 looks at a modern programming paradigm that was popularized by Google called “MapReduce”. • Specifically, you’ll be gathering statistics on newsgroup files via multiple processes and delivering them back to the MONITOR, a piece of code we provide for you.
MP5 Overview • You have three key resources to help you understand and program MP5: • The README.txt • A “high-level” README • (PDF, found in the announcement newsgroup) • A presentation entirely focused on MP5 details • (PPT/Powerpoint, found in the announcement newsgroup)
MP5: Warnings! • Closing your shared memory is IMPORTANT! • Shared memory is a system-wide resource, not a user-wide resource. • Excessive open shared memory resources can cause problems in other programs – don’t do it! • When you’re done programming for the night, run: • ./cleanmemory.sh • and make sure your program is running:shmctl(shmid, IPC_RMID, &buf);
MP5: Warnings! • Ensuring you’re not fork-bombing is CRITICAL! • If you’re running fork() inside a loop, or if your fork()’d program ended up inside an infinite loop, MAKE SURE your loops terminates. • (Test your program with printf() calls before you add fork() calls.) • Before running your program, you can run: • bash(unless you’re already using bash) • ulimit -u 64(set your session max processes)
MP5: Warnings! • If you have excessive processes after your program exits: • ps -af | grep NetID: • wfagen2 19283 19281 0 21:37 pts/117 00:00:00 ./mapreduce • wfagen2 20324 20322 0 21:41 pts/117 00:00:00 ./mapreduce • wfagen2 22687 22685 0 21:51 pts/117 00:00:00 ./mapreduce • kill -9 the processes you want to kill: • kill -9 19283 • kill -9 20324 • kill -9 22687
Signals • As you learned in lecture, signals are simply asynchronous notifications delivered to your program. • Every signal that is delivered must be acted upon! • By default, every signal has a “default action” • As a programmer, you can redefine what action some of the signals perform when they’re delivered.
Example Signals • You already use signals all the time: • SIGINT: SIGnal to INTerrupt execution. • (Ctrl+C will sent this signal to the currently running process in a shell window.) • Default Action: Terminate Process • SIGSEGV: Signal of invalid memory reference. • (This is the signal the kernel sends to your program when a segmentation fault occurs.) • Default Action: Terminate Process
Signals • There are two primary ways of changing the default behavior of signals: • Signal Handler: • Run a specific piece of code when a specific signal is delivered. • Function Calls: sigaction(), signal() • Block Signal: • Acknowledges the signal, possibly blocking until it’s received. • Function Calls: sigwait(), sigsuspend(), sigprocmask()
Signal Sets • Many of the signal calls require the use of a “signal set” (sigset_t *set parameter). • This “signal set” works like a mathematical set, where you can either start with: • a set of all signals (sigfullset()) • OR a set of no signals (sigemptyset()) • And you can: • add signals to the set (sigaddset()) • OR remove signals (sigremoveset())
Signal Magic • You have everything you need to program a signal!
Signal Example • Goal: Run a function upon receiving SIGINT. • struct sigaction action; • action.sa_handler = my_function; • sigemptyset (&action.sa_mask); • action.sa_flags = 0; • sigaction(SIGINT, &action, NULL);
Signal Example • Goal: Block SIGINT from terminating the program (but don’t run any function). • sigset_t set, oldset; • sigemptyset(&set); • sigaddset(&set, SIGINT); • sigprocmask(SIG_BLOCK, &set, &oldset);
Signal Magic • Like many things in C, there are multiple ways to accomplish the same end result. • In MP5, you’ll need to both send signals and catch signals. • You’ll find the provided MONITOR will help you out with some signal code. • Lecture and these slides also provide great resources.