Skip to content

Instantly share code, notes, and snippets.

@sokrato
Created September 18, 2015 06:11
Show Gist options
  • Save sokrato/8d40de6180dbac912022 to your computer and use it in GitHub Desktop.
Save sokrato/8d40de6180dbac912022 to your computer and use it in GitHub Desktop.
文件描述符的O_CLOEXEC 参数
// main.c
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
static char buf[5];
int main(int argc, char* argv[])
{
if (argc < 3) {
fprintf(stderr, "missing arg\n");
return 1;
}
int fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, 0644);
if (fd < 0) {
perror("打开文件失败");
return 2;
}
write(fd, "hello\n", 6);
sprintf(buf, "%d", fd);
printf("fd is %s\n", buf);
execl(argv[2], "execd", buf, NULL);
return 0;
}
// sub.c
// #include <stdio.h>
// #include <unistd.h>
// #include <stdlib.h>
// int main(int argc, char *argv[])
// {
// if (argc < 2) {
// fprintf(stderr, "缺少参数 fd");
// return 1;
// }
// int fd = (int) strtol(argv[1], NULL, 10);
// if (fd < 0) {
// fprintf(stderr, "无效的 fd: %s\n", argv[1]);
// return 1;
// }
// if (1 > write(fd, "\nAAA\n", 5)) {
// perror("fd写入失败");
// return 2;
// }
// close(fd);
// return 0;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment