Process 查找linux中进程的CPU时间和系统时间
我有一个主程序,它创建两个子项,每个子项调用Process 查找linux中进程的CPU时间和系统时间,process,resources,Process,Resources,我有一个主程序,它创建两个子项,每个子项调用execlv。在程序结束时,如何计算父进程和两个进程的CPU时间和系统时间 #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <stdlib.h> #incl
execlv
。在程序结束时,如何计算父进程和两个进程的CPU时间和系统时间
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
int main()
{
pid_t pid1,pid2,wid; // variable for parent and two children
char *my_args[3]; // strign array for containing the arguments for executing sigShooter1
// int aInt = 368; //
char str[15]; // strign to contain the pids of children when passing as command line arguments
pid1 = fork();
if (pid1 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}
if(pid1 == 0)
{
my_args[0] = "sigperf1";
my_args[1] = "0";
my_args[2] = NULL;
execv("sigshooter1",my_args);
fprintf(stderr,"sigshooter1 cannot be executed by first child...");
exit(-1);
}
pid2 = fork();
if(pid2 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}
if(pid2 == 0)
{
sprintf(str, "%d", pid1);
my_args[0] = "sigperf1";
my_args[1] = str;
my_args[2] = NULL;
// printf("this is converted = %s\n",my_args[1]);
//sleep(1);
execv("sigshooter1",my_args);
fprintf(stderr,"sigshooter1 cannot be executed by second child...");
exit(-1);
}
wid = wait(NULL);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
pid_t pid1,pid2,wid;//父项和两个子项的变量
char*my_args[3];//包含用于执行sigShooter1的参数的strign数组
//int=368//
char str[15];//当作为命令行参数传递时,strign包含子级的PID
pid1=fork();
if(pid1<0)
{
fprintf(stderr,“:fork失败:%s\n”,strerror(errno));
出口(1);
}
如果(pid1==0)
{
我的参数[0]=“sigperf1”;
我的参数[1]=“0”;
my_args[2]=NULL;
execv(“sigshooter1”,我的参数);
fprintf(stderr,“sigshooter1不能由第一个子代执行…”);
出口(-1);
}
pid2=fork();
if(pid2<0)
{
fprintf(stderr,“:fork失败:%s\n”,strerror(errno));
出口(1);
}
如果(pid2==0)
{
sprintf(str,“%d”,pid1);
我的参数[0]=“sigperf1”;
my_args[1]=str;
my_args[2]=NULL;
//printf(“此已转换=%s\n”,my_args[1]);
//睡眠(1);
execv(“sigshooter1”,我的参数);
fprintf(stderr,“sigshooter1不能由第二个子项执行…”);
出口(-1);
}
wid=等待(空);
}
您需要一个分析器。对于初学者,您可以运行perf stat./a.out
获取所有三个进程的总CPU时间,并运行perf stat-i./a.out
仅获取父进程的CPU时间
如果您需要更详细的信息,请查看更重要的工具,如valgrind
或gprof