• Home
  • Raw
  • Download

Lines Matching +full:in +full:- +full:process

4 libuv offers considerable child process management, abstracting the platform
5 differences and allowing communication with the child process using streams or
8 A common idiom in Unix is for every process to do one thing and do it well. In
9 such a case, a process often uses multiple child processes to achieve tasks
10 (similar to using pipes in shells). A multi-process model with messages
14 A common refrain against event-based programs is that they cannot take
15 advantage of multiple cores in modern computers. In a multi-threaded program
18 workaround can be to launch multiple processes instead, with each process
19 running an event loop, and each process getting assigned to a separate CPU
23 ------------------------
25 The simplest case is when you simply want to launch a process and know when it
32 :lines: 6-8,15-
33 :emphasize-lines: 11,13-17
44 ``uv_process_options_t``. To simply launch a process, you need to set only the
50 After the call to ``uv_spawn``, ``uv_process_t.pid`` will contain the process
51 ID of the child process.
62 :lines: 9-12
63 :emphasize-lines: 3
65 It is **required** to close the process watcher after the process exits.
67 Changing process parameters
68 ---------------------------
70 Before the child process is launched you can control the execution environment
71 using fields in ``uv_process_options_t``.
81 ``uv_process_options_t.env`` is a null-terminated array of strings, each of the
82 form ``VAR=VALUE`` used to set up the environment variables for the process. Set
83 this to ``NULL`` to inherit the environment from the parent (this) process.
89 modifies the child process behaviour:
91 * ``UV_PROCESS_SETUID`` - sets the child's execution user ID to ``uv_process_options_t.uid``.
92 * ``UV_PROCESS_SETGID`` - sets the child's execution group ID to ``uv_process_options_t.gid``.
97 * ``UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS`` - No quoting or escaping of
99 * ``UV_PROCESS_DETACHED`` - Starts the child process in a new session, which
100 will keep running after the parent process exits. See example below.
103 -------------------
113 :lines: 9-30
114 :emphasize-lines: 12,19
117 won't exit. Use ``uv_unref()`` if you want to be more *fire-and-forget*.
120 ----------------------------
124 ``SIGINT`` and ``SIGKILL``, lead to termination of the process. The signature
131 the pid. In this case, **remember to call** ``uv_close`` on the watcher _after_
135 -------
151 :emphasize-lines: 17-18,27-28
156 in that it will process only one event. UV_RUN_ONCE blocks if there are no
161 Send ``SIGUSR1`` to the process, and you'll find the handler being invoked
168 Child Process I/O
169 -----------------
171 A normal, newly spawned process has its own set of file descriptors, with 0,
174 applications launches a sub-command and you want any errors to go in the log
176 child be the same as the stderr of the parent. In this case, libuv supports
177 *inheriting* file descriptors. In this sample, we invoke the test program,
180 .. rubric:: proc-streams/test.c
181 .. literalinclude:: ../../code/proc-streams/test.c
184 The actual program ``proc-streams`` runs this while sharing only ``stderr``.
185 The file descriptors of the child process are set using the ``stdio`` field in
190 .. code-block:: c
208 .. rubric:: proc-streams/main.c
209 .. literalinclude:: ../../code/proc-streams/main.c
212 :lines: 15-17,27-
213 :emphasize-lines: 6,10,11,12
215 If you run ``proc-stream`` you'll see that only the line "This is stderr" will
219 to ``UV_INHERIT_STREAM`` and setting ``data.stream`` to the stream in the
220 parent process, the child process can treat that stream as standard I/O. This
238 :lines: 49-63
239 :emphasize-lines: 10
248 :lines: 16, 25-45
249 :emphasize-lines: 8-9,18,20
253 read/write buffering to the operating system, so in terms of convenience this
258 Parent-child IPC
259 ----------------
262 settings ``uv_stdio_container_t.flags`` to a bit-wise combination of
264 read/write flag is from the perspective of the child process. In this case,
272 but supports any streaming file-like objects. On Windows, the only object of
281 .. _pipe(7): https://man7.org/linux/man-pages/man7/pipe.7.html
282 .. _mkfifo(1): https://man7.org/linux/man-pages/man1/mkfifo.1.html
283 .. _socketpair(2): https://man7.org/linux/man-pages/man2/socketpair.2.html
284 .. _Unix Domain Socket: https://man7.org/linux/man-pages/man7/unix.7.html
285 .. _Named Pipe: https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipes
288 Arbitrary process IPC
291 Since domain sockets [#]_ can have a well known name and a location in the
292 file-system they can be used for IPC between unrelated processes. The D-BUS_
298 .. _D-BUS: https://www.freedesktop.org/wiki/Software/dbus
300 When using domain sockets, a client-server pattern is usually followed with the
302 messaging is no different from TCP, so we'll re-use the echo server example.
304 .. rubric:: pipe-echo-server/main.c
305 .. literalinclude:: ../../code/pipe-echo-server/main.c
308 :lines: 70-
309 :emphasize-lines: 5,10,14
311 We name the socket ``echo.sock`` which means it will be created in the local
315 $ socat - /path/to/socket
325 .. _socat: http://www.dest-unreach.org/socat/
332 to hand off their I/O to other processes. Applications include load-balancing
337 clients to worker processes in a round-robin fashion. This program is a bit
338 involved, and while only snippets are included in the book, it is recommended
341 The worker process is quite simple, since the file-descriptor is handed over to
344 .. rubric:: multi-echo-server/worker.c
345 .. literalinclude:: ../../code/multi-echo-server/worker.c
348 :lines: 7-9,81-
349 :emphasize-lines: 6-8
351 ``queue`` is the pipe connected to the master process on the other end, along
354 inter-process communication! Since the master will write the file handle to the
358 .. rubric:: multi-echo-server/worker.c
359 .. literalinclude:: ../../code/multi-echo-server/worker.c
362 :lines: 51-79
363 :emphasize-lines: 10,15,20
368 Although ``accept`` seems odd in this code, it actually makes sense. What
377 .. rubric:: multi-echo-server/main.c
378 .. literalinclude:: ../../code/multi-echo-server/main.c
381 :lines: 9-13
383 The ``child_worker`` structure wraps the process, and the pipe between the
384 master and the individual process.
386 .. rubric:: multi-echo-server/main.c
387 .. literalinclude:: ../../code/multi-echo-server/main.c
390 :lines: 51,61-95
391 :emphasize-lines: 17,20-21
393 In setting up the workers, we use the nifty libuv function ``uv_cpu_info`` to
396 argument as 1. We then indicate that the child process' ``stdin`` is to be
401 It is in ``on_new_connection`` (the TCP infrastructure is initialized in
403 worker in the round-robin.
405 .. rubric:: multi-echo-server/main.c
406 .. literalinclude:: ../../code/multi-echo-server/main.c
409 :lines: 31-49
410 :emphasize-lines: 9,12-13
413 passing in the handle (``client``) as the right argument. With this our
414 multi-process echo server is operational.
416 Thanks to Kyle for `pointing out`_ that ``uv_write2()`` requires a non-empty
421 ----
423 .. [#] In this section domain sockets stands in for named pipes on Windows as