Solved

Fork a child process???Howto?

Posted on 2002-03-05
7
320 Views
Last Modified: 2009-07-29
Experts,

I have written a module to do socket programming. The module resides in UNIX server. It will listen to a port and response to client request. Basically, I have completed the basic functions.

The server process will accept input from client (NT) and process the data accordingly and then write back to the client the end result.

So, I need to create some sort like a server daemon ...

First, the server process will be started. Any request from client will be served by a child process spawned from the parent process. The child process will send the end result to client upon finishing the data (input from client) processing and exit. Meanwhile the server will listen to the next request.

Pls refer any useful links / examples in order for me to achive this.

Thanks.
0
Comment
Question by:qiang8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 

Author Comment

by:qiang8
ID: 6846551
My main question is how to create a daemon that is able to serve the clients' request ... Thanks.
0
 

Author Comment

by:qiang8
ID: 6846736
Can I use popen to carry out something after fork process??? All examples that I have seen are using execlp or execvp ... Pls advise and thanks.
0
 
LVL 1

Expert Comment

by:pzpn
ID: 6860196
You need to fork() your parent process. After this is called, the newly forked process will be an exact copy of the parent process. You can therefore use the data to service the request. It is necessary after the fork to check whether you are in the parent, in which case you go on to listen for more requests, or whether you are in the child, in which case you service the request and _exit.

To find out whether you are in the child or the parent, check the return value of fork(), i.e.

int iReturnCode = fork();

if(-1 == iReturnCode)
{
     //The process has failed to fork....
}
else if(0 == iReturnCode)
{
     //We are in the child process
     //Service the request....
}
else
{
     //We are in the parent and iReturnCode is the
     //process id of the child...
     //Continue listening for new requests....
}

Hope this helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:pzpn
ID: 6860549
Yes. You only need to call exec (whatever variant) if you want to start a totally new executable. e.g. if you have one executable listening for a request, call it WaitForRequest and another that services requests, call it ServiceRequest, then you have to call exec to start Service request. Otherwise, if it is all done in the same executable, but you check to see which part of the code will be executed next by checking whether you are in the parent or child, then exec does not have to be called.
0
 

Author Comment

by:qiang8
ID: 6863040
I have tried the fork process with popen (I save the output to a buffer, pointed to by a file pointer) and successfully. So, my main concern is how to create daemon which will be started automatically upon activation. Any examples on the internet is appreciated. Any example on exec is welcome too.

Sorry for not making the question clear at the beginning. Thanks.
0
 
LVL 1

Accepted Solution

by:
pzpn earned 200 total points
ID: 6863844
The best way to get your main server process to start automatically is to use inetd. With this you can set the internet daemon to listen on a specific port and when a message is sent to that port inetd will start up a pre-defined application (in this case your main server app).

To do this you will have to modify a couple of files. First, you will need to modify the inetd.conf file. This is usually /etc/inetd.conf but if it is not there you can use the UNIX whereis command to find it. In this file you need to add service name, socket type, protoco, {wait|nowait}[/max-child[/max-connections-per-ip-per-minute]], user[:group][/login-class], server program, server program arguments. Just follow the format of the other entries in this file.

You must also edit the /etc/services to add the name of your service.

For more information look up inetd in the UNIX man pages or, if you haven't got then, which I haven't :(, follow this link:
 http://www.freebsd.org/cgi/man.cgi?query=inetd&sektion=8

Hopefully this will help.

Cheers,
Marek.

0
 
LVL 11

Expert Comment

by:griessh
ID: 6955270
Dear qiang8

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "pxpn"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 160
Why isn't object file created? 6 108
parameter pack in c++11 2 23
cmake and message 1 23
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question