제 9 장프로세스관계 숙대창병모 1
Contents 1. Logins 2. Process Groups 3. Sessions 4. Controlling Terminal 5. Job Control 숙대창병모 2
로그인 숙대창병모 3
터미널로그인 /etc/ttys: 1 line per terminal device getty: opens terminal device for reading and writing gettytab: data file for getty login: login program getpass: read password crypt: encrypts plain text (password) 숙대창병모 4
터미널로그인과정 process ID 1 init init fork exec getty /reads/etc/ttys; y; forks once per terminal; create empty environment; each child execs ecs getty 숙대창병모 5
로그인후상태 process ID 1 init init fork exec getty exec login /reads/etc/ttys; forks once per terminal; create empty environment; each child execs getty open terminal device (file descriptors 0, 1, 2); reads user name; initial environment set 숙대창병모 6
네트워크로그인 inetd - Internet superserver waits for TCP/IP connection requests when request arrives, does a fork and exec of the appropriate program telnet a remote login application(client) that uses TCP/IP protocol. telnetd TELNET server opens pseudo-terminal device splits into two processes using fork The parent handles the communication across the network connection The child does an exec of the login program. 숙대창병모 7
TELNET 서버 TCP connection request from TELNET client process ID 1 init inetd fork inetd exec telnetd fork /exec of /bin/sh which executes shell script /etc/rc when system comes up multiuser when connection request arrives from TELNET client 숙대창병모 8
프로세스그룹 숙대창병모 9
프로세스그룹 프로세스 IDs Process ID(PID) Process Group ID(GID) 각프로세스는하나의프로세스그룹에속함. 각프로세스는자신이속한 process group ID를가지며 fork 시물려받는다. #include <sys/types.h> #include <unistd.h> pid_t getpgrp(void); p Returns: process GID of calling process 숙대창병모 10
프로세스그룹 프로세스그룹이란? 통상한부모 ( 그룹리더 ) 가생성하는자손프로세스들이하나의프로세스그룹을형성한다. Each process group has a process group leader Process GID = PID 프로세스그룹은무엇을하는데사용되나요? 프로세스그룹은주로 signal 전달등을위해설정됨 kill -INT -3245 명령은프로세스그룹 3245 에 SIGINT 보냄 kill -INT 3245 명령은프로세스 3245에만 SIGINT 보냄 숙대창병모 11
프로세스그룹 : 예 #include <sys/types.h> #include <unistd.h> main() { int pid, gid; printf("parent: PID = %d GID = %d\n", getpid(), getpgrp()); pid = fork(); if (pid == 0) { // 자식프로세스 printf("child: PID = %d GID = %d\n", getpid(), getpgrp()); } } 숙대창병모 12
프로세스그룹 프로세스그룹만들기 자식프로세스는 setpgrp 호출을통하여기존의프로세스그룹에서벗어나서새로운프로세스그룹을형성할수있다. A process can create a new process group and become leader int setpgid(pid_t t pid, pid_t pgid); A process group leader can create processes in the group 프로세스그룹소멸 the last process terminates OR joins another process group (leader may terminate first) 숙대창병모 13
Process Groups #include <sys/types.h> #include <unistd.h> int setpgid(pid_t pid, pid_t pgid); Returns: 0 if OK, -1 on error 역할 Create new group or join existing group. Set the process GID of the process pid to pgid. pid == pgid leader pid!= pgid pid becomes a member of the process group pid == 0 PID of caller 사용 pgid == 0 pid 가 process group leader 됨 Process can set PGID of itself or its children 숙대창병모 14
Example #include <sys/types.h> #include <unistd.h> main() { int pid, gid; printf( PARENT: PID = %d GID = %d \n, getpid(), getpgrp()); pid = fork(); if (pid == 0) { setpgid(0, 0); printf( CHILD: PID = %d GID = %d \n, getpid(), getpgrp()); } } 숙대창병모 15
세션 / 컨트롤터미널 숙대창병모 16
세션 login shell process group proc1 proc2 proc3 proc4 process group proc5 session process group 세션이란? 예 Collection of one or more process groups Job 제어에사용된다. $proc1 proc2 & $proc3 proc4 proc5 숙대창병모 17
Sessions #include <sys/types.h> #include <unistd.h> pid_t setsid(void); Returns: PGID if OK, -1 on error 새로운세션만들기 A process (not a process group leader) creates a new session The process becomes session leader (new session contains this process only) The process become process group leader of a new process group (PGID=PID) 숙대창병모 18
컨트롤터미널 세션을컨트롤할수있는터미널 A session can have a controlling terminal The session leader is the controlling process 세션내의프로세스그룹들 1 foreground, 1 or more backgrounds 이 foreground group 이입력을받는다. 컨트롤터미널로부터키보드입력및 signal 받음 Interrupt key from control terminal SIGINT to all foreground processes 숙대창병모 19
컨트롤터미널 session login shell proc1 proc2 proc3 proc4 background process group session leader = controlling process background process group proc5 foreground process group network disconnect (hangup signal) controlling terminal terminal input and terminal-generated signals 숙대창병모 20
init or inetd Job Control getty or telnetd login exec login shell change in status of children setpgid setpgid exec, after setsid, then establishing controlling terminal change in status of children background process group(s) tcsetpgrp to set process group for controlling terminal write to terminal may generate SIGTTOU terminal input/output foreground process group Read from terminal generate SIGTTIN terminal driver user at a terminal terminal-generated signals (SIGINT, SIGQUIT, SIGSTP) delivered to process group 숙대창병모 21
Job Control 숙대창병모 22
Job Control A feature added by Berkeley in 1980 Job A group of processes To start multiple jobs from a single terminal To control: which jobs can access terminal which jobs are to run in background. 숙대창병모 23
Job Control 3 forms of support required by job control: A shell to support job control Terminal driver must support job control Job-control signals Example cat /etc/passwd grep faculty sort > faculty.out & 2 background ls l grep test sort > test.out & jobs vi main.c 1 foreground job 숙대창병모 24
Shell for Job Control Shells assign a job ID to a background job $cat /etc/passwd grep faculty sort > faculty.out & [1] 19277 19278 19279 $ls l grep test sort > test.out & [2] 19280 19281 19282 $ [2] + Done cat /etc/passwd grep & [1] + Done ls l grep test & job number process IDs 숙대창병모 25
Terminal Driver for Job Control Terminal driver looks out for 3 special characters: Interrupt character (DELETE or CTRL-C) generates SIGINT Quit character (CTRL-BACKSLASH) generates SIGQUIT Suspend character (CTRL-Z) generates SIGTSTP SIGTTIN (1) When background job tries to read from the terminal, (2) the terminal driver sends it to the background job (3) This stops the background job. SIGTTOU (stty tostop 했을경우에 ) (1) When background job tries to write to the terminal, (2) the terminal driver sends it to the background job (3) This stops the background job. 숙대창병모 26
Job Control $ cat > temp.foo & [1] 1719 $ [1] + Stopped(tty 입력 ) cat>temp.foo & $ fg %1 cat> temp.foo hello, world ^D $cat temp.foo hello, world 숙대창병모 27
Job Control $ cat temp.foo & [1] 1719 $ hello, world [1] + Done cat temp.foo & $ stty tostop $ cat temp.foo & [1] 1721 $ [1] + Stopped(tty output) cat temp.foo & $ fg %1 cat temp.foo hello, world 숙대창병모 28
Job Control fg command The shell places the job into the foreground process group, and sends SIGCONT to the process group stty command stty tostop disable ability of background jobs to output to the controlling terminal 숙대창병모 29
init or inetd Job Control getty or telnetd login exec login shell change in status of children setpgid setpgid exec, after setsid, then establishing controlling terminal change in status of children background process group(s) tcsetpgrp to set process group for controlling terminal write to terminal may generate SIGTTOU terminal input/output foreground process group Read from terminal generate SGITTIN terminal driver user at a terminal terminal-generated signals (SIGINT, SIGQUIT, SIGSTP) delivered to process group 숙대창병모 30