Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> Problems with fork(), Problems with fork()
abstract_void
post Apr 4 2003, 06:37 PM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 1
Joined: 4-April 03
Member No.: 631



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?
Go to the top of the page
 
+Quote Post
digitalturbulenc...
post Apr 5 2003, 07:22 PM
Post #2


Whats this Lie-nix Thing?
*

Group: Members
Posts: 21
Joined: 5-April 03
Member No.: 633



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


--------------------
Free disposable email addresses to
stop spam mails!
Try it: TrashMail.net
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 13th December 2017 - 11:48 AM