On Wed, Dec 15, 1999 at 12:45:04AM -0000, Joao Pedras wrote:
> Hello all!
>
> I am in need to run on linux a small app that calls other apps and leaves them
> running in background. What's so special about this ? It is suitable for
> performing this kind of operation from php3 scripts.
> My problem is that this was conceived to work on bsd systems and I have heard
> that bsd signals stuff in linux should be taken into consideration, in order to
> make it work properly on linux. This doesn't perform ok on linux without
> modifications which I am not qualified to perform.
> It compiles ok, but, when I run it, the called app doesn't stay on the
> background.
>
> Here's the code :
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> int main(int argc, char *argv[])
> {
> int pid;
pid_t pid;
> pid=fork();
>
> if (pid!=0)
> {
> pid=setsid();
> close(STDOUT_FILENO);
> close(STDIN_FILENO);
> close(STDERR_FILENO);
I think your logic is at fault. You want to execl() in the child process,
correct? fork() returns 0 in the child, so the correct code would be:
if (pid == 0) {
pid=setsid();
/*
* I would check setsid()'s return and declare another variable to
* store its return value, too.
*/
close(STDOUT_FILENO);
close(STDIN_FILENO);
close(STDERR_FILENO);
execl(argv[1],argv[2],0);
}
> execl(argv[1],argv[2],0);
> }
Um, the argv vector begins at 0. What happens to that argument? As
written, the following will fail:
$ ./a.out ls
and the following will not:
./a.out foobar ls
>
> return 0;
> }
I would also add a check for fork()'s return, since it may fail:
if((pid = fork()) < 0) {
/*
* Handle error here
*/
} elseif(pid == 0) {
/*
* In the child
*/
} else {
/*
* In the parent
*/
}
Kurt
--
I know that an engineer designed the human body. Who else would put a
waste dispoal plant next to a pleasure center?