PDA

View Full Version : Fork + Exec + ? Question!



Ignorant
04-08-2015, 04:19 PM
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?



#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!

Ignorant
04-08-2015, 07:46 PM
I think I understood now. The parent process was being terminated.


I think it is solved.


Thanks for reading!