os_cp:fork_exec_pipes
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| os_cp:fork_exec_pipes [2024/05/08 11:31] – utworzono jkonczak | os_cp:fork_exec_pipes [2026/05/20 18:31] (current) – jkonczak | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| To create a new process, POSIX defines the '' | To create a new process, POSIX defines the '' | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| < | < | ||
| Line 14: | Line 14: | ||
| \\ | \\ | ||
| **Upon success, '' | **Upon success, '' | ||
| - | process, and the value of '' | + | process, and the value of '' |
| \\ | \\ | ||
| Fork may fail if the resource limits are exhausted, and in such case the | Fork may fail if the resource limits are exhausted, and in such case the | ||
| Line 20: | Line 20: | ||
| There is a list of things that '' | There is a list of things that '' | ||
| - | child process upon '' | + | child process upon '' |
| + | \\ See POSIX standard or | ||
| + | [[https:// | ||
| + | for details. | ||
| + | \\ | ||
| Notice that: | Notice that: | ||
| * all stack and heap memory is copied | * all stack and heap memory is copied | ||
| Line 33: | Line 37: | ||
| To learn its own process identifier, the process can execute | To learn its own process identifier, the process can execute | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| < | < | ||
| Line 39: | Line 43: | ||
| To learn the process identifier of its parent, the process can execute | To learn the process identifier of its parent, the process can execute | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| < | < | ||
| Line 57: | Line 61: | ||
| \\ | \\ | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| '' | '' | ||
| Line 91: | Line 95: | ||
| \\ | \\ | ||
| Run the program and observe it in a live process viewer. \\ | Run the program and observe it in a live process viewer. \\ | ||
| - | < | + | < |
| '' | '' | ||
| - | '' | + | '' |
| C standard includes now the equivalent '' | C standard includes now the equivalent '' | ||
| \\ | \\ | ||
| Line 117: | Line 121: | ||
| So, typically to start a new process, one has to '' | So, typically to start a new process, one has to '' | ||
| < | < | ||
| - | prog1 ,------. | + | prog1 , |
| - | -------------------| fork |-------------------------------------- | + | -------------------| fork |-------------------------------------------------- |
| - | pid: x (ppid: y) | + | pid: x (ppid: y) |
| - | \ prog1 ,-------. prog2 | + | \ |
| - | `-------------------| exec… |-------- | + | |
| - | | + | |
| + | pid: z (ppid: x) | ||
| </ | </ | ||
| To this end, a family of functions starting with '' | To this end, a family of functions starting with '' | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| '' | '' | ||
| - | '' | + | '' |
| '' | '' | ||
| '' | '' | ||
| Line 144: | Line 149: | ||
| following '' | following '' | ||
| - | Importantly, | + | Importantly, |
| + | the exception of files on which the programmer explicitly set the // | ||
| + | flag for the file descriptor, for instance by passing '' | ||
| + | upon opening the file or by issuing a '' | ||
| + | file.)). | ||
| \\ | \\ | ||
| Almost all resources are released. See the documentation for other exceptions. | Almost all resources are released. See the documentation for other exceptions. | ||
| Line 155: | Line 164: | ||
| < | < | ||
| <code c> | <code c> | ||
| - | char arg0[] = " | ||
| - | char arg1[] = " | ||
| # Argument list: | # Argument list: | ||
| - | execlp(" | + | execlp(" |
| - | # Argument vector: | + | |
| - | char *argv[] = {arg0, arg1, arg2, arg3, NULL}; | + | # Argument vector: |
| + | char *argv[] = {" | ||
| execvp(" | execvp(" | ||
| </ | </ | ||
| Line 192: | Line 200: | ||
| < | < | ||
| one must have in the source code the following lines: | one must have in the source code the following lines: | ||
| + | < | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| Line 200: | Line 209: | ||
| (Normally, to access such variables of the running process one shall use the | (Normally, to access such variables of the running process one shall use the | ||
| '' | '' | ||
| - | '' | + | '' |
| functions. \\ | functions. \\ | ||
| < | < | ||
| Line 217: | Line 226: | ||
| < | < | ||
| To measure time, you can use the following C11 code: | To measure time, you can use the following C11 code: | ||
| + | < | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| Line 226: | Line 236: | ||
| double elapsedSec = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; | double elapsedSec = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; | ||
| </ | </ | ||
| + | < | ||
| + | Instead of the C11 function '' | ||
| + | use the POSIX function '' | ||
| + | < | ||
| </ | </ | ||
| Line 239: | Line 253: | ||
| POSIX defines the following functions: \\ | POSIX defines the following functions: \\ | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| '' | '' | ||
| Line 297: | Line 311: | ||
| wrote something. | wrote something. | ||
| - | When a function | + | When a function |
| - | waits for something to happen, then it **blocks**. | + | the OS waits for something to happen |
| + | the function | ||
| \\ | \\ | ||
| - | Functions that may block are called **blocking**. (Cf. definition of | + | Functions that may ever block are called **blocking**. (Cf. definition of |
| - | [[https:// | + | [[https:// |
| in POSIX standard.) | in POSIX standard.) | ||
| Line 321: | Line 336: | ||
| \\ | \\ | ||
| To set/clear the O_NONBLOCK flag, one shall first read the flags with | To set/clear the O_NONBLOCK flag, one shall first read the flags with | ||
| - | '' | + | '' |
| then set/clear the flag (e.g., '' | then set/clear the flag (e.g., '' | ||
| new flags with '' | new flags with '' | ||
| Line 332: | Line 347: | ||
| \\ | \\ | ||
| < | < | ||
| - | <a href=" | + | <a href=" |
| '' | '' | ||
| < | < | ||
| \\ | \\ | ||
| The ''// | The ''// | ||
| - | for writing. | + | for writing. |
| \\ | \\ | ||
| Pipes can be used to send data from one process to another process, or from | Pipes can be used to send data from one process to another process, or from | ||
| Line 366: | Line 381: | ||
| end of a pipe by providing a path to the file. A FIFO file can be created with | end of a pipe by providing a path to the file. A FIFO file can be created with | ||
| '' | '' | ||
| - | '' | + | '' |
| function. | function. | ||
| \\ | \\ | ||
| Line 378: | Line 393: | ||
| < | < | ||
| equivalent. See '' | equivalent. See '' | ||
| - | for details.</ | + | for details. |
| + | \\ | ||
| + | POSIX standard also defines (but not mandates) | ||
| + | [[https:// | ||
| + | </ | ||
| ~~Exercise.# | ~~Exercise.# | ||
os_cp/fork_exec_pipes.1715167914.txt.gz · Last modified: (external edit)