进程基本概念
发布日期:2021-05-09 16:56:20 浏览次数:20 分类:精选文章

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

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

������������������������������������������������������������������������������Linux������������������������������������������������������������������CPU���������������������������������������������������������������������������������������PCB���������task_struct���������������������������������������������������

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

  • ���������������������������������������������������������������������������
  • ������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������
  • ������������������task_struct���������������������������������������������������������������������������������������������������������������

task_struct������������

  • ���������������������������������ID���
  • ������������������������������������������������������������������������������������������
  • ������������������������������������������������������
  • ���������������������������������������������������������������
  • ������������������������������������������������������
  • ���������������������������������������������������
  • I/O������������������������������I/O������������������������
  • ������������������������������������CPU������������������������

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

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

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

  • ������������������������
  • ������������������������������
  • ���������������������������������������
  • fork���������������
  • Linux���������������������user-space���������������������kernel-space������������������������������������mm_struct���������������������������������������������������������������������������

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

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

  • receiving the Halt������ (HALT)���
  • ���������������������������������������������
  • ���������������exit���wait���������
  • ������������������������������������������������
  • ������������������

    ���Linux������������������������following system API���������������������

    • getpid()������������������������������������
    • getppid()���������������������������������������
    • ps axj | grep <���������>���������������������������������������������

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

    ������fork()������������������������������������fork���������������

    • ������������parent������������������PID������������������0���
    • parent������������������fork������������������������������
    • ���������������������������������������������������������������
    • parent���������������������������������������������������������

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

    ������������������state���������������������������������������

    • R (Running)���������������������������������������������
    • S (Sleeping)���������������������������������������������
    • D (Deep Sleep)���������������������I/O���������������
    • T (Stopped)���������������������������������SIGCONT���������������
    • X (Dead)���������������������
    • Z (Zombie)������������������������������

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

    ���������������������������������������������������������������������������������������������wait���������������������������������������������������������������������������������������������

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

    ������������������������������������������������������������������������������������������������init���������PID 1������������������������������������������������������������parent������������init���������

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

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

    #include 
    #include
    #include
    int main() {
    pid_t pid = getpid();
    printf("������������������PID: %d\n", pid);
    pid_t ppid = getppid();
    printf("���������������PID: %d\n", ppid);
    while(1) {
    sleep(1);
    }
    return 0;
    }

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

    ps axj | grep ./.background

    ������������������������������������������fork()���������

    #include 
    #include
    #include
    int main() {
    int ret = fork();
    if (ret < 0) {
    perror("fork");
    return 1;
    } else if (ret == 0) {
    printf("���������PID: %d\n", getpid());
    sleep(5);
    } else {
    printf("������������������PID: %d\n", getpid());
    sleep(5);
    }
    return 0;
    }

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

    上一篇:继承相关概念
    下一篇:引用计数写时拷贝

    发表评论

    最新留言

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