当前位置: 代码网 > 服务器>服务器>Linux > Linux进程管理之如何创建和销毁进程

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

2024年05月18日 Linux 我要评论
linux是一个多任务操作系统,进程管理是其核心功能之一。本文将详细介绍如何在linux中创建和销毁进程,包括示例代码和详细说明。创建进程在linux中,可以使用多种方法创建新的进程。以下是几种常见的

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进程管理的概念和技巧。也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com