Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9289695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:14:35+00:00 2026-06-18T20:14:35+00:00

I wrote this shell for my operating systems course, and for some reason my

  • 0

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.

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.

Anybody see the problem? Thank you for all your help

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#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<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", &c);
            printf("$ ");
        } 
        else 
        {
            strncat(temp, &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<LENGTH; i++)
    {                                                   
        temp[i] = 0;                                                       
    }                                                                          
}

void clean(char **orderedIds)
{
    //garbage collection                                 
    int i;                                                                     
    for(i=0; i<MAXCMD; i++)
    {                                                   
        free(orderedIds[i]);                                               
    }                                                                          
    free(orderedIds);                                                          
}                                                                                  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:14:36+00:00Added an answer on June 18, 2026 at 8:14 pm

    You do a fork(), and it’s in the child process where you are checking for the command “exit”. The child process does indeed exit, but the parent process keeps running. What you should do is check for internal commands (like “exit”) before you call fork().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this short program which has a tiny GUI. Its supposed to allow
I wrote this function to make columns sortable. I want to rearrange divs based
I wrote this function in php to check user/pass against account on linux server.
I am new to linux and its shell programming. I wrote a shell script
I'm trying to implement pipelining in an Operating Systems shell project by creating a
I wrote an application in a JS-based framework called Titanium. This framework doesn't have
This is an excerpt from the instructions of my homework for my operating systems
I have some very specific questions about writing operating systems that I was hoping
I'm trying to develop a shell in Linux as an Operating Systems project. One
Can someone write a PHP script that reproduces the functionality of this linux shell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.