?
Solved

fork and execl

Posted on 2003-02-20
8
Medium Priority
?
418 Views
Last Modified: 2010-04-15
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
Comment
Question by:meowmog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 7990779
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
 
LVL 2

Expert Comment

by:ellesd
ID: 7991495
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
 

Accepted Solution

by:
silentlucidity earned 400 total points
ID: 7992269
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.

 

Author Comment

by:meowmog
ID: 7999871
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
 

Author Comment

by:meowmog
ID: 8000087
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
 

Expert Comment

by:silentlucidity
ID: 8006461
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10011057
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question