1@node Job Control, Name Service Switch, Inter-Process Communication, Top 2@c %MENU% All about process groups and sessions 3@chapter Job Control 4 5@cindex process groups 6@cindex job control 7@cindex job 8@cindex session 9@dfn{Job control} refers to the protocol for allowing a user to move 10between multiple @dfn{process groups} (or @dfn{jobs}) within a single 11@dfn{login session}. The job control facilities are set up so that 12appropriate behavior for most programs happens automatically and they 13need not do anything special about job control. So you can probably 14ignore the material in this chapter unless you are writing a shell or 15login program. 16 17You need to be familiar with concepts relating to process creation 18(@pxref{Process Creation Concepts}) and signal handling (@pxref{Signal 19Handling}) in order to understand this material presented in this 20chapter. 21 22@vindex _POSIX_JOB_CONTROL 23Some old systems do not support job control, but @gnusystems{} always 24have, and it is a required feature in the 2001 revision of POSIX.1 25(@pxref{POSIX}). If you need to be portable to old systems, you can 26use the @code{_POSIX_JOB_CONTROL} macro to test at compile-time 27whether the system supports job control. @xref{System Options}. 28 29@menu 30* Concepts of Job Control:: Jobs can be controlled by a shell. 31* Controlling Terminal:: How a process gets its controlling terminal. 32* Access to the Terminal:: How processes share the controlling terminal. 33* Orphaned Process Groups:: Jobs left after the user logs out. 34* Implementing a Shell:: What a shell must do to implement job control. 35* Functions for Job Control:: Functions to control process groups. 36@end menu 37 38@node Concepts of Job Control 39@section Concepts of Job Control 40 41@cindex shell 42The fundamental purpose of an interactive shell is to read 43commands from the user's terminal and create processes to execute the 44programs specified by those commands. It can do this using the 45@code{fork} (@pxref{Creating a Process}) and @code{exec} 46(@pxref{Executing a File}) functions. 47 48A single command may run just one process---but often one command uses 49several processes. If you use the @samp{|} operator in a shell command, 50you explicitly request several programs in their own processes. But 51even if you run just one program, it can use multiple processes 52internally. For example, a single compilation command such as @samp{cc 53-c foo.c} typically uses four processes (though normally only two at any 54given time). If you run @code{make}, its job is to run other programs 55in separate processes. 56 57The processes belonging to a single command are called a @dfn{process 58group} or @dfn{job}. This is so that you can operate on all of them at 59once. For example, typing @kbd{C-c} sends the signal @code{SIGINT} to 60terminate all the processes in the foreground process group. 61 62@cindex session 63A @dfn{session} is a larger group of processes. Normally all the 64processes that stem from a single login belong to the same session. 65 66Every process belongs to a process group. When a process is created, it 67becomes a member of the same process group and session as its parent 68process. You can put it in another process group using the 69@code{setpgid} function, provided the process group belongs to the same 70session. 71 72@cindex session leader 73The only way to put a process in a different session is to make it the 74initial process of a new session, or a @dfn{session leader}, using the 75@code{setsid} function. This also puts the session leader into a new 76process group, and you can't move it out of that process group again. 77 78Usually, new sessions are created by the system login program, and the 79session leader is the process running the user's login shell. 80 81@cindex controlling terminal 82A shell that supports job control must arrange to control which job can 83use the terminal at any time. Otherwise there might be multiple jobs 84trying to read from the terminal at once, and confusion about which 85process should receive the input typed by the user. To prevent this, 86the shell must cooperate with the terminal driver using the protocol 87described in this chapter. 88 89@cindex foreground job 90@cindex background job 91The shell can give unlimited access to the controlling terminal to only 92one process group at a time. This is called the @dfn{foreground job} on 93that controlling terminal. Other process groups managed by the shell 94that are executing without such access to the terminal are called 95@dfn{background jobs}. 96 97@cindex stopped job 98If a background job needs to read from its controlling 99terminal, it is @dfn{stopped} by the terminal driver; if the 100@code{TOSTOP} mode is set, likewise for writing. The user can stop 101a foreground job by typing the SUSP character (@pxref{Special 102Characters}) and a program can stop any job by sending it a 103@code{SIGSTOP} signal. It's the responsibility of the shell to notice 104when jobs stop, to notify the user about them, and to provide mechanisms 105for allowing the user to interactively continue stopped jobs and switch 106jobs between foreground and background. 107 108@xref{Access to the Terminal}, for more information about I/O to the 109controlling terminal. 110 111@node Controlling Terminal 112@section Controlling Terminal of a Process 113 114One of the attributes of a process is its controlling terminal. Child 115processes created with @code{fork} inherit the controlling terminal from 116their parent process. In this way, all the processes in a session 117inherit the controlling terminal from the session leader. A session 118leader that has control of a terminal is called the @dfn{controlling 119process} of that terminal. 120 121@cindex controlling process 122You generally do not need to worry about the exact mechanism used to 123allocate a controlling terminal to a session, since it is done for you 124by the system when you log in. 125@c ??? How does GNU system let a process get a ctl terminal. 126 127An individual process disconnects from its controlling terminal when it 128calls @code{setsid} to become the leader of a new session. 129@xref{Process Group Functions}. 130 131@c !!! explain how it gets a new one (by opening any terminal) 132@c ??? How you get a controlling terminal is system-dependent. 133@c We should document how this will work in the GNU system when it is decided. 134@c What Unix does is not clean and I don't think GNU should use that. 135 136@node Access to the Terminal, Orphaned Process Groups, Controlling Terminal, Job Control 137@section Access to the Controlling Terminal 138@cindex controlling terminal, access to 139 140Processes in the foreground job of a controlling terminal have 141unrestricted access to that terminal; background processes do not. This 142section describes in more detail what happens when a process in a 143background job tries to access its controlling terminal. 144 145@cindex @code{SIGTTIN}, from background job 146When a process in a background job tries to read from its controlling 147terminal, the process group is usually sent a @code{SIGTTIN} signal. 148This normally causes all of the processes in that group to stop (unless 149they handle the signal and don't stop themselves). However, if the 150reading process is ignoring or blocking this signal, then @code{read} 151fails with an @code{EIO} error instead. 152 153@cindex @code{SIGTTOU}, from background job 154Similarly, when a process in a background job tries to write to its 155controlling terminal, the default behavior is to send a @code{SIGTTOU} 156signal to the process group. However, the behavior is modified by the 157@code{TOSTOP} bit of the local modes flags (@pxref{Local Modes}). If 158this bit is not set (which is the default), then writing to the 159controlling terminal is always permitted without sending a signal. 160Writing is also permitted if the @code{SIGTTOU} signal is being ignored 161or blocked by the writing process. 162 163Most other terminal operations that a program can do are treated as 164reading or as writing. (The description of each operation should say 165which.) 166 167For more information about the primitive @code{read} and @code{write} 168functions, see @ref{I/O Primitives}. 169 170 171@node Orphaned Process Groups, Implementing a Shell, Access to the Terminal, Job Control 172@section Orphaned Process Groups 173@cindex orphaned process group 174 175When a controlling process terminates, its terminal becomes free and a 176new session can be established on it. (In fact, another user could log 177in on the terminal.) This could cause a problem if any processes from 178the old session are still trying to use that terminal. 179 180To prevent problems, process groups that continue running even after the 181session leader has terminated are marked as @dfn{orphaned process 182groups}. 183 184When a process group becomes an orphan, its processes are sent a 185@code{SIGHUP} signal. Ordinarily, this causes the processes to 186terminate. However, if a program ignores this signal or establishes a 187handler for it (@pxref{Signal Handling}), it can continue running as in 188the orphan process group even after its controlling process terminates; 189but it still cannot access the terminal any more. 190 191@node Implementing a Shell, Functions for Job Control, Orphaned Process Groups, Job Control 192@section Implementing a Job Control Shell 193 194This section describes what a shell must do to implement job control, by 195presenting an extensive sample program to illustrate the concepts 196involved. 197 198@iftex 199@itemize @bullet 200@item 201@ref{Data Structures}, introduces the example and presents 202its primary data structures. 203 204@item 205@ref{Initializing the Shell}, discusses actions which the shell must 206perform to prepare for job control. 207 208@item 209@ref{Launching Jobs}, includes information about how to create jobs 210to execute commands. 211 212@item 213@ref{Foreground and Background}, discusses what the shell should 214do differently when launching a job in the foreground as opposed to 215a background job. 216 217@item 218@ref{Stopped and Terminated Jobs}, discusses reporting of job status 219back to the shell. 220 221@item 222@ref{Continuing Stopped Jobs}, tells you how to continue jobs that 223have been stopped. 224 225@item 226@ref{Missing Pieces}, discusses other parts of the shell. 227@end itemize 228@end iftex 229 230@menu 231* Data Structures:: Introduction to the sample shell. 232* Initializing the Shell:: What the shell must do to take 233 responsibility for job control. 234* Launching Jobs:: Creating jobs to execute commands. 235* Foreground and Background:: Putting a job in foreground of background. 236* Stopped and Terminated Jobs:: Reporting job status. 237* Continuing Stopped Jobs:: How to continue a stopped job in 238 the foreground or background. 239* Missing Pieces:: Other parts of the shell. 240@end menu 241 242@node Data Structures, Initializing the Shell, , Implementing a Shell 243@subsection Data Structures for the Shell 244 245All of the program examples included in this chapter are part of 246a simple shell program. This section presents data structures 247and utility functions which are used throughout the example. 248 249The sample shell deals mainly with two data structures. The 250@code{job} type contains information about a job, which is a 251set of subprocesses linked together with pipes. The @code{process} type 252holds information about a single subprocess. Here are the relevant 253data structure declarations: 254 255@smallexample 256@group 257/* @r{A process is a single process.} */ 258typedef struct process 259@{ 260 struct process *next; /* @r{next process in pipeline} */ 261 char **argv; /* @r{for exec} */ 262 pid_t pid; /* @r{process ID} */ 263 char completed; /* @r{true if process has completed} */ 264 char stopped; /* @r{true if process has stopped} */ 265 int status; /* @r{reported status value} */ 266@} process; 267@end group 268 269@group 270/* @r{A job is a pipeline of processes.} */ 271typedef struct job 272@{ 273 struct job *next; /* @r{next active job} */ 274 char *command; /* @r{command line, used for messages} */ 275 process *first_process; /* @r{list of processes in this job} */ 276 pid_t pgid; /* @r{process group ID} */ 277 char notified; /* @r{true if user told about stopped job} */ 278 struct termios tmodes; /* @r{saved terminal modes} */ 279 int stdin, stdout, stderr; /* @r{standard i/o channels} */ 280@} job; 281 282/* @r{The active jobs are linked into a list. This is its head.} */ 283job *first_job = NULL; 284@end group 285@end smallexample 286 287Here are some utility functions that are used for operating on @code{job} 288objects. 289 290@smallexample 291@group 292/* @r{Find the active job with the indicated @var{pgid}.} */ 293job * 294find_job (pid_t pgid) 295@{ 296 job *j; 297 298 for (j = first_job; j; j = j->next) 299 if (j->pgid == pgid) 300 return j; 301 return NULL; 302@} 303@end group 304 305@group 306/* @r{Return true if all processes in the job have stopped or completed.} */ 307int 308job_is_stopped (job *j) 309@{ 310 process *p; 311 312 for (p = j->first_process; p; p = p->next) 313 if (!p->completed && !p->stopped) 314 return 0; 315 return 1; 316@} 317@end group 318 319@group 320/* @r{Return true if all processes in the job have completed.} */ 321int 322job_is_completed (job *j) 323@{ 324 process *p; 325 326 for (p = j->first_process; p; p = p->next) 327 if (!p->completed) 328 return 0; 329 return 1; 330@} 331@end group 332@end smallexample 333 334 335@node Initializing the Shell, Launching Jobs, Data Structures, Implementing a Shell 336@subsection Initializing the Shell 337@cindex job control, enabling 338@cindex subshell 339 340When a shell program that normally performs job control is started, it 341has to be careful in case it has been invoked from another shell that is 342already doing its own job control. 343 344A subshell that runs interactively has to ensure that it has been placed 345in the foreground by its parent shell before it can enable job control 346itself. It does this by getting its initial process group ID with the 347@code{getpgrp} function, and comparing it to the process group ID of the 348current foreground job associated with its controlling terminal (which 349can be retrieved using the @code{tcgetpgrp} function). 350 351If the subshell is not running as a foreground job, it must stop itself 352by sending a @code{SIGTTIN} signal to its own process group. It may not 353arbitrarily put itself into the foreground; it must wait for the user to 354tell the parent shell to do this. If the subshell is continued again, 355it should repeat the check and stop itself again if it is still not in 356the foreground. 357 358@cindex job control, enabling 359Once the subshell has been placed into the foreground by its parent 360shell, it can enable its own job control. It does this by calling 361@code{setpgid} to put itself into its own process group, and then 362calling @code{tcsetpgrp} to place this process group into the 363foreground. 364 365When a shell enables job control, it should set itself to ignore all the 366job control stop signals so that it doesn't accidentally stop itself. 367You can do this by setting the action for all the stop signals to 368@code{SIG_IGN}. 369 370A subshell that runs non-interactively cannot and should not support job 371control. It must leave all processes it creates in the same process 372group as the shell itself; this allows the non-interactive shell and its 373child processes to be treated as a single job by the parent shell. This 374is easy to do---just don't use any of the job control primitives---but 375you must remember to make the shell do it. 376 377 378Here is the initialization code for the sample shell that shows how to 379do all of this. 380 381@smallexample 382/* @r{Keep track of attributes of the shell.} */ 383 384#include <sys/types.h> 385#include <termios.h> 386#include <unistd.h> 387 388pid_t shell_pgid; 389struct termios shell_tmodes; 390int shell_terminal; 391int shell_is_interactive; 392 393 394/* @r{Make sure the shell is running interactively as the foreground job} 395 @r{before proceeding.} */ 396 397void 398init_shell () 399@{ 400 401 /* @r{See if we are running interactively.} */ 402 shell_terminal = STDIN_FILENO; 403 shell_is_interactive = isatty (shell_terminal); 404 405 if (shell_is_interactive) 406 @{ 407 /* @r{Loop until we are in the foreground.} */ 408 while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ())) 409 kill (- shell_pgid, SIGTTIN); 410 411 /* @r{Ignore interactive and job-control signals.} */ 412 signal (SIGINT, SIG_IGN); 413 signal (SIGQUIT, SIG_IGN); 414 signal (SIGTSTP, SIG_IGN); 415 signal (SIGTTIN, SIG_IGN); 416 signal (SIGTTOU, SIG_IGN); 417 signal (SIGCHLD, SIG_IGN); 418 419 /* @r{Put ourselves in our own process group.} */ 420 shell_pgid = getpid (); 421 if (setpgid (shell_pgid, shell_pgid) < 0) 422 @{ 423 perror ("Couldn't put the shell in its own process group"); 424 exit (1); 425 @} 426 427 /* @r{Grab control of the terminal.} */ 428 tcsetpgrp (shell_terminal, shell_pgid); 429 430 /* @r{Save default terminal attributes for shell.} */ 431 tcgetattr (shell_terminal, &shell_tmodes); 432 @} 433@} 434@end smallexample 435 436 437@node Launching Jobs, Foreground and Background, Initializing the Shell, Implementing a Shell 438@subsection Launching Jobs 439@cindex launching jobs 440 441Once the shell has taken responsibility for performing job control on 442its controlling terminal, it can launch jobs in response to commands 443typed by the user. 444 445To create the processes in a process group, you use the same @code{fork} 446and @code{exec} functions described in @ref{Process Creation Concepts}. 447Since there are multiple child processes involved, though, things are a 448little more complicated and you must be careful to do things in the 449right order. Otherwise, nasty race conditions can result. 450 451You have two choices for how to structure the tree of parent-child 452relationships among the processes. You can either make all the 453processes in the process group be children of the shell process, or you 454can make one process in group be the ancestor of all the other processes 455in that group. The sample shell program presented in this chapter uses 456the first approach because it makes bookkeeping somewhat simpler. 457 458@cindex process group leader 459@cindex process group ID 460As each process is forked, it should put itself in the new process group 461by calling @code{setpgid}; see @ref{Process Group Functions}. The first 462process in the new group becomes its @dfn{process group leader}, and its 463process ID becomes the @dfn{process group ID} for the group. 464 465@cindex race conditions, relating to job control 466The shell should also call @code{setpgid} to put each of its child 467processes into the new process group. This is because there is a 468potential timing problem: each child process must be put in the process 469group before it begins executing a new program, and the shell depends on 470having all the child processes in the group before it continues 471executing. If both the child processes and the shell call 472@code{setpgid}, this ensures that the right things happen no matter which 473process gets to it first. 474 475If the job is being launched as a foreground job, the new process group 476also needs to be put into the foreground on the controlling terminal 477using @code{tcsetpgrp}. Again, this should be done by the shell as well 478as by each of its child processes, to avoid race conditions. 479 480The next thing each child process should do is to reset its signal 481actions. 482 483During initialization, the shell process set itself to ignore job 484control signals; see @ref{Initializing the Shell}. As a result, any child 485processes it creates also ignore these signals by inheritance. This is 486definitely undesirable, so each child process should explicitly set the 487actions for these signals back to @code{SIG_DFL} just after it is forked. 488 489Since shells follow this convention, applications can assume that they 490inherit the correct handling of these signals from the parent process. 491But every application has a responsibility not to mess up the handling 492of stop signals. Applications that disable the normal interpretation of 493the SUSP character should provide some other mechanism for the user to 494stop the job. When the user invokes this mechanism, the program should 495send a @code{SIGTSTP} signal to the process group of the process, not 496just to the process itself. @xref{Signaling Another Process}. 497 498Finally, each child process should call @code{exec} in the normal way. 499This is also the point at which redirection of the standard input and 500output channels should be handled. @xref{Duplicating Descriptors}, 501for an explanation of how to do this. 502 503Here is the function from the sample shell program that is responsible 504for launching a program. The function is executed by each child process 505immediately after it has been forked by the shell, and never returns. 506 507@smallexample 508void 509launch_process (process *p, pid_t pgid, 510 int infile, int outfile, int errfile, 511 int foreground) 512@{ 513 pid_t pid; 514 515 if (shell_is_interactive) 516 @{ 517 /* @r{Put the process into the process group and give the process group} 518 @r{the terminal, if appropriate.} 519 @r{This has to be done both by the shell and in the individual} 520 @r{child processes because of potential race conditions.} */ 521 pid = getpid (); 522 if (pgid == 0) pgid = pid; 523 setpgid (pid, pgid); 524 if (foreground) 525 tcsetpgrp (shell_terminal, pgid); 526 527 /* @r{Set the handling for job control signals back to the default.} */ 528 signal (SIGINT, SIG_DFL); 529 signal (SIGQUIT, SIG_DFL); 530 signal (SIGTSTP, SIG_DFL); 531 signal (SIGTTIN, SIG_DFL); 532 signal (SIGTTOU, SIG_DFL); 533 signal (SIGCHLD, SIG_DFL); 534 @} 535 536 /* @r{Set the standard input/output channels of the new process.} */ 537 if (infile != STDIN_FILENO) 538 @{ 539 dup2 (infile, STDIN_FILENO); 540 close (infile); 541 @} 542 if (outfile != STDOUT_FILENO) 543 @{ 544 dup2 (outfile, STDOUT_FILENO); 545 close (outfile); 546 @} 547 if (errfile != STDERR_FILENO) 548 @{ 549 dup2 (errfile, STDERR_FILENO); 550 close (errfile); 551 @} 552 553 /* @r{Exec the new process. Make sure we exit.} */ 554 execvp (p->argv[0], p->argv); 555 perror ("execvp"); 556 exit (1); 557@} 558@end smallexample 559 560If the shell is not running interactively, this function does not do 561anything with process groups or signals. Remember that a shell not 562performing job control must keep all of its subprocesses in the same 563process group as the shell itself. 564 565Next, here is the function that actually launches a complete job. 566After creating the child processes, this function calls some other 567functions to put the newly created job into the foreground or background; 568these are discussed in @ref{Foreground and Background}. 569 570@smallexample 571void 572launch_job (job *j, int foreground) 573@{ 574 process *p; 575 pid_t pid; 576 int mypipe[2], infile, outfile; 577 578 infile = j->stdin; 579 for (p = j->first_process; p; p = p->next) 580 @{ 581 /* @r{Set up pipes, if necessary.} */ 582 if (p->next) 583 @{ 584 if (pipe (mypipe) < 0) 585 @{ 586 perror ("pipe"); 587 exit (1); 588 @} 589 outfile = mypipe[1]; 590 @} 591 else 592 outfile = j->stdout; 593 594 /* @r{Fork the child processes.} */ 595 pid = fork (); 596 if (pid == 0) 597 /* @r{This is the child process.} */ 598 launch_process (p, j->pgid, infile, 599 outfile, j->stderr, foreground); 600 else if (pid < 0) 601 @{ 602 /* @r{The fork failed.} */ 603 perror ("fork"); 604 exit (1); 605 @} 606 else 607 @{ 608 /* @r{This is the parent process.} */ 609 p->pid = pid; 610 if (shell_is_interactive) 611 @{ 612 if (!j->pgid) 613 j->pgid = pid; 614 setpgid (pid, j->pgid); 615 @} 616 @} 617 618 /* @r{Clean up after pipes.} */ 619 if (infile != j->stdin) 620 close (infile); 621 if (outfile != j->stdout) 622 close (outfile); 623 infile = mypipe[0]; 624 @} 625 626 format_job_info (j, "launched"); 627 628 if (!shell_is_interactive) 629 wait_for_job (j); 630 else if (foreground) 631 put_job_in_foreground (j, 0); 632 else 633 put_job_in_background (j, 0); 634@} 635@end smallexample 636 637 638@node Foreground and Background, Stopped and Terminated Jobs, Launching Jobs, Implementing a Shell 639@subsection Foreground and Background 640 641Now let's consider what actions must be taken by the shell when it 642launches a job into the foreground, and how this differs from what 643must be done when a background job is launched. 644 645@cindex foreground job, launching 646When a foreground job is launched, the shell must first give it access 647to the controlling terminal by calling @code{tcsetpgrp}. Then, the 648shell should wait for processes in that process group to terminate or 649stop. This is discussed in more detail in @ref{Stopped and Terminated 650Jobs}. 651 652When all of the processes in the group have either completed or stopped, 653the shell should regain control of the terminal for its own process 654group by calling @code{tcsetpgrp} again. Since stop signals caused by 655I/O from a background process or a SUSP character typed by the user 656are sent to the process group, normally all the processes in the job 657stop together. 658 659The foreground job may have left the terminal in a strange state, so the 660shell should restore its own saved terminal modes before continuing. In 661case the job is merely stopped, the shell should first save the current 662terminal modes so that it can restore them later if the job is 663continued. The functions for dealing with terminal modes are 664@code{tcgetattr} and @code{tcsetattr}; these are described in 665@ref{Terminal Modes}. 666 667Here is the sample shell's function for doing all of this. 668 669@smallexample 670@group 671/* @r{Put job @var{j} in the foreground. If @var{cont} is nonzero,} 672 @r{restore the saved terminal modes and send the process group a} 673 @r{@code{SIGCONT} signal to wake it up before we block.} */ 674 675void 676put_job_in_foreground (job *j, int cont) 677@{ 678 /* @r{Put the job into the foreground.} */ 679 tcsetpgrp (shell_terminal, j->pgid); 680@end group 681 682@group 683 /* @r{Send the job a continue signal, if necessary.} */ 684 if (cont) 685 @{ 686 tcsetattr (shell_terminal, TCSADRAIN, &j->tmodes); 687 if (kill (- j->pgid, SIGCONT) < 0) 688 perror ("kill (SIGCONT)"); 689 @} 690@end group 691 692 /* @r{Wait for it to report.} */ 693 wait_for_job (j); 694 695 /* @r{Put the shell back in the foreground.} */ 696 tcsetpgrp (shell_terminal, shell_pgid); 697 698@group 699 /* @r{Restore the shell's terminal modes.} */ 700 tcgetattr (shell_terminal, &j->tmodes); 701 tcsetattr (shell_terminal, TCSADRAIN, &shell_tmodes); 702@} 703@end group 704@end smallexample 705 706@cindex background job, launching 707If the process group is launched as a background job, the shell should 708remain in the foreground itself and continue to read commands from 709the terminal. 710 711In the sample shell, there is not much that needs to be done to put 712a job into the background. Here is the function it uses: 713 714@smallexample 715/* @r{Put a job in the background. If the cont argument is true, send} 716 @r{the process group a @code{SIGCONT} signal to wake it up.} */ 717 718void 719put_job_in_background (job *j, int cont) 720@{ 721 /* @r{Send the job a continue signal, if necessary.} */ 722 if (cont) 723 if (kill (-j->pgid, SIGCONT) < 0) 724 perror ("kill (SIGCONT)"); 725@} 726@end smallexample 727 728 729@node Stopped and Terminated Jobs, Continuing Stopped Jobs, Foreground and Background, Implementing a Shell 730@subsection Stopped and Terminated Jobs 731 732@cindex stopped jobs, detecting 733@cindex terminated jobs, detecting 734When a foreground process is launched, the shell must block until all of 735the processes in that job have either terminated or stopped. It can do 736this by calling the @code{waitpid} function; see @ref{Process 737Completion}. Use the @code{WUNTRACED} option so that status is reported 738for processes that stop as well as processes that terminate. 739 740The shell must also check on the status of background jobs so that it 741can report terminated and stopped jobs to the user; this can be done by 742calling @code{waitpid} with the @code{WNOHANG} option. A good place to 743put a such a check for terminated and stopped jobs is just before 744prompting for a new command. 745 746@cindex @code{SIGCHLD}, handling of 747The shell can also receive asynchronous notification that there is 748status information available for a child process by establishing a 749handler for @code{SIGCHLD} signals. @xref{Signal Handling}. 750 751In the sample shell program, the @code{SIGCHLD} signal is normally 752ignored. This is to avoid reentrancy problems involving the global data 753structures the shell manipulates. But at specific times when the shell 754is not using these data structures---such as when it is waiting for 755input on the terminal---it makes sense to enable a handler for 756@code{SIGCHLD}. The same function that is used to do the synchronous 757status checks (@code{do_job_notification}, in this case) can also be 758called from within this handler. 759 760Here are the parts of the sample shell program that deal with checking 761the status of jobs and reporting the information to the user. 762 763@smallexample 764@group 765/* @r{Store the status of the process @var{pid} that was returned by waitpid.} 766 @r{Return 0 if all went well, nonzero otherwise.} */ 767 768int 769mark_process_status (pid_t pid, int status) 770@{ 771 job *j; 772 process *p; 773@end group 774 775@group 776 if (pid > 0) 777 @{ 778 /* @r{Update the record for the process.} */ 779 for (j = first_job; j; j = j->next) 780 for (p = j->first_process; p; p = p->next) 781 if (p->pid == pid) 782 @{ 783 p->status = status; 784 if (WIFSTOPPED (status)) 785 p->stopped = 1; 786 else 787 @{ 788 p->completed = 1; 789 if (WIFSIGNALED (status)) 790 fprintf (stderr, "%d: Terminated by signal %d.\n", 791 (int) pid, WTERMSIG (p->status)); 792 @} 793 return 0; 794 @} 795 fprintf (stderr, "No child process %d.\n", pid); 796 return -1; 797 @} 798@end group 799@group 800 else if (pid == 0 || errno == ECHILD) 801 /* @r{No processes ready to report.} */ 802 return -1; 803 else @{ 804 /* @r{Other weird errors.} */ 805 perror ("waitpid"); 806 return -1; 807 @} 808@} 809@end group 810 811@group 812/* @r{Check for processes that have status information available,} 813 @r{without blocking.} */ 814 815void 816update_status (void) 817@{ 818 int status; 819 pid_t pid; 820 821 do 822 pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG); 823 while (!mark_process_status (pid, status)); 824@} 825@end group 826 827@group 828/* @r{Check for processes that have status information available,} 829 @r{blocking until all processes in the given job have reported.} */ 830 831void 832wait_for_job (job *j) 833@{ 834 int status; 835 pid_t pid; 836 837 do 838 pid = waitpid (WAIT_ANY, &status, WUNTRACED); 839 while (!mark_process_status (pid, status) 840 && !job_is_stopped (j) 841 && !job_is_completed (j)); 842@} 843@end group 844 845@group 846/* @r{Format information about job status for the user to look at.} */ 847 848void 849format_job_info (job *j, const char *status) 850@{ 851 fprintf (stderr, "%ld (%s): %s\n", (long)j->pgid, status, j->command); 852@} 853@end group 854 855@group 856/* @r{Notify the user about stopped or terminated jobs.} 857 @r{Delete terminated jobs from the active job list.} */ 858 859void 860do_job_notification (void) 861@{ 862 job *j, *jlast, *jnext; 863 864 /* @r{Update status information for child processes.} */ 865 update_status (); 866 867 jlast = NULL; 868 for (j = first_job; j; j = jnext) 869 @{ 870 jnext = j->next; 871 872 /* @r{If all processes have completed, tell the user the job has} 873 @r{completed and delete it from the list of active jobs.} */ 874 if (job_is_completed (j)) @{ 875 format_job_info (j, "completed"); 876 if (jlast) 877 jlast->next = jnext; 878 else 879 first_job = jnext; 880 free_job (j); 881 @} 882 883 /* @r{Notify the user about stopped jobs,} 884 @r{marking them so that we won't do this more than once.} */ 885 else if (job_is_stopped (j) && !j->notified) @{ 886 format_job_info (j, "stopped"); 887 j->notified = 1; 888 jlast = j; 889 @} 890 891 /* @r{Don't say anything about jobs that are still running.} */ 892 else 893 jlast = j; 894 @} 895@} 896@end group 897@end smallexample 898 899@node Continuing Stopped Jobs, Missing Pieces, Stopped and Terminated Jobs, Implementing a Shell 900@subsection Continuing Stopped Jobs 901 902@cindex stopped jobs, continuing 903The shell can continue a stopped job by sending a @code{SIGCONT} signal 904to its process group. If the job is being continued in the foreground, 905the shell should first invoke @code{tcsetpgrp} to give the job access to 906the terminal, and restore the saved terminal settings. After continuing 907a job in the foreground, the shell should wait for the job to stop or 908complete, as if the job had just been launched in the foreground. 909 910The sample shell program handles both newly created and continued jobs 911with the same pair of functions, @w{@code{put_job_in_foreground}} and 912@w{@code{put_job_in_background}}. The definitions of these functions 913were given in @ref{Foreground and Background}. When continuing a 914stopped job, a nonzero value is passed as the @var{cont} argument to 915ensure that the @code{SIGCONT} signal is sent and the terminal modes 916reset, as appropriate. 917 918This leaves only a function for updating the shell's internal bookkeeping 919about the job being continued: 920 921@smallexample 922@group 923/* @r{Mark a stopped job J as being running again.} */ 924 925void 926mark_job_as_running (job *j) 927@{ 928 Process *p; 929 930 for (p = j->first_process; p; p = p->next) 931 p->stopped = 0; 932 j->notified = 0; 933@} 934@end group 935 936@group 937/* @r{Continue the job J.} */ 938 939void 940continue_job (job *j, int foreground) 941@{ 942 mark_job_as_running (j); 943 if (foreground) 944 put_job_in_foreground (j, 1); 945 else 946 put_job_in_background (j, 1); 947@} 948@end group 949@end smallexample 950 951@node Missing Pieces, , Continuing Stopped Jobs, Implementing a Shell 952@subsection The Missing Pieces 953 954The code extracts for the sample shell included in this chapter are only 955a part of the entire shell program. In particular, nothing at all has 956been said about how @code{job} and @code{program} data structures are 957allocated and initialized. 958 959Most real shells provide a complex user interface that has support for 960a command language; variables; abbreviations, substitutions, and pattern 961matching on file names; and the like. All of this is far too complicated 962to explain here! Instead, we have concentrated on showing how to 963implement the core process creation and job control functions that can 964be called from such a shell. 965 966Here is a table summarizing the major entry points we have presented: 967 968@table @code 969@item void init_shell (void) 970Initialize the shell's internal state. @xref{Initializing the 971Shell}. 972 973@item void launch_job (job *@var{j}, int @var{foreground}) 974Launch the job @var{j} as either a foreground or background job. 975@xref{Launching Jobs}. 976 977@item void do_job_notification (void) 978Check for and report any jobs that have terminated or stopped. Can be 979called synchronously or within a handler for @code{SIGCHLD} signals. 980@xref{Stopped and Terminated Jobs}. 981 982@item void continue_job (job *@var{j}, int @var{foreground}) 983Continue the job @var{j}. @xref{Continuing Stopped Jobs}. 984@end table 985 986Of course, a real shell would also want to provide other functions for 987managing jobs. For example, it would be useful to have commands to list 988all active jobs or to send a signal (such as @code{SIGKILL}) to a job. 989 990 991@node Functions for Job Control, , Implementing a Shell, Job Control 992@section Functions for Job Control 993@cindex process group functions 994@cindex job control functions 995 996This section contains detailed descriptions of the functions relating 997to job control. 998 999@menu 1000* Identifying the Terminal:: Determining the controlling terminal's name. 1001* Process Group Functions:: Functions for manipulating process groups. 1002* Terminal Access Functions:: Functions for controlling terminal access. 1003@end menu 1004 1005 1006@node Identifying the Terminal, Process Group Functions, , Functions for Job Control 1007@subsection Identifying the Controlling Terminal 1008@cindex controlling terminal, determining 1009 1010You can use the @code{ctermid} function to get a file name that you can 1011use to open the controlling terminal. In @theglibc{}, it returns 1012the same string all the time: @code{"/dev/tty"}. That is a special 1013``magic'' file name that refers to the controlling terminal of the 1014current process (if it has one). To find the name of the specific 1015terminal device, use @code{ttyname}; @pxref{Is It a Terminal}. 1016 1017The function @code{ctermid} is declared in the header file 1018@file{stdio.h}. 1019@pindex stdio.h 1020 1021@deftypefun {char *} ctermid (char *@var{string}) 1022@standards{POSIX.1, stdio.h} 1023@safety{@prelim{}@mtsafe{@mtsposix{/!string}}@assafe{}@acsafe{}} 1024@c This function is a stub by default; the actual implementation, for 1025@c posix systems, returns a pointer to a string literal if passed a NULL 1026@c string. It's not clear we want to commit to being MT-Safe in the 1027@c !string case, so maybe add mtasurace{:ctermid/!string} when we take 1028@c prelim out, to make room for using a static buffer in the future. 1029The @code{ctermid} function returns a string containing the file name of 1030the controlling terminal for the current process. If @var{string} is 1031not a null pointer, it should be an array that can hold at least 1032@code{L_ctermid} characters; the string is returned in this array. 1033Otherwise, a pointer to a string in a static area is returned, which 1034might get overwritten on subsequent calls to this function. 1035 1036An empty string is returned if the file name cannot be determined for 1037any reason. Even if a file name is returned, access to the file it 1038represents is not guaranteed. 1039@end deftypefun 1040 1041@deftypevr Macro int L_ctermid 1042@standards{POSIX.1, stdio.h} 1043The value of this macro is an integer constant expression that 1044represents the size of a string large enough to hold the file name 1045returned by @code{ctermid}. 1046@end deftypevr 1047 1048See also the @code{isatty} and @code{ttyname} functions, in 1049@ref{Is It a Terminal}. 1050 1051 1052@node Process Group Functions, Terminal Access Functions, Identifying the Terminal, Functions for Job Control 1053@subsection Process Group Functions 1054 1055Here are descriptions of the functions for manipulating process groups. 1056Your program should include the header files @file{sys/types.h} and 1057@file{unistd.h} to use these functions. 1058@pindex unistd.h 1059@pindex sys/types.h 1060 1061@deftypefun pid_t setsid (void) 1062@standards{POSIX.1, unistd.h} 1063@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1064@c This is usually a direct syscall, but if a syscall is not available, 1065@c we use a stub, or Hurd- and BSD-specific implementations. The former 1066@c uses a mutex and a hurd critical section, and the latter issues a few 1067@c syscalls, so both seem safe, the locking on Hurd is safe because of 1068@c the critical section. 1069The @code{setsid} function creates a new session. The calling process 1070becomes the session leader, and is put in a new process group whose 1071process group ID is the same as the process ID of that process. There 1072are initially no other processes in the new process group, and no other 1073process groups in the new session. 1074 1075This function also makes the calling process have no controlling terminal. 1076 1077The @code{setsid} function returns the new process group ID of the 1078calling process if successful. A return value of @code{-1} indicates an 1079error. The following @code{errno} error conditions are defined for this 1080function: 1081 1082@table @code 1083@item EPERM 1084The calling process is already a process group leader, or there is 1085already another process group around that has the same process group ID. 1086@end table 1087@end deftypefun 1088 1089@deftypefun pid_t getsid (pid_t @var{pid}) 1090@standards{SVID, unistd.h} 1091@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1092@c Stub or direct syscall, except on hurd, where it is equally safe. 1093 1094The @code{getsid} function returns the process group ID of the session 1095leader of the specified process. If a @var{pid} is @code{0}, the 1096process group ID of the session leader of the current process is 1097returned. 1098 1099In case of error @code{-1} is returned and @code{errno} is set. The 1100following @code{errno} error conditions are defined for this function: 1101 1102@table @code 1103@item ESRCH 1104There is no process with the given process ID @var{pid}. 1105@item EPERM 1106The calling process and the process specified by @var{pid} are in 1107different sessions, and the implementation doesn't allow to access the 1108process group ID of the session leader of the process with ID @var{pid} 1109from the calling process. 1110@end table 1111@end deftypefun 1112 1113@deftypefun pid_t getpgrp (void) 1114@standards{POSIX.1, unistd.h} 1115@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1116The @code{getpgrp} function returns the process group ID of 1117the calling process. 1118@end deftypefun 1119 1120@deftypefun int getpgid (pid_t @var{pid}) 1121@standards{POSIX.1, unistd.h} 1122@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1123@c Stub or direct syscall, except on hurd, where it is equally safe. 1124 1125The @code{getpgid} function 1126returns the process group ID of the process @var{pid}. You can supply a 1127value of @code{0} for the @var{pid} argument to get information about 1128the calling process. 1129 1130In case of error @code{-1} is returned and @code{errno} is set. The 1131following @code{errno} error conditions are defined for this function: 1132 1133@table @code 1134@item ESRCH 1135There is no process with the given process ID @var{pid}. 1136The calling process and the process specified by @var{pid} are in 1137different sessions, and the implementation doesn't allow to access the 1138process group ID of the process with ID @var{pid} from the calling 1139process. 1140@end table 1141@end deftypefun 1142 1143@deftypefun int setpgid (pid_t @var{pid}, pid_t @var{pgid}) 1144@standards{POSIX.1, unistd.h} 1145@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1146@c Stub or direct syscall, except on hurd, where it is equally safe. 1147The @code{setpgid} function puts the process @var{pid} into the process 1148group @var{pgid}. As a special case, either @var{pid} or @var{pgid} can 1149be zero to indicate the process ID of the calling process. 1150 1151If the operation is successful, @code{setpgid} returns zero. Otherwise 1152it returns @code{-1}. The following @code{errno} error conditions are 1153defined for this function: 1154 1155@table @code 1156@item EACCES 1157The child process named by @var{pid} has executed an @code{exec} 1158function since it was forked. 1159 1160@item EINVAL 1161The value of the @var{pgid} is not valid. 1162 1163@item ENOSYS 1164The system doesn't support job control. 1165 1166@item EPERM 1167The process indicated by the @var{pid} argument is a session leader, 1168or is not in the same session as the calling process, or the value of 1169the @var{pgid} argument doesn't match a process group ID in the same 1170session as the calling process. 1171 1172@item ESRCH 1173The process indicated by the @var{pid} argument is not the calling 1174process or a child of the calling process. 1175@end table 1176@end deftypefun 1177 1178@deftypefun int setpgrp (pid_t @var{pid}, pid_t @var{pgid}) 1179@standards{BSD, unistd.h} 1180@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1181@c Direct syscall or setpgid wrapper. 1182This is the BSD Unix name for @code{setpgid}. Both functions do exactly 1183the same thing. 1184@end deftypefun 1185 1186 1187@node Terminal Access Functions, , Process Group Functions, Functions for Job Control 1188@subsection Functions for Controlling Terminal Access 1189 1190These are the functions for reading or setting the foreground 1191process group of a terminal. You should include the header files 1192@file{sys/types.h} and @file{unistd.h} in your application to use 1193these functions. 1194@pindex unistd.h 1195@pindex sys/types.h 1196 1197Although these functions take a file descriptor argument to specify 1198the terminal device, the foreground job is associated with the terminal 1199file itself and not a particular open file descriptor. 1200 1201@deftypefun pid_t tcgetpgrp (int @var{filedes}) 1202@standards{POSIX.1, unistd.h} 1203@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1204@c Stub, or ioctl on BSD and GNU/Linux. 1205This function returns the process group ID of the foreground process 1206group associated with the terminal open on descriptor @var{filedes}. 1207 1208If there is no foreground process group, the return value is a number 1209greater than @code{1} that does not match the process group ID of any 1210existing process group. This can happen if all of the processes in the 1211job that was formerly the foreground job have terminated, and no other 1212job has yet been moved into the foreground. 1213 1214In case of an error, a value of @code{-1} is returned. The 1215following @code{errno} error conditions are defined for this function: 1216 1217@table @code 1218@item EBADF 1219The @var{filedes} argument is not a valid file descriptor. 1220 1221@item ENOSYS 1222The system doesn't support job control. 1223 1224@item ENOTTY 1225The terminal file associated with the @var{filedes} argument isn't the 1226controlling terminal of the calling process. 1227@end table 1228@end deftypefun 1229 1230@deftypefun int tcsetpgrp (int @var{filedes}, pid_t @var{pgid}) 1231@standards{POSIX.1, unistd.h} 1232@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1233@c Stub, or ioctl on BSD and GNU/Linux. 1234This function is used to set a terminal's foreground process group ID. 1235The argument @var{filedes} is a descriptor which specifies the terminal; 1236@var{pgid} specifies the process group. The calling process must be a 1237member of the same session as @var{pgid} and must have the same 1238controlling terminal. 1239 1240For terminal access purposes, this function is treated as output. If it 1241is called from a background process on its controlling terminal, 1242normally all processes in the process group are sent a @code{SIGTTOU} 1243signal. The exception is if the calling process itself is ignoring or 1244blocking @code{SIGTTOU} signals, in which case the operation is 1245performed and no signal is sent. 1246 1247If successful, @code{tcsetpgrp} returns @code{0}. A return value of 1248@code{-1} indicates an error. The following @code{errno} error 1249conditions are defined for this function: 1250 1251@table @code 1252@item EBADF 1253The @var{filedes} argument is not a valid file descriptor. 1254 1255@item EINVAL 1256The @var{pgid} argument is not valid. 1257 1258@item ENOSYS 1259The system doesn't support job control. 1260 1261@item ENOTTY 1262The @var{filedes} isn't the controlling terminal of the calling process. 1263 1264@item EPERM 1265The @var{pgid} isn't a process group in the same session as the calling 1266process. 1267@end table 1268@end deftypefun 1269 1270@deftypefun pid_t tcgetsid (int @var{fildes}) 1271@standards{Unix98, termios.h} 1272@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} 1273@c Ioctl call, if available, or tcgetpgrp followed by getsid. 1274This function is used to obtain the process group ID of the session 1275for which the terminal specified by @var{fildes} is the controlling terminal. 1276If the call is successful the group ID is returned. Otherwise the 1277return value is @code{(pid_t) -1} and the global variable @code{errno} 1278is set to the following value: 1279@table @code 1280@item EBADF 1281The @var{filedes} argument is not a valid file descriptor. 1282 1283@item ENOTTY 1284The calling process does not have a controlling terminal, or the file 1285is not the controlling terminal. 1286@end table 1287@end deftypefun 1288