小编kaf*_*f89的帖子

如何计算C中fork()系统调用的运行时间?

我正在尝试查找fork()系统调用的运行时间.每个子进程都需要立即退出,父进程需要在每个子进程wait()创建下一个进程之前.我还想使用命名的shell内置命令time来测量程序的执行时间.

到目前为止我有这个代码,但不确定如果我做得对.

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int global = 100;

int main(int argc, char** argv)
{
    int local = 5;

    pid_t pid;

    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "error -- failed to fork()");
        return 1;
    }

    if (pid > 0) {

        int child_ret;

        waitpid(pid, &child_ret, 0);
        printf("parent -- global: %i, local: %i\n", global, local);
        printf("parent -- child exited with code %i\n", child_ret);
    } else {
        global++;
        local++; …
Run Code Online (Sandbox Code Playgroud)

c time fork child-process

6
推荐指数
1
解决办法
2078
查看次数

标签 统计

c ×1

child-process ×1

fork ×1

time ×1