Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Technique to terminate the parent program 1

Status
Not open for further replies.

cdlvj

MIS
Nov 18, 2003
678
US
I have a server program (sockets) listening on a port, the clients are executed, do the connection, and the server kicks of a child process to do communications. Just as telnet.

All this is working great, at the end, client sends a command, the child terms etc.

What would be a technique to cause the parent server program to shutdown gracefully. ie, how can the child return something for the server to stop.
 
It's your application level protocol issue. Invent stop command then send it to the server, let it stops...
 
It's your application level protocol issue. Invent stop command then send it to the server, let it stops...

The client is communicating with the child, I do have a stop command, which terminates the child process ok. But I would like to pass the exit of the child to the parent.

With another execution of client, the parent responds and forks another child which is how it should work. The issue is how do you notify the parent that it is time to shut down.
 
Looking at the parent closer, there is a signal, and when each child terminates, that code is being executed.

By using wait, I get the pid of the child that quit, but how do I get the return or exit code of the child?
 
Use shared memory variables to send info back and forth between the parent and child. When the child finishes it can set a bool in the shared memory to true or false (or whatever return type you need)...
 
Pass it in, according to sys/hait.h the function wait:
pid_t wait(int *);

The passed int pointer is status (exit code). So you can
Code:
int* status = new int(0);
int pid = wait(status);
if(status == 0)
{
  //everything is fine
}else{
  // bad stuff happened
}

[plug=shameless]
[/plug]
 
I thought that is was a simple matter, although I exit the child with a code of 9, the parent shows 25444.

This is AIX. But the one thing that is happening is that only the 1st child terminate is going through the interrupt. Does the signal need to be reset each time?
 
You can't return an exit code from a forked child. You get the process id (PID) returned immediately when it starts.


The 25444 you see is almost certainly the PID of the child process.



Trojan.
 
The status has to be run thru the WEXITSTATUS macro, and now I am getting the correct number.

But still have not figured out why signal just is done one time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top