The Unix Process


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

The Unix Process

  1. 1. Nezer J. Zaidenberg
  2. 2. <ul><li>Advanced programming for the unix environment (chapters about processes) </li></ul>
  3. 3. <ul><li>Memory </li></ul><ul><li>Environment variables </li></ul><ul><li>Fd table </li></ul><ul><li>Signal handlers </li></ul>
  4. 4. <ul><li>Signals are “software interrupt” </li></ul><ul><li>Whenever the OS wants to tell a process something it sends a signal </li></ul><ul><li>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)) </li></ul>
  5. 5. <ul><li>Sigabrt/term please shutdown </li></ul><ul><li>Sigkill – kill program (cannot be caught) </li></ul><ul><li>SIGUSR1/2 – for user use </li></ul><ul><li>Sighup – terminal hanghup (usually used in most servers to re-read conf file) </li></ul><ul><li>Sigsusp – suspand program (cannot be caught) </li></ul><ul><li>sigio,SIGALRM – generated by select(2), alarm(2) </li></ul><ul><li>SIGCHLD(SIGCLD) – child process died </li></ul>
  6. 6. <ul><li>Wait(2) </li></ul><ul><li>Select(2) </li></ul><ul><li>Alarm(2) </li></ul>
  7. 7. <ul><li>Set of parameters that are inherited from process to process. </li></ul><ul><li>Getenv(2) setenv(2) </li></ul><ul><li>See also in bash(1) set, export in tcsh(1) set, setenv </li></ul><ul><li>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)) </li></ul>
  8. 8. <ul><li>Heap </li></ul><ul><li>Stack </li></ul><ul><li>Global variables </li></ul><ul><li>Program (compiled code) </li></ul><ul><li>Ex. (not for submission) – write a class that finds out if it is in the stack, global variable or heap. (Idea by Scott Mayers) </li></ul><ul><li>Notable functions malloc(2), free(2), alloca </li></ul><ul><li>We will talk about memory management later </li></ul>
  9. 9. <ul><li>Each process has its parent. </li></ul><ul><li>Several process with common ancestor will have the same process group (the pid of the first parent is the group gid) </li></ul><ul><li>Processes from the same session have the same session id (session>group>process) </li></ul><ul><li>Getpid(2) </li></ul><ul><li>Getppid(2) </li></ul><ul><li>Setsid(2) </li></ul>
  10. 10. <ul><li>Daemon – process that does some service for users. (usually process that never returns) </li></ul><ul><li>Examples – Apache httpd, Wu-ftpd, and almost any process that ends with “d” you see on ps(1) </li></ul><ul><li>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. </li></ul><ul><li>Exact code will be shown in ex. </li></ul>
  11. 11. <ul><li>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. </li></ul><ul><li>Openlog(3) </li></ul><ul><li>Syslog(3) (don’t be mistaken by syslog(1)) </li></ul><ul><li>Closelog(3) </li></ul>
  12. 12. <ul><li>Nov 20 04:57:39 89-138-166-80 login[16146]: USER_PROCESS: 16146 ttys000 </li></ul><ul><li>Nov 20 04:59:35 89-138-166-80 pppd[17475]: Connection terminated. </li></ul><ul><li>Nov 20 04:59:36 89-138-166-80 pppd[17475]: PPTP disconnecting... </li></ul><ul><li>Nov 20 04:59:36 89-138-166-80 pppd[17475]: PPTP disconnected </li></ul><ul><li>Nov 20 04:59:37 Macintosh configd[14]: setting hostname to &quot;Macintosh.local&quot; </li></ul><ul><li>Nov 20 04:59:38 Macintosh pppd[16479]: pppd 2.4.2 (Apple version 314) started by root, uid 501 </li></ul><ul><li>Nov 20 04:59:38 Macintosh pppd[16479]: PPTP connecting to server '' ( </li></ul><ul><li>Nov 20 04:59:38 Macintosh pppd[16479]: PPTP connection established. </li></ul><ul><li>Nov 20 04:59:38 Macintosh pppd[16479]: Connect: ppp0 <--> socket[34:17] </li></ul><ul><li>Nov 20 04:59:38 Macintosh pppd[16479]: PAP authentication succeeded </li></ul><ul><li>Nov 20 04:59:38 Macintosh pppd[16479]: local IP address </li></ul><ul><li>Nov 20 04:59:38 Macintosh pppd[16479]: remote IP address </li></ul>
  13. 13. <ul><li>void syslog(int priority, const char *message, ...); </li></ul><ul><li>void openlog(const char *ident, int logopt, int facility); </li></ul><ul><li>void closelog(void); </li></ul><ul><li>Syslog output file is usually found in /var/log (usually /var/log/messages under Linux) </li></ul><ul><li>Multiple log files and compressed enteries will usually be found this is handled by logrotate(1) which is beyond our scope </li></ul>
  14. 14. <ul><li>Historically many servers have used the same TCP code. (socket/bind/listen/fork/accept) those same lines of code were replicated </li></ul><ul><li>Many times, those files were linked with faulty tcp libraries and faulty tcp connections. </li></ul><ul><li>In order to address code reusability and security inetd super server is created </li></ul><ul><li>Inetd super server forks a process with its stdin and stdout opened as TCP socket of given port. </li></ul><ul><li>As a result coding servers became much easier. </li></ul>