1、fork():
创建子进程
pid_t fork(void);
函数的作用:用于创建子进程。
包含头文件:
#include <sys/types.h>
#include <unistd.h>
返回值:
fork()的返回值会返回两次。一次是在父进程中,一次是在子进程中。
在父进程中返回创建的子进程的ID,
在子进程中返回0
在父进程中返回-1,表示创建子进程失败,并且设置errno
写时复用,(vfork()函数基本废弃。)
2、exec():
子进程中执行别的程序,参考该文
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve

本文详细介绍了进程创建(fork)、程序替换(exec)和子进程等待(wait)在编程中的应用,包括函数用法、返回值及注意事项。特别强调了避免僵尸进程、正确配合使用以实现有效进程管理。
最低0.47元/天 解锁文章
2751

被折叠的 条评论
为什么被折叠?



