多进程编程函数posix_spawn实例

本文通过两个实例介绍如何运用posix_spawn函数进行多进程编程。实例一和实例二展示了不同的进程创建情况,讨论了可能存在的问题和正确结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 #include <spawn.h>
 int posix_spawn(pid_t *restrict pid, const char *restrict path,
        const posix_spawn_file_actions_t *file_actions,
        const posix_spawnattr_t *restrict attrp,
        char *const argv[restrict], char *const envp[restrict]);

  在main函数中调用该函数可以将一个可执行文件运行起来;如下面所示:
posix_spawn(&child_pid, "ls", NULL, NULL, argv, NULL);
执行完该函数之后,会将该目录下的ls可执行程序运行起来,会输出该目录下的文件;
argv参数可有可无,在例子中,如果没有带参数,那么直接执行ls;也可以带上参数-l,那么直接执行ls -l,则以列表的形式显示该目录下的文件;
在第二个例子中,参数是test.txt,那么对于第一个进程,则执行 ls test.txt,则输出test.txt;对于第二个进程,则执行cat test.txt输出test.txt的内容;

在例子二中吊起了两个可执行文件ls 和 cat(这两个文件是我从/bin目录下直接拷贝过来的,也可以自己生成一个可执行文件,只是这里我太懒了。。。。。)

wait(&wait_val);

刚才运行的进程的返回值输入到wait_val中;(感觉我例子二中有一点问题,第一个wait也有可能会得到的是第二个吊起的进程,不过这个例子得到的确实是正确的)


实例一:

/*************************************************************************
	> File Name: useposix_spawn.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 14时21分52秒
 ************************************************************************/

#include <iostream>
#include <spawn.h>
#include <string.h>
#include <sys/wait.h>
#include <cstdlib>
#include <error.h>

using namespace std;

/*
    #include <spawn.h>

    int posix_spawn(pid_t *restrict pid, const char *restrict path,
                                    const posix_spawn_file_actions_t *file_actions,
                                    const posix_spawnattr_t *restrict attrp,
                                    char *const argv[restrict], char *const envp[restrict]);
*/

int main(int argc, char *argv[])
{
    pid_t child_pid;
    int ret;
    int wait_val;

    cout << "This is main process......" << endl;
    ret = posix_spawn(&child_pid, "ls", NULL, NULL, argv, NULL);
    if (ret != 0){
        cout << "posix_spawn is error" << endl;
        exit(-1);

    }

    wait(&wait_val);
    cout << "This is main process and the wait value is " << wait_val << endl;

    exit(0);
}




实例二:

/*************************************************************************
	> File Name: useposix_spawn2.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 14时21分52秒
 ************************************************************************/

#include <iostream>
#include <spawn.h>
#include <string.h>
#include <sys/wait.h>
#include <cstdlib>
#include <error.h>

using namespace std;

/*
    #include <spawn.h>

    int posix_spawn(pid_t *restrict pid, const char *restrict path,
                                    const posix_spawn_file_actions_t *file_actions,
                                    const posix_spawnattr_t *restrict attrp,
                                    char *const argv[restrict], char *const envp[restrict]);
*/

int main(int argc, char *argv[])
{
    pid_t child_pid[2];
    int ret;
    int wait_val[2];

    cout << "This is main process......" << endl;
    ret = posix_spawn(&child_pid[0], "ls", NULL, NULL, argv, NULL);
    if (ret != 0){
        cout << "posix_spawn is error" << endl;
        exit(-1);
    }

    ret = posix_spawn(&child_pid[1], "cat", NULL, NULL, argv, NULL);
    if (ret != 0){
        cout << "posix_spawn is error" << endl;
        exit(-1);
    }
    wait(&wait_val[0]);
    cout << "This is main process and the wait value of ls is " << wait_val[0] << endl;
    wait(&wait_val[1]);
    cout << "This is main process and the wait value of cat is " << wait_val[1] << endl;

    exit(0);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值