【Linux】进程控制及理解
发布日期:2021-05-10 06:33:05 浏览次数:21 分类:精选文章

本文共 5433 字,大约阅读时间需要 18 分钟。

������������

1���������������

���������������������������������������������������������������������������������Linux������������fork���������������������������������������

1.1 ������������������fork������

���������fork���������������������������������������������������������(task_struct)������Linux���������������������������������������������������������������������������������������������������������������������������������������������������������

fork������������������

pid_t fork(void);
������������
���������������0������������������������������ID���������������-1

���������������

#include 
int main() {
pid_t pid = fork();
if (pid > 0) {
printf("this is parent: %d\n", getpid());
} else if (pid == 0) {
printf("this is child: %d\n", getpid());
} else {
printf("error\n");
sleep(100);
printf("this is process\n");
}
return 0;
}

1.2 fork������������������������

������������������������������������������������������������������������������

  • ������������: ������������������������������������������������������
  • ������������: ���������������������������������������������������������������������������������������������
  • ������������������: ���������������������������������������������
  • ������: ������������������������������������������
  • ���������������������fork������������������������������������������������������������������������������������������������������������������������������������������������������������������

    1.3 fork���������������������

    ������������������fork������������������������

    #include 
    #include
    int main() {
    pid_t pid = fork();
    if (pid < 0) {
    perror("fork");
    return 1;
    }
    if (pid == 0) {
    // ���������������
    sleep(20);
    exit(10);
    } else {
    // ������������������������������
    int status;
    pid_t ret = wait(&status);
    if (ret > 0 && (status & 0x7f) == 0) {
    printf("child exit code: %d\n", (status >> 8) & 0xff);
    } else if (ret > 0) {
    printf("sig code: %d\n", status & 0x7f);
    }
    }
    return 0;
    }

    ���������������

    child is run, pid: 45110
    this is test for wait
    wait child 5s success, child return code: 1!

    ���������������

  • ���������������������wait������������������������������
  • ��������� sleeps 20������������exit(10)���������
  • ���������������wait������������������������������������
  • 3���������������

    ���������������������������������������������������������������������������������������������������������������������

    3.1 ���������������������������������

    1. ������������ _exit

    • ���������������������void _exit(int status)
    • ���������������������

    2. ������������ exit

    • ���������������������void exit(int status)
    • ������������������������������������������

    3. main������������������������

    • return n ���������exit(n)

    3.2 ������������

    • ���������������������������������������������������(ctrl + c������������������������kill���������������

    3.3 exit���_exit���������

    • exit���������������_exit���������������������

    4���������������

    ������������������������������������������������������������������������������������������������������������wait���waitpid������������

    4.1 ������������������������

    • ���������������������������������������������������������
    • ���������������������������������������

    4.2 ������������������

    • ������wait���������waitpid������

    wait���������

    pid_t wait(int *status);
    ���������������������������������ID���������������-1
    ���������status������������NULL���

    waitpid���������

    int waitpid(int pid, int *status, int option);
    ���������
    pid������������ID���-1������������������
    status���������������������������������������������NULL
    option������������������`WONOHANG`

    4.3 ������������������������������������

    #include 
    #include
    #include
    #include
    #include
    int main() {
    pid_t child_pid = fork();
    if (child_pid == -1) {
    perror("fork");
    return 1;
    }
    if (child_pid == 0) {
    sleep(20);
    exit(10);
    } else {
    int status = 0;
    pid_t ret = waitpid(-1, &status, 0);
    if (WIFEXITED(status) && ret == child_pid) {
    printf("child exit code: %d\n", WEXITSTATUS(status));
    } else {
    printf("wait failed\n");
    }
    }
    return 0;
    }

    ���������������

    child is run, pid: 45110
    this is test for wait
    wait child 5s success, child exit code:1!

    5���������������������

    ������������������������������������������������fork���������������������������exec������������������������������������������

    5.1 ���������������������������

    • fork������������������������������������������������������
    • ������exec������������������������������������������������������

    5.2 ������������

    exec���������������������������

    • ������������������������
    • ������������������������������������������������/���������
    • ���������������������������������p���������e���������
    • ���������������������������e���������

    ���������

    #include 
    #include
    int main() {
    execl("/usr/bin/pwd", "pwd", NULL);
    while (1) {
    printf("������������������������\n");
    sleep(1);
    }
    return 0;
    }

    ���������������������

    • execl������������������������������ID���������������
    • ��������������������������������������������������������� -����������������������������������������������������������������������������������������������PrimeStar`
    上一篇:【C++】本地代码提交到github远程仓库
    下一篇:【Linux】程序地址空间,分段式、分页式存储理解

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月18日 09时53分25秒