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 ?