Visit Our UNIX and Linux User Community

forking and killing parent processes

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers forking and killing parent processes
# 1  
Old 10-17-2005
forking and killing parent processes

Hi everybody,

I'm having some problems wiriting a program in UNIX using the "fork" and "kill" system calls.

I have to create a C program P0, which creates 9 other processes
P1, P2, ..., P9, where P0 is the father of P1, P1 the father of P2, and so on.

All the processes contain an infinite loop.

When all the processes are created P9 executes the command "ps".
Then P9 kills P8, execute "ps", then kills P7, executes "ps", and so
on till only the process P0 and P9 remain.

I've created the 9 processes in the following way:

P0 does a fork. The child process executes p1 and the parent process enters and infinite loop.
p1 to p8 do the same thing.
p9 then executes ps.

Up to here there are no problems, ps is executed and everything seems to be working fine, the PID and PPID are correct, and all the processes are marked as running.

The problem I have is how to kill the parents of p9.

I use the kill function, but for this I need the PID of the processes to kill.
How can this be found?

To see at least if the kill process does what's it supposed to do, I use the getppid() system call to retrieve the PID of process p9's father and loop it back from there (not very elegant but should work in theory).

With this pid I then use the kill function with the SIGKILL signal and the pid of the process to be killed.

The problem here is that only p8 gets killed while the rest of the processes keep running!

Therefore the process p9 does something like this:

int main() {




void execute_ps() {

	pid = fork();

	if (fork != 0)



void kill_parents() {

	parent = getppid();

	for (i=0; i<8; i++) {

		kill(parent, SIGKILL);




Any help/suggestions are welcome!

Thanks a lot


Previous Thread | Next Thread
Test Your Knowledge in Computers #423
Difficulty: Medium
C# is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.
True or False?

10 More Discussions You Might Find Interesting

1. Solaris

Child killing parent process and how to set up SMF

Hello, A little background on what we are doing first. We are running several applications from a CLI, and not all of them are fully functional. They do on occasion core dump, not a problem. We are running a service that takes a screen scrape of those apps and displays them in a more user... (5 Replies)
Discussion started by: Bryan.Eidson
5 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Programming

Parent forking

My question is, how do you fork only the parent processes in unix? For example how would I use the fork function to fork the parent process more than once and leave the children processes alone. This way I do not have children of children. The way I have it set up now it the parent process forks 3... (7 Replies)
Discussion started by: TWhitt24
7 Replies

4. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

5. Shell Programming and Scripting

Forking a bunch of processes and filling up the process table

I have a bash script that has been used for months here at work for doing an SSH into other machines both Linux and Solaris and running a script on the remote machine. Recently I have started to noticed that things are being left being on the maching doing the SSH. For example.... tivoli ... (1 Reply)
Discussion started by: LRoberts
1 Replies

6. UNIX for Advanced & Expert Users

Child Killing Parent

Hi all, I am writing a script which calls other third party scripts that perform numerous actions. I have no control over these scripts. My problem is, one of these scripts seems to execute and do what it is meant to do, but my calling / parent script always exits at that point. I need to... (4 Replies)
Discussion started by: mark007
4 Replies

7. UNIX for Advanced & Expert Users

Forking a new process without parent dependance

hi, I want my program to fork a new process and then I want to kill the parent process. The parent program before dying will issue a SIGTERM to all its childs. Which eventually kills all Children. I cant handle the SIGTERM at the child level.:( What I was thinking of was the Parent... (3 Replies)
Discussion started by: tyler_durden
3 Replies

8. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies

9. Shell Programming and Scripting

Killing parent shells from subshells (KSH)

Hi all, I have a shell script which calls other shell scripts, depending on the input. Within, I have a command which calls (ie. ksh Normally, we use the exit function to terminate a shell. However, if I choose to call exit from, I will return to the parent shell who... (4 Replies)
Discussion started by: rockysfr
4 Replies

10. Programming

forking n number of processes in a loop and not 2^n

Hi, Am working on linux. while forking in a loop how to avoid the child process from forking..for example int n = 5; pid_t pid; pid_t ch_pid; /*exactly wanted to create n processes and not 2^n processes*/ for(i = 0; i < n;i++) { if(pid = fork()) { /* parent... (4 Replies)
Discussion started by: rvan
4 Replies

Featured Tech Videos