Multi-process programming in Linux

Source: Internet
Author: User
Multi-process programming in Linux-general Linux technology-Linux programming and kernel information. For details, see the following. Abstr:
The concept of multi-threaded programming was proposed as early as 1960s, but it was not until the middle of 1980s that the multi-threaded mechanism was introduced in Unix systems. Nowadays, due to its many advantages, multi-threaded programming has been widely used. This article will introduce some preliminary knowledge about writing multi-process and multi-threaded programs in Linux.


--------------------------------------------------------------------------------

Body:
Multi-process programming in Linux

1 Introduction
For those who have never been familiar with Unix/Linux operating systems, fork is one of the most difficult concepts to understand: it executes once but returns two values. The fork function is one of the most outstanding achievements of Unix systems. It was one of the achievements made by developers in the early 1970s s after a long and painstaking theoretical and practical exploration, it minimizes the cost of process management by the operating system. On the other hand, it provides programmers with a simple and clear multi-process method. Unlike DOS and earlier Windows systems, Unix/Linux systems are truly multi-task operating systems. It can be said that programming in a Linux environment cannot be considered true without multi-process programming.
The concept of multi-threaded programming was proposed as early as 1960s, but it was not until the middle of 1980s that the multi-threaded mechanism was introduced in Unix systems. Nowadays, due to its many advantages, multi-threaded programming has been widely used.
Next, we will introduce some preliminary knowledge about writing multi-process and multi-threaded programs in Linux.

2 multi-process Programming
What is a process? The concept of process is for the system rather than for the user. For the user, the concept of process is for the program. When you press a command to execute a program, the system starts a process. However, unlike programs, in this process, the system may need to start one or more processes to complete multiple independent tasks. The main content of multi-process programming includes Process Control and inter-process communication. Before learning about this, we must first know the structure of the process.

2.1 Structure of processes in Linux
The next process in Linux has three parts of data in the memory: "code segment", "Stack segment", and "data segment ". In fact, people who have learned assembly language know that the general CPU has the above three register segments to facilitate the operation of the operating system. These three parts are also necessary to form a complete execution sequence.
"Code segment", as its name implies, stores the data of program code. If several processes on the machine run the same program, they can use the same code segment. The "Stack segment" stores the return address of the subroutine, the parameters of the subroutine, and the local variables of the program. The data segment stores the global variables, constants, and dynamic data space allocated by the Program (for example, space obtained using functions such as malloc ). There are many details here, so we will not discuss them much here. If the system runs several identical programs at the same time, the same stack segment and data segment cannot be used between them.

2.2 Process Control in Linux
In the traditional Unix environment, there are two basic operations used to create and modify processes: the fork () function is used to create a new process, which is almost a full copy of the current process; the exec () function family is used to start another process to replace the currently running process. The Process Control in Linux is basically the same as that in traditional Unix processes, but there are some differences in some details. For example, in Linux systems, calling vfork and fork is exactly the same, in some versions of Unix systems, vfork calls have different functions. Since these differences hardly affect most of our programming, we will not consider them here.
2.2.1 fork ()
Fork means "fork" in English. Why is this name used? Because a process is running, if fork is used, another process is generated, so the process is "Forked", so this name is very good. The following describes how to use fork. This program demonstrates the basic framework of using fork:
QUOTE:

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.