File: proc_lib.html

package info (click to toggle)
erlang-doc-html 1%3A11.b.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 23,284 kB
  • ctags: 10,724
  • sloc: erlang: 505; ansic: 323; makefile: 62; perl: 61; sh: 45
file content (392 lines) | stat: -rw-r--r-- 14,329 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
  <TITLE>proc_lib</TITLE>
  <SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
  <STYLE TYPE="text/css">
<!--
    .REFBODY     { margin-left: 13mm }
    .REFTYPES    { margin-left: 8mm }
-->
  </STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
      ALINK="#FF0000">
<!-- refpage -->
<CENTER>
<A HREF="http://www.erlang.se">
  <IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif">
</A>
<H1>proc_lib</H1>
</CENTER>

<H3>MODULE</H3>
<DIV CLASS=REFBODY>
proc_lib
</DIV>

<H3>MODULE SUMMARY</H3>
<DIV CLASS=REFBODY>
Plug-in Replacements for spawn/1,2,3,4,
spawn_link/1,2,3,4, and spawn_opt/2,3,4,5.

</DIV>

<H3>DESCRIPTION</H3>
<DIV CLASS=REFBODY>

<P>The <CODE>proc_lib</CODE> module is used to initialize some useful
information when a process starts. The registered names, or the
process identities, of the <CODE>parent</CODE> process, and the parent
ancestors, are stored together with information about the
function initially called in the process.


<P>A crash report is generated if the process terminates with
a reason other than <CODE>normal</CODE> or <CODE>shutdown</CODE>.
<CODE>shutdown</CODE> is used to terminate an abnormal process in a
controlled manner. A crash report contains the previously stored
information such as ancestors and initial function, the
termination reason, and information regarding other processes
which terminate as a result of this process terminating.
<P>The crash report is sent to the <CODE>error_logger</CODE>. An event
handler has to be installed in the <CODE>error_logger</CODE> event
manager in order to handle these reports. The crash report is
tagged <CODE>crash_report</CODE> and the <CODE>format/1</CODE> function
should be called in order to format the report.

</DIV>

<H3>EXPORTS</H3>

<P><A NAME="spawn/1"><STRONG><CODE>spawn(Fun) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn/2"><STRONG><CODE>spawn(Node,Fun) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn/3"><STRONG><CODE>spawn(Module,Func,Args) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn/4"><STRONG><CODE>spawn(Node,Module,Func,Args) -&#62; Pid</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Fun = fun() -&#62; void()</CODE></STRONG><BR>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Func = atom()</CODE></STRONG><BR>
<STRONG><CODE>Args = [Arg]</CODE></STRONG><BR>
<STRONG><CODE>Arg = term()</CODE></STRONG><BR>
<STRONG><CODE>Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Pid = pid()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Spawns a new process and initializes it as described above.
         The process is spawned using the <CODE>spawn</CODE> BIFs. The
         process can be spawned on another <CODE>Node</CODE>.

</DIV>

<P><A NAME="spawn_link/1"><STRONG><CODE>spawn_link(Fun) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn_link/2"><STRONG><CODE>spawn_link(Node,Fun) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn_link/3"><STRONG><CODE>spawn_link(Module,Func,Args) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn_link/4"><STRONG><CODE>spawn_link(Node,Module,Func,Args) -&#62; Pid</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Fun = fun() -&#62; void()</CODE></STRONG><BR>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Func = atom()</CODE></STRONG><BR>
<STRONG><CODE>Args = [Arg]</CODE></STRONG><BR>
<STRONG><CODE>Arg = term()</CODE></STRONG><BR>
<STRONG><CODE>Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Pid = pid()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Spawns a new process and initializes it as described above.
        The process is spawned using the <CODE>spawn_link</CODE> BIFs.
        The process can be spawned on another <CODE>Node</CODE>.        

</DIV>

<P><A NAME="spawn_opt/2"><STRONG><CODE>spawn_opt(Fun,Opts) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn_opt/3"><STRONG><CODE>spawn_opt(Node,Fun,Opts) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn_opt/4"><STRONG><CODE>spawn_opt(Module,Func,Args,Opts) -&#62; Pid</CODE></STRONG></A><BR>
<A NAME="spawn_opt/5"><STRONG><CODE>spawn_opt(Node,Module,Func,Args,Opts) -&#62; Pid</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Fun = fun() -&#62; void()</CODE></STRONG><BR>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Func = atom()</CODE></STRONG><BR>
<STRONG><CODE>Args = [Arg]</CODE></STRONG><BR>
<STRONG><CODE>Arg = term()</CODE></STRONG><BR>
<STRONG><CODE>Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Opts = list()</CODE></STRONG><BR>
<STRONG><CODE>Pid = pid()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Spawns a new process and initializes it as described above.
        The process is spawned using the <CODE>spawn_opt</CODE> BIFs. The
        process can be spawned on another <CODE>Node</CODE>.

