进程退出的两个接口( exit()、_exit() )

进程终止

  进程在被创建后,开始运行完成其工作。通常在下列几个条件使得进程退出:

  1. 正常退出
  2. 出错退出
  3. 严重错误:程序中有bug
  4. 被其他进程杀死

进程退出的两个接口(exit 、 _exit)

#include <stdlib.h>
void exit(int status);

#include <unistd.h>
void _exit(int status);

参数:

  • status :进程退出时的一个状态信息。父进程在回收子进程资源时获取。

示例1:exit

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

int main(){
    printf("hello\n");
    printf("world");

    exit(0);
    return 0;
}

编译执行,打印hello\n,在打印world ,最后程序退出
exit

示例2:_exit

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

int main(){
    printf("hello\n");
    printf("world");

    _exit(0);
    return 0;
}

编译执行,只打印 hello\n ,不打印 world,程序直接退出
_exit
为什么C 标准库与 系统接口 打印的结果不同?
exitvs_exit

  在执行程序时,字符串会被保存在缓冲区中,使用标准C库中 exit ,在调用系统_exit 之前,首先调用退出处理函数,刷新I/O缓冲,关闭文件描述符,最后调用_exit 退出进程。所以在执行 printf("world") 后,调用exit ,会刷新至控制台,再退出进程。而_exit 则不会刷新缓冲区,导致不会输出world所以尽可能使用C标准库中的 exit()

一键三连是对我最大支持与鼓励!欢迎关注编程小镇,每天涨一点新姿势😄。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现cp功能,可以使用两个进程,一个进程负责读取源文件,另一个进程负责写入目标文件。实现的步骤如下: 1. 创建两个进程,一个进程作为读取进程,一个进程作为写入进程。 2. 读取进程打开源文件,写入进程创建目标文件。 3. 读取进程循环从源文件中读取数据块,将数据块发送给写入进程。 4. 写入进程循环接收数据块,将数据块写入目标文件中。 5. 当读取进程读取到源文件末尾时,向写入进程发送一个结束标志,写入进程接收到结束标志后退出循环,并关闭目标文件。 6. 读取进程和写入进程都需要在完成任务后关闭文件,并退出进程。 示例代码(仅供参考): 读取进程: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define BLOCK_SIZE 1024 int main(int argc, char *argv[]) { int fd_in, fd_out; char buf[BLOCK_SIZE]; ssize_t nread; if (argc != 3) { fprintf(stderr, "Usage: %s source_file target_file\n", argv[0]); exit(EXIT_FAILURE); } fd_in = open(argv[1], O_RDONLY); if (fd_in == -1) { perror("open source_file"); exit(EXIT_FAILURE); } fd_out = creat(argv[2], 0644); if (fd_out == -1) { perror("create target_file"); exit(EXIT_FAILURE); } while ((nread = read(fd_in, buf, BLOCK_SIZE)) > 0) { if (write(fd_out, buf, nread) != nread) { perror("write target_file"); exit(EXIT_FAILURE); } } if (nread == -1) { perror("read source_file"); exit(EXIT_FAILURE); } close(fd_in); close(fd_out); return 0; } ``` 写入进程: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define BLOCK_SIZE 1024 int main(int argc, char *argv[]) { int fd_in, fd_out; char buf[BLOCK_SIZE]; ssize_t nread; if (argc != 3) { fprintf(stderr, "Usage: %s source_file target_file\n", argv[0]); exit(EXIT_FAILURE); } fd_in = open(argv[1], O_RDONLY); if (fd_in == -1) { perror("open source_file"); exit(EXIT_FAILURE); } fd_out = creat(argv[2], 0644); if (fd_out == -1) { perror("create target_file"); exit(EXIT_FAILURE); } while ((nread = read(fd_in, buf, BLOCK_SIZE)) > 0) { if (write(fd_out, buf, nread) != nread) { perror("write target_file"); exit(EXIT_FAILURE); } } if (nread == -1) { perror("read source_file"); exit(EXIT_FAILURE); } close(fd_in); close(fd_out); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值