在 C 中使用 fork()

发布于 2024-12-05 09:11:56 字数 817 浏览 1 评论 0原文

我正在编写一个使用CPU 功率来处理一些信息的程序。该程序取决于 CPU 内核。如果有 2 个核心,程序将 fork() 两次以创建 2 个工作实例并返回结果。

#define CORES 4

void worker(int id)
{    
    // blablabla work here
    printf("worker %d\n",id);
    printf("[%d] I'm child of %d\n",getpid(),getppid());    
}

int main (int argc, const char * argv[])
{
    int pid;

    for (int i=0; i<CORES; i++)
    {
        pid = fork();
        if (pid == 0) // if child
        {
            worker(i);
            exit(0);
        }
        else if (pid>0)
        {
            printf("[%d] Big father here!\n",getpid());
        }
        else
        {
            printf("--- Fork problem ---");
        }
    }

    return 0;

}

我的问题:

  1. 我该怎么做才能使程序仅在所有子进程处理完所需信息后才终止? (我认为他们正在成为孤儿)
  2. 如何计算从第一个进程开始工作到最后一个进程终止所花费的时间

I'm writing a program that uses the cpu power to process some information. The program depends on the CPU cores. If there are 2 cores, the program will fork() twice to create 2 instances of the work and return the results.

#define CORES 4

void worker(int id)
{    
    // blablabla work here
    printf("worker %d\n",id);
    printf("[%d] I'm child of %d\n",getpid(),getppid());    
}

int main (int argc, const char * argv[])
{
    int pid;

    for (int i=0; i<CORES; i++)
    {
        pid = fork();
        if (pid == 0) // if child
        {
            worker(i);
            exit(0);
        }
        else if (pid>0)
        {
            printf("[%d] Big father here!\n",getpid());
        }
        else
        {
            printf("--- Fork problem ---");
        }
    }

    return 0;

}

My questions:

  1. What can I do so the program only terminates when ALL the child processes are done processing the required information? (i think they're becoming orphans)
  2. How to count the time it took since the first process started working until the last process terminates

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

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

发布评论

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

评论(3

七色彩虹 2024-12-12 09:11:57

要等待子进程完成,您可以使用任何 wait 系统调用系列。如果您使用 wait4,内核将为您提供以下信息:每个进程消耗了多少 CPU 和挂钟时间。但是,您可能会发现在开头调用 gettimeofday跑步结束时会更容易。

To wait for the child processes to be finished, you use any of the wait family of system calls. If you use wait4, the kernel will give you information about how much CPU and wall-clock time each process consumed. However, you may find that calling gettimeofday at the beginning and the end of the run is easier.

仅一夜美梦 2024-12-12 09:11:57

完成您想要的操作的一种方法是:编写一个 SIGCHLD 处理程序来递增计数器。 (声明计数器易失,否则可能会发生恶作剧。)然后 sigsuspend() 重复等待 SIGCHLD。当计数器与 CORES 匹配时,终止。

要计时,请在生成工作线程之前调用 time() ,然后在终止之前调用 time() ; difftime(3) 将为您提供以秒为单位的双精度时间差。

One way to do what you want: Write a SIGCHLD handler that increments a counter. (Declare the counter volatile or mischief may ensue.) Then sigsuspend() repeatedly waiting on SIGCHLD. When the counter matches CORES, terminate.

To time this, call time() just before spawning the worker threads and then just before termination; difftime(3) will give you the time difference in seconds as a double.

北恋 2024-12-12 09:11:56

使用 wait() 等待子进程终止:

int status;
pid_t pid;

while ((pid = wait(&status)) != -1) {
    // pid just terminated
}

// all children terminated

请参阅man 2 wait

要测量时间,请参阅gettimeofday()

struct timeval tv = {0};

gettimeofday(&tv, NULL);

<代码>结构时间值:

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

Use wait() to wait for children to terminate:

int status;
pid_t pid;

while ((pid = wait(&status)) != -1) {
    // pid just terminated
}

// all children terminated

See man 2 wait.

For measuring the time, see gettimeofday():

struct timeval tv = {0};

gettimeofday(&tv, NULL);

struct timeval:

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