📄 library_24.html
字号:
continued jobs. The definitions of these functions were given insection <A HREF="library_24.html#SEC421" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC421">Foreground and Background</A>. When continuing a stopped job, anonzero value is passed as the <VAR>cont</VAR> argument to ensure that the<CODE>SIGCONT</CODE> signal is sent and the terminal modes reset, asappropriate.<P>This leaves only a function for updating the shell's internal bookkeepingabout the job being continued:<P><PRE>/* Mark a stopped job J as being running again. */voidmark_job_as_running (job *j){ Process *p; for (p = j->first_process; p; p = p->next) p->stopped = 0; j->notified = 0;}/* Continue the job J. */voidcontinue_job (job *j, int foreground){ mark_job_as_running (j); if (foreground) put_job_in_foreground (j, 1); else put_job_in_background (j, 1);}</PRE><P><H3><A NAME="SEC424" HREF="library_toc.html#SEC424" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC424">The Missing Pieces</A></H3><P>The code extracts for the sample shell included in this chapter are onlya part of the entire shell program. In particular, nothing at all hasbeen said about how <CODE>job</CODE> and <CODE>program</CODE> data structures areallocated and initialized.<P>Most real shells provide a complex user interface that has support fora command language; variables; abbreviations, substitutions, and patternmatching on file names; and the like. All of this is far too complicatedto explain here! Instead, we have concentrated on showing how to implement the core process creation and job control functions that canbe called from such a shell.<P>Here is a table summarizing the major entry points we have presented:<P><DL COMPACT><DT><CODE>void init_shell (void)</CODE><DD>Initialize the shell's internal state. See section <A HREF="library_24.html#SEC419" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC419">Initializing the Shell</A>.<P><DT><CODE>void launch_job (job *<VAR>j</VAR>, int <VAR>foreground</VAR>)</CODE><DD>Launch the job <VAR>j</VAR> as either a foreground or background job.See section <A HREF="library_24.html#SEC420" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC420">Launching Jobs</A>.<P><DT><CODE>void do_job_notification (void)</CODE><DD>Check for and report any jobs that have terminated or stopped. Can becalled synchronously or within a handler for <CODE>SIGCHLD</CODE> signals.See section <A HREF="library_24.html#SEC422" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC422">Stopped and Terminated Jobs</A>.<P><DT><CODE>void continue_job (job *<VAR>j</VAR>, int <VAR>foreground</VAR>)</CODE><DD>Continue the job <VAR>j</VAR>. See section <A HREF="library_24.html#SEC423" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC423">Continuing Stopped Jobs</A>.</DL><P>Of course, a real shell would also want to provide other functions formanaging jobs. For example, it would be useful to have commands to listall active jobs or to send a signal (such as <CODE>SIGKILL</CODE>) to a job.<P><A NAME="IDX1759"></A><A NAME="IDX1760"></A><H2><A NAME="SEC425" HREF="library_toc.html#SEC425" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC425">Functions for Job Control</A></H2><P>This section contains detailed descriptions of the functions relatingto job control.<P><A NAME="IDX1761"></A><H3><A NAME="SEC426" HREF="library_toc.html#SEC426" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC426">Identifying the Controlling Terminal</A></H3><P>You can use the <CODE>ctermid</CODE> function to get a file name that you canuse to open the controlling terminal. In the GNU library, it returnsthe same string all the time: <CODE>"/dev/tty"</CODE>. That is a special"magic" file name that refers to the controlling terminal of thecurrent process (if it has one). The function <CODE>ctermid</CODE> isdeclared in the header file <TT>`stdio.h'</TT>.<A NAME="IDX1762"></A><P><A NAME="IDX1763"></A><U>Function:</U> char * <B>ctermid</B> <I>(char *<VAR>string</VAR>)</I><P>The <CODE>ctermid</CODE> function returns a string containing the file name ofthe controlling terminal for the current process. If <VAR>string</VAR> isnot a null pointer, it should be an array that can hold at least<CODE>L_ctermid</CODE> characters; the string is returned in this array.Otherwise, a pointer to a string in a static area is returned, whichmight get overwritten on subsequent calls to this function.<P>An empty string is returned if the file name cannot be determined forany reason. Even if a file name is returned, access to the file itrepresents is not guaranteed.<P><A NAME="IDX1764"></A><U>Macro:</U> int <B>L_ctermid</B><P>The value of this macro is an integer constant expression thatrepresents the size of a string large enough to hold the file namereturned by <CODE>ctermid</CODE>.<P>See also the <CODE>isatty</CODE> and <CODE>ttyname</CODE> functions, in section <A HREF="library_16.html#SEC269" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html#SEC269">Identifying Terminals</A>.<P><H3><A NAME="SEC427" HREF="library_toc.html#SEC427" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC427">Process Group Functions</A></H3><P>Here are descriptions of the functions for manipulating process groups.Your program should include the header files <TT>`sys/types.h'</TT> and<TT>`unistd.h'</TT> to use these functions.<A NAME="IDX1766"></A><A NAME="IDX1765"></A><P><A NAME="IDX1767"></A><U>Function:</U> pid_t <B>setsid</B> <I>(void)</I><P>The <CODE>setsid</CODE> function creates a new session. The calling processbecomes the session leader, and is put in a new process group whoseprocess group ID is the same as the process ID of that process. Thereare initially no other processes in the new process group, and no otherprocess groups in the new session.<P>This function also makes the calling process have no controlling terminal.<P>The <CODE>setsid</CODE> function returns the new process group ID of thecalling process if successful. A return value of <CODE>-1</CODE> indicates anerror. The following <CODE>errno</CODE> error conditions are defined for thisfunction:<P><DL COMPACT><DT><CODE>EPERM</CODE><DD>The calling process is already a process group leader, or there isalready another process group around that has the same process group ID.</DL><P>The <CODE>getpgrp</CODE> function has two definitions: one derived from BSDUnix, and one from the POSIX.1 standard. The feature test macros youhave selected (see section <A HREF="library_1.html#SEC12" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_1.html#SEC12">Feature Test Macros</A>) determine which definitionyou get. Specifically, you get the BSD version if you define<CODE>_BSD_SOURCE</CODE>; otherwise, you get the POSIX version if you define<CODE>_POSIX_SOURCE</CODE> or <CODE>_GNU_SOURCE</CODE>. Programs written for oldBSD systems will not include <TT>`unistd.h'</TT>, which defines<CODE>getpgrp</CODE> specially under <CODE>_BSD_SOURCE</CODE>. You must link suchprograms with the <CODE>-lbsd-compat</CODE> option to get the BSD definition.<A NAME="IDX1769"></A><A NAME="IDX1770"></A><A NAME="IDX1768"></A><P><A NAME="IDX1771"></A><U>POSIX.1 Function:</U> pid_t <B>getpgrp</B> <I>(void)</I><P>The POSIX.1 definition of <CODE>getpgrp</CODE> returns the process group ID ofthe calling process.<P><A NAME="IDX1772"></A><U>BSD Function:</U> pid_t <B>getpgrp</B> <I>(pid_t <VAR>pid</VAR>)</I><P>The BSD definition of <CODE>getpgrp</CODE> returns the process group ID of theprocess <VAR>pid</VAR>. You can supply a value of <CODE>0</CODE> for the <VAR>pid</VAR>argument to get information about the calling process.<P><A NAME="IDX1773"></A><U>Function:</U> int <B>setpgid</B> <I>(pid_t <VAR>pid</VAR>, pid_t <VAR>pgid</VAR>)</I><P>The <CODE>setpgid</CODE> function puts the process <VAR>pid</VAR> into the processgroup <VAR>pgid</VAR>. As a special case, either <VAR>pid</VAR> or <VAR>pgid</VAR> canbe zero to indicate the process ID of the calling process.<P>This function fails on a system that does not support job control.See section <A HREF="library_24.html#SEC413" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html#SEC413">Job Control is Optional</A>, for more information.<P>If the operation is successful, <CODE>setpgid</CODE> returns zero. Otherwiseit returns <CODE>-1</CODE>. The following <CODE>errno</CODE> error conditions aredefined for this function:<P><DL COMPACT><DT><CODE>EACCES</CODE><DD>The child process named by <VAR>pid</VAR> has executed an <CODE>exec</CODE>function since it was forked.<P><DT><CODE>EINVAL</CODE><DD>The value of the <VAR>pgid</VAR> is not valid.<P><DT><CODE>ENOSYS</CODE><DD>The system doesn't support job control.<P><DT><CODE>EPERM</CODE><DD>The process indicated by the <VAR>pid</VAR> argument is a session leader,or is not in the same session as the calling process, or the value ofthe <VAR>pgid</VAR> argument doesn't match a process group ID in the samesession as the calling process.<P><DT><CODE>ESRCH</CODE><DD>The process indicated by the <VAR>pid</VAR> argument is not the callingprocess or a child of the calling process.</DL><P><A NAME="IDX1774"></A><U>Function:</U> int <B>setpgrp</B> <I>(pid_t <VAR>pid</VAR>, pid_t <VAR>pgid</VAR>)</I><P>This is the BSD Unix name for <CODE>setpgid</CODE>. Both functions do exactlythe same thing.<P><H3><A NAME="SEC428" HREF="library_toc.html#SEC428" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC428">Functions for Controlling Terminal Access</A></H3><P>These are the functions for reading or setting the foregroundprocess group of a terminal. You should include the header files<TT>`sys/types.h'</TT> and <TT>`unistd.h'</TT> in your application to usethese functions.<A NAME="IDX1776"></A><A NAME="IDX1775"></A><P>Although these functions take a file descriptor argument to specifythe terminal device, the foreground job is associated with the terminalfile itself and not a particular open file descriptor.<P><A NAME="IDX1777"></A><U>Function:</U> pid_t <B>tcgetpgrp</B> <I>(int <VAR>filedes</VAR>)</I><P>This function returns the process group ID of the foreground processgroup associated with the terminal open on descriptor <VAR>filedes</VAR>.<P>If there is no foreground process group, the return value is a numbergreater than <CODE>1</CODE> that does not match the process group ID of anyexisting process group. This can happen if all of the processes in thejob that was formerly the foreground job have terminated, and no otherjob has yet been moved into the foreground.<P>In case of an error, a value of <CODE>-1</CODE> is returned. Thefollowing <CODE>errno</CODE> error conditions are defined for this function:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is not a valid file descriptor.<P><DT><CODE>ENOSYS</CODE><DD>The system doesn't support job control.<P><DT><CODE>ENOTTY</CODE><DD>The terminal file associated with the <VAR>filedes</VAR> argument isn't thecontrolling terminal of the calling process.</DL><P><A NAME="IDX1778"></A><U>Function:</U> int <B>tcsetpgrp</B> <I>(int <VAR>filedes</VAR>, pid_t <VAR>pgid</VAR>)</I><P>This function is used to set a terminal's foreground process group ID.The argument <VAR>filedes</VAR> is a descriptor which specifies the terminal;<VAR>pgid</VAR> specifies the process group. The calling process must be amember of the same session as <VAR>pgid</VAR> and must have the samecontrolling terminal.<P>For terminal access purposes, this function is treated as output. If itis called from a background process on its controlling terminal,normally all processes in the process group are sent a <CODE>SIGTTOU</CODE>signal. The exception is if the calling process itself is ignoring orblocking <CODE>SIGTTOU</CODE> signals, in which case the operation isperformed and no signal is sent.<P>If successful, <CODE>tcsetpgrp</CODE> returns <CODE>0</CODE>. A return value of<CODE>-1</CODE> indicates an error. The following <CODE>errno</CODE> errorconditions are defined for this function:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is not a valid file descriptor.<P><DT><CODE>EINVAL</CODE><DD>The <VAR>pgid</VAR> argument is not valid.<P><DT><CODE>ENOSYS</CODE><DD>The system doesn't support job control.<P><DT><CODE>ENOTTY</CODE><DD>The <VAR>filedes</VAR> isn't the controlling terminal of the calling process.<P><DT><CODE>EPERM</CODE><DD>The <VAR>pgid</VAR> isn't a process group in the same session as the callingprocess.</DL><P><P>Go to the <A HREF="library_23.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html">previous</A>, <A HREF="library_25.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html">next</A> section.<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -