翼度科技»论坛 云主机 服务器技术 查看内容

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

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
Linux是一个多任务操作系统,进程管理是其核心功能之一。
本文将详细介绍如何在Linux中创建和销毁进程,包括示例代码和详细说明。

创建进程

在Linux中,可以使用多种方法创建新的进程。
以下是几种常见的方法:

1. 使用fork()系统调用
  1. fork()
复制代码
系统调用是创建新进程的最常见方式。
它会创建一个与父进程几乎完全相同的子进程。
  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main() {
  4.     pid_t child_pid;

  5.     child_pid = fork();

  6.     if (child_pid == 0) {
  7.         // 子进程代码
  8.         printf("This is the child process\n");
  9.     } else if (child_pid > 0) {
  10.         // 父进程代码
  11.         printf("This is the parent process, child PID: %d\n", child_pid);
  12.     } else {
  13.         // 创建进程失败
  14.         perror("fork");
  15.         return 1;
  16.     }

  17.     return 0;
  18. }
复制代码
2. 使用exec()系列函数
  1. exec()
复制代码
系列函数用于在当前进程中执行一个新的程序。
它们通常与
  1. fork()
复制代码
一起使用,以替换子进程的内存映像。
  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main() {
  4.     pid_t child_pid;

  5.     child_pid = fork();

  6.     if (child_pid == 0) {
  7.         // 子进程代码
  8.         printf("This is the child process\n");

  9.         // 在子进程中执行新程序
  10.         execl("/bin/ls", "ls", "-l", NULL);
  11.     } else if (child_pid > 0) {
  12.         // 父进程代码
  13.         printf("This is the parent process, child PID: %d\n", child_pid);
  14.     } else {
  15.         // 创建进程失败
  16.         perror("fork");
  17.         return 1;
  18.     }

  19.     return 0;
  20. }
复制代码
3. 使用系统调用clone()
  1. clone()
复制代码
系统调用与
  1. fork()
复制代码
类似,但它允许更精细的控制,例如共享文件描述符和内存空间。
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <sched.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>

  6. int child_function(void *arg) {
  7.     printf("This is the child process\n");
  8.     return 0;
  9. }

  10. int main() {
  11.     char *stack;
  12.     char *stack_top;
  13.     pid_t child_pid;

  14.     stack = (char *)malloc(8192);
  15.     if (stack == NULL) {
  16.         perror("malloc");
  17.         return 1;
  18.     }

  19.     stack_top = stack + 8192;

  20.     child_pid = clone(child_function, stack_top, CLONE_VM | CLONE_FS | CLONE_FILES, NULL);

  21.     if (child_pid == -1) {
  22.         perror("clone");
  23.         return 1;
  24.     }

  25.     printf("This is the parent process, child PID: %d\n", child_pid);

  26.     return 0;
  27. }
复制代码
销毁进程

Linux中,有几种方法可以销毁进程,其中最常见的是使用
  1. exit()
复制代码
系统调用。
以下是一个示例:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>

  4. int main() {
  5.     pid_t child_pid;

  6.     child_pid = fork();

  7.     if (child_pid == 0) {
  8.         // 子进程代码
  9.         printf("This is the child process\n");

  10.         // 子进程退出
  11.         exit(0);
  12.     } else if (child_pid > 0) {
  13.         // 父进程代码
  14.         printf("This is the parent process, child PID: %d\n", child_pid);

  15.         // 等待子进程退出
  16.         wait(NULL);

  17.         printf("Child process has exited\n");
  18.     } else {
  19.         // 创建进程失败
  20.         perror("fork");
  21.         return 1;
  22.     }

  23.     return 0;
  24. }
复制代码
进程组与会话

在Linux中,进程组和会话是进程管理的重要概念。进程组是一组相关联的进程的集合,而会话则是一组进程组的集合。
进程组通常用于将多个相关的进程组织在一起,以便更好地进行控制和信号处理。
以下是创建进程组和会话的示例:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>

  4. int main() {
  5.     pid_t child_pid;

  6.     child_pid = fork();

  7.     if (child_pid == 0) {
  8.         // 子进程代码
  9.         printf("This is the child process (PID: %d)\n", getpid());

  10.         // 创建一个新会话并成为会话领袖
  11.         if (setsid() == -1) {
  12.             perror("setsid");
  13.             return 1;
  14.         }

  15.         // 创建一个新进程组
  16.         if (setpgid(0, 0) == -1) {
  17.             perror("setpgid");
  18.             return 1;
  19.         }

  20.         printf("Child process is in a new session and process group\n");
  21.         sleep(10); // 保持进程运行10秒
  22.     } else if (child_pid > 0) {
  23.         // 父进程代码
  24.         printf("This is the parent process, child PID: %d\n", child_pid);

  25.         // 等待子进程退出
  26.         wait(NULL);

  27.         printf("Child process has exited\n");
  28.     } else {
  29.         // 创建进程失败
  30.         perror("fork");
  31.         return 1;
  32.     }

  33.     return 0;
  34. }
复制代码
在上述示例中,子进程首先创建了一个新会话并成为会话领袖,然后创建了一个新进程组。
这将导致子进程脱离父进程的控制终端和进程组,成为一个独立的会话。这对于守护进程等后台任务非常有用。

杀死进程

在Linux中,可以使用
  1. kill
复制代码
命令或
  1. kill()
复制代码
系统调用来杀死进程。
以下是一个示例:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>

  5. void sig_handler(int signo) {
  6.     if (signo == SIGTERM) {
  7.         printf("Received SIGTERM, exiting...\n");
  8.         exit(0);
  9.     }
  10. }

  11. int main() {
  12.     pid_t child_pid;

  13.     child_pid = fork();

  14.     if (child_pid == 0) {
  15.         // 子进程代码
  16.         printf("This is the child process (PID: %d)\n", getpid());

  17.         // 注册信号处理函数
  18.         signal(SIGTERM, sig_handler);

  19.         while (1) {
  20.             // 子进程持续运行
  21.             sleep(1);
  22.         }
  23.     } else if (child_pid > 0) {
  24.         // 父进程代码
  25.         printf("This is the parent process, child PID: %d\n", child_pid);

  26.         // 等待一段时间后向子进程发送SIGTERM信号
  27.         sleep(5);
  28.         kill(child_pid, SIGTERM);
  29.         printf("Sent SIGTERM to child process\n");

  30.         // 等待子进程退出
  31.         wait(NULL);

  32.         printf("Child process has exited\n");
  33.     } else {
  34.         // 创建进程失败
  35.         perror("fork");
  36.         return 1;
  37.     }

  38.     return 0;
  39. }
复制代码
在上述示例中,父进程通过
  1. kill()
复制代码
系统调用向子进程发送
  1. SIGTERM
复制代码
信号,以请求子进程优雅地退出。

总结

Linux进程管理是操作系统的核心功能之一,对于系统开发和管理人员来说是重要的知识点。
本文详细介绍了如何创建和销毁进程,以及如何使用进程组和会话来组织进程。此外,还介绍了如何杀死进程。
希望本文提供的示例代码和详细说明有助于大家更好地理解和应用Linux进程管理的概念和技巧。也希望大家多多支持脚本之家。

来源:https://www.jb51.net/server/315729fzi.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具