Linux学习_系统进程的创建
发布日期:2021-05-14 23:42:56 浏览次数:14 分类:精选文章

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

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������i������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������i-node������������������������������i������������������������������������������������������������������������������������������������������

���������������������������������������������������������ID������������ID���������������������������������������������������������������������������������������������

���������������������**fork()**���������������������������������������������������������fork()������������������������������������������������������������vfork()������������������������������������������������������������������������������������������������vfork()���������������������������������������������

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

main() {
// ...
fork(); // ���������������
fork(); // ���������������������
// ...
}

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

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

  • ���������������������
  • #include 
    #include
    int main() {
    printf("Parent PID: %d\n", getpid());
    pid_t child = fork();
    if (child < 0) {
    perror("fork failed");
    exit(EXIT_FAILURE);
    } else if (child > 0) {
    printf("Parent running with child PID: %d\n", child);
    } else {
    printf("Child running with PID: %d\n", getpid());
    }
    sleep(1);
    return EXIT_SUCCESS;
    }

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

    Parent PID: 1234
    Parent running with child PID: 5678
    Child running with PID: 5678
    1. ������������������
    2. #include 
      #include
      int main() {
      printf("Current PID: %d\n", getpid());
      pid_t child = fork();
      if (child < 0) {
      perror("fork failed");
      exit(EXIT_FAILURE);
      } else if (child > 0) {
      printf("Parent running (PID: %d)\n", child);
      for(int i = 0; i < 5; i++){
      printf("Parent: %d\n", i);
      sleep(1);
      }
      } else {
      printf("Child running (PID: %d)\n", child);
      for(int i = 0; i < 5; i++){
      printf("Child: %d\n", i);
      sleep(1);
      }
      }
      return EXIT_SUCCESS;
      }

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

      Current PID: 1234
      Parent running (PID: 5678)
      Parent: 0
      Parent: 1
      ...
      1. ���������������������
      2. ���������������������������IO������fprintf���fgets���������������������������������������������������IO������������write���read������������������������������������������������������������������������

        1. ������������������
        2. #include 
          #include
          #include
          int main() {
          int fd = open("file.txt", O_WRONLY);
          if(fd < 0){
          perror("open failed");
          return -1;
          }
          pid_t child = fork();
          if(child < 0){
          perror("fork failed");
          return -1;
          } else if(child > 0){
          //������������������������������������
          long pos = lseek(fd, 0L, SEEK_END);
          if(pos < 0){
          perror("lseek failed");
          return -1;
          }
          } else {
          //���������������������
          char* str = "\ndata appended\n";
          ssize_t size = strlen(str) * sizeof(char);
          if(write(fd, str, size) != size){
          perror("write failed");
          return -1;
          }
          sleep(3); //������������������������
          }
          return EXIT_SUCCESS;
          }

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

          parent PID: 1234, ������������
          child PID: 5678, ������������
    上一篇:Linux学习_系统进程结构与分类
    下一篇:Linux学习_系统进程概念

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2025年04月27日 12时44分28秒