Process Relationships (Chapter 9 )
Total Page:16
File Type:pdf, Size:1020Kb
Process Relationships (Chapter 9 ) Terminal Logins & Network Logins Process Groups and Sessions Job Control Relationships explained with examples Amy Lin, UAH 6/25/2019 Terminal Login Procedure init getty login shell “init” • At old time, the sysadmin would create a text file called /etc/ttys, each line would specify the device name and other parameters passed to getty • On Linux, the information is in /etc/inittab, a typical line in inittab is - 5:2345:respawn:/sbin/mingetty tty5 (man 5 inittab) - mingetty is almost the same as getty except it’s not suitable for serial line connections , you might consider to use (m)getty if dialup connection is needed. • On system startup, init reads inittab to get terminal information and fork/exec a “getty” process for each terminal “getty” • Calls open for the terminal device (read and write) • Opens descriptors 0, 1, and 2 to the device and outputs “login:” • Waits for someone to enter a user name Amy Lin, UAH 6/25/2019 The “login” After user enters a user name, login invokes exec function execle(“/bin/login”, “login”, “-p”, username, (char*)0, envp); • login calls “getpwnam” to fetch the password file entry • login reads the entered password, call crypt (3) to encrypt the password then compare to the encrypted password file entry • If log incorrectly, login exits with “1”, init then respawns getty to start over In successful login, login performs the following - Change to user’s home directory - Change permissions of terminal device so we can read from and write to it - Set group IDs - Initialize environment variables, such as PATH, HOME, SHELL, USER, LOGNAME Amy Lin, UAH 6/25/2019 The “shell” At the end of login process, a login shell is invoked with fork/exec execl(“/bin/sh”,“-sh”,(char*)0) - The prefix “-” before argv[0] is a flag to all the shells that they are being invoked as a login shell. The login shell reads its start-up files - .profile, .bashrc for bash - .cshrc and .login for C shell Amy Lin, UAH 6/25/2019 Network Login- inetd inetd (internet service daemon) is an Internet superserver waiting for TCP/IP connection requests • it forks and execs the appropriate program When you “telnet” to a remove host, the inetd on the host will spawn the “telnetd” process • The telnetd then opens a pseudo-terminal device and splits into two processes using fork. - The parent process handles the communication across the network - The child does an exec of the login program - The parent and the child are connected through the pseudo- terminal Amy Lin, UAH 6/25/2019 Process Groups A process group is a collection of one or more processes i.e: these THREE are in a group: ls | grep “cs590” | more Each process group has a unique process group ID - PGID is the similar to PID, non negative integer PGID can be obtained through pid_t getpgrp() getpgid(0) – the pgid of the calling process pid_t getpgid(pid_t pid); Each process group has a group leader the leader is the process whose pid is the same as it’s process group ID Processes can be added to a process group through int setpgid(pid_t pid, pid_t pgid); - pid is the process being placed in a new process group pgid • pid=0:add the current process to the process group pgid • pgid=0: pid is used as pgid, i.e setpgid(pid, 0) setpgid(pid, pid) • If both pid=0, pgid=0, setpgid(0,0) is commonly used create a new group, the current pid is the leader of the new group, and gpid=pid Amy Lin, UAH 6/25/2019 Sessions A session is a collection of one or more process groups - proc1 | proc2 & - background jobs - proc3 | proc4 | proc5 - foreground jobs The process groups within a session can be divided into • a single foreground process group and • one or more background process groups Create a new session: pid_t setsid() If the calling process is not a process group leader • The process becomes the session leader of the new session • The process becomes the process group leader of a new process group • The process has no controlling terminal Returns error if the caller is already a process group leader A session can have one single controlling terminal Amy Lin, UAH 6/25/2019 Controlling Terminal Every session is tied to a terminal from which processes in the session get their I/O The terminal to which a session is related is called the controlling terminal of the session. Usually, it is established automatically when we login. If a session has a controlling terminal, then it has a single foreground process, and all other process groups in the session are background process groups Only the foreground job receives terminal input - SIGINT generated by pressing Ctrl-C - SIGQUIT, generated by pressing Ctrl-\ - SIGTSTP, generated by Ctrl-Z tcgetpgrp, tcsetpgrp, & tcgetsid functions • Get process group ID of foreground process group • Get session leader’s process group ID • Used to tell the kernel which process group is the foreground group, so the terminal device driver knows where to send the terminal input and the terminal- generated signals. Amy Lin, UAH 6/25/2019 Job control Job control is a feature that allows users to start multiple jobs (groups of processes) from a single terminal and to control which jobs can access the terminal and which jobs are to run in the background. Job control requires three forms of support • A shell that supports job control (Bourne does not) • The terminal driver in the kernel must support job control • Kernel must support certain job-control signals - SIGINT (ctrl-C) - SIGQUIT (ctrl-\) - SIGTSTP (ctrl-Z) ( used to suspend a job) Amy Lin, UAH 6/25/2019 Program Execution in Shell Shell Single command If we type in a normal command in a shell, then • The shell calls fork to make a copy of itself • The child calls exec to run the program • The parent shell waits for the child process to complete • In case of a background process, i.e. cat file& then the shell would not have waited to the child process to complete before continuing. Pipeline: ps xj | cat • The process order depends on the type of shell • As shown by the textbook, it’s different between bash and bourne shell Amy Lin, UAH 6/25/2019 Relationships of Processes, Process groups & sessions ps –o pid,ppid,pgid,sid,tpgid,comm PID PPID PGRP SESS TPGID COMMAND 27248 27247 27248 27248 27487 bash 27487 27248 27487 27248 27487 ps Parent of the login shell is 27247, which is the original init spawned getty process ps (27487) is the child, of bash shell (27248) ps is the group leader of process group 27248, it is the only process in this process group bash and ps are in two groups belonging to one session sid=27248 • bash is in background group • ps is foreground group Process 27248 (bash) is the leader of this session (27248) but has no control over the terminal Amy Lin, UAH 6/25/2019 Background Job ps –o pid,ppid,pgid,sid,tpgid,comm & PID PPID PGRP SESS TPGID COMMAND 27248 27247 27248 27248 27248 bash 27413 27248 27413 27248 27248 ps We are still in the same session (27248) PGID=27248 (bash) controls the terminal, which means bash is the foreground process group ps is a background group Both bash and ps are their process group leaders (why?) • Each group has only one process in this case bash is still the session leader who is the one having created the shell Amy Lin, UAH 6/25/2019 2-Processes through pipe ps –o pid,ppid,pgid,sid,comm | cat PID PPID PGRP SESS TPGID COMMAND 27248 27247 27248 27248 27603 bash 27603 27248 27603 27248 27603 ps 27604 27248 27603 27248 27603 cat ps and cat are put in one group, 27603, with ps being the group leader, this group is in foreground process group, controlling the terminal ps –o pid,ppid,pgid,sid,tpgid,comm|cat & PID PPID PGID SESS TPGID COMMAND 28675 28674 28675 28675 28675 bash 28794 28675 28794 28675 28675 ps 28795 28675 28794 28675 28675 cat Both ps and cat processes are put in the background process group Amy Lin, UAH 6/25/2019 2+ Processes Through pipeline Executing more processes in a pipeline ps -o pid,ppid,pgid,session,tpgid,comm |./cat1 |./cat2 PID PPID PGID SESS TPGID COMMAND 28675 28674 28675 28675 28910 bash 28910 28675 28910 28675 28910 ps 28911 28675 28910 28675 28910 cat1 28912 28675 28910 28675 28910 cat2 Amy Lin, UAH 6/25/2019.