Single-Process vs. multi-process relationships and differences (multi-process system Linux)

Source: Internet
Author: User

Single-process programming: Sequential execution of data synchronization complexity low-use single
Multi-process programming: Simultaneous execution of data asynchronous complexity high versatility

1.  The advantage of multi-process is that the independence of tasks, such as a task alone as a process, the crash only affects their own services, other tasks are not affected. If multiple tasks are handled within the same process using multiple threads, a thread that has an unhandled exception can cause the entire process to finish , and all the tasks were to suffer.
2.  in terms of resource allocation, multi-process programmes are more flexible and free than multithreaded programmes
3.  However, the multi-tasking process is more complicated than multithreading, and it is much more difficult to compile a good multi-process communication scheme than a multi-threaded communication scheme.
In the case of Web server, for example, I have three sites on my server, and if it is managed by a single process, site A is attacked and killed, which means that the other two sites will appear the same way. If the separate process is separate, three sites do not affect each other


single-Process vs multi-process Advantages :

1. Early implementation is relatively simple and fast, eliminating the work of inter-process communication

2. Simplicity makes deployment and operations easier

3. Memory is small, now the memory is very cheap, this advantage is not obvious

4. Process internal communication efficiency is more efficient than ipc/socket, etc.

Single process vs multi-process disadvantage : (citation: http://blog.csdn.net/blade2001/article/details/6790890)

1. The single process becomes bloated and difficult to maintain as business logic complicates and needs increase in the middle and later stages. Splitting a task into multiple processes makes the logic of a single process simple and error-prone

2. In-process modules are strongly dependent on each other and need to be compiled together with greater impact on each other. This is relative to the multi-process communication, the coupling degree is large, which is not conducive to multi-team parallel development. Multi-process facilitates multi-lingual collaborative development .

3. Any module crashes will cause the entire process to fail, the multi-process mode is more robust, the business process is isolated, and one crash will not affect the other

4. Performance issues: If the inter-process cascading is not supported, the single-process capacity is limited, and this performance bottleneck is especially important in support of group-class services. The multi-process deployment is extremely flexible, with the ability to expand the number of machines to improve system processing performance and to avoid single points of failure from the hardware.

5. Multi-Threading in single process difficult to debug

If you need high-performance processing, as long as the actual design is reasonable, multi-process and multi-threading is feasible.

How to choose depends on the business requirements, stability is key in high-end applications, efficiency and functionality can only be ranked second, such as the financial system. Even heavy-duty programs can be divided into many different types: compute-intensive, I/o-intensive, communication-intensive?

In response to frequent crashes in browsers, many browser vendors now use the concept of multi-process tabbed browsing, including the main IE8, Chrome, and Firefox browsers. Many WebKit-based browsers will be able to use multi-process tagging after Apple has released the WebKit2 kernel.

Sogou Browser is a multi-process browser,

After adding multi-process browsing in the browser, the other processes will not be affected, even if one of the browsers is crashing. For example, a Web site with vulnerabilities or malicious code, Trojan Horse and other network attacks, it is possible to destroy the current running on the site of the label, but will not affect other processes or the entire browser. Multi-process is the trend of browser development, Microsoft Windows system comes with IE will not have a single process
What problems should be noticed in multi-process and multi-threaded programming in Linux?
Watch the sync. Don't be a dead lock.
Linux Multi-process programming example
Create two sub-processes; one child process (producer process) writes an integer 0,1,2,..., 9 to the buffer, and another child process (consumer process) pauses 5s, reads one from the buffer, and deletes the readout from the buffer, then displays the number The parent process waits for the exit information of child process 2 (consumer process), and the parent process returns after the information is collected.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (void)
{
int i = 0;
int *ncount;
int pfd[2];
Char szbuff[11] = {0};
int status;
pid_t pid;

Pipe (PFD);

if (fork () = = 0)
{
ncount = malloc (sizeof (int) * 10);
for (i = 0; i <; ++i)
{
Ncount[i] = i;
sprintf (Szbuff, "%s%d", Szbuff, Ncount[i]);
}

Write (pfd[1], Szbuff, strlen (Szbuff));
Free (ncount);
Close (pfd[0]);
Close (pfd[1]);
}
Else
{
if (fork () = = 0)
{
printf ("Sleep (5)!\n");
Sleep (5);
Read (Pfd[0], Szbuff, 10);
printf ("%s\n", Szbuff);
Close (pfd[0]);
Close (pfd[1]);
}
Else
{
PID = Wait (&status);
i = wexitstatus (status);
printf ("Child is%d, exit status =%d\n", PID, I);
PID = Wait (&status);
i = wexitstatus (status);
printf ("Child is%d, exit status =%d\n", PID, I);
}
}
Close (pfd[0]);
Close (pfd[1]);
return 0;
}

-------------------------------------------------------
Explain, because the fork out of the process has its own independent space, so two sub-process communication with pipe (pipeline), by a child process malloc out of space, to the child process 2, the child process 2 received after the return!

Single-Process vs. multi-process relationships and differences (multi-process system Linux)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.