Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++函数运行两次,但只调用一次 我学习C++ [java背景FWI]并尝试编写UNIX shell作为一个项目。我遇到了一个有趣的小问题,将输入标记化以执行。tok函数被调用了两次,我不知道为什么。我当前的测试代码如下: #include <iostream> #include <vector> #include <sstream> #include <unistd.h> #include <signal.h> #include <string.h> #include <sys/wait.h> #include <sys/types.h> using namespace std; void tok(string, char**); int main(){ const char* EXIT = "exit"; string input; cout << "shell>> "; getline(cin, input); pid_t pid = fork(); char* args[64]; //arbitrary size, 64 possible whitespace-delimited tokens in command tok(input, args); return 0; } //copied from http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c void tok(string inStr, char** args){ int last = 0, next = 0, i = 0; while( (next = inStr.find(' ', last)) != -1){ cout << i++ << ": " << inStr.substr(last, next-last) << endl; *args++ = strdup(inStr.substr(last, next-last).c_str()); last = next + 1; } cout << i++ << ": " << inStr.substr(last) << endl; *args++ = strdup(inStr.substr(last).c_str()); *args = '\0'; cout << "done tokenizing..." << endl; }_C++_Shell_Unix - Fatal编程技术网

C++函数运行两次,但只调用一次 我学习C++ [java背景FWI]并尝试编写UNIX shell作为一个项目。我遇到了一个有趣的小问题,将输入标记化以执行。tok函数被调用了两次,我不知道为什么。我当前的测试代码如下: #include <iostream> #include <vector> #include <sstream> #include <unistd.h> #include <signal.h> #include <string.h> #include <sys/wait.h> #include <sys/types.h> using namespace std; void tok(string, char**); int main(){ const char* EXIT = "exit"; string input; cout << "shell>> "; getline(cin, input); pid_t pid = fork(); char* args[64]; //arbitrary size, 64 possible whitespace-delimited tokens in command tok(input, args); return 0; } //copied from http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c void tok(string inStr, char** args){ int last = 0, next = 0, i = 0; while( (next = inStr.find(' ', last)) != -1){ cout << i++ << ": " << inStr.substr(last, next-last) << endl; *args++ = strdup(inStr.substr(last, next-last).c_str()); last = next + 1; } cout << i++ << ": " << inStr.substr(last) << endl; *args++ = strdup(inStr.substr(last).c_str()); *args = '\0'; cout << "done tokenizing..." << endl; }

C++函数运行两次,但只调用一次 我学习C++ [java背景FWI]并尝试编写UNIX shell作为一个项目。我遇到了一个有趣的小问题,将输入标记化以执行。tok函数被调用了两次,我不知道为什么。我当前的测试代码如下: #include <iostream> #include <vector> #include <sstream> #include <unistd.h> #include <signal.h> #include <string.h> #include <sys/wait.h> #include <sys/types.h> using namespace std; void tok(string, char**); int main(){ const char* EXIT = "exit"; string input; cout << "shell>> "; getline(cin, input); pid_t pid = fork(); char* args[64]; //arbitrary size, 64 possible whitespace-delimited tokens in command tok(input, args); return 0; } //copied from http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c void tok(string inStr, char** args){ int last = 0, next = 0, i = 0; while( (next = inStr.find(' ', last)) != -1){ cout << i++ << ": " << inStr.substr(last, next-last) << endl; *args++ = strdup(inStr.substr(last, next-last).c_str()); last = next + 1; } cout << i++ << ": " << inStr.substr(last) << endl; *args++ = strdup(inStr.substr(last).c_str()); *args = '\0'; cout << "done tokenizing..." << endl; },c++,shell,unix,C++,Shell,Unix,我不知道为什么会这样。谁能给我指一下正确的方向吗?谢谢fork函数返回两次,一次在原始流程中,一次在新创建的fork流程中。然后,这两个过程都调用tok 你打电话给福克似乎没有任何明确的理由。因此,修复可能与取消对fork的调用一样简单。fork函数返回两次,一次在原始进程中,一次在新创建的fork进程中。然后,这两个过程都调用tok 你打电话给福克似乎没有任何明确的理由。因此,修复可能很简单,只需取消对fork的调用。下面的代码可以正常工作 #include <iostream>

我不知道为什么会这样。谁能给我指一下正确的方向吗?谢谢

