File: forkfuns.tm

package info (click to toggle)
slang2 2.3.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,488 kB
  • sloc: ansic: 101,756; sh: 3,435; makefile: 1,046; pascal: 440
file content (143 lines) | stat: -rw-r--r-- 5,461 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
\function{fork}
\synopsis{Create a new process via the fork system function}
\usage{Int_Type fork ()}
\description
 The \ifun{fork} function creates a new child process via the
 \exmp{fork} system function.  See the approriate documentation for
 the actual semantics involved such as what is preserved in the child
 process.  Upon sucess, the function returns a value that is greater
 than 0 to the parent process that represents the child process's id.
 It will return 0 to the child process.  If the fork function fails, a
 value of -1 will be returned and \ivar{errno} set accordingly.
\example
 The following example creates a child process to invoke the ``ls''
 command on a Unix system.  It also illustrates the low-level nature
 of the \ifun{fork} and related system calls.
#v+
    define ls ()
    {
       variable pid = fork ();
       if (pid == -1)
         throw OSError, "fork failed: " + errno_string(errno);

       if ((pid == 0)
            && (-1 == execvp ("ls", ["ls", "-l"])))
         {
           () = fprintf (stderr, "execvp failed: " + errno_string(errno));
           _exit (1);
         }

       forever
         {
           variable status = waitpid (pid, 0);
           if (status == NULL)
             {
               if (errno == EINTR) continue;
               throw OSError, "waitpid failed: " + errno_string(errno);
             }
           return status.exit_status;
         }
     }
#v-
\seealso{waitpid, execv, execvp, execve, _exit, system}
\done

\function{execve, execv, execvp}
\synopsis{Execute a new process}
\usage{Int_Type execve(path, argv[], envp[])}
#v+
  Int_Type execvp(path, argv[])}
  Int_Type execv(path, argv[])}
#v-
\description
  The \ifun{execv} family of functions overlay the current process
  with a new process that corresponds to the program specified by the
  \exmp{path} argument.  If for some reason the function fails, a
  value of -1 will be returned with \ivar{errno} set accordingly.
  Normally the function will not return.

  The \exmp{argv} parameter is an array of strings that will
  correspond to the argument list used when invoking the program.  For
  example, if the invoked program is a C program, the \exmp{argv}
  parameter will correspond to the C program's argv-list.

  The \ifun{execve} function takes an array of strings that will be
  used to initialize the environment of the overlayed program.

  The \ifun{execvp} function will mimick the actions \exmp{/bin/sh} when
  searching for the executable file.
\notes
  These function are wrappers around the corresponding system library
  functions.  See the system documentation for more information.
\seealso{execve, execvp, system, fork, _exit}
\done

\function{_exit}
\synopsis{Exit the current processes}
\usage{_exit(Int_Type status)}
\description
  Like the related \ifun{exit} function, \ifun{_exit} may be used to
  terminate the current process.  One of the differences between the two
  functions is that the \ifun{_exit} will not invoke various ``atexit''
  handlers that are normally run when a program is terminated.  See
  the system-specific C runtime library documentation for more
  information about this function.

  The main use of the \ifun{_exit} function is after the failure of
  one of the \ifun{execv} functions in a child process.
\seealso{fork, execv, execvp, execve, waitpid, exit}
\done

\function{waitpid}
\synopsis{Wait for a specified process}
\usage{Struct_Type waitpid (pid, options)}
\description
  The \exmp{waitpid} function will cause the calling process to wait
  for the child process whose process id is \exmp{pid} to change its
  state, e.g., exit.  It returns information about the process in the
  form of a structure with the following fields:
#v+
    pid           The process-id of the child process, 0 if the process
                    has not changed state.
    exited        Non-zero if the child exited normally.
    exit_status   The exit status of the child.  This field is
                    meaninful only if the exited field is non-zero.
    signal        Non-zero of the child was terminated by a signal.
                    The value of this field is that of the signal.
    coredump      Non-zero if the child produced a core-dump as the
                    result of termination by a signal.
    stopped       Non-zero if the child was stopped via a signal.  The
                    value is that of the signal that stopped it.
    continued     Non-zero if the process was continued.
#v-

  Upon error, the function will return \NULL and set \ifun{errno}
  accordingly.

  The value of the \exmp{options} parameter is the bitwise-or of one
  of the following values:
#v+
    WNOHANG      causes waitpid to return immediately if the child has
                  not terminated.  In this case, the value of the pid
                  field will be 0 if the child is still running.

    WUNTRACED    (see semantics in the OS documentation)
    WCONTINUED   (see semantics in the OS documentation)
#v-
\example
#v+
   s = waitpid (pid, WNOHANG);
   if (s == NULL)
     throw OSError, "waitpid failed: " + errno_string ();
   if (s.pid == 0)
     message ("The child is still running");
   else if (s.exited)
     vmessage ("child exited with status %d", s.exit_status);
   else if (s.signal)
     {
        vmessage ("child terminated by signal %d %s",
                  s.signal, (s.coredump ? "(coredump)" : ""));
     }
#v-
\seealso{fork, new_process}
\done