【问题标题】:Child doesn't terminate correctly in fork孩子没有在叉子中正确终止
【发布时间】:2023-03-29 15:59:01
【问题描述】:

我正在为一个小型 shell 类编写一个 c 程序。用户输入一个命令,代码使用exec() 函数执行它。

我需要在进程中有一个 fork,以便所有工作都在子进程中完成。唯一的问题是孩子不会正确终止并执行命令。当我在没有 fork 的情况下运行代码时,它可以完美地执行命令。

问题似乎来自我创建要在execv 调用中使用的字符串。这是我调用strcpy 的代码行。如果我将其注释掉,一切正常。我也尝试将其更改为strncat,但遇到了同样的问题。我不知道是什么原因造成的,欢迎任何帮助。

#include <sys/wait.h>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

string *tokenize(string line);
void setCommand(string *ary);

string command;
static int argument_length;

int main() {
    string argument;
    cout << "Please enter a unix command:\n";
    getline(cin, argument);
    string *ary = tokenize(argument);

    //begin fork process
    pid_t pID = fork();
    if (pID == 0) { // child
        setCommand(ary);

        char *full_command[argument_length];
        for (int i = 0; i <= argument_length; i++) {
            if (i == 0) {
                full_command[i] = (char *) command.c_str();
                //  cout<<"full_command " <<i << " = "<<full_command[i]<<endl;
            } else if (i == argument_length) {
                full_command[i] = (char *) 0;
            } else {
                full_command[i] = (char *) ary[i].c_str();
            //  cout<<"full_command " <<i << " = "<<full_command[i]<<endl;
            }
        }    

        char* arg1;
        const char *tmpStr=command.c_str();        
        strcpy(arg1, tmpStr);
        execv((const char*) arg1, full_command);
        cout<<"I'm the child"<<endl;
    } else if (pID < 0) { //error
        cout<<"Could not fork"<<endl;
    } else { //Parent
        int childExitStatus;
        pid_t wpID = waitpid(pID, &childExitStatus, WCONTINUED);
        cout<<"wPID = "<< wpID<<endl;
        if(WIFEXITED(childExitStatus))
            cout<<"Completed "<<ary[0]<<endl;
        else
            cout<<"Could not terminate child properly."<<WEXITSTATUS(childExitStatus)<<endl;
    }

    // cout<<"Command = "<<command<<endl;
    return 0;
}

string *tokenize(string line) //splits lines of text into seperate words
{
    int counter = 0;
    string tmp = "";
    istringstream first_ss(line, istringstream::in);
    istringstream second_ss(line, istringstream::in);

    while (first_ss >> tmp) {
        counter++;
    }

    argument_length = counter;
    string *ary = new string[counter];
    int i = 0;
    while (second_ss >> tmp) {
        ary[i] = tmp;
        i++;
    }

    return ary;
}

void setCommand(string *ary) {
    command = "/bin/" + ary[0];

// codeblock paste stops here

【问题讨论】:

  • 我清理了你的代码,但仍然无法理解你想要做什么。我猜你也不太了解它。向你的导师寻求帮助。

标签: c++ exec fork strcpy


【解决方案1】:

你说:

这是我调用的代码行 strcpy。

您没有分配任何内存来存储您的字符串。 strcpy 的第一个参数是目标指针,并且您正在为该指针使用未初始化的值。从 strcpy 手册页:

char *strcpy(char *s1, const char *s2);

stpcpy() 和 strcpy() 函数将字符串 s2 复制到 s1(包括 终止的 `\0' 字符)。

可能还有其他问题,但这是我首先想到的。

【讨论】:

  • 我现在真的很讨厌自己,因为修复如此简单,但这似乎是确切的问题。我将 arg1 的定义更改为 char arg1[command.length()] 并且它现在可以完美运行。非常感谢!
猜你喜欢
  • 2019-03-13
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 2021-03-21
  • 2021-07-25
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
相关资源
最近更新 更多