fork函数返回两次,一次在原始流程中,一次在新创建的fork流程中。然后,这两个过程都调用tok

你打电话给福克似乎没有任何明确的理由。因此,修复可能与取消对fork的调用一样简单。

fork函数返回两次,一次在原始进程中,一次在新创建的fork进程中。然后,这两个过程都调用tok


你打电话给福克似乎没有任何明确的理由。因此,修复可能很简单,只需取消对fork的调用。

下面的代码可以正常工作

#include <iostream>
#include <vector>
#include <sstream>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>

using namespace std;

void tok(string, char**);

int main(){
const char* EXIT = "exit";

 string input;

 cout << "shell>> ";
 getline(cin, input);

// pid_t pid = fork();

 char* args[64]; 
 tok(input, args);
 return 0;
}


void tok(string inStr, char** args){
int last = 0, next = 0, i = 0;
while( (next = inStr.find(' ', last)) != -1){
    cout << i++ << ": " <<  inStr.substr(last, next-last) << endl;
    *args++ = strdup(inStr.substr(last, next-last).c_str());
    last = next + 1;
}
cout << i++ << ": " << inStr.substr(last) << endl;
*args++ = strdup(inStr.substr(last).c_str());
*args = '\0';
cout << "done tokenizing..." << endl;
}

下面的代码可以正常工作

#include <iostream>
#include <vector>
#include <sstream>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>

using namespace std;

void tok(string, char**);

int main(){
const char* EXIT = "exit";

 string input;

 cout << "shell>> ";
 getline(cin, input);

// pid_t pid = fork();

 char* args[64]; 
 tok(input, args);
 return 0;
}


void tok(string inStr, char** args){
int last = 0, next = 0, i = 0;
while( (next = inStr.find(' ', last)) != -1){
    cout << i++ << ": " <<  inStr.substr(last, next-last) << endl;
    *args++ = strdup(inStr.substr(last, next-last).c_str());
    last = next + 1;
}
cout << i++ << ": " << inStr.substr(last) << endl;
*args++ = strdup(inStr.substr(last).c_str());
*args = '\0';
cout << "done tokenizing..." << endl;
}
调用fork时,您创建了两个进程。每个进程都有几乎完全相同的状态,除了您收到的各自pid。如果该值大于0,则您在父进程main中,否则您在子进程或fork中失败

在不检查返回的pid_t的情况下,两个进程都将调用tok,从而导致您看到的双重调用行为

在检查pid后隐藏调用,如下所示:

要查看父进程和子进程还有哪些共同点:请检查

调用fork时,创建两个进程。每个进程都有几乎完全相同的状态,除了您收到的各自pid。如果该值大于0,则您在父进程main中,否则您在子进程或fork中失败

在不检查返回的pid_t的情况下,两个进程都将调用tok,从而导致您看到的双重调用行为

在检查pid后隐藏调用,如下所示:


要查看父进程和子进程还有哪些共同点:请检查调用fork然后调用函数的

。当然会叫两次。如果您只希望其中一个进程调用tok,那么需要检查id。我看到的建议是不要使用namespace std调用tok;我明白了。非常感谢。我完全忘了叉子是上面说的。如果您将您的评论作为答案,我将接受它。fork函数复制该进程,并且两个进程在调用fork后从该点继续运行。为什么要调用FooR?我也在学习C/C++——不,你要么学习C语言,要么C++,但是在这一点上,尽管语言是完全不同的,但在3年的时间里,它们一直都是比较好的。祝你学习顺利,但是你的语言很棒。你调用了fork,然后调用了一个函数。当然会叫两次。如果您只希望其中一个进程调用tok,那么需要检查id。我看到的建议是不要使用namespace std调用tok;我明白了。非常感谢。我完全忘了叉子是上面说的。如果您将您的评论作为答案,我将接受它。fork函数复制该进程,并且两个进程在调用fork后从该点继续运行。为什么要调用FooR?我也在学习C/C++——不,你要么学习C语言,要么C++,但是在这一点上,尽管语言是完全不同的,但在3年的时间里,它们一直都是比较好的。祝你学习顺利,但是你的语言很棒。
pid_t pid = fork();
if (pid > 0) // have parent process call tok
{
   char* args[64]; //arbitrary size, 64 possible whitespace-delimited tokens in command
   tok(input, args);
}