473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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",argv[0]);
exit(223);
}

timer=strtol(argv[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,&status,WNOHANG);
usleep(250);
count=count+250;

if (count > timer)
{
pgpid=getpgid(retval);
/* 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",status);
exit(status);
break;
}
return 0;
}
Nov 14 '05 #1
1 3435
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.programmer.

Lawrence

Nov 14 '05 #2

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

Similar topics

1
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...
2
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...
1
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. ...
16
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...
27
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();...
1
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...
2
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...
5
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.