Medium
File descriptors across exec()
main.cpp opens data.txt (suppose open() returns fd 3), then execl()s into child_prog without closing it. child_prog immediately calls read(3, buf, sizeof(buf)), using the hard-coded fd number 3. Does that read succeed and return bytes from data.txt?
main.cpp
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
int main() {
int fd = open("data.txt", O_RDONLY); // suppose this returns fd 3
execl("/usr/bin/child_prog", "child_prog", nullptr);
perror("execl failed");
return 1;
}3 of 53 in Operating Systems