我正在使用系统 api 启动一个命令(我可以将这个 api 与C/C++一起使用)。我传递的命令有时可能会挂起,因此我想在特定超时后终止。
目前我将它用作:
system("COMMAND");
Run Code Online (Sandbox Code Playgroud)
我想像这样使用它:
使用独立于系统的 API 运行命令(我不想使用 CreateProcess,因为它仅适用于 Windows)如果它在“X”分钟后没有退出,则终止该进程。
我知道fork() sys_call从线程调用是一个坏主意.但是,如果线程使用fork()?创建新进程会发生什么?
新进程将是创建线程的主线程的子进程.我认为.
如果其父级首先完成,则新进程将附加到init进程.它的父级是主线程,而不是创建它的线程.
如果我错了,请纠正我.
#include <stdio.h>
#include <pthread.h>
int main ()
{
thread_t pid;
pthread_create(&(pid), NULL, &(f),NULL);
pthread_join(tid, NULL);
return 0;
}
void* f()
{
int i;
i = fork();
if (i < 0) {
// handle error
} else if (i == 0) // son process
{
// Do something;
} else {
// Do something;
}
}
Run Code Online (Sandbox Code Playgroud) 我需要编写一个C程序来检查其他程序的输出.它应该基本上像这样工作:
./otherprogram|./myprogram
但我找不到如何从stdout(或管道)逐行读取.然后将所有这些写入stdout.
如本答案中所述,我尝试在 Visual Studio 项目中执行 Windows 命令并在 C++ 中获取输出。
std::string executeCommand (const char* cmd) {
char buffer[128];
std::string result = "";
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
问题是,这不是编译。给出了以下错误:
Error: identifier "popen" is undefined.
Error: identifier "pclose" is undefined.
Run Code Online (Sandbox Code Playgroud) http://pstreams.sourceforge.net/ pstreams显然是一个非常简单的库,为C++重新实现了popen().
该库安装非常简单,只包含一个头文件.您可以在此处下载头文件并将其添加到您的应用程序中:http:
//pstreams.cvs.sourceforge.net/viewvc/pstreams/pstreams/pstream.h?view = log
我认为我想要的非常简单:向系统发送命令并获取其输出.pstreams的主页(上图)和文档提供了以下示例:
redi::ipstream in("ls ./*.h");
std::string str;
while (in >> str) {
std::cout << str << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它似乎运行良好,但代码实际上从输出中剥离所有空格和回车.试试这个:
redi::ipstream in("ls -l"); /* The command has a slightly more complicated output. */
std::string str;
while (in >> str) {
std::cout << str
}
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)
我们得到一个没有任何空间的looong连接字符串.
我尽力理解文档,最接近解决方案的是:
redi::ipstream in("ls -l");
std::string str;
while (!in.rdbuf()->exited()) {
in >> str;
}
std::cout << str << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是,输出在第一个空格处完全截断.
我受到了阻碍.
相关问题:
c ++中的popen等价
如何使用POSIX执行命令并在C++中获取命令输出?
主持人请注意:此问题和上述其他两个问题需要pstreams标记.
有没有办法从 c++ 运行 linux 命令 ls,并在 c++ 中获取存储在一个数组中的所有输出?
谢谢
我正在使用 MS Visual Studio 2008 进行 C 编码。
我知道我们可以使用“ int system(const char *command)”来执行命令。
有没有其他方法可以在C程序中执行系统命令。我还需要将执行命令的输出存储在变量中。
system()函数执行命令并将输出发送到stdout,有什么方法可以读取stdout变量并将其存储在变量中。
所以我的最终目标是在 Windows 的 C 程序中执行系统命令(使用 Visual Studio)并将该命令的输出存储在变量中。有什么建议 ?
我正在使用以下代码尝试df在Linux中读取命令的结果popen.
#include <iostream> // file and std I/O functions
int main(int argc, char** argv) {
FILE* fp;
char * buffer;
long bufSize;
size_t ret_code;
fp = popen("df", "r");
if(fp == NULL) { // head off errors reading the results
std::cerr << "Could not execute command: df" << std::endl;
exit(1);
}
// get the size of the results
fseek(fp, 0, SEEK_END);
bufSize = ftell(fp);
rewind(fp);
// allocate the memory to contain the results
buffer = (char*)malloc( sizeof(char) …Run Code Online (Sandbox Code Playgroud) 我有一个c ++应用程序包含队列中的某些项目,然后这些项目将由python脚本处理.我想要它,以便最多运行10个python脚本实例.我计划使用execl()来启动python进程,有没有办法告诉进程已经退出而不必将消息传递回父进程?
我编写了一个程序,在主程序中创建一个线程,并用于system()从该线程启动另一个进程。我也使用主函数中的 来启动相同的过程system()。即使父进程死亡,从线程启动的进程似乎仍保持活动状态。但是从 main 函数调用的函数会随着父函数一起死亡。任何想法为什么会发生这种情况。
请找到下面的代码结构:
void *thread_func(void *arg)
{
system(command.c_str());
}
int main()
{
pthread_create(&thread_id, NULL, thread_func, NULL);
....
system(command.c_str());
while (true)
{
....
}
pthread_join(thread_id, NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 子进程运行一个 bin 文件,该文件由 Qualcomm 提供。子进程由我开发的父进程调用。
子进程运行时,总会在shell命令中打印大量日志。
那么,我是否能够将 Qualcomm 的输出从 stdout 重定向到父进程中的另一个文件?
如您所知,推动高通更新此 bin 文件几乎是不可能的。
我在我的 C 代码中使用函数 execl() 执行 grep 命令,并且我想在我的 C 程序中使用这个命令的输出。我该怎么做?