Linux进程管理之如何创建和销毁进程,


目录
  • 创建进程
    • 1. 使用fork()系统调用
    • 2. 使用exec()系列函数
    • 3. 使用系统调用clone()
  • 销毁进程
    • 进程组与会话
      • 杀死进程
        • 总结

          Linux是一个多任务操作系统,进程管理是其核心功能之一。

          本文将详细介绍如何在Linux中创建和销毁进程,包括示例代码和详细说明。

          创建进程

          在Linux中,可以使用多种方法创建新的进程。

          以下是几种常见的方法:

          1. 使用fork()系统调用

          fork()系统调用是创建新进程的最常见方式。

          它会创建一个与父进程几乎完全相同的子进程。

          #include <stdio.h>
          #include <unistd.h>
          
          int main() {
              pid_t child_pid;
          
              child_pid = fork();
          
              if (child_pid == 0) {
                  // 子进程代码
                  printf("This is the child process\n");
              } else if (child_pid > 0) {
                  // 父进程代码
                  printf("This is the parent process, child PID: %d\n", child_pid);
              } else {
                  // 创建进程失败
                  perror("fork");
                  return 1;
              }
          
              return 0;
          }
          

          2. 使用exec()系列函数

          exec()系列函数用于在当前进程中执行一个新的程序。

          它们通常与fork()一起使用,以替换子进程的内存映像。

          #include <stdio.h>
          #include <unistd.h>
          
          int main() {
              pid_t child_pid;
          
              child_pid = fork();
          
              if (child_pid == 0) {
                  // 子进程代码
                  printf("This is the child process\n");
          
                  // 在子进程中执行新程序
                  execl("/bin/ls", "ls", "-l", NULL);
              } else if (child_pid > 0) {
                  // 父进程代码
                  printf("This is the parent process, child PID: %d\n", child_pid);
              } else {
                  // 创建进程失败
                  perror("fork");
                  return 1;
              }
          
              return 0;
          }
          

          3. 使用系统调用clone()

          clone()系统调用与fork()类似,但它允许更精细的控制,例如共享文件描述符和内存空间。

          #define _GNU_SOURCE
          #include <stdio.h>
          #include <sched.h>
          #include <stdlib.h>
          #include <unistd.h>
          
          int child_function(void *arg) {
              printf("This is the child process\n");
              return 0;
          }
          
          int main() {
              char *stack;
              char *stack_top;
              pid_t child_pid;
          
              stack = (char *)malloc(8192);
              if (stack == NULL) {
                  perror("malloc");
                  return 1;
              }
          
              stack_top = stack + 8192;
          
              child_pid = clone(child_function, stack_top, CLONE_VM | CLONE_FS | CLONE_FILES, NULL);
          
              if (child_pid == -1) {
                  perror("clone");
                  return 1;
              }
          
              printf("This is the parent process, child PID: %d\n", child_pid);
          
              return 0;
          }
          

          销毁进程

          Linux中,有几种方法可以销毁进程,其中最常见的是使用exit()系统调用。

          以下是一个示例:

          #include <stdio.h>
          #include <stdlib.h>
          #include <unistd.h>
          
          int main() {
              pid_t child_pid;
          
              child_pid = fork();
          
              if (child_pid == 0) {
                  // 子进程代码
                  printf("This is the child process\n");
          
                  // 子进程退出
                  exit(0);
              } else if (child_pid > 0) {
                  // 父进程代码
                  printf("This is the parent process, child PID: %d\n", child_pid);
          
                  // 等待子进程退出
                  wait(NULL);
          
                  printf("Child process has exited\n");
              } else {
                  // 创建进程失败
                  perror("fork");
                  return 1;
              }
          
              return 0;
          }
          

          进程组与会话

          在Linux中,进程组和会话是进程管理的重要概念。进程组是一组相关联的进程的集合,而会话则是一组进程组的集合。

          进程组通常用于将多个相关的进程组织在一起,以便更好地进行控制和信号处理。

          以下是创建进程组和会话的示例:

          #include <stdio.h>
          #include <stdlib.h>
          #include <unistd.h>
          
          int main() {
              pid_t child_pid;
          
              child_pid = fork();
          
              if (child_pid == 0) {
                  // 子进程代码
                  printf("This is the child process (PID: %d)\n", getpid());
          
                  // 创建一个新会话并成为会话领袖
                  if (setsid() == -1) {
                      perror("setsid");
                      return 1;
                  }
          
                  // 创建一个新进程组
                  if (setpgid(0, 0) == -1) {
                      perror("setpgid");
                      return 1;
                  }
          
                  printf("Child process is in a new session and process group\n");
                  sleep(10); // 保持进程运行10秒
              } else if (child_pid > 0) {
                  // 父进程代码
                  printf("This is the parent process, child PID: %d\n", child_pid);
          
                  // 等待子进程退出
                  wait(NULL);
          
                  printf("Child process has exited\n");
              } else {
                  // 创建进程失败
                  perror("fork");
                  return 1;
              }
          
              return 0;
          }
          

          在上述示例中,子进程首先创建了一个新会话并成为会话领袖,然后创建了一个新进程组。

          这将导致子进程脱离父进程的控制终端和进程组,成为一个独立的会话。这对于守护进程等后台任务非常有用。

          杀死进程

          在Linux中,可以使用kill命令或kill()系统调用来杀死进程。

          以下是一个示例:

          #include <stdio.h>
          #include <stdlib.h>
          #include <unistd.h>
          #include <signal.h>
          
          void sig_handler(int signo) {
              if (signo == SIGTERM) {
                  printf("Received SIGTERM, exiting...\n");
                  exit(0);
              }
          }
          
          int main() {
              pid_t child_pid;
          
              child_pid = fork();
          
              if (child_pid == 0) {
                  // 子进程代码
                  printf("This is the child process (PID: %d)\n", getpid());
          
                  // 注册信号处理函数
                  signal(SIGTERM, sig_handler);
          
                  while (1) {
                      // 子进程持续运行
                      sleep(1);
                  }
              } else if (child_pid > 0) {
                  // 父进程代码
                  printf("This is the parent process, child PID: %d\n", child_pid);
          
                  // 等待一段时间后向子进程发送SIGTERM信号
                  sleep(5);
                  kill(child_pid, SIGTERM);
                  printf("Sent SIGTERM to child process\n");
          
                  // 等待子进程退出
                  wait(NULL);
          
                  printf("Child process has exited\n");
              } else {
                  // 创建进程失败
                  perror("fork");
                  return 1;
              }
          
              return 0;
          }
          

          在上述示例中,父进程通过kill()系统调用向子进程发送SIGTERM信号,以请求子进程优雅地退出。

          总结

          Linux进程管理是操作系统的核心功能之一,对于系统开发和管理人员来说是重要的知识点。

          本文详细介绍了如何创建和销毁进程,以及如何使用进程组和会话来组织进程。此外,还介绍了如何杀死进程。

          希望本文提供的示例代码和详细说明有助于大家更好地理解和应用Linux进程管理的概念和技巧。也希望大家多多支持PHP之友。

          您可能感兴趣的文章:
          • linux 系统进程管理工具systemd详解(systemctl命令、创建自己的systemd服务)
          • Linux创建进程达到65535的方法
          • Linux 创建子进程执行任务的实现方法
          • linux下如何创建守护进程的步骤

          相关内容