</DIV>

<P><A NAME="start/3"><STRONG><CODE>start(Module,Func,Args) -&#62; Ret</CODE></STRONG></A><BR>
<A NAME="start/4"><STRONG><CODE>start(Module,Func,Args,Time) -&#62; Ret</CODE></STRONG></A><BR>
<A NAME="start/5"><STRONG><CODE>start(Module,Func,Args,Time,SpawnOpts) -&#62; Ret</CODE></STRONG></A><BR>
<A NAME="start_link/3"><STRONG><CODE>start_link(Module,Func,Args) -&#62; Ret</CODE></STRONG></A><BR>
<A NAME="start_link/4"><STRONG><CODE>start_link(Module,Func,Args,Time) -&#62; Ret</CODE></STRONG></A><BR>
<A NAME="start_link/5"><STRONG><CODE>start_link(Module,Func,Args,Time,SpawnOpts) -&#62; Ret</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Func = atom()</CODE></STRONG><BR>
<STRONG><CODE>Args = [Arg]</CODE></STRONG><BR>
<STRONG><CODE>Arg = term()</CODE></STRONG><BR>
<STRONG><CODE>Time = integer &#62;= 0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>SpawnOpts = list()</CODE></STRONG><BR>
<STRONG><CODE>Ret = term() | {error, Reason}</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Starts a new process synchronously. Spawns the process
         using <CODE>proc_lib:spawn/3</CODE> or
         <CODE>proc_lib:spawn_link/3</CODE>, and waits for the process to
         start. When the process has started, it <STRONG>must</STRONG> call
         <CODE>proc_lib:init_ack(Parent, Ret)</CODE> or
         <CODE>proc_lib:init_ack(Ret)</CODE>, where <CODE>Parent</CODE>
         is the process that evaluates <CODE>start</CODE>. At this time,
         <CODE>Ret</CODE> is returned from <CODE>start</CODE>.

        
<P>If the <CODE>start_link</CODE> function is used and the
         process crashes before <CODE>proc_lib:init_ack</CODE> is called,
         <CODE>{error, Reason}</CODE> is returned if the calling process
         traps exits.

        
<P>If <CODE>Time</CODE> is specified as an integer, this function
         waits for <CODE>Time</CODE> milliseconds for the process to start
         (<CODE>proc_lib:init_ack</CODE>). If it has not
         started within this time, <CODE>{error, timeout}</CODE> is
         returned, and the process is killed.
<P>The <CODE>SpawnOpts</CODE> argument, if given, will be passed
         as the last argument to the <CODE>spawn_opt/4</CODE> BIF. Refer to
         the <CODE>erlang</CODE> module for information about the
         <CODE>spawn_opt</CODE> options.
</DIV>

<P><A NAME="init_ack/2"><STRONG><CODE>init_ack(Parent, Ret) -&#62; void()</CODE></STRONG></A><BR>
<A NAME="init_ack/1"><STRONG><CODE>init_ack(Ret) -&#62; void()</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Parent = pid()</CODE></STRONG><BR>
<STRONG><CODE>Ret = term()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>This function is used by a process that has been started by
         a <CODE>proc_lib:start</CODE> function. It tells <CODE>Parent</CODE>
         that the process has initialized itself, has started, or has
         failed to initialize itself. The <CODE>init_ack/1</CODE> function
         uses the parent value previously stored by the
         <CODE>proc_lib:start</CODE> function. If the <CODE>init_ack</CODE>
         function is not called (e.g. if the init function crashes)
         and <CODE>proc_lib:start/3</CODE> is used, that function never
         returns and the parent hangs forever. This can be avoided by
         using a time out in the call to <CODE>start</CODE>, or by using
         <CODE>start_link</CODE>.
<P>The following example illustrates how this function and
        <CODE>proc_lib:start_link</CODE> are used. 
<PRE>
-module(my_proc).
-export([start_link/0]).
start_link() -&#62;
    proc_lib:start_link(my_proc, init, [self()]).
init(Parent) -&#62;
    case do_initialization() of
        ok -&#62;
            proc_lib:init_ack(Parent, {ok, self()});
        {error, Reason} -&#62;
            exit(Reason)
    end,
    loop().
loop() -&#62;
    receive
        ....
</PRE>

</DIV>

<P><A NAME="format/1"><STRONG><CODE>format(CrashReport) -&#62; string()</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>CrashReport = void()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Formats a previously generated crash report. The formatted
report is returned as a string.

</DIV>

