Thread: strdup() and execvp()

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    strdup() and execvp()

    I am working on an intro to OS book and have some questions about this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/wait.h>
    
    
    int main(int argc, char *argv[]) {
         printf("hello world (pid:%d)\n", (int) getpid());
         int rc = fork();
         if (rc < 0) { // fork failed
              fprintf(stderr, "fork failed\n");
              exit(1);
         } else if (rc == 0) { // child (new process)
              printf("hello, I am child (pid:%d)\n", (int) getpid());
              char *myargs[3];
              myargs[0] = strdup("wc"); // program: "wc" (word count)
              myargs[1] = strdup("p3.c"); // argument: file to count
              myargs[2] = NULL; // marks end of array
              execvp(myargs[0], myargs); // runs word count
              printf("this shouldn’t print out");
         } else {              // parent goes down this path (main)
              int wc = wait(NULL);
              printf("hello, I am parent of %d (wc:%d) (pid:%d)\n",
                      rc, wc, (int) getpid());
         }
    
    
         return 0;
    }
    Because I am using strdup() to create this new strings, shouldn't I be free()ing the pointers? Or does execvp() somehow handle this?

    The book says the heap and stack and other parts of the memory space of the program are reinitialized. I know that these pointers reside in the heap, but what does it mean to be "reinitialized"?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    On execve() (which execvp() winds up calling), any allocated memory is freed, so you're fine. That's probably what the author means by reinitialized: set back to the initial state, i.e. the memory is freed.

    How exactly it's freed is going to be system-dependent, but it will probably include things like an implicit munmap() of mmap()ed memory, setting the program break to its initial value, etc.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Thank you.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strdup
    By darren78 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2010, 08:29 AM
  2. Execvp help
    By hambergler in forum C Programming
    Replies: 16
    Last Post: 06-02-2010, 09:06 PM
  3. Strdup
    By Ducky in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2010, 12:52 PM
  4. Regarding 'strdup()'
    By subhashish1213 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2009, 09:18 AM
  5. strdup
    By hankspears in forum C Programming
    Replies: 4
    Last Post: 05-09-2002, 02:02 PM