Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It really depends on what you're trying to do, exactly, as it's:</p> <ol> <li>OS dependent</li> <li>Not quite clear what you're trying to do.</li> </ol> <p>Nevertheless, I'll try to provide some information for you to decide.<br> On UNIX, <code>fork()</code> creates a clone of your process from the place where you called fork. Meaning, if I have the following process:</p> <pre><code>#include &lt;unistd.h&gt; #include &lt;stdio.h&gt; int main() { printf( "hi 2 u\n" ); int mypid = fork(); if( 0 == mypid ) printf( "lol child\n" ); else printf( "lol parent\n" ); return( 0 ); } </code></pre> <p>The output will look as follows:</p> <blockquote> <blockquote> <p>hi 2 u<br> lol child<br> lol parent </p> </blockquote> </blockquote> <p>When you <code>fork()</code> the pid returned in the child is 0, and the pid returned in the parent is the child's pid. Notice that "hi2u" is only printed once... by the <strong>parent</strong>.</p> <p><code>execve()</code> and its family of functions are almost always used with <code>fork().</code> <code>execve()</code> and the like overwrite the current stackframe with the name of the application you pass to it. <code>execve()</code> is almost always used with <code>fork()</code> where you fork a child process and if you're the parent you do whatever you need to keep doing and if you're the child you exec a new process. <code>execve()</code> is also almost always used with <code>waitpid()</code> -- waitpid takes a pid of a child process and, quite literally, <em>waits</em> until the child terminates and returns the child's exit status to you. </p> <p>Using this information, you should be able to write a very basic shell; one that takes process names on the command line and runs processes you tell it to. Of course, shells do more than that, like piping input and output, but you should be able to accomplish the basics using <code>fork()</code>, <code>execve()</code> and <code>waitpid()</code>.</p> <p><strong>NOTE: This is *nix specific! This will NOT work on Windows.</strong> </p> <p>Hope this helped.</p>
 

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