我尝试使用popen从c中获取nvidia-smi命令的输出,如下所示
命令存储在缓冲区字符串中,并使用相同的字符串存储输出:
const size_t pid = getpid();
char pid_s[1000];
sprintf(pid_s, "%lu", pid);
FILE *file_command = NULL;
char buffer[1000];
sprintf(buffer, "nvidia-smi | grep %s |awk '{print $6}'", pid_s);
file_command = popen(buffer, "r");
if (file_command == NULL) //No error : file_command is not NULL
{
throw std::runtime_error("Command failed to execute");
}
sleep(4); //To get time for the command to be executed.
while (fgets(buffer, sizeof(buffer), file_command) != NULL) //Read output
{
printf("Result from command %s \n", buffer); //Nothing printed
}..。
我试图获得nvidia输出的第6个参数,它是当前运行的程序的内存(它的pid来自getpid())。
屏幕上没有打印出来的东西。
fgets返回NULL,尽管在GPU上已经分配了内存(使用cudaMalloc)。
我一直在按照这篇文章中的建议来使用睡眠
https://stackoverflow.com/questions/60674575
复制相似问题