c23ab7f09fc06f4f354231c0a200909e.ppt
- Количество слайдов: 12
Today’s topic • UNIX process relationship and job control – Groups, Sessions, Foreground and background processes – Our program can now have many processes, when Ctrl-C is typed, which process should receive SIGINT?
Process groups – A process group is a collection of (related) processes. Each group has a process group ID. – Each group has a group leader who pid = pgid – To get the group ID of a process: pid_t getpgrp(void) – A signal can be sent to the whole group of processes.
• Process groups: – A process may joint an existing group, create a new group. int setpgid(pid_t, pgid) • A process can set group ID of itself or its children • _POSIX_JOB_CONTROL must be defined • Most shells with job control create new group for each line of command (job).
• Sessions – A session is one or more process groups – proc 1 | proc 2 & proc 3 | proc 4 | proc 5 results in a session with three groups, see ‘ps –j’ proc 1 proc 3 proc 2 Login shell proc 4 proc 5 – A login shell is a session in general.
Session • A session can have a single controlling terminal – Terminal device for a terminal login – Pseudo-terminal device for a network login – The I/O devices somewhat link to the window and keyboard. • The session leader that establishes the connection to the control terminal is called the controlling process.
Session • Only one I/O device for all processes (and process groups) in a session. Which process should get the input from the keyboard? • Foreground and background process – One foreground group – Many background groups – Input • Only foreground group • Terminals’ interrupt signals are only sent to the processes in the foreground group. – Output • Typically shared
• Sessions – To establish a new session: pid setsid(void); • Process become the session leader • Process become a new group leader of a new group • Process has no controlling terminal (break up the old one) – Each shell is a session. When a shell is created, a terminal must be setup. • Fails if the caller is a group leader. – A process can open file /dev/tty to talk to the controlling terminal regardless how standard IO are redirected. • A way to by pass I/O redirection, see example 1. c
• How to make a group foreground and background? – So that the terminal device driver knows where to send the terminal input and the terminalgenerated signals. pid_t tcgetpgrp(int filedes); Return the process group ID of the foreground process group associated with fieldes. int tcsetpgrp(int filedes, pid_t pgrpid); – If the process has control terminal, set the foreground process group ID to pgrpid. – Pgrpid must be group ID in the same session.
• Job control – Allows start multiple jobs from a single terminal and control which job can access the terminal. – Foreground jobs can access terminal – Background jobs may not: • When a backgound job try to read, SIGTTIN signal is sent • A background job must be able to output to the terminal (options may be set by the stty command) – See control. c for an example of switching terminal among groups.
• Orphaned process group: – Parent of every member is either in the orphaned group or is not a member of the group’s session. – Happens when a process forks a child and then dies. • The child becomes a member of the orphaned group. • Can have problems: the child may transform from a foreground process to a background process automatically. – Remember in the control. c program, foreground group is set in both the parent and the child. – Can be in an inconsistent state. – If any IO is involved, strange things may happen.
• How to make sure that a shell program handles terminal I/O and signals correctly – Create a new group for each job • Both parent and child do setpgid – For foreground job: • After fork, shell set tcsetpgrp to give foreground jobs control over terminal • Shell waits for all foreground processes in the foreground job to finish. After that, shell set tcsetpgrp to itself and print the prompt. – For background job: • Create a separate group so that processes in background jobs do not have access to terminal.
Review • What do tcgetpgrp and tcsetpgrp do? • How are foreground and background processes different? • How can a shell make sure that the signal is only sent to the foreground processes?
c23ab7f09fc06f4f354231c0a200909e.ppt