<P><A NAME="initial_call/1"><STRONG><CODE>initial_call(PidOrPinfo) -&#62; {Module,Function,Args} | Fun | false
</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>PidOrPinfo = pid() | {X,Y,Z} | ProcInfo</CODE></STRONG><BR>
<STRONG><CODE>X = Y = Z = int()</CODE></STRONG><BR>
<STRONG><CODE>ProcInfo = [void()]</CODE></STRONG><BR>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Fun = fun() -&#62; void()</CODE></STRONG><BR>
<STRONG><CODE>Function = atom()</CODE></STRONG><BR>
<STRONG><CODE>Args = [term()]</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Extracts the initial call of a process that was spawned
         using the spawn functions described above. <CODE>PidOrPinfo</CODE>
         can either be a Pid, an integer tuple (from which a pid can
         be created), or the process information of a process
         (fetched through an <CODE>erlang:process_info/1</CODE> function
         call).
</DIV>

<P><A NAME="translate_initial_call/1"><STRONG><CODE>translate_initial_call(PidOrPinfo) -&#62; 
{Module,Function,Arity} | Fun</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>PidOrPinfo = pid() | {X,Y,Z} | ProcInfo</CODE></STRONG><BR>
<STRONG><CODE>X = Y = Z = int()</CODE></STRONG><BR>
<STRONG><CODE>ProcInfo = [void()]</CODE></STRONG><BR>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Fun = fun() -&#62; void()</CODE></STRONG><BR>
<STRONG><CODE>Function = atom()</CODE></STRONG><BR>
<STRONG><CODE>Arity = int()</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>Extracts the initial call of a process which was spawned
         using the spawn functions described above. If the initial
         call is to one of the system defined behaviours such as
         <CODE>gen_server</CODE> or <CODE>gen_event</CODE>, it is translated to
         more useful information. If a <CODE>gen_server</CODE> is spawned,
         the returned <CODE>Module</CODE> is the name of the callback
         module and <CODE>Function</CODE> is <CODE>init</CODE> (the function that
         initiates the new server).
<P>A <CODE>supervisor</CODE> and a <CODE>supervisor_bridge</CODE> are also
<CODE>gen_server</CODE> processes. In order to return information
that this process is a supervisor and the name of the
call-back module, <CODE>Module</CODE> is <CODE>supervisor</CODE> and
<CODE>Function</CODE> is the name of the supervisor callback
module. <CODE>Arity</CODE> is <CODE>1</CODE> since the <CODE>init/1</CODE>
function is called initially in the callback module.
<P>By default, <CODE>{proc_lib,init_p,5}</CODE> is returned if no
information about the initial call can be found. It is
assumed that the caller knows that the process has been
spawned with the <CODE>proc_lib</CODE> module.
<P><CODE>PidOrPinfo</CODE> can either be a Pid, an integer tuple
         (from which a pid can be created), or the process
         information of a process (fetched through an
         <CODE>erlang:process_info/1</CODE> function call).
<P>This function is used by the c:i/0 and c:regs/0 functions
         in order to present process information.

</DIV>

<P><A NAME="hibernate/3"><STRONG><CODE>hibernate(Module, Function, Arguments)</CODE></STRONG></A><BR>

<DIV CLASS=REFBODY><P>Types:
  <DIV CLASS=REFTYPES>
<P>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Function = atom()</CODE></STRONG><BR>
<STRONG><CODE>Arguments = [term()]</CODE></STRONG><BR>

  </DIV>
</DIV>

<DIV CLASS=REFBODY>

<P>
<P><CODE>hibernate/3</CODE> gives a way to put a process started
        using one of the functions in the <CODE>proc_lib</CODE> module into
        a wait state where its memory allocation has been reduced as much
        as possible, which is useful if the process does not expect to
        receive any messages in the near future.

        
<P>The process will be awaken when a message is sent to it, and
        control will resume in <CODE>Module:Function</CODE> with the arguments
        given by <CODE>ArgumentList</CODE>.

        
<P>If the process has any message in its message queue, the process
        will be awaken immediately in the same way as described above.

        
<P>Note: The actual work is done by the <CODE>erlang:hibernate/3</CODE> BIF.
        To ensure that exception handling and logging continues to work in
        a process started by <CODE>proc_lib</CODE>, always use <CODE>proc_lib:hibernate</CODE>
        rather than <CODE>erlang:hibernate/3</CODE>.

</DIV>

<H3>See Also</H3>
<DIV CLASS=REFBODY>

<P>error_logger(3)

</DIV>

<H3>AUTHORS</H3>
<DIV CLASS=REFBODY>
Magnus Frberg - support@erlang.ericsson.se<BR>

</DIV>
<CENTER>
<HR>
<SMALL>stdlib 1.14.2<BR>
Copyright &copy; 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>