system ("cd ..");
This doesn’t produce any error but also doesn’t do anything meaningful. Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
system()function makes afork()that creates a process being a copy of the initial one.The current directory depends on the environment of a process (it is stored within the environment variables of a process). Thus when the child process, having its own environment, makes a
cd, that affects only the child process, not the parent.Parent process: in /home/x/y
Child process (after the fork): in /home/x/y
Doing a
cd ..in the child process sets its local environment to /home/xBut the parent process is still in /home/x/y
Parent process waits for child to complete the
systemcall, then continue its own execution having its own environment (current directory) unchanged.