ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
To explain the behavior that the original poster is seeing, and why his approach will never work, the exec() call spawns a new process, and any actions performed within the context of that process are completely independent of the parent process. If it were any other way, then all processes would have the same working directory, as all processes are descendants of the original init process. The same effect applies to modification of environment variables, which is a similar thing that people trip over. bash uses the source command to work around this.
Hope this clears things up.
if exec is able to spawn a new process then why we would be in a need to have fork system call
fork system call would create a new process while
exec system call would overlay the process image of a process with the code and data of a new process copied
Sorry, you're right. However, since exec() never returns, the original process becomes effectively terminated. I was thinking of the system() function, which does a fork internally.
On re-reading the original post, I answered a question not even asked. My double-oops. The first reply answered the whole question. mea culpa.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.