]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - share/doc/psd/04.uprog/p5
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / share / doc / psd / 04.uprog / p5
1 .\" Copyright (C) Caldera International Inc. 2001-2002.  All rights reserved.
2 .\" 
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions are
5 .\" met:
6 .\" 
7 .\" Redistributions of source code and documentation must retain the above
8 .\" copyright notice, this list of conditions and the following
9 .\" disclaimer.
10 .\" 
11 .\" Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 
15 .\" All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" 
18 .\" This product includes software developed or owned by Caldera
19 .\" International, Inc.  Neither the name of Caldera International, Inc.
20 .\" nor the names of other contributors may be used to endorse or promote
21 .\" products derived from this software without specific prior written
22 .\" permission.
23 .\" 
24 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
25 .\" INTERNATIONAL, INC.  AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 .\" DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34 .\" OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
35 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 .\" 
37 .\" $FreeBSD$
38 .\"
39 .\"     @(#)p5  8.1 (Berkeley) 6/8/93
40 .\"
41 .NH
42 PROCESSES
43 .PP
44 It is often easier to use a program written
45 by someone else than to invent one's own.
46 This section describes how to
47 execute a program from within another.
48 .NH 2
49 The ``System'' Function
50 .PP
51 The easiest way to execute a program from another
52 is to use
53 the standard library routine
54 .UL system .
55 .UL system
56 takes one argument, a command string exactly as typed
57 at the terminal
58 (except for the newline at the end)
59 and executes it.
60 For instance, to time-stamp the output of a program,
61 .P1
62 main()
63 {
64         system("date");
65         /* rest of processing */
66 }
67 .P2
68 If the command string has to be built from pieces,
69 the in-memory formatting capabilities of
70 .UL sprintf
71 may be useful.
72 .PP
73 Remember than
74 .UL getc
75 and
76 .UL putc
77 normally buffer their input;
78 terminal I/O will not be properly synchronized unless
79 this buffering is defeated.
80 For output, use 
81 .UL fflush ;
82 for input, see
83 .UL setbuf 
84 in the appendix.
85 .NH 2
86 Low-Level Process Creation \(em Execl and Execv
87 .PP
88 If you're not using the standard library,
89 or if you need finer control over what
90 happens,
91 you will have to construct calls to other programs
92 using the more primitive routines that the standard
93 library's
94 .UL system
95 routine is based on.
96 .PP
97 The most basic operation is to execute another program
98 .ul
99 without
100 .IT returning ,
101 by using the routine
102 .UL execl  .
103 To print the date as the last action of a running program,
104 use
105 .P1
106 execl("/bin/date", "date", NULL);
107 .P2
108 The first argument to
109 .UL execl
110 is the
111 .ul
112 file name
113 of the command; you have to know where it is found
114 in the file system.
115 The second argument is conventionally
116 the program name
117 (that is, the last component of the file name),
118 but this is seldom used except as a place-holder.
119 If the command takes arguments, they are strung out after
120 this;
121 the end of the list is marked by a 
122 .UL NULL
123 argument.
124 .PP
125 The
126 .UL execl
127 call
128 overlays the existing program with
129 the new one,
130 runs that, then exits.
131 There is
132 .ul
133 no
134 return to the original program.
135 .PP
136 More realistically,
137 a program might fall into two or more phases
138 that communicate only through temporary files.
139 Here it is natural to make the second pass
140 simply an
141 .UL execl
142 call from the first.
143 .PP
144 The one exception to the rule that the original program never gets control
145 back occurs when there is an error, for example if the file can't be found
146 or is not executable.
147 If you don't know where
148 .UL date
149 is located, say
150 .P1
151 execl("/bin/date", "date", NULL);
152 execl("/usr/bin/date", "date", NULL);
153 fprintf(stderr, "Someone stole 'date'\en");
154 .P2
155 .PP
156 A variant of
157 .UL execl
158 called
159 .UL execv
160 is useful when you don't know in advance how many arguments there are going to be.
161 The call is
162 .P1
163 execv(filename, argp);
164 .P2
165 where
166 .UL argp
167 is an array of pointers to the arguments;
168 the last pointer in the array must be 
169 .UL NULL
170 so
171 .UL execv
172 can tell where the list ends.
173 As with
174 .UL execl ,
175 .UL filename
176 is the file in which the program is found, and
177 .UL argp[0]
178 is the name of the program.
179 (This arrangement is identical to the
180 .UL argv
181 array for program arguments.)
182 .PP
183 Neither of these routines provides the niceties of normal command execution.
184 There is no automatic search of multiple directories \(em
185 you have to know precisely where the command is located.
186 Nor do you get the expansion of metacharacters like
187 .UL < ,
188 .UL > ,
189 .UL * ,
190 .UL ? ,
191 and
192 .UL []
193 in the argument list.
194 If you want these, use
195 .UL execl
196 to invoke the shell
197 .UL sh ,
198 which then does all the work.
199 Construct a string
200 .UL commandline
201 that contains the complete command as it would have been typed
202 at the terminal, then say
203 .P1
204 execl("/bin/sh", "sh", "-c", commandline, NULL);
205 .P2
206 The shell is assumed to be at a fixed place,
207 .UL /bin/sh .
208 Its argument
209 .UL -c
210 says to treat the next argument
211 as a whole command line, so it does just what you want.
212 The only problem is in constructing the right information
213 in
214 .UL commandline .
215 .NH 2
216 Control of Processes \(em Fork and Wait
217 .PP
218 So far what we've talked about isn't really all that useful by itself.
219 Now we will show how to regain control after running
220 a program with
221 .UL execl
222 or
223 .UL execv .
224 Since these routines simply overlay the new program on the old one,
225 to save the old one requires that it first be split into
226 two copies;
227 one of these can be overlaid, while the other waits for the new,
228 overlaying program to finish.
229 The splitting is done by a routine called
230 .UL fork :
231 .P1
232 proc_id = fork();
233 .P2
234 splits the program into two copies, both of which continue to run.
235 The only difference between the two is the value of
236 .UL proc_id ,
237 the ``process id.''
238 In one of these processes (the ``child''),
239 .UL proc_id
240 is zero.
241 In the other
242 (the ``parent''),
243 .UL proc_id
244 is non-zero; it is the process number of the child.
245 Thus the basic way to call, and return from,
246 another program is
247 .P1
248 if (fork() == 0)
249         execl("/bin/sh", "sh", "-c", cmd, NULL);        /* in child */
250 .P2
251 And in fact, except for handling errors, this is sufficient.
252 The
253 .UL fork
254 makes two copies of the program.
255 In the child, the value returned by
256 .UL fork
257 is zero, so it calls
258 .UL execl
259 which does the
260 .UL command
261 and then dies.
262 In the parent,
263 .UL fork
264 returns non-zero
265 so it skips the
266 .UL execl.
267 (If there is any error,
268 .UL fork
269 returns
270 .UL -1 ).
271 .PP
272 More often, the parent wants to wait for the child to terminate
273 before continuing itself.
274 This can be done with
275 the function
276 .UL wait :
277 .P1
278 int status;
279
280 if (fork() == 0)
281         execl(...);
282 wait(&status);
283 .P2
284 This still doesn't handle any abnormal conditions, such as a failure
285 of the
286 .UL execl
287 or
288 .UL fork ,
289 or the possibility that there might be more than one child running simultaneously.
290 (The
291 .UL wait
292 returns the
293 process id
294 of the terminated child, if you want to check it against the value
295 returned by
296 .UL fork .)
297 Finally, this fragment doesn't deal with any
298 funny behavior on the part of the child
299 (which is reported in
300 .UL status ).
301 Still, these three lines
302 are the heart of the standard library's
303 .UL system
304 routine,
305 which we'll show in a moment.
306 .PP
307 The
308 .UL  status 
309 returned by
310 .UL wait
311 encodes in its low-order eight bits
312 the system's idea of the child's termination status;
313 it is 0 for normal termination and non-zero to indicate
314 various kinds of problems.
315 The next higher eight bits are taken from the argument
316 of the call to
317 .UL exit
318 which caused a normal termination of the child process.
319 It is good coding practice
320 for all programs to return meaningful
321 status.
322 .PP
323 When a program is called by the shell,
324 the three file descriptors
325 0, 1, and 2 are set up pointing at the right files,
326 and all other possible file descriptors
327 are available for use.
328 When this program calls another one,
329 correct etiquette suggests making sure the same conditions
330 hold.
331 Neither
332 .UL fork
333 nor the
334 .UL exec
335 calls affects open files in any way.
336 If the parent is buffering output
337 that must come out before output from the child,
338 the parent must flush its buffers
339 before the
340 .UL execl .
341 Conversely,
342 if a caller buffers an input stream,
343 the called program will lose any information
344 that has been read by the caller.
345 .NH 2
346 Pipes
347 .PP
348 A
349 .ul
350 pipe
351 is an I/O channel intended for use
352 between two cooperating processes:
353 one process writes into the pipe,
354 while the other reads.
355 The system looks after buffering the data and synchronizing
356 the two processes.
357 Most pipes are created by the shell,
358 as in
359 .P1
360 ls | pr
361 .P2
362 which connects the standard output of
363 .UL ls
364 to the standard input of
365 .UL pr .
366 Sometimes, however, it is most convenient
367 for a process to set up its own plumbing;
368 in this section, we will illustrate how
369 the pipe connection is established and used.
370 .PP
371 The system call
372 .UL pipe
373 creates a pipe.
374 Since a pipe is used for both reading and writing,
375 two file descriptors are returned;
376 the actual usage is like this:
377 .P1
378 int     fd[2];
379
380 stat = pipe(fd);
381 if (stat == -1)
382         /* there was an error ... */
383 .P2
384 .UL fd
385 is an array of two file descriptors, where
386 .UL fd[0]
387 is the read side of the pipe and
388 .UL fd[1] 
389 is for writing.
390 These may be used in
391 .UL read ,
392 .UL write
393 and
394 .UL close
395 calls just like any other file descriptors.
396 .PP
397 If a process reads a pipe which is empty,
398 it will wait until data arrives;
399 if a process writes into a pipe which
400 is too full, it will wait until the pipe empties somewhat.
401 If the write side of the pipe is closed,
402 a subsequent
403 .UL  read 
404 will encounter end of file.
405 .PP
406 To illustrate the use of pipes in a realistic setting,
407 let us write a function called
408 .UL popen(cmd,\ mode) ,
409 which creates a process
410 .UL cmd
411 (just as
412 .UL system 
413 does),
414 and returns a file descriptor that will either
415 read or write that process, according to 
416 .UL mode .
417 That is,
418 the call
419 .P1
420 fout = popen("pr", WRITE);
421 .P2
422 creates a process that executes
423 the
424 .UL pr
425 command;
426 subsequent
427 .UL write
428 calls using the file descriptor
429 .UL fout
430 will send their data to that process
431 through the pipe.
432 .PP
433 .UL popen
434 first creates the
435 the pipe with a
436 .UL pipe
437 system call;
438 it then
439 .UL fork s
440 to create two copies of itself.
441 The child decides whether it is supposed to read or write,
442 closes the other side of the pipe,
443 then calls the shell (via
444 .UL execl )
445 to run the desired process.
446 The parent likewise closes the end of the pipe it does not use.
447 These closes are necessary to make end-of-file tests work properly.
448 For example, if a child that intends to read
449 fails to close the write end of the pipe, it will never
450 see the end of the pipe file, just because there is one writer
451 potentially active.
452 .P1
453 #include <stdio.h>
454
455 #define READ    0
456 #define WRITE   1
457 #define tst(a, b)       (mode == READ ? (b) : (a))
458 static  int     popen_pid;
459
460 popen(cmd, mode)
461 char    *cmd;
462 int     mode;
463 {
464         int p[2];
465
466         if (pipe(p) < 0)
467                 return(NULL);
468         if ((popen_pid = fork()) == 0) {
469                 close(tst(p[WRITE], p[READ]));
470                 close(tst(0, 1));
471                 dup(tst(p[READ], p[WRITE]));
472                 close(tst(p[READ], p[WRITE]));
473                 execl("/bin/sh", "sh", "-c", cmd, 0);
474                 _exit(1);       /* disaster has occurred if we get here */
475         }
476         if (popen_pid == -1)
477                 return(NULL);
478         close(tst(p[READ], p[WRITE]));
479         return(tst(p[WRITE], p[READ]));
480 }
481 .P2
482 The sequence of
483 .UL close s
484 in the child
485 is a bit tricky.
486 Suppose
487 that the task is to create a child process that will read data from the parent.
488 Then the first
489 .UL close
490 closes the write side of the pipe,
491 leaving the read side open.
492 The lines
493 .P1
494 close(tst(0, 1));
495 dup(tst(p[READ], p[WRITE]));
496 .P2
497 are the conventional way to associate the pipe descriptor
498 with the standard input of the child.
499 The 
500 .UL close
501 closes file descriptor 0,
502 that is, the standard input.
503 .UL dup
504 is a system call that
505 returns a duplicate of an already open file descriptor.
506 File descriptors are assigned in increasing order
507 and the first available one is returned,
508 so
509 the effect of the
510 .UL dup
511 is to copy the file descriptor for the pipe (read side)
512 to file descriptor 0;
513 thus the read side of the pipe becomes the standard input.
514 (Yes, this is a bit tricky, but it's a standard idiom.)
515 Finally, the old read side of the pipe is closed.
516 .PP
517 A similar sequence of operations takes place
518 when the child process is supposed to write
519 from the parent instead of reading.
520 You may find it a useful exercise to step through that case.
521 .PP
522 The job is not quite done,
523 for we still need a function
524 .UL pclose
525 to close the pipe created by
526 .UL popen .
527 The main reason for using a separate function rather than
528 .UL close
529 is that it is desirable to wait for the termination of the child process.
530 First, the return value from
531 .UL pclose
532 indicates whether the process succeeded.
533 Equally important when a process creates several children
534 is that only a bounded number of unwaited-for children
535 can exist, even if some of them have terminated;
536 performing the
537 .UL wait
538 lays the child to rest.
539 Thus:
540 .P1
541 #include <signal.h>
542
543 pclose(fd)      /* close pipe fd */
544 int fd;
545 {
546         register r, (*hstat)(), (*istat)(), (*qstat)();
547         int      status;
548         extern int popen_pid;
549
550         close(fd);
551         istat = signal(SIGINT, SIG_IGN);
552         qstat = signal(SIGQUIT, SIG_IGN);
553         hstat = signal(SIGHUP, SIG_IGN);
554         while ((r = wait(&status)) != popen_pid && r != -1);
555         if (r == -1)
556                 status = -1;
557         signal(SIGINT, istat);
558         signal(SIGQUIT, qstat);
559         signal(SIGHUP, hstat);
560         return(status);
561 }
562 .P2
563 The calls to
564 .UL signal
565 make sure that no interrupts, etc.,
566 interfere with the waiting process;
567 this is the topic of the next section.
568 .PP
569 The routine as written has the limitation that only one pipe may
570 be open at once, because of the single shared variable
571 .UL popen_pid ;
572 it really should be an array indexed by file descriptor.
573 A
574 .UL popen
575 function, with slightly different arguments and return value is available
576 as part of the standard I/O library discussed below.
577 As currently written, it shares the same limitation.