1 / 15

Today’s topic

Today’s topic. UNIX process relationship Parent/child Group Session Job control Foreground and background processes Given that many processes can be executed concurrently, which processes should have accesses to the keyboard/screen (I/O)?. Process groups.

jadyn
Download Presentation

Today’s topic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s topic • UNIX process relationship • Parent/child • Group • Session • Job control • Foreground and background processes • Given that many processes can be executed concurrently, which processes should have accesses to the keyboard/screen (I/O)?

  2. 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.

  3. 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 itself or its children • _POSIX_JOB_CONTROL must be defined • Most shells with job control create new group for each line of command (job).

  4. Sessions • A session is one or more process groups proc1 | proc2 & proc3 | proc4 | proc5 • results in a session with three groups, see ‘ps –j’ • A login shell is a session in general. proc1 proc3 Login shell proc2 proc4 proc5

  5. Session • A session can have a single controlling terminal for performing I/O • 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.

  6. 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

  7. Sessions • To establish a new session: pidsetsid(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 example1.c

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. Shells without job control • The original Bourne shell (sh) does not have job control. • Try sh on program.cs.fsu.edu to see the behavior • The key question: how are io and signals handled in this situation?

  13. Shell without job control • No new sessions or process groups are created • All processes are in the same session and same group • No background process groups vs. foreground process groups • It will not use tcsetpgrp() to set foreground process group • We do have background process(es) in shells without job control, such as sh –In the sense that parent process (sh) will not wait for background process(es) to finish –After forking a child process to run a command, the shell continues to show prompt to accept user input –The next line of user input should be the next command line to shell, not the input to the background process(es)

  14. Shell without job control • How do we guarantee that the next line is for shell, not read by the background command? • It depends on if the standard input of the command has been redirected to a file • If yes, the shell does nothing special • If no, the shell will redirect the standard input of the command to /dev/null before invoking the command • As a consequence, in case the command will read from standard input, it will get nothing (immediately encounter end of file)

  15. Shell without job control • How about accessing file /dev/tty in shells without job control? • Every process can open it • Since, from terminal device driver’s viewpoint, they are all in the same process group (no background process group or foreground process group)

More Related