生出一个独立的孩子

发布于 2024-11-08 00:52:50 字数 143 浏览 0 评论 0原文

如何使用 C 生成一个独立的子进程来处理其业务而不考虑父进程?

我想生成几个进程,在它们创建后不久,它们会在完成工作之前休眠大约 2 分钟。

但是,我不希望父亲等到孩子完成,因为与此同时我想产生更多进程。

我在Linux上。

How can you use C to spawn an independent child process that goes about its business without thinking about the father?

I want to spawn several processes and shortly after they have been created they go to sleep for about 2 minutes before they do their job.

However, I don't want the father to wait until the child is finished because in the meantime I want to spawn off more processes.

I'm on Linux.

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

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

发布评论

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

评论(3

木槿暧夏七纪年 2024-11-15 00:52:50

使用 fork()。
fork() 系统调用

pid_t pid = fork (); 
if (pid < 0) 
  // Error
else if (pid == 0) 
  // Child Process
  // Sleep 2 min. ?!
else 
  // Code for Father

Use fork().
The fork() System Call

pid_t pid = fork (); 
if (pid < 0) 
  // Error
else if (pid == 0) 
  // Child Process
  // Sleep 2 min. ?!
else 
  // Code for Father
囍笑 2024-11-15 00:52:50

只需使用 fork(2) 生成所需数量的进程,然后在父进程中调用 exit() 即可。孤儿将被 init 进程收养。

Simply spawn as many processes as you need using fork(2), then call exit() in the parent process. The orphaned children will be adopted by the init process.

烟沫凡尘 2024-11-15 00:52:50

如果您想要创建多个子进程,在创建后立即执行其“自己的业务”,则应该使用 vfork()(用于创建新进程而不完全复制父进程的地址空间)和 exec() 系列,用您想要的任何内容替换子进程的图像。

如果您不希望父亲等待孩子完成,则应该利用异步信号处理。当子进程结束时会发送 SIGCHLD。因此,您可以将 wait() 放在 SIGCHLD 的信号处理程序中而不是父进程中,并让信号处理程序收集子进程的返回状态。

下面是一个玩具示例:

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

sig_atomic_t child_process_ret_status;

void spawn(char *program,char *argv[]){
    pid_t child_pid=vfork();

    if(child_pid>0){
        printf("the parent process pid: %d\n",(int)getpid());
        printf("the cpid: %d\n",(int)child_pid);
        system("ping -c 10 www.google.com");
    }else{
        printf("the new process %d is going to execute the new program %s\n",(int)getpid(),program);
        execvp(program,argv);
        printf("you'll never see this if everything goes well.\n");
    }
}


void child_process_ret_handle(int sigval){
    if(sigval == SIGCHLD){
        printf("SIGCHLD received !\n");
        wait(&child_process_ret_status);
    }
}

int main(void){
    signal(SIGCHLD,child_process_ret_handle);
    char *program="sleep";
    char *argv[]={
        "sleep",
        "5",
        NULL
    };

    spawn(program,argv);
    if(WIFEXITED (child_process_ret_status)){
        printf("child process exited successfully with %d\n",WEXITSTATUS (child_process_ret_status));
    }else{
        printf("the child process exited abnormally\n");    
    }

    printf("parent process: %d returned!\n",getpid());
    return 0;
}

If what you want is creating multiple child processes doing their "own business" right after their creation, you should use vfork() (used to create new processes without fully copying the address space of the father process) and exec() family to replace the children processes' images with whatever you want.

if you don't want the father to wait until the child is finished, you should take advantage of asynchronous signal handling. A SIGCHLD is sent when a child process ends. So you can put the wait() within the signal handler for SIGCHLD rather than the father process and let the signal handler collect returning status for child process.

Below is a toy example:

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

sig_atomic_t child_process_ret_status;

void spawn(char *program,char *argv[]){
    pid_t child_pid=vfork();

    if(child_pid>0){
        printf("the parent process pid: %d\n",(int)getpid());
        printf("the cpid: %d\n",(int)child_pid);
        system("ping -c 10 www.google.com");
    }else{
        printf("the new process %d is going to execute the new program %s\n",(int)getpid(),program);
        execvp(program,argv);
        printf("you'll never see this if everything goes well.\n");
    }
}


void child_process_ret_handle(int sigval){
    if(sigval == SIGCHLD){
        printf("SIGCHLD received !\n");
        wait(&child_process_ret_status);
    }
}

int main(void){
    signal(SIGCHLD,child_process_ret_handle);
    char *program="sleep";
    char *argv[]={
        "sleep",
        "5",
        NULL
    };

    spawn(program,argv);
    if(WIFEXITED (child_process_ret_status)){
        printf("child process exited successfully with %d\n",WEXITSTATUS (child_process_ret_status));
    }else{
        printf("the child process exited abnormally\n");    
    }

    printf("parent process: %d returned!\n",getpid());
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文