Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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
使用`fork()反转字符串` 我试图用C++来使用一个字符串,使用 Frk()/,这样每个进程最多只打印一个字符。我的想法是,在打印每个字符后,我将分叉成一个新进程,结束父进程,然后继续。这是我的密码: #include <string> #include <iostream> #include <unistd.h> /* Recursively print one character at a time, each in a separate process. */ void print_char(std::string str, int index, pid_t pid) { /* If this is the same process, or the beginning of the string has been reached, quit. */ if (pid != 0 || index <= -1) return; std::cout << str[index]; if (index == 0) { std::cout << std::endl; return; } print_char(str, index-1, fork()); } int main(int argc, char** argv) { std::string str(argv[1]); print_char(str, str.length()-1, 0); } #包括 #包括 #包括 /* 一次递归打印一个字符, 每一个都在一个单独的过程中。 */ 无效打印字符(std::string str,int index,pid\u t pid) { /* 如果这是相同的过程, 或者已到达字符串的开头,请退出。 */ 如果(pid!=0 | | index_C++_Process_Fork - Fatal编程技术网

使用`fork()反转字符串` 我试图用C++来使用一个字符串,使用 Frk()/,这样每个进程最多只打印一个字符。我的想法是,在打印每个字符后,我将分叉成一个新进程,结束父进程,然后继续。这是我的密码: #include <string> #include <iostream> #include <unistd.h> /* Recursively print one character at a time, each in a separate process. */ void print_char(std::string str, int index, pid_t pid) { /* If this is the same process, or the beginning of the string has been reached, quit. */ if (pid != 0 || index <= -1) return; std::cout << str[index]; if (index == 0) { std::cout << std::endl; return; } print_char(str, index-1, fork()); } int main(int argc, char** argv) { std::string str(argv[1]); print_char(str, str.length()-1, 0); } #包括 #包括 #包括 /* 一次递归打印一个字符, 每一个都在一个单独的过程中。 */ 无效打印字符(std::string str,int index,pid\u t pid) { /* 如果这是相同的过程, 或者已到达字符串的开头,请退出。 */ 如果(pid!=0 | | index

使用`fork()反转字符串` 我试图用C++来使用一个字符串,使用 Frk()/,这样每个进程最多只打印一个字符。我的想法是,在打印每个字符后,我将分叉成一个新进程,结束父进程,然后继续。这是我的密码: #include <string> #include <iostream> #include <unistd.h> /* Recursively print one character at a time, each in a separate process. */ void print_char(std::string str, int index, pid_t pid) { /* If this is the same process, or the beginning of the string has been reached, quit. */ if (pid != 0 || index <= -1) return; std::cout << str[index]; if (index == 0) { std::cout << std::endl; return; } print_char(str, index-1, fork()); } int main(int argc, char** argv) { std::string str(argv[1]); print_char(str, str.length()-1, 0); } #包括 #包括 #包括 /* 一次递归打印一个字符, 每一个都在一个单独的过程中。 */ 无效打印字符(std::string str,int index,pid\u t pid) { /* 如果这是相同的过程, 或者已到达字符串的开头,请退出。 */ 如果(pid!=0 | | index,c++,process,fork,C++,Process,Fork,看起来,您的代码还可以,但是您在cout方面遇到了问题 尝试仅更改输出行 std::cout << str[index]; std::cout看起来,您的代码还可以,但是您在cout方面遇到了问题 尝试仅更改输出行 std::cout << str[index]; std::cout有更好更简单的方法来反转字符串。您可能需要使用wait()或它的一个亲戚。第一个过程需要派生一个子进程,该子进程将打印字符串的其余部分,并等待它完成后再打印其字符(和换行符)。我还将注意到

看起来,您的代码还可以,但是您在
cout
方面遇到了问题

尝试仅更改输出行

std::cout << str[index];

std::cout看起来,您的代码还可以,但是您在
cout
方面遇到了问题

尝试仅更改输出行

std::cout << str[index];

std::cout有更好更简单的方法来反转字符串。您可能需要使用
wait()
或它的一个亲戚。第一个过程需要派生一个子进程,该子进程将打印字符串的其余部分,并等待它完成后再打印其字符(和换行符)。我还将注意到,我想不出在什么情况下会使用
fork()
作为函数调用的参数。有更好更简单的方法来反转字符串。您可能需要使用
wait()
或它的一个亲戚。第一个进程需要fork一个子进程,该子进程将打印字符串的其余部分,并等待它完成后再打印其字符(和换行符)。我还将观察到,我想不出在什么情况下会使用
fork()
作为函数调用的参数。