Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It depends on what you want to do with the script (or any other program you want to run).</p> <p>If you just want to run the script <a href="http://man7.org/linux/man-pages/man3/system.3.html" rel="noreferrer"><code>system</code></a> is the easiest thing to do, but it does some other stuff too, including running a shell and having it run the command (/bin/sh under most *nix).</p> <p>If you want to either feed the shell script via its standard input or consume its standard output you can use <code>popen</code> (and <code>pclose</code>) to set up a pipe. This also uses the shell (/bin/sh under most *nix) to run the command.</p> <p>Both of these are library functions that do a lot under the hood, but if they don't meet your needs (or you just want to experiment and learn) you can also use system calls directly. This also allows you do avoid having the shell (/bin/sh) run your command for you.</p> <p>The system calls of interest are <code>fork</code>, <code>execve</code>, and <code>waitpid</code>. You may want to use one of the library wrappers around <code>execve</code> (type <code>man 3 exec</code> for a list of them). You may also want to use one of the other wait functions (<code>man 2 wait</code> has them all). Additionally you may be interested in the system calls <code>clone</code> and <code>vfork</code> which are related to fork.</p> <p><code>fork</code> duplicates the current program, where the only main difference is that the new process gets 0 returned from the call to fork. The parent process gets the new process's process id (or an error) returned.</p> <p><code>execve</code> replaces the current program with a new program (keeping the same process id).</p> <p><code>waitpid</code> is used by a parent process to wait on a particular child process to finish.</p> <p>Having the fork and execve steps separate allows programs to do some setup for the new process before it is created (without messing up itself). These include changing standard input, output, and stderr to be different files than the parent process used, changing the user or group of the process, closing files that the child won't need, changing the session, or changing the environmental variables.</p> <p>You may also be interested in the <code>pipe</code> and <code>dup2</code> system calls. <code>pipe</code> creates a pipe (with both an input and an output file descriptor). <code>dup2</code> duplicates a file descriptor as a specific file descriptor (<code>dup</code> is similar but duplicates a file descriptor to the lowest available file descriptor).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload