我想在 C 程序中执行 Linux 命令并在程序stdout
中从此命令中读取(解析)。下面的代码有效,但除了字符串和字节读取限制之外,我不知道如何限制命令的执行时间。有任何想法吗?
FILE *ps_pipe;
int bytes_read;
int nbytes = 100;
char *my_string=NULL;
char message[1024];
message=sprintf(message,"any command here");
ps_pipe = popen (message, "r");
my_string = (char *) malloc (nbytes + 1);
bytes_read = getdelim (&my_string, &nbytes, "delimiter_word", ps_pipe);
pclose(ps_pipe);
free(my_string);