Note that there are some explanatory texts on larger screens.

plurals
  1. POC programming return 0 does not terminate the program
    primarykey
    data
    text
    <p>I wrote this shell for my operating systems course, and for some reason my main function doesn't terminate when I type "exit", its supposed to break from the while loop and then return 0 to main, which should end the program, but instead it just keeps going. </p> <p>In fact, it will tell me its "a bout to exit" and then it will go back into the while loop where it keeps running as usual.</p> <p>Anybody see the problem? Thank you for all your help</p> <pre><code>#include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;string.h&gt; #define LENGTH 1000 #define MAXCMD 11 void init(char *temp); void clean(char **orderedIds); int main (int argc, char *argv[], char *envp[]) { char temp[LENGTH]; char * tok; char c = '\0'; int *ret_status; pid_t pid; printf("\n$"); int i, j = 0; while(c !=EOF) { //while not ^D // Source: LinuxGazzette Ramankutty c = getchar(); if(c=='\n') { //enter command char **orderedIds = malloc(MAXCMD * sizeof(char*)); for (i=0; i&lt;MAXCMD; i++) { orderedIds[i] = malloc(MAXCMD * sizeof(char*)); } int k=0; tok = strtok(temp, " "); while (tok !=NULL) { strcpy(orderedIds[k], tok); k++; tok = strtok (NULL, " "); } orderedIds[k] = NULL; //END with NULL init(temp); //initialize the array pid = fork(); if (pid != 0) { //printf("Waiting for child (%d)\n", pid); pid = wait(ret_status); } else { if (strcmp(orderedIds[0],"exit")==0) { //check for exit printf("Thank you, now exiting\n"); break; } if (execvp(orderedIds[0], orderedIds)) { printf("%s\n", orderedIds[0]); puts(strerror(errno)); exit(127); } } clean(orderedIds); printf("%s", &amp;c); printf("$ "); } else { strncat(temp, &amp;c, 1); } } printf("\n I'm a bout to exit!"); return 0; exit(0); } void init(char *temp) { //Function to initialize/rest=et the array int i; for(i=0; i&lt;LENGTH; i++) { temp[i] = 0; } } void clean(char **orderedIds) { //garbage collection int i; for(i=0; i&lt;MAXCMD; i++) { free(orderedIds[i]); } free(orderedIds); } </code></pre>
    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.
 

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