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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site