多进程函数系列fork(), wait(), exec()系列,system(), posix_spawn()实例详解

fork():

fork()用来创建一个子进程,该子进程是执行该函数的父进程的一个复制的映像;

#include <unistd.h>
pid_t fork(void);

注意:fork()函数有一个特点是一次调用返回两个值;
如果返回值为0,则是子进程;如果返回值大于0,则是父进程(此时返回值就是子进程的PID);(如果返回值为-1,则创建子进程失败)
问题就在于为什么这个fork()函数会返回两个不同的值呢???
当执行了该函数的时候已经创建了一个子进程的进程空间,fork()会在父进程空间返回一个值(子进程PID),同样也会在子进程空间中返回一个值(0);

wait():

wait函数是在父进程中使用,用来获取子进程的状态;
#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);

wait系统调用会使父进程暂停执行,直到它的等待的子进程结束为止。也就是说wait是阻塞的。
wait可以返回两个信息,直接返回子进程的PID,还有status(注意这个值不是在子进程中调用exit函数中的退出码,下面有专门的宏使用该status),子进程的退出状态信息存放在wait的参数status指向的内存中。
WIFEXITED(status)//如果子进程正常退出,那么返回1;见实例输出结果;
WEXITSTATUS(status)//返回子进程的退出码;如果退出码的值很大,那么它只会返回退出码的低八位
 WIFEXITED(status)
              returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
 WEXITSTATUS(status)
        returns  the  exit status of the child.  This consists of the least significant 8 bits of the status argument that the child specified in a
        call to exit(3) or _exit(2) or as the argument for a return statement in main().  This macro should be employed only if WIFEXITED  returned
        true.
 WIFSIGNALED(status)
        returns true if the child process was terminated by a signal.
 WTERMSIG(status)
        returns  the  number  of the signal that caused the child process to terminate.  This macro should be employed only if WIFSIGNALED returned
        true.
 WCOREDUMP(status)
        returns true if the child produced a core dump.  This macro should be employed only if WIFSIGNALED returned true.  This macro is not speci‐
        fied  in POSIX.1-2001 and is not available on some UNIX implementations (e.g., AIX, SunOS).  Only use this enclosed in #ifdef WCOREDUMP ...
        #endif.
 WIFSTOPPED(status)
        returns true if the child process was stopped by delivery of a signal; this is possible only if the call was done using WUNTRACED  or  when
        the child is being traced (see ptrace(2)).
 WSTOPSIG(status)
        returns the number of the signal which caused the child to stop.  This macro should be employed only if WIFSTOPPED returned true.
 WIFCONTINUED(status)
        (since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.

waitpid(

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`posix_spawn` 函数是一个用于创建新进程的函数,它比 `fork/exec` 更加高效、安全和灵活。下面是一个 `posix_spawn` 函数使用实例: ```c #include <stdio.h> #include <stdlib.h> #include <spawn.h> #include <unistd.h> int main(int argc, char *argv[]) { pid_t pid; char *args[] = {"/bin/ls", "-al", NULL}; posix_spawn_file_actions_t actions; posix_spawn_file_actions_init(&actions); posix_spawn_file_actions_addclose(&actions, STDIN_FILENO); posix_spawn_file_actions_addclose(&actions, STDOUT_FILENO); posix_spawn_file_actions_addclose(&actions, STDERR_FILENO); if (posix_spawn(&pid, args[0], &actions, NULL, args, NULL) != 0) { perror("posix_spawn"); return EXIT_FAILURE; } waitpid(pid, NULL, 0); posix_spawn_file_actions_destroy(&actions); return EXIT_SUCCESS; } ``` 在上面的例子中,我们首先定义了一个 `args` 数组,它包含了我们要执行的命令及其参数。然后,我们初始化了一个 `posix_spawn_file_actions_t` 对象,并为其添加了三个关闭文件描述符的操作,以避免子进程继承父进程的标准输入、标准输出和标准错误流。 接着,我们调用了 `posix_spawn` 函数来创建新进程。如果成功,该函数将返回子进程的进程 ID。最后,我们调用了 `waitpid` 函数等待子进程退出,并销毁了 `posix_spawn_file_actions_t` 对象。 需要注意的是,`posix_spawn` 函数在创建新进程时会执行一个新的程序映像,因此它不会继承父进程的内存空间和上下文。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值