|XP is just a number|
RE: RE: chdir vs. shell behaviorby ahunter (Monk)
|on Jun 15, 2000 at 21:47 UTC||Need Help??|
It is a FAQ anyway, but I'll explain and risk setting a precedent...
DOS does not have anything remotely like the Unix concept of a process. When a piece of software in DOS launches another one, it loads it into its own memory space and continues from that programs start point. This means that all programs under DOS share the same environment, including directory, environment variables, and so on.
In Unix and workalikes, there is the concept of processes. These have their own environment, and usually have a parent process, which their initial environment is inherited from. This means that when you start a new process with system() or whatever, it gets a copy of CSD and environment variables of the process that started it. When it alters its environment, it only affects itself and any child processes it launches after that point. So, if you do a chdir from a process, it only affects that process's environment. If you consider that under Unix that the processes could be running in parallel, you should realise that this is a Good Thing.
However, this means that a child process cannot directly manipulate its parents environment. It needs to communicate with the parent process - easiest way being backticks, supported in both perl and shell scripts. IE:
Would cause the output of myscript to be evaluated in the process that launched it.
Next up - the meaning of exec()! exec() is the closest thing Unix has to DOS's model of execution - it replaces the program running in the current process with a new one, without creating a new process. The old program is never re-entered, though - if you want to do that, you have to do it yourself (the design of most modern Unixen makes this less costly than most people think). A lot of beginners make the mistake of using system() where exec() would be more appropriate (system() does a fork() and then an exec() under Unix. Actually, it also waits for the child process to terminate). Use exec() if you've finished with the current process.
Andrew (mysteries explained, or at least made more mysterious)