473,216 Members | 1,292 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,216 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?)


#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]);

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

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

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]);

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;
fprintf(stderr, "221 exec failed (%i)\n",errno);

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

timer=timer * 10000;
/* printf("PID: %i\n",pid); */
retval = waitpid(pid,&status,WNOHANG);

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

fprintf(stderr, "%i status\n",status);
return 0;
Nov 14 '05 #1
1 3423
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


Nov 14 '05 #2

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

Similar topics

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...
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...
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. ...
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...
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();...
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...
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...
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...
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...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.