DOC PREVIEW
Princeton COS 217 - Signals and Writing Portable Programs

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Signals and Writing Portable Programs and Course Wrap Up Prof David August COS 217 1 Signals Event notification sent to a process at any time An event generates a signal OS stops the process immediately Signal handler executes and completes The process resumes where it left off Process movl pushl call foo addl movl handler signal 2 Signals Can Originate From Keyboard Ctrl C INT signal process terminates Ctrl Z TSTP signal process suspends Ctrl ABRT signal process dumps core Program itself Illegal memory reference SIGSEGV segmentation fault The kill and raise library functions Example send a signal to self if kill getpid SIGABRT exit 0 3 Signals Can Originate From Command Line kill signal PID Example kill INT 1234 y Send the INT signal to process with PID 1234 y Same as pressing Ctrl C if process 1234 is running If no signal specified the default is SIGTERM fg foreground On UNIX shells this command sends a CONT signal Resume execution of the process that was suspended with Ctrl Z or a command bg See man pages for fg and bg 4 Predefined and Defined Signals Find out the predefined signals kill l HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS RTMIN RTMIN 1 RTMIN 2 RTMIN 3 RTMAX 3 RTMAX 2 RTMAX 1 RTMAX Applications can define their own signals An application can define signals with unused values 5 Some Predefined Signals in UNIX define define define define define define define define define define define define define define define define define define define define define SIGHUP SIGINT SIGQUIT SIGILL SIGTRAP SIGABRT SIGFPE SIGKILL SIGUSR1 SIGSEGV SIGUSR2 SIGPIPE SIGALRM SIGTERM SIGCHLD SIGCONT SIGSTOP SIGTSTP SIGTTIN SIGTTOU SIGPROF 1 2 3 4 5 6 8 9 10 11 12 13 14 15 17 18 19 20 21 22 27 Hangup POSIX Interrupt ANSI Quit POSIX Illegal instruction ANSI Trace trap POSIX Abort ANSI Floating point exception ANSI Kill unblockable POSIX User defined signal 1 POSIX Segmentation violation ANSI User defined signal 2 POSIX Broken pipe POSIX Alarm clock POSIX Termination ANSI Child status has changed POSIX Continue POSIX Stop unblockable POSIX Keyboard stop POSIX Background read from tty POSIX Background write to tty POSIX Profiling alarm clock 4 2 BSD 6 Signal Handling Signals have default handlers Usually terminate the process and generate core image Programs can over ride default for most signals Define their own handlers Ignore certain signals or temporarily block them Two signals are not catchable in user programs KILL y Terminate the process immediately y Catchable termination signal is TERM STOP y Suspend the process immediately y Can resume the process with signal CONT y Catchable suspension signal is TSTP 7 Installing A Signal Handler Predefined signal handlers SIG DFL Default handler SIG IGN Ignore the signal To install a handler use include signal h typedef void sighandler t int sighandler t signal int sig sighandler t handler Handler will be invoked when signal sig occurs Return the old handler on success SIG ERR on error On most UNIX systems after the handler executes the OS resets the handler to SIG DFL 8 Example Clean Up Temporary File Program generates a lot of intermediate results Store the data in a temporary file e g temp xxx Remove the file when the program ends i e unlink include stdio h char tmpfile temp xxx int main FILE fp fp fopen tmpfile rw fclose fp unlink tmpfile return 0 9 Solution Clean Up Signal Handler include stdio h include signal h include stdlib h char tmpfile temp xxx void cleanup void unlink tmpfile exit EXIT FAILURE int main void if signal SIGINT cleanup SIG ERR fprintf stderr Cannot set up signal n return 0 10 Portability Multiple kinds of hardware 32 bit Intel Architecture 64 bit IA PowerPC Sparc MIPS Arms Multiple operating systems Linux Windows Mac Sun AIX Multiple character sets ASCII Latin 1 unicode Multiple byte orderings Little endian Big endian 11 Size of Data Types What are the sizes of char short int long float and double in C and C char has at least 8 bits short and int at least 16 bits sizeof char sizeof short sizeof int sizeof long sizeof float sizeof double In Java sizes are defined byte 8 bits char 16 bits short 16 bits int 32 bits long 64 bits Our advice always use sizeof to be safe 12 Order of Evaluation Order of evaluation may be ambiguous strings i names i y i can be incremented before or after indexing strings printf c c n getchar getchar y The second character in stdin can be printed first What are the rules in C and C Side effects and function calls must be completed at Our advice do not depend on the order of evaluation in an expression 13 Alignment of Structures and Unions Structure consisting of multiple elements struct Foo char x int y Items are laid out in the order of declaration But the alignment is undefined There might be holes between the elements E g y may be 2 4 or 8 bytes from x 14 Internationalization Don t assume ASCII Many countries do not use English Asian languages use 16 bits per character Standardizations Latin 1 augments ASCII by using all 8 bits Unicode uses 16 bits per character Java uses Unicode as its native character set for strings Issues with Unicode Byte order issue Solution use UTF 8 as an intermediate representation or define the byte order for each character 15 Avoid Conditional Compilation Writing platform specific code is possible some common code ifdef MAC else ifdef WINDOWSXP endif endif But ifdef code is difficult to manage Platform specific code may be all over the place Plus each part requires separate testing 16 Isolation Common feature may not always work Life is hard Localize system dependencies in separate files Separate file to wrap the interface calls for each system Example unix c windows c mac c Hide system dependencies behind interfaces Abstraction can serve as the boundary between portable and non portable components Java goes one big step further Virtual machine which abstracts the entire machine Independent of operating systems and the hardware 17 Course Wrap Up 18 Lessons About Computer Science Modularity Well defined interfaces between components Allows changing the implementation of one component without changing another The key to managing complexity in large systems Resource sharing Time sharing of the CPU by multiple processes Sharing of the physical memory by multiple processes Indirection Representing address space with virtual memory Manipulating data via pointers or


View Full Document

Princeton COS 217 - Signals and Writing Portable Programs

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Signals and Writing Portable Programs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Signals and Writing Portable Programs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Signals and Writing Portable Programs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?