Thread: Fork + Exec + ? Question!

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    8

    Fork + Exec + ? Question!

    I am trying to accomplish a simple task... But I couldn't make it to work properly.

    The code below is an attempt to "fork + execute a new process" but it is not working as I expected because when the child executes the process, it behaves differently when it is called by the parent.

    Can you say what am I doing wrong?

    Code:
    #include <unistd.h>
    
    
    long int main(long argc, char** argv, char** envp)
    {
      char* fpath = "/home/ignorant/Desktop/test2";
      char* agv[] = {fpath, 0};
    
    
      int frk = fork();
    
    
      if (frk != 0)
      {
        // parent!
        if (frk > 0)
        {
          // successful! Executed program behaves OK from here...
          //execve(fpath, agv, NULL);
          return 0;
        }
        else
        {
          // failed!
          return 3;
        }
      }
      else
      {
        // child! Executed program behaves differently from here...
        execve(fpath, agv, NULL);
        return 3;
      }
    }
    Thank you!

  2. #2
    Registered User
    Join Date
    Mar 2015
    Posts
    8
    I think I understood now. The parent process was being terminated.


    I think it is solved.


    Thanks for reading!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-27-2014, 03:53 AM
  2. Fork() and exec() help
    By TuXaKoS in forum C Programming
    Replies: 3
    Last Post: 11-01-2010, 11:35 AM
  3. Question about fork and exec
    By steli89 in forum Linux Programming
    Replies: 4
    Last Post: 04-13-2010, 07:17 AM
  4. fork/exec vi problem
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 10-15-2009, 01:12 AM
  5. fork + exec
    By vipul_vgp in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 08:00 AM

Tags for this Thread