Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Changing environment variables

derm0derm0 Member Posts: 4
Hi all,

I'm currently working on a final year project which involves the creation of a number of named pipes.

Currently i'm forking 3 programs from my main application and I was wondering if anyone would be able to give me any help or advice on setting up the environment variables with the names of the FIFOs by setenv??

Just can't seem to get my head around it!

Thanks in advance,
Derm

Comments

  • nugentnugent Member Posts: 87
    hope this helps.

    [code]
    #include
    #include
    #include

    int main()
    {
    pid_t pid, child, par;
    int status;

    /* change 0 to 1, if you want to overwrite the value of an existing variable */
    setenv("MY_ENV", "VARIABLE", 0);

    pid = fork();
    if(pid == 0)
    {
    /* this is the child process */
    par = getppid(); /* get pid of parent process */
    child = getpid(); /* get pid of current (child) process */
    printf("Parent pid: %i
    Child pid: %i
    ", par, child);

    char *tmp = getenv("MY_ENV"); /* get the value of the variable */
    printf("%s
    ", tmp);
    }

    else if (pid < 0) /* fork failed */
    status = -1;

    else /* back to parent process */
    if(waitpid(pid, &status, 0) != pid)
    status = -1;

    /* unset the variable */
    unsetenv("MY_ENV");
    return 0;
    }
    [/code]

    ------
    nugent



  • derm0derm0 Member Posts: 4
    Tanx a mil - thats loads of help!

    : hope this helps.
    :
    : [code]
    : #include
    : #include
    : #include
    :
    : int main()
    : {
    : pid_t pid, child, par;
    : int status;
    :
    : /* change 0 to 1, if you want to overwrite the value of an existing variable */
    : setenv("MY_ENV", "VARIABLE", 0);
    :
    : pid = fork();
    : if(pid == 0)
    : {
    : /* this is the child process */
    : par = getppid(); /* get pid of parent process */
    : child = getpid(); /* get pid of current (child) process */
    : printf("Parent pid: %i
    Child pid: %i
    ", par, child);
    :
    : char *tmp = getenv("MY_ENV"); /* get the value of the variable */
    : printf("%s
    ", tmp);
    : }
    :
    : else if (pid < 0) /* fork failed */
    : status = -1;
    :
    : else /* back to parent process */
    : if(waitpid(pid, &status, 0) != pid)
    : status = -1;
    :
    : /* unset the variable */
    : unsetenv("MY_ENV");
    : return 0;
    : }
    : [/code]
    :
    : ------
    : nugent
    :
    :
    :
    :

Sign In or Register to comment.