CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Fork Vs Thread

  1. #1
    Join Date
    Aug 2005
    Posts
    98

    Fork Vs Thread

    Hello firends!

    Is it right compare between Thread and Fork:
    Someone told me this in this subject:

    1. fork()
    Note that this only works on UNIX-like systems (like Linux). fork() is a library function which, when called, copies the entire process (including all variables) into a second process. Anything after the fork() call is therefore executed twice. fork() returns 0 to new process (the child) and the PID of the child to the calling process (the parent). Note that this is different from threads, because two seperate processes are run. This approach is sometimes used in web servers, so that a new process ifs forked for every client. This is easy to program, but inefficient, as you've got a new process for every client. Interaction between two of those processes is really hard (you'd have to use an intermediate file or something).

    2. Threads
    As far as I know, the Windows API supports threads instead of fork. When creating a thread in WinAPI, you call the CreateThread function (I'm not certain, I don't know too much about it), and you pass it a function pointer to the function you want the thread to execute


    May you compare thread and fork?It means in Linux we don't use thread?

  2. #2
    Join Date
    Jul 2006
    Location
    Buenos Aires, Argentina
    Posts
    103

    Re: Fork Vs Thread

    In Linux you use threads, see POXIS Thread for more information.

  3. #3
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Fork Vs Thread

    Thread is more effective in memory management because it uses the same
    memory block of parent process instead of creating new memory block to
    run new process. But in *NIX environment, it is not as easy as MS-Win
    environment to use thread.

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Fork Vs Thread

    But in *NIX environment, it is not as easy as MS-Win
    environment to use thread.
    Actually only in non-Posix compliant implementations of *NIX, which (thankfully) are rather rare these days....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Fork Vs Thread

    well correct me if i am wrong, unix systems always favor processes than threads maybe becaus e of the fact that creating/forking a process is very fast in unix.. i once created a PHP script server (somekind of com+) that manages requests from the web, i ended up forking processes because i could not find any support on threads. i've took advantage of of using the System V IPC to communicate between processes. so far it is working perfectly
    Busy

  6. #6
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: Fork Vs Thread

    Well, on unix systems the effectiveness of threads vs. processes is about the same (in contrast to windows where processes are pretty expensive), In the end it boils down to what you need to do - threads share the same memory, but it mean locking management, making functions reentrant etc.

  7. #7
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Fork Vs Thread

    threads are definitly faster than running a second proccess no doubts.
    its not really hard to make threads, forking is alot harder.
    for those wich have difficult threads, dont bother...?

  8. #8
    Join Date
    Feb 2007
    Posts
    1

    Re: Fork Vs Thread

    Hi For a C code in Linux i want to reset the stack size of the main() that is only one thread is there in the process. Any pointers on that.

    For any function do we have to use that function inside the main i.e we are resetting the stacksize of that thread or process inside that process or thread?

    a snippet
    main ()


    main ( int argc, char **argv )
    {
    int rc = 0;
    pthread_attr_t threadAttr;


    pthread_attr_init( &threadAttr );

    pthread_attr_setstacksize( &threadAttr, 3584);
    }
    would it work in setting the stacksize of main to 3584 kbytes

  9. #9
    Join Date
    Jan 2010
    Posts
    1

    Re: Fork Vs Thread


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