LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Killing child process (https://www.linuxquestions.org/questions/programming-9/killing-child-process-236175/)

coolfrog 09-28-2004 07:52 AM

Killing child process
 
Hi,
i am currently working on the Diner Philosophers problem. I have this code :
Code:

while(ctr < 5)
        {
                switch(pid=fork())
                {
                        case 0:        p.pid = ctr;
                                for(i=0;i<iteration;i++)
                                {
                                        cout << "\n" << p.pid << ": Thinking.....";
                                        sleep(rand());        //        THINKING
                                        cout << "\n" << p.pid << ": Acquiring right chopstick.....";
                                        lock(p.pid);        //        LOCK RIGHT CHOPSTICK
                                        cout << "\n" << p.pid << ": Acquiring left chopstick.....";
                                        lock((p.pid+1)%5);        //        LOCK LEFT CHOPSTICK
                                        cout << "\n" << p.pid << ": Eating.....";
                                        sleep(rand());
                                        cout << "\n" << p.pid << ": Releasing chopsticks.....";
                                        unlock(p.pid);        //        LOCK RIGHT CHOPSTICK
                                        unlock((p.pid+1)%5);        //        LOCK LEFT CHOPSTICK                               
                                }
                                cout << "\nProcess " << p.pid << " exiting....";
                                exit(0);
                                break;
                        case -1: cout << "\nError";
                                break;
                        default: ctr++;
                                continue;
                }
                break;
        }

Now after fork the child process does enter case 0 but does not enter for loop. Also after the entire program terminates and when i execute the command 'ps' i still have 5 a.out processes running. How do i kill the processes permanently through C/C++ code ? What about the for loop problem ?

koyi 09-28-2004 11:11 AM

As far as I know, there are two ways to prevent the child process from becoming a zombie process.

1. Call wait() from the parent process.
2. After the first fork(), fork() again and kill the child process immediately thus handing the grand-child process to the init process. I am not sure whether you should do a wait() in the parent process in this case. So please do experiment yourself.

Search for "kill zombie process" may help you understand why those child processes exist.

trickykid 09-28-2004 12:48 PM

Moved: Seems more suitable in the Programming forum.

aluser 09-28-2004 02:58 PM

sleep() takes its time in seconds, and rand returns a number between 0 and RAND_MAX. On my system, RAND_MAX is over 200 million, so your program would sleep, on average, around 3 years at the first "THINKING" comment. That sounds wrong.

To kill a process, see the kill() syscall's man page.


All times are GMT -5. The time now is 06:19 PM.