LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2015, 07:07 PM   #1
Blue-Knight
LQ Newbie
 
Registered: Jan 2014
Distribution: LinuxMint
Posts: 11

Rep: Reputation: Disabled
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!
 
Old 04-08-2015, 08:45 PM   #2
Blue-Knight
LQ Newbie
 
Registered: Jan 2014
Distribution: LinuxMint
Posts: 11

Original Poster
Rep: Reputation: Disabled
I think I understood now. The parent process was being terminated.

I think it is solved.

Thanks for reading!
 
Old 04-10-2015, 02:52 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Blue-Knight View Post
I think I understood now. The parent process was being terminated.

I think it is solved.

Thanks for reading!
Yes, that's exactly correct. It is a very common mistake many make when they play with fork(), they neglect to realize that if they merely exit their parent, it then kills the child as well.

The parent should invoke something like waitpid() to wait for signals from the child and further to consider checking the status return to determine if the child exited by signal, normally, in error, and whether or not there was a core dump.

Some of this is in some old blog entries I made on the subject:
http://www.linuxquestions.org/questi...ocesses-35540/
http://www.linuxquestions.org/questi...zombies-35575/
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gdb, fork(), exec() jason_m Programming 1 08-28-2010 11:03 AM
Fork exec problem bhupeshchawda Programming 3 04-19-2007 04:58 AM
fork/exec problem djgerbavore Programming 2 04-13-2006 11:13 PM
how to fork/ exec schneidz Programming 10 08-30-2005 11:50 AM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 02:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration