Linux编程之posix标准中的进程管理API使用

在 Linux 编程中,POSIX 标准提供了丰富的进程管理函数,进程管理不仅是操作系统的核心功能之一,也是编写高效、稳定、安全应用程序的基础。

1. fork()

功能: 创建一个新进程,子进程是父进程的副本。返回值:

  • 子进程: 返回 0。
  • 父进程: 返回子进程的 PID。
  • 错误: 返回 -1。

示例代码:

#include <stdio.h>
#include <unistd.h>

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        printf("This is the child process. PID: %d\n", getpid());
    } else if (pid > 0) {
        printf("This is the parent process. PID: %d, Child PID: %d\n", getpid(), pid);
    } else {
        perror("fork");
    }
    return 0;
}

2. exec() 系列函数

功能: 用于替换当前进程映像,执行新程序。常用函数:

  • execl(): 使用参数列表。
  • execv(): 使用参数数组。
  • execle(): 附带环境变量。
  • execve(): 执行程序,附带环境变量和参数数组。

示例代码 (使用 execl()):

#include <stdio.h>
#include <unistd.h>

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        execl("/bin/ls", "ls", "-l", (char *)NULL);
        perror("execl"); // 如果 execl 返回,则表示出错
    } else if (pid > 0) {
        wait(NULL); // 等待子进程结束
    } else {
        perror("fork");
    }
    return 0;
}

3. wait()waitpid()

功能: 等待子进程结束并获取其状态。

  • wait(): 等待任意子进程。
  • waitpid(): 等待特定子进程,可以指定不同选项。

示例代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        sleep(2); // 子进程模拟工作
        _exit(0); // 退出子进程
    } else if (pid > 0) {
        int status;
        waitpid(pid, &status, 0); // 等待子进程结束
        if (WIFEXITED(status)) {
            printf("Child exited with status %d\n", WEXITSTATUS(status));
        }
    } else {
        perror("fork");
    }
    return 0;
}

4. getpid()

功能: 获取当前进程的进程 ID。

示例代码:

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Current process ID: %d\n", getpid());
    return 0;
}

5. getppid()

功能: 获取当前进程的父进程 ID。

示例代码:

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Parent process ID: %d\n", getppid());
    return 0;
}

6. kill()

功能: 向进程发送信号(如终止信号)。

示例代码:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        // 子进程中模拟工作
        sleep(10);
    } else if (pid > 0) {
        // 父进程中发送 SIGKILL 信号
        sleep(1);
        kill(pid, SIGKILL);
        printf("Sent SIGKILL to child process.\n");
    } else {
        perror("fork");
    }
    return 0;
}

7. exit()

功能: 终止进程并返回状态码。

示例代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Exiting process with status 0.\n");
    exit(0);
}

实际应用中的例程

下面是一个完整的实际应用例程,它演示了创建一个子进程、在子进程中执行一个新程序,并在父进程中等待子进程结束的过程。

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        // 子进程
        printf("Child process PID: %d\n", getpid());
        execl("/bin/ls", "ls", "-l", (char *)NULL);
        // execl 失败时的处理
        perror("execl");
        exit(1);
    } else if (pid > 0) {
        // 父进程
        int status;
        printf("Parent process PID: %d\n", getpid());
        waitpid(pid, &status, 0); // 等待子进程结束
        if (WIFEXITED(status)) {
            printf("Child process exited with status %d\n", WEXITSTATUS(status));
        } else {
            printf("Child process did not exit normally\n");
        }
    } else {
        perror("fork");
        exit(1);
    }
    return 0;
}

编译命令

gcc -o process_example process_example.c

输出结果

运行编译后的程序 process_example,你会看到类似以下的输出:

Parent process PID: 1234
Child process PID: 1235
total 4
drwxr-xr-x 2 user user 4096 Sep 12 12:34 .
drwxr-xr-x 5 user user 4096 Sep 12 12:34 ..
-rw-r--r-- 1 user user  123 Sep 12 12:34 process_example.c
Child process exited with status 0

输出中:

  • Parent process PID: 1234Child process PID: 1235 显示了父进程和子进程的进程 ID。
  • ls -l 命令的输出显示了当前目录的文件列表。
  • Child process exited with status 0 表示子进程成功退出。
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值