140 likes | 330 Views
The UNIX process. Nezer J. Zaidenberg. Referance. Advanced programming for the unix environment (chapters about processes). Process environment. Memory Environment variables Fd table Signal handlers. Signals. Signals are “software interrupt”
E N D
The UNIX process Nezer J. Zaidenberg
Referance • Advanced programming for the unix environment (chapters about processes)
Process environment • Memory • Environment variables • Fd table • Signal handlers
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))
Interesting signals • Sigabrt/term 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
System calls involving signals • Wait(2) • Select(2) • Alarm(2)
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))
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
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)
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.
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)
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
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
Inetd super server • Historically many servers have used the same TCP code. (socket/bind/listen/fork/accept) those same lines of code were replicated • Many times, those files were linked with faulty tcp libraries and faulty tcp connections. • In order to address code reusability and security inetd super server is created • Inetd super server forks a process with its stdin and stdout opened as TCP socket of given port. • As a result coding servers became much easier.