473,698 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fork(): how can I kill forked process and all ITS children, but leaveparent alive?

I am trying to write a program that will fork a process, and execute the
given task... the catch is that if it runs too long, I want to clean it up.

this seemed pretty straight forward with a single executable being run
from the fork. The problem I am having now is that if I call a shell
scripts, then lets say calls "xterm &", after the timeout has occurred,
I kill the shell script, but the xterm is still running... I cannot seem
to kill the "xterm" without giving a vicious enough kill that ends up
terminating everything including the original parent process...

anyone know how to send a kill forcibly signal to a forked pid, and ITS
PGPID (while not having the parent get hit with the signal?)

--------------------------CUT--------------------------

#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>

extern int errno;

int main(int argc, char** argv)
{
pid_t pid; /* PID of child */
pid_t retval;
int status; /* exit status from child, returned by wait */
long timer=0;
int count=0;
pid_t pgpid;

if (argc < 3)
{
printf("223 syntax: %s [timeout] [command] {params}\n",arg v[0]);
exit(223);
}

timer=strtol(ar gv[1], (char **)NULL, 10);

if (timer == 0)
{
fprintf(stderr, "224 invalid time: %i\n",timer);
exit(224);
}

switch (pid = fork())
{
case 0:
/* child process */
/* printf("trying to execute: %s\n",argv[2]); */
if (argc > 3)
{
int i=0;
for(i=3;argv[i] !=NULL;i++)
printf("arg %i: %s\n",i,argv[i]);

execv(argv[2],&argv[2]);
}
else
{
/*************** ***
okay, what is going on here?!?!?
in linux, I can just put "NULL" as the second
param in execv, but when running on solaris,
it causes a segfault... so this way seems to
work for both linux and solaris....
*************** ****/
char *nully_furtado= NULL;
execv(argv[2],&nully_furtado );
}
fprintf(stderr, "221 exec failed (%i)\n",errno);
exit(221);
break;

case -1:
/* fork has failed, so do nothing */
fprintf(stderr, "222 fork failed\n");
exit(222);
break;

default:
timer=timer * 10000;
do
{
/* printf("PID: %i\n",pid); */
retval = waitpid(pid,&st atus,WNOHANG);
usleep(250);
count=count+250 ;

if (count > timer)
{
pgpid=getpgid(r etval);
/* printf("pgpid: %i\n",pgpid); */
fprintf(stderr, "220 too long\n");
kill((-1 * pgpid),9); /* kill everyone in the group */
exit(220);
}
}
while (retval != pid);

fprintf(stderr, "%i status\n",statu s);
exit(status);
break;
}
return 0;
}
Nov 14 '05 #1
1 3460
On Fri, 12 Nov 2004 13:24:10 +0000, Alexander N. Spitzer wrote:
I am trying to write a program that will fork a process, and execute the
given task... the catch is that if it runs too long, I want to clean it up.


fork() and process handling are features of the Unix system unterface,
they aren't part of the C language. A good place to discuss them would be
comp.unix.progr ammer.

Lawrence

Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2802
by: Yin | last post by:
I am writing a script that monitors a child process. If the child process dies on its own, then the parent continues on. If the child process is still alive after a timeout period, the parent will kill the child process. Enclosed is a snippet of the code I have written. For some reason, unless I put in two time.sleep(4) commands in the parent, the process will not sleep. Am I forgetting something? Any reasons for this strange...
2
3390
by: Axel | last post by:
Hello I m trying to having X child process from 1 original process to generate random numbers. I m using (1+(int) (10.0*rand()/(RAND_MAX+1.0))) from the man page of rand() /random(). The problem is that all these childs give as result the absolutly same value.
1
1882
by: Gvs | last post by:
Hi, I'm just playing around with forking processes and was wondering how can you keep a child alive as long as the parent is alive. To give an example here is my code. ----------------------------------------------------------------------- for(i=0; i<2; i++) { if((pid = fork()) == -1) //run the fork command
16
15213
by: bie2 | last post by:
Hi, Here's what I want to do: 1. Start the application. 2. A SplashScreen running in its own thread is started. 3. I'm checking if the application needs an update. 4. If an update is needed I close the SplashScreen and copy the file. 5. Restart the application.
27
5970
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork(); value = value + 2; number = number + 2;
1
5077
by: vduber6er | last post by:
Hi I want to have a wait page while the rest of the cgi does its process, but it seems like the wait page waits till everything is complete and never appears. I've tried forking twice already as suggested in a faq, but it still doesnt seem to work. Below is what I have done: pid = fork() if (pid == 0){ printf ("http://my.wait.page"); exit (100);
2
2156
by: Poly-poly man | last post by:
I am working on a GTK Midi player (timidgtk.sourceforge.net). As you can see, it's just a timidity frontend. With version 0.03, I'm trying to devel it to use SDL_sound to play the midis. First off, is there a better library for this? Right now I'm writing a test program to play on console, just so that I can narrow problems down very quickly. I have a "sound.c" (basically playsound from SDL_sound, but without a lot of features. I added...
5
12227
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I recently explained it to a friend of mine and at that time decided to see if I could replicate it in C#. I have created an application that, to me at least, mimics what fork would/should do on *nix/bsd systems. I know that fork spawns a new...
1
1648
by: 3srt | last post by:
Hello, I created a simple 'server' script that will accept input from a cgi script. The server will handle multiple connections on the same port, also. In a nut shell, this 'server' script will take in variables, and fork off a process to sleep for a given amount of time, and then send the user an email (and perform other functions). Here is my problem: I receive a confirmation e-mail once the connection has been made, but i do not...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.