CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2001
    Posts
    2,529

    winsock32 lack of fork command

    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...

    any ideas?

    ahoodin

  2. #2
    Join Date
    Oct 2000
    Location
    India
    Posts
    489
    Hi,
    An alternative to the fork is to create a copy of the same process
    with the 'bInheritHandles' to true.

    As for starting the same exe you could get the path
    to the current exe by using the foll. fn and passing
    hModule as NULL.

    DWORD GetModuleFileName(
    HMODULE hModule, // handle to module
    LPTSTR lpFilename, // file name of module
    DWORD nSize // size of buffer
    );

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529
    Does the UNIX fork command preserve the original arguments to the .exe?

    ahoodin

  4. #4
    Join Date
    May 2002
    Posts
    6
    Hi

    I made a winsock program on Windows as a assignment of my class.

    If you want to see that please give me an e-mail then I'll give you the program.
    I tried to upload but the size is too big.

    I uploaded just ppt file which is explain about difference between UNIX , Windows Socket.

    I hope that It will help you.
    Attached Files Attached Files

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529
    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.

    ahoodin

  6. #6
    Join Date
    Mar 2001
    Posts
    2,529
    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!

    ahoodin

  7. #7
    Join Date
    Apr 2002
    Location
    Switzerland
    Posts
    31
    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() ?!?

    Thanks
    Flavio

  8. #8
    Join Date
    Jun 2002
    Posts
    14
    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?

  9. #9
    Join Date
    Jun 2002
    Location
    Sweden
    Posts
    81
    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).

  10. #10
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310
    fork() & UNIX??????

    Forget them, they are behind the times ( I would love to argue with any UNIX buffs here )

    Use threads, you are coding for windows use the power over the crummy UNIX OS, and those UNIX nerds!!!!

    Regards

    Shaun

    (Anti-UNIX developer!!!!!!!)

  11. #11
    Join Date
    Jun 2002
    Location
    Sweden
    Posts
    81
    (Anti-UNIX developer!!!!!!!)
    What does this mean? You develop software against UNIX??? LOL!

  12. #12
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Originally posted by Filbert Fox
    fork() & UNIX??????

    Forget them, they are behind the times ( I would love to argue with any UNIX buffs here )

    Use threads, you are coding for windows use the power over the crummy UNIX OS, and those UNIX nerds!!!!

    Regards

    Shaun

    (Anti-UNIX developer!!!!!!!)
    My words... :-) Unix is dead!!! :-))

  13. #13
    Join Date
    Apr 2002
    Location
    United Kingdom
    Posts
    310
    Originally posted by amag

    What does this mean? You develop software against UNIX??? LOL!
    If you like!!! UNIX is dead and those developers who still cannot see this!!!!

  14. #14
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured