qfedu-linux-advanced-level/day4/file4_0.c

21 lines
589 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 配合file4使用
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
// 设置 1 的 close_on_exec 打开状态(默认为关闭)
int flags;
flags = fcntl(1, F_GETFD);
flags |= FD_CLOEXEC;
// flags &= ~FD_CLOEXEC; // 关闭 FD_CLOEXEC 标志位
fcntl(1, F_SETFD, flags);
execl("./file4_1", "file4_1", NULL); // 当 file4_1 存在时会切换到file4_1运行不会继续执行本程序后续行内容
printf("file4_0"); // 当file4_1不存在时才会执行到这里
return 0;
}