ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ive found this code on "Advanced Linux Programming - Chapter 3 : Processes" it should terminate fork()ed processes after they done their job
but it doesnt, as soon as the forked processes finishes its job and calls exit, the whole application restarts :/
here is the code
Code:
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
sig_atomic_t child_exit_status;
void clean_up_child_process (int signal_number)
{
/* Clean up the child process. */
int status;
wait (&status);
/* Store its exit status in a global variable. */
child_exit_status = status;
}
int main ()
{
/* Handle SIGCHLD by calling clean_up_child_process. */
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);
/* Now do things, including forking a child process. */
/* ... */
return 0;
}
and thats how i fork processes
Code:
if (fork() == 0)
{
/* Call the Function required here */
rename("/home/killercode/data.zip", "/home/killercode/file.zip");
exit(0);
}
thanks.
Last edited by KillerCode; 02-17-2011 at 12:24 PM.
yea, but with ur method ill have to call the cleanup code reguraly, im trying to get the one i posted to work since the forked processes sends a SIGCHLD signal whenever it finishes working. so its not blocking any of the app functions.
thanks.
EDIT: this app should sleep for 100 seconds, but it doesnt :/
Code:
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
sig_atomic_t child_exit_status;
void clean_up_child_process (int signal_number)
{
/* Clean up the child process. */
int status;
wait (&status);
/* Store its exit status in a global variable. */
child_exit_status = status;
}
int main ()
{
/* Handle SIGCHLD by calling clean_up_child_process. */
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);
if (fork() == 0)
{
exit(0);
}
sleep(100);
return 0;
}
Last edited by KillerCode; 02-17-2011 at 01:13 PM.
yea, but with ur method ill have to call the cleanup code reguraly, im trying to get the one i posted to work since the forked processes sends a SIGCHLD signal whenever it finishes working. so its not blocking any of the app functions.
thanks.
EDIT: this app should sleep for 100 seconds, but it doesnt :/
That is because the sigchild signal interrupts the sleep call. Try something like the following to sleep for the full time.
Code:
sleeptime = 100;
while ((sleepTime = sleep(sleepTime)) > 0);
See the man page for sleep for more info on how it works.
its not just sleep, any function i do, such as receiving file from http server, it restarts the whole application as soon as the download (forked process) completes !!
I ran your test code replacing sleep with the while loop I provided and it sleeps the whole time.
What are you doing in the parent process? I assume you have a loop doing something? what is the exit condition of the loop? Other system calls are affected by signals and need to be 'restarted' after a signal occurs in a process.
ok, i can do something like that, one more question, is there is no other way to make it uninterrupted? maybe a something when assigning the signal handler? or there is no way to evade the interruption without modifiying recv()?
What I showed is a pretty standard way of dealing with it as it will work everywhere and always.
Take a look at the man page for signal (http://www.kernel.org/doc/man-pages/.../signal.7.html) for a info on signals and interruptable system calls. On linux you can possible use SA_RESTART with your signal handler, but it doesn't all interruptable system calls. You'll notice on the man page that SA_RESTART works for recv, depending on how it is called.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.