What does exactly does fork command do? It is not just like create process. If you write a c program that uses fork, does the child process created by the fork have access to all the arguments of the main() function. ie argc and argv?
I looked at the MSDN but it was kind of unclear here. I want to port some UNIX code to Windows...but it uses fork...
Uh...
I have already code for the socket listener and the client. I have a single process listener and a client. I want to port a good multi-threaded socket server from UNIX sockets to WinSock32. The UNIX code makes extensive use of the fork() command and I want to duplicate the functionality. UNIX man pages are obtuse so I will figure it out at home where I have a LINUX box.
Thank you for taking the time to dig that up for me kyle. The PPT presentation was basic and encompasses socket basics. It does not however answer my questions....so again I must thank you for your ingenuity and forward thinking. You may want to monitor this thread to see what the answer is!
I try to do exactly what discribed in the ppt-file on side 1:
The client sends a data[request] and the server replys with a data[reply]. But it doesnt work.
My client announce a recveive-error, when he should receive the data[reply] !
Does the recv()-command has a timer or something else?
How does send() and recv() really co-ordinate ? I thought it is just necessary to have a connection and then I could what I want with recv() and send() ?!?
Originally posted by ahoodin Does the UNIX fork command preserve the original arguments to the .exe?
I seem to recall that that is the case, yes. The new process is (I think, this is all from memory!) a duplicate of the other/parent process. Code execution starts at the next instruction following the "fork", for example. So if you can get at the args in the first case you should be able to get them for the second, I would have thought. How does the code behave on Unix, because if you are trying to port something then that is your benchmark?
What fork() does is to copy the whole address-space to a new address-space, so a fork()-ed process has access to argv and so on (if they are in scope).
Example:
int main(int argc, char **argv)
{
if(fork() == 0) // ok this is the child-process
{
printf("My name: %s\n", argv[0]);
}
}
There are different versions of fork() that also copies open file-handles and stuff.
However fork() may not be the best because it creates a separate address-space. If the two processes needs to interact in any way I suggest you use threads instead. Posix-threads are supported both by WinNT/2k(/XP?) and by some (most?) *NIX-systems (Solaris for sure).
Fork command starts another process just like CreateProcess does. The difference is that forked processes start after the fork command and CreateProcess start at the beginning.
if ( fork( foo ) == 0 )
{
printf( "I'm the Parent\n");
}
else
{
printf( "I'm the Child\n");
}
printf( "Who am I now?\n");
The answer to "Who am I now" is both cause the execution path's merge. Fork uses the same mirror code set the only difference between the parent and child process is the returned value of Fork. So yes the command parameters are the same!!
If you wanted to use create process to simulate this you could. You would have to pass in the command string into the new process, then you would need to set a flag in that command string to have your programm's execution jump to where you wish it to go.
It won't be pretty or elegant like fork, but you could do it.
As for threads, Threads are lighter weight and more efficient both in memory usage and execution but they aren't as safe. If a thread blows up then your underlying program blows up. If a forked process goes the parrent process is uneffected. Generally fork though comes from a time before threads were invented. A distant time called the 1970's and early 80's.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.