NOTE: Reply parts are posted inside original message.
> Hello everyone!
> I'm quite new in UNIX programming and I'm trying to create a program
> that will do something like that:
> - there are some script files that set some environment variables
> on the disk and there are also some programs that do a short work, but
> using the variables set by these scripts.
> - the program, first, needs to invoke one of the script files to set
> the environment
Try 'status = system(". /path/script_name")'
The '.' is used to "source" the shell script into the current process.
I use this when I make changes to my .profile and want the changes to
show up in the current session. You may have to make modifications to
the scripts to get it to work. Or, you can read the script and
use "putenv" (stdlib.h) to change the environment variables.
> - then after some computation it has to launch other program in the
> environment set by the script file invoked at the begining.
> - launch another program, and so on...
> Is it possible to do something like this using C programming (not
Most anything is possible, it may just be difficult.
Quote:> Using popen/pclose I can only execute one command at a time, using
> I also do not see the solution.
> Is there any other way?
Multiple commands can be executed at the same (actually similar) time
by forking, then using execl.
example (in pseudo code):
int runCommand(const char *commmand_str)
int forkpid, status;
forkpid = fork();
if (forkpid == 0) /* This is the child */
status = execl(command_str,...);
exit(status); /* Note: you must exit here, */
/* or you will have multiple */
/* mainlines running... */
else if (forkpid > 0) /* Parent, no error */
status = 0;
else /* Parent, with error (forkpid == -1)
status = -1;
int proc_count = 0, child_status = 0, status = 0, dead_pid = 0;
int exit_flag = 0;
/* Fire off the commands */
status = runCommand("command1");
status = runCommand("command2");
status = runCommand("command3");
/* Wait till the commands complete */
while (proc_count && !exit_flag)
dead_pid = waitpid(-1,
if (dead_pid < 0)
/* wait pid error - exit the loop */
exit_flag = 1;
else if (dead_pid > 0)
/* You can check the process exit status here - its in the */
/* child_status variable. man waitpid for more info */
else /* dead_pid == 0, no processes died */
sleep(3); /* give them time to die */
Oh, another way to wait for the children is to enable the SIGCHILD
signal, and write a handler.
> Please help.
> Kind regards,
> Pawel, Szymanski Data Development Sig/DA
> Networks Application Development phone +48 52 349 1394
Welcome to UNIX, which despite its name does have children....
Sent via Deja.com http://www.deja.com/
Before you buy.