Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

fork and execl

Hi,

Does anyone have any idea how to call gmake clean and gmake using fork and execl?

Instead of having the following in my C program, I would like to use fork a child process and get the child process to do it

system("gmake clean > dev/null ");
system("gmake > /dev/null ");

Please help.

0
meowmog
Asked:
meowmog
1 Solution
 
grg99Commented:
I'd suggest you keep on using system() to do this.

You could get a smidgen more efficiency using fork and exec, but there are a lot of little details you have to get just right.  It's probably not worth the effort.


0
 
ellesdCommented:
You would fork to create the child, close stdout file descriptor, open /dev/null, and then do your execl:

if(fork() == 0)
{
  close(1);
  open("/dev/null", "w");
  execl("gmake", "gmake", "clean", NULL);
  fprintf(stderr, "execl 'gmake clean' failed\n");
}

if(fork() == 0)
{
  close(1);
  open("/dev/null", "w");
  execl("gmake", "gmake", NULL);
  fprintf(stderr, "execl 'gmake' failed\n");
}

If you want the parent to wait for the first child to finish then add a wait() sytem call between the two forks.

You might want to consider using execlp() so that the system will follow your PATH to locate 'gmake'.  If you use execl(), you will want to provide the pathname to execl:
execl("/path/gmake", "gmake", . . .)

Notice that the second reference to gmake does not get the path; that's because it is supplied as argv[0] to the exec'd program.

Let me know if you need further assistance.
0
 
silentlucidityCommented:
You could have both the gmake commands in a shell script and do a system("script &") which would run the script in the background instead of a fork/execl. The underlying assumption is that you don't want the process to wait for the gmake to get done coz if you do, I think the code is best left as is.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
meowmogAuthor Commented:
Hi,

I've tried to fork and use the execl like what ellesd had mentioned. it didn't work. nothing happen. Maybe the execl is trying to locate the executable file gmake because I set the path to be /my/path/gmake. But the fact is there wasn't any gmake program in the directory. I only want to run gmake from that directory and this gmake uses a file called Makefile which is in that directory.

My Makefile is in /my/path/ so how can I get this gmake to locate the Makefile and execute gmake?

I've also tried execl("/my/path/Makefile",  "gmake", NULL); but it doesn't work.

I need to use fork and execl instead of system call because it seems that I have to provide the actual path instead of a variable when using system call.

meowmog
0
 
meowmogAuthor Commented:
Hi,

I've tried to fork and use the execl like what ellesd had mentioned. it didn't work. nothing happen. Maybe the execl is trying to locate the executable file gmake because I set the path to be /my/path/gmake. But the fact is there wasn't any gmake program in the directory. I only want to run gmake from that directory and this gmake uses a file called Makefile which is in that directory.

My Makefile is in /my/path/ so how can I get this gmake to locate the Makefile and execute gmake?

I've also tried execl("/my/path/Makefile",  "gmake", NULL); but it doesn't work.

I need to use fork and execl instead of system call because it seems that I have to provide the actual path instead of a variable when using system call.

meowmog
0
 
silentlucidityCommented:
Try one of the following -
a) execlp("gmake","gmake",NULL) as suggested by ellesd. This would pick up the PATH environment variable which sets the search path for the shell of the parent process.
Amke sure that you have /my/path:/usr/bin in the PATH environment variable of the shell in which you are executing.

b)execl("/usr/bin/gmake","gmake","-f","/my/path/Makefile",
NULL);

c) write a script called makescript to have the following.
   #!/usr/bin/sh
   gmake clean > /dev/null
   gmake > /dev/null

change permission of thsi script to execute. (chmod 777 makescript)

in your code replace your system calls with
system("<path>/makescript &");
This would execute the script in the background and your program would continue as normal.
0
 
jmcgOwnerCommented:
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by silentlucidity.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now