Download
reference n.
Skip this Video
Loading SlideShow in 5 Seconds..
Reference PowerPoint Presentation

Reference

142 Vues Download Presentation
Télécharger la présentation

Reference

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Reference • Advanced programming for the unix environment (chapters 7,8,9 of both editions + chapter 13(2nd edition))

  2. Process environment • Memory • Environment variables • Fd table • Signal handlers

  3. Signals • Signals are “software interrupt” • Whenever the OS wants to tell a process something it sends a signal • Signal example : you are about to be killed, you are about to be suspended (both of this cannot be caught), div by 0, segmentation violation etc. (those are often caught by debuggers), child process has terminated or as a response to other system call (wait(2), alarm(2))

  4. Interesting signals • SIGABRT/SIGTERM please shutdown • SIGKILL– kill program (cannot be caught) • SIGUSR1/2 – for user use • SIGHUP– terminal hanghup (usually used in most servers to re-read conf file) • SIGSUSP – suspand program (cannot be caught) • SIGIO,SIGALRM – generated by select(2), alarm(2) • SIGCHLD(SIGCLD) – child process died

  5. System calls involving signals • wait(2) – wait for child process to die • select(2) – wait for IO • alarm(2) – set SIGALRM • pause(2) – wait for any signal

  6. Environment • Set of parameters that are inherited from process to process. • getenv(2) setenv(2) • See also in bash(1) set, export in tcsh(1) set, setenv • Usages – set default parameters for all process for example setting EDITOR or VISUAL makes other programs open the editor as default editor (for example cron(8))

  7. memory • Heap • Stack • Global variables • Program (compiled code) • Ex. (not for submission) – write a class that finds out if it is in the stack, global variable or heap. (Idea by Scott Mayers) • Notable functions malloc(2), free(2), alloca • We will talk about memory management later

  8. Process relations Each process has its parent. Several process with common ancestor will have the same process group (the pid of the first parent is the group gid) Processes from the same session have the same session id (session>group>process) • getpid(2) • getppid(2) • setsid(2)

  9. Daemon process and losing controlling terminal • Daemon – process that does some service for users. (usually process that never returns) • Examples – Apache httpd, Wu-ftpd, and almost any process that ends with “d” you see on ps(1) • In order to make a process daemon make it start his own process group (fork and cause the father to terminate) then lose controlling terminal and fork() again. • Exact code will be shown in ex.

  10. syslog = printf server style • Losing controlling terminal means that nobody sees I/O messages. Also daemon process runs for very long time. Sometimes we would want to see what happened at a certain moment even if there was nobody at the moment at the computer. • openlog(3) • syslog(3) (don’t be mistaken by syslog(1)) • closelog(3)

  11. Example for syslog Nov 20 04:57:39 89-138-166-80 login[16146]: USER_PROCESS: 16146 ttys000 Nov 20 04:59:35 89-138-166-80 pppd[17475]: Connection terminated. Nov 20 04:59:36 89-138-166-80 pppd[17475]: PPTP disconnecting... Nov 20 04:59:36 89-138-166-80 pppd[17475]: PPTP disconnected Nov 20 04:59:37 Macintosh configd[14]: setting hostname to "Macintosh.local" Nov 20 04:59:38 Macintosh pppd[16479]: pppd 2.4.2 (Apple version 314) started by root, uid 501 Nov 20 04:59:38 Macintosh pppd[16479]: PPTP connecting to server '172.26.255.245' (172.26.255.245)... Nov 20 04:59:38 Macintosh pppd[16479]: PPTP connection established. Nov 20 04:59:38 Macintosh pppd[16479]: Connect: ppp0 <--> socket[34:17] Nov 20 04:59:38 Macintosh pppd[16479]: PAP authentication succeeded Nov 20 04:59:38 Macintosh pppd[16479]: local IP address 89.138.196.228 Nov 20 04:59:38 Macintosh pppd[16479]: remote IP address 212.143.205.162

  12. Openlog, closelog, syslog • void syslog(int priority, const char *message, ...); • void openlog(const char *ident, int logopt, int facility); • void closelog(void); • Syslog output file is usually found in /var/log (usually /var/log/messages under Linux) • Multiple log files and compressed enteries will usually be found this is handled by logrotate(1) which is beyond our scope.