posix_spawn_用C语言在Linux上创建子进程的posix_spawn示例

本文介绍了如何使用C语言在Linux上通过`posix_spawn`函数创建子进程,作为替代传统的`fork-exec-wait`方法。虽然`posix_spawn`需要6个参数,但其具有灵活性和优势,程序逻辑都在同一进程中执行,避免了`fork()`可能带来的问题。
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

posix_spawn

The posix_spawn() and posix_spawnp() functions create a new child process from the specified process image constructed from a regular executable file. It can be used to replace the relative complex “fork-exec-wait” methods with fork() and exec(). However, compared to fork() and exec(), posix_spawn() is less introduced if you search on the Web. The posix_spawn() manual provides details. However, it is still not sufficient especially for beginners. Here, I give an example of C program using posix_spawn() to create child processes.

posix_spawn()posix_spawnp()函数根据由常规可执行文件构造的指定过程映像创建新的子过程。 它可以用来用fork()exec()代替相对复杂的“ fork-exec-wait”方法。 但是,与fork()exec() ,如果在Web上搜索,则不会引入posix_spawn()posix_spawn()手册提供了详细信息。 但是,对于初学者来说,这仍然是不够的。 在这里,我举一个使用posix_spawn()创建子进程的C程序示例。

The program is to run the command by /bin/sh -c that you pass as the first argument to the program. The run.c source code is as follows.

该程序将通过/bin/sh -c运行命令,并将其作为第一个参数传递给该程序。 run.c源代码如下。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>

extern char **environ;

void run_cmd(char *cmd)
{
    pid_t pid;
    char *argv[] = {"sh", "-c", cmd, NULL};
    int status;
    printf("Run command: %s\n", cmd);
    status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ);
    if (status == 0) {
        printf("Child pid: %i\n", pid);
        if (waitpid(pid, &status, 0) != -1) {
            printf("Child exited with status %i\n", status);
        } else {
            perror("waitpid");
        }
    } else {
        printf("posix_spawn: %s\n", strerror(status));
    }
}

int main(int argc, char* argv[])
{
    run_cmd(argv[1]);
    return 0;
}

From the example, you can find the posix_spawn() has its advantages and flexibility over other similar ones although it is a little tedious with 6 arguments.

从该示例中,您可以发现posix_spawn()具有优于其他类似优点和灵活性的优点,尽管它有6个参数有点乏味。

  • system()system()exec(), it returns the new child process’ pid which you can wait by exec() ,它返回新的子进程的pid,您可以通过waitpid(). Of course, waitpid()等待。 当然, system()system ()
  • Difference from fork()/vfork(), the logic you implement is within the same process and you do not need to think about which piece of code is executed by the child process and which is executed by the parent process. It also avoid problems from vfork().

    fork() / vfork()不同之处在于,您实现的逻辑在同一进程内,因此您无需考虑子进程执行哪段代码以及父进程执行哪段代码。 它还避免了来自vfork()问题。

翻译自: https://www.systutorials.com/a-posix_spawn-example-in-c-to-create-child-process-on-linux/

posix_spawn

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值