Help - Search - Members - Calendar
Full Version: Problems with fork()
Linuxhelp > Support > Technical Support
abstract_void
I have written a daemon process that is started as the system boots into run level 5 (i.e. I have a symbolic link in /etc/rc.d/rc5.d to /etc/rc.d/init.d). This daemon in turn uses the typical fork()/exec() combination to spawn processes (lets call it new_process):

if ((pid = fork() == 0) {
if (execv("new_process", NULL)) {
_exit(1);
}
_exit(0);
}

If the "new_process" calls system(3), or tries to execute another process by using the fork()/exec() method mentioned earlier, I always seem to get a failure with errno == 13, access denied.

If I start the daemon on the command line, I never get this problem, only when it is started as the system boots into runlevel 5.

Any ideas on why this occurs?
digitalturbulence
Your code is not really correct.
You don't check correctly the return values of execv. If execv is successfully, the process will be ended.
If not, exev will return the value -1. Your exit(1) will never be called.

In the man pages are written:
RETURN VALUE
If any of the exec functions returns, an error will have occurred. The return value is -1, and the global
variable errno will be set to indicate the error.

So you should modify your code so:
int pid;

pid = fork();
if (!pid)
{
if (execl("new_process", "new_process", NULL) == -1)
{
perror("execv"); /* make an error message if execl failed! */
exit(1);
}
}
wait(&pid);
exit(0);
}

replace "new_process" by something like, "/bin/ls"
don't use execv, use excel, because you need to give your called program at least one argument: The program name.

You can use execv if you give as argument a char ** table with one entry. Some programs could work without this argument, but it's recommended to do the things correctly, if you don't like to debug some hours for some little C code. :-)

saf@[no-spam]xy1.org
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.