Using Multi-threading and/or Multi-process

This is a discussion on Using Multi-threading and/or Multi-process within the C++ Programming forums, part of the General Programming Boards category; Hi, Today I read some code which creates multiple threads. For each of them, a child process is created to ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    Using Multi-threading and/or Multi-process

    Hi,
    Today I read some code which creates multiple threads. For each of them, a child process is created to call a stand-alone executable with communication in input to and output from it. I asked myself why not simple using multi-process without multi-thread.

    So here I would like to know in principle how to choose between multiple threading and multiple processing and when to combine them together with multi-process in each thread or multi-thread in each process, i.e. in what scenarios which way is better in terms of speed and difficulty in communication and implementation, etc?

    If I have multiple tasks on a multiple core/cpu server, which way is better(faster): call them as multi-process in a code file or open several terminals each of which calls the executable of a task?

    Thanks and regards!
    Last edited by lehe; 07-14-2009 at 08:56 AM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Today I read some code which creates multiple threads. For each of them, a child process is created to call a stand-alone executable with communication in input to and output from it. I asked myself why not simple using multi-process without multi-thread.
    My best guess is that the threads act as the interface between the main thread and the sub-processes. The "process threads" do I/O with the sub-process, and communicate with the main thread in some way internal to the program.

    The main difference between processes and threads is that threads within the same process share the same address space. Different processes of course don't. That means, creating processes are much more expensive than creating threads (not sure if that's still true with systems implementing copy-on-write). Also, inter-process communication (IPC) is more difficult. For threads, you would just have both threads read/write the same places in memory (with proper protections, of course).

    EDIT: and for when to use what, use processes if the tasks are relatively independent from each other, and communication between processes is not needed much (since IPC is usually expensive) and/or you want each task to be better protected (one process crash won't take down other processes, a thread crashing will take down other threads in the same process). Use threads if you need speed (creating and destroying a lot of threads) and/or a lot of data sharing and/or to save memory (each process has their own addressing space and file descriptors and other things. Especially important if you are creating many processes).
    Last edited by cyberfish; 07-14-2009 at 10:36 AM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks!
    A few questions on multi-process:
    If I have several tasks(executables) and I write a program, where I create a sub-process to execute each task without waiting it to finish, will the runing of several tasks be parallel on a server with multiple core/cpus by the OS automatically? If not, how to do it in my code?
    Is this way a fast way to execute several independent tasks? Is it the same as open a terminal for each task to execute it?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Yes, they will run parallel.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,258
    That means, creating processes are much more expensive than creating threads
    This is dependent on the OS and the threading implementation. On Linux for instance, threads are implemented as processes. In other words, there is little difference between calling pthread_create(), and fork(). They both just call clone() internally with different parameters.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Although, threads are usually seen as different parts of a program. You can easily suspend and resume threads, for example.
    Aside from that, you may also take into account that if one thread crashes, it brings down the entire process, whereas if you make them all processes, they can't bring down each other (but you may need to use different techniques to communicate between them).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 11:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 02:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM
  5. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 02:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21