130 likes | 349 Views
Process groups, sessions, controlling terminal, and job control. Process relationship: Parent/child Same group Same session. Process groups, sessions, controlling terminal, and job control. Process groups: A process group is a collection of processes. Each group has a process group ID.
E N D
Process groups, sessions, controlling terminal, and job control • Process relationship: • Parent/child • Same group • Same session
Process groups, sessions, controlling terminal, and job control • Process groups: • A process group is a collection of 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)
Process groups: • A process may joint an existing group, create a new group. int setpgid(pid_t, pid, pid_t, pgid) • A process can set group ID of it self or its children • _POSIX_JOB_CONTROL must be defined • Most shells with job control create new group for each job (foreground or background).
Sessions • A session is one or more process groups proc1 | proc2 & proc3 | proc4 | proc5 • results in a session with three groups proc1 proc3 Login shell proc2 proc4 proc5
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, what must be done? • getty • Fails if the caller if a group leader.
Sessions/groups • A session can have a single controlling terminal. Usually the one that we log in. (the window and the keyboard that we can do IO). • The session leader that establishes the connection to the control terminal is called the controlling process. • There can be one foreground groups and any number of background groups in a session. • Terminals’ interrupt signals are only sent to the processes in the foreground group. • A process can open file /dev/tty to take to the controlling terminal regardless how standard IO are redirected.
How to make a group foreground and background? pid_t tcgetpgrp(int filedes); int tcsetpgrp(int filedes, pid_t 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) • How shell execution programs • Shell without job control may redirect standard IO to /dev/null for background jobs
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. • If any IO is involved, strange things may happen. • How should we modify the shell implementation to make sure the I/O and signals are correctly handled?
Answer: • Create a new 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.