【问题标题】:C programming return 0 does not terminate the program [closed]C编程返回0不会终止程序[关闭]
【发布时间】:2013-01-26 02:57:21
【问题描述】:

我为我的操作系统课程编写了这个 shell,由于某种原因,当我键入“exit”时我的 main 函数没有终止,它应该从 while 循环中中断,然后返回 0 到 main,这应该结束程序,但它只是继续运行。

事实上,它会告诉我它“即将退出”,然后它会返回到 while 循环,并照常运行。

有人看到问题了吗?谢谢大家的帮助

#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);                                                          
}                                                                                  

【问题讨论】:

  • 非常有趣的缩进样式。我不确定我应该将它重新缩进我的风格还是学习你的风格。 :)
  • 这是一种流行的样式,用于通过在前一行的末尾而不是单独一行来保存行。
  • 它叫Egyptian Style。
  • @Lee Taylor 好多了(缩进)
  • 你明白fork是做什么的吗?

标签: c exit terminate


【解决方案1】:

您执行了一个 fork(),它位于您正在检查命令“exit”的子进程中。子进程确实退出了,但父进程继续运行。你应该做的是检查内部命令(如“退出”)在你调用 fork()。

【讨论】:

  • 这解决了我的循环问题,谢谢
【解决方案2】:

嗯,它确实退出了。问题是你正在分叉执行。你需要小心你所做的事情。我假设如果一个子进程退出,它应该退出链上的所有父进程。你这样做:

pid = wait(ret_status);

但永远不要对结果做任何事情。顺便说一句,仅仅因为wait 需要一个指针并不意味着您应该将一个未初始化的指针传递给它。你定义:

int *ret_status;

你真正应该做的是:

int ret_status;
pid = wait(&ret_status);

然后测试ret_status的值。如果它表明你(父进程)应该退出,那么你必须break。

【讨论】:

  • 啊,这很有意义。谢谢
【解决方案3】:

在子进程完成工作后尝试杀死它

 kill(pid);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多