What would happen if fork were called prior to chdir and chd
What would happen if fork() were called prior to chdir(), and chdir() invoked within the forked child process?
if (fork() == 0) {
if (chdir(exec_argv[1]))
/* Error: change directory failed */
fprintf(stderr, \"cd: failed to chdir %s\ \", exec_argv[1]); exit(EXIT_SUCCESS);
}
Solution
If the fork() is invoked prior to chdir() a new child process is created. Now this forked child calls chdir() and if that fails it will print an error and program will exit but if it was success it will change the directory for the child only not the parent as it is being called in forked child.
![What would happen if fork() were called prior to chdir(), and chdir() invoked within the forked child process? if (fork() == 0) { if (chdir(exec_argv[1])) /* Er What would happen if fork() were called prior to chdir(), and chdir() invoked within the forked child process? if (fork() == 0) { if (chdir(exec_argv[1])) /* Er](/WebImages/42/what-would-happen-if-fork-were-called-prior-to-chdir-and-chd-1132281-1761605163-0.webp)