LinuxC fork() exit() wait() waitpid() exec() getenv() setenv() unsetenv() getpid() getppid()

fork()

  • 创建子进程。失败返回-1,子进程返回0,父进程返回子进程的PID
#include <sys/types.h>
#include <unistd.h>

pid_t fork();

getpid() getppid()

#include <sys/types.h>
#include <unistd.h>

pid_t getpid(); //父子进程调用会得到自己的id
pit_t getppid(); //子进程调用得到父进程的id
  • 父进程要得到子进程的ID只能记录fork()的返回值

  • 代码1
#include <stdio.h>
#include <unistd.h> //fork()
#include <stdlib.h> //exit()
#include <sys/wait.h> //wait

int main() {
    printf("%s\n", "我是鸣人!");
    int pid = fork(); //创建子进程
    if (pid != 0) { //父进程
        int status;
        int result = wait(&status);
        if (result == -1 || status != 0) {
            printf("%s\n", "可恶,又失败了,再来一次!");
            return -1;
        } else {
            printf("%s\n", "我负责性质变化!"); //最后输出
        }
    } else {
        int second_pid = fork();
        if (second_pid != 0) { //子进程
            int new_status;
            int new_result = waitpid(-1, &new_status, 0); //wait(&new_status);
            if (new_result == -1 || new_status != 0) {
                exit(-1);
            } else {
                printf("%s\n", "我负责形态变化!"); //第二次输出
                exit(0);
            }
        } else { //子进程的子进程
            printf("%s\n", "我负责产生查克拉!"); //第一次输出
            exit(0);
        }
    }
    return 0;
}
  • 输出
我是鸣人!
我负责产生查克拉!
我负责形态变化!
我负责性质变化!


  • 代码2
#include <stdio.h>
#include <unistd.h> //pid_t fork(void)
#include <stdlib.h> //void exit(int status)
#include <sys/wait.h> //pid_t wait(int* stat_loc)
int cnt = 0;
int main() {
    int pid = fork();
    if (pid) {
        wait(NULL);
        printf("[pid!=0:%d].", cnt); //0
        return 0;
    }
    cnt++;
    int pid2 = fork();
    if (pid2) {
        wait(NULL);
        printf("[pid2!=0:%d],", cnt); //1
        exit(0);
    } else {
        char** argv = (char**)malloc(sizeof(char *) * 3);
        argv[0] = "echo", argv[1] = "Hello!", argv[2] = NULL;
        int pid3 = fork();
        cnt++;
        if (pid3) {
            free(argv);
            wait(NULL);
            printf("[pid3!=0:%d],", cnt); //2
            exit(0);
        } else {
            cnt++;
            execvp("echo", argv); //输出 Hello!
            printf("Count %d,", cnt); //没有输出
            free(argv);
            exit(0);
        }
    }
    return 0;
}
  • 输出
Hello!
[pid3!=0:2],[pid2!=0:1],[pid!=0:0].

  • exec函数族
    具有在现有的进程中打开某-一可执行文件、覆盖现有程序执行的功能
    只有失败返回-1
#inclde <unistd.h>
extern char **environ; //环境变量

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 execvpe(const char* filename, char* const argv[], char* const envp[]);

//系统调用函数
int execve(const char* filename, char* const argv[], char* const envp[]);
  • 我们可以将这些函数表示为: “exec + /v + p/e/pe”,所以这六个函数主要有两处不同。第一处不同(Iv) 在参数中表现为
    “execl"类函数将所有参数作为-整个字符串代入,而"execv"类函数将 参数分别放入-一个字符串的数组中,将数组作为参数代入函数运行。第二处不同(p/e/pe) 表现为包含p (代表path)的函数可以模仿计算机中壳的功能,在"file"中不含”/"时应用环境变量PATH中的目录来寻找可执行文件,而包含e (代表environment)的函数可以像execve一样将"变量=数值"对作为环境变量代入程序。
  • 可变参数最后一个为NULL

  • getenv() setenv() unsetenv()
#include <stdlib.h>
extern char **environ; //环境变量表:name = value

char *getenv(const char *name); //查询对应name的value
int setenv(const char *name, const char *value, int rewrite); //设置name的值为value
void unsetenv(const char *name);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值