1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <sys/wait.h> #include <sys/types.h> #include <fcntl.h> #include <dirent.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include "apue.h"
int testDir() { DIR *dirp;
if ((dirp = opendir("/home/emerald/Documents/Apue/8")) == NULL) err_sys("opendir error"); int fd = dirfd(dirp); int status; if ((status = fcntl(fd,F_GETFD)) == -1) err_sys("fcntl error"); if (status & FD_CLOEXEC) printf("close-on-exec on"); else printf("close-on-exec close");
return fd; }
int testFile() { int fd; if ((fd = open("/home/emerald/Documents/Apue/8/1.in",O_RDWR)) == -1) err_sys("open error"); int status; if ((status = fcntl(fd,F_GETFD,0)) == -1) err_sys("fcntl error"); if (status & FD_CLOEXEC) printf("close on exec on"); else printf("close on exec close"); return fd; }
int main(int argc,char *argv[]) {
int fd = testFile();
pid_t pid; char buf[10]; sprintf(buf,"%d",fd);
if ((pid = fork()) < 0) err_sys("fork error"); else if (pid == 0) { char *path = "/home/emerald/Documents/Apue/8/b"; if (execl(path,"b",buf,(char *)0) < 0) err_sys("execl error"); }
if (waitpid(pid,NULL,0) == -1) err_sys("error waitpid"); exit(0); }
|