Thread: Exchanging environment between father and child processes

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    7

    Exchanging environment between father and child processes

    I´m making a very simple scratch of a shell. The father process prints out the current working directory and waits for some user input. For each user input, it creates a child process to execute the specified command and pass to it the **environ variable. I´m using execve. I created a cd program to change the current directory. It changes the current directory to the specified and updates the PWD environment variable to the new value. But when this child process finish and the father prints out the current working directory again, the PWD seens to be the same as before the changing by the cd child process.
    So I dint manage to make the parent process get the environment changed by the child.
    How can I do that?

    To clarify a little bit, here is a code snippet

    -------------
    Father(shell)
    -------------

    extern char **environ;

    void main()
    {
    (...)

    do {
    path = getenv("PWD");
    printf("\n[%s]# ", path);
    }
    while (true)
    }

    -------------
    Child (cd)
    -------------

    extern char **environ;

    int main(int argc, char *argv[])
    {
    char cwd[100],
    *newDir;

    if (argc == 1)
    {
    newDir = getenv("HOME");
    chdir(newDir);
    }
    else
    {
    newDir = argv[1];
    chdir(newDir);
    }

    setenv("PWD", newDir, 1);
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    7
    < Read the Unix FAQ (your question also belongs into the Linux programming forum, please ask there next time), where I recently posted an explanation of why a command like cd cannot be a separate program.

    I´ve been serching in the Linux programming forum but didnt found there, neither in the Unix Faq you´ve mentioned. I think I´m not asking the right question.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    7
    About looking for: Sure we have to spend some time looking for answers, like I did, but the aswers for my question were in topics different from the mine. Your FAQ is a bit bigger to read everything to find the right topic wherever you could indicating it.

    About the problem explanation, I realized the scope in one process is different from the other. Said that, is there a way to process intercomunication? Can the child change the environment variable PWD (or something like that)?

    Thanks in advance

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    7
    > That's not an argument. You are supposed to have read the entire thing before you ask any questions about Unix system programming.

    I respect your point of view, but don't agree with that. I think we should keep focus on the problem solution. If we waste too much time reading a lot of things not related to our problem since we can go direct to the point, we´re not focusing on the problem.

    Once again, thank you for your help.

    Regards,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM