c++ - child 没有在 fork 中正确终止

标签 c++ exec fork strcpy

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

我需要在进程中有一个分支,以便所有工作都在子进程中完成。唯一的问题是 child 不会正确终止并执行命令。当我在没有 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

最佳答案

你说:

Its the line of code where I call strcpy.

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

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

The stpcpy() and strcpy() functions copy the string s2 to s1 (including the terminating `\0' character).

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

关于c++ - child 没有在 fork 中正确终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5691446/

相关文章:

C 命名管道 (fifo)。父进程卡住

c++ - 在 MessageBox husing 句柄中显示 dll 的路径

c++ - NVidia NVML nvmlDeviceGetMemoryInfo() 立即加载和卸载 nvapi64.dll

c++ - 如何扩展库类的功能?

c# - 如何从 Visual Studio 将可执行文件发布到我的 Azure Function?

C 程序告诉用户哪个子进程首先完成

c++ - 求大 n 和 k 模 m 的二项式系数

java - 如何知道用户何时关闭 Java 中使用 exec() 执行的程序

C - 父进程无限期地等待运行 authopen 的 fork 子进程

linux - 对于 jailed Linux 用户,forkpty 失败