getrusage() 获取系统时间、用户时间。 Unix 编程帮助

发布于 2024-08-06 03:49:53 字数 590 浏览 8 评论 0原文

我正在编写一个 shell,我需要一次启动多个子进程并记录系统时间和用户时间。

到目前为止我能够做到。唯一的问题是我使用 wait4 来获取子程序使用的系统资源并将其放入我的 rusage 结构中,称为“usage”。

如何同时启动所有进程并跟踪用户和系统时间?我可以删除 wait4() 系统调用并在外部循环使用它,这样我就可以让父进程等待,但如果我这样做,那么我只能记录最后一个进程的时间,而不是所有进程的时间。

你知道我该如何解决这个问题吗?

execute(commandPipev,"STANDARD",0);
wait4(pid,&status,0,&usage);
printf("Child process: %s\t PID:%d\n", commandPipev[0], pid);
printf("System time: %ld.%06ld sec\n",usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
printf("User time:   %ld.%06ld sec\n\n",usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);

I am writing a shell where I need to launch several child processes at once and record the system time and user time.

So far I am able to do it. The only problem is that I am using wait4 to grab the system resources used by the child program and put it in my rusage structure called usage.

How can I launch all the processes at the same time and keep track of the user and system times? I can remove the wait4() system call and use it outside to loop so I can make the parent wait, but if I do that then I can only record the times for the last process and not all of them.

Do you have any idea how I can fix this?

execute(commandPipev,"STANDARD",0);
wait4(pid,&status,0,&usage);
printf("Child process: %s\t PID:%d\n", commandPipev[0], pid);
printf("System time: %ld.%06ld sec\n",usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
printf("User time:   %ld.%06ld sec\n\n",usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-08-13 03:49:53

一个令人费解的答案。

在 POSIX 环境中,启动子进程,然后使用 waitid()WNOWAIT 选项来告诉您某个子进程已退出。该选项使子进程处于等待状态 - 也就是说,您可以使用另一个 wait-family 调用来获取您需要的信息。然后,您可以使用非 POSIX wait4() 系统调用来获取刚刚退出的子进程的使用信息,并处理您需要执行的记帐操作。请注意,您可能会发现在 waitid()wait4() 调用之间终止了不同的进程;您需要使用循环和适当的标志和测试来收集所有可用的尸体(死亡的子进程),然后返回到 waitid() 调用以找出其他先前未完成的子进程。您还必须担心任何等待系列函数返回先前在后台启动但现在已完成的进程的信息。

wait4(2)< 的 Linux 手册页/a> 建议 WNOWAIT 可以直接与 wait4(2) 一起使用,因此您可以更干净地完成这一切 - 如果您确实需要以下选项全部。

考虑是否可以使用进程组将子进程分组在一起,以便更轻松地等待进程组的成员。

A convoluted answer.

In a POSIX environment, launch the children, then use waitid() with the WNOWAIT option to tell you that some child has exited. The option leaves the child in a waitable state - that is, you can use another wait-family call to garner the information you need. You can then use the non-POSIX wait4() system call to garner the usage information for the just exited child, and deal with the accounting you need to do. Note that you might find a different process has terminated between the waitid() and wait4() calls; you need to use a loop and appropriate flags and tests to collect all the available corpses (dead child processes) before going back to the waitid() call to find out about the other previously incomplete child processes. You also have to worry about any of the wait-family of functions returning the information for a process that was previously started in the background and has now finished.

The Linux man page for wait4(2) suggests that WNOWAIT might work directly with wait4(2), so you may be able to do it all more cleanly - if, indeed, you need the option at all.

Consider whether you can use process groups to group the child processes together, to make waiting for the members of the process group easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文