Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you fork, you now have two processes at that point. Your child will print the <code>HERE</code> message and then call <code>execvp</code>. You do not check the return value of <code>execvp</code> and so it may be returning an error. <code>cmdargs</code> should be a vector - that is, a null-pointer-terminated array of string pointers. Instead, you are passing <code>execvp</code> a string. In other words, it expects a <code>char* []</code>, which is what <code>cmdargs</code> is, however you have previously treated <code>cmdargs</code> incorrectly.</p> <p>For example, you say <code>strcpy((char*)cmdargs, tokens[1]);</code>. This places a string at <code>*cmdargs</code>. A string is an array of zero or more non-zero characters followed by an ascii <code>NUL</code> which is 8 bits wide: </p> <pre><code>char* cmdargs[] is a double pointer you treat cmdargs as a single pointer and feed it to strcpy cmdargs points to: 'H' 'e' 'l' 'l' 'o' '\0' </code></pre> <p>However, this is not what execvp wants. Execvp wants a vector, which looks somewhat more like this:</p> <pre><code>char* cmdargs[] is a double pointer cmdargs[0] is a single pointer cmdargs[0] points to: 'H' 'e' 'l' 'l' 'o' '\0' cmdargs[1] points to: 'w' 'o' 'r' 'l' 'd' '\0' cmdargs[2] is a null pointer, indicating that it is the end of the vector </code></pre> <p>Therefore, execvp is unable to find the end of the vector and fails, returning <code>-1</code>. You do not test for this, so the child process continues back to the top of the loop, as does the parent, and both processes print <code>shell&gt;</code>.</p> <p>EDIT: By the way, the FIRST string in the <code>argv</code> vector SHOULD be the name of the file being executed - in this case, <code>echo</code>, and then the second and third strings should be the first and second 'arguments' - here <code>hello</code> and <code>world</code>. This is the argv vector fed to the program you call, and by convention the first element in that vector is the name of the called program. If you ignore that convention, <code>echo</code> will get terribly confused.</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