Linux system programming @ multithreaded Programming

Source: Internet
Author: User

Multithreaded programming

Operating system principle Concept
Time slices
Process status

Context: For a process, it is the execution environment of the process, specifically the variables and data, including all the register variables, open files, memory information and so on.

Write-time replication of the process: since the General Fork is followed by exec, the fork is now used to copy the technology, as implies, that is, the data segment, heap, stack, the beginning is not copied, by the parent, the child process is shared, and the memory is set to read-only. The kernel is not a copy of the memory that needs to be modified until the parent, the child process, attempts to write these areas. Doing so can improve the efficiency of the fork.

The reentrant nature of the thread function: the so-called "re-entry", the common case is that the program executes to a function foo (), received a signal, so pause the currently executing function, go to the signal processing function, and this signal processing function in the execution of the process, but also into the function just executed Foo (), so the so-called re-entry occurs. If foo () is able to run correctly, and when the processing is done, the previously paused foo () will be able to run correctly, which means it is reentrant.

To ensure that the function can be re-entered, you need to meet a few conditions:

1. Do not use static or global data inside the function
2. Do not return static or global data, all data is provided by the caller of the function.
3. Use local data, or protect global data by making local copies of global data.
4, do not call the non-reentrant function.

********************************************************************

Thread-to-process comparison

A process is a program with a certain independent function on a data set on a running activity, is the system for resource allocation and scheduling of an independent unit.

A thread is the smallest unit of execution in a process, is an entity in a process, is the basic unit of dispatch and dispatch by the system, the thread does not own the system resources, and only has the resources necessary to run it, but it can share all the resources owned by the process with other threads belonging to one process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process.

Relationship of process and thread:

(1) A thread can belong to only one process, while a process may have multiple threads, but at least one thread.

(2) A resource is allocated to a process, and all the threads of the same process share all the resources of that process.  

(3) The processor is assigned to a thread, that is, a thread that is actually running on the processing machine.

(4) Threads need to be synchronized during execution. Synchronization is achieved between threads of different processes using the means of message communication. A thread is an execution unit within a process and a scheduler within a process.

The difference between a process and a thread:

(1) Dispatch: The thread acts as the basic unit of dispatch and allocation, and the process as the basic unit of resources

(2) Concurrency: Not only can concurrent execution between processes, but also concurrent execution between multiple threads of the same process

(3) Owning a resource: a process is an independent unit that owns resources, and threads do not own system resources, but they can access resources that belong to the process.

(4) Overhead: When you create or revoke a process, the overhead of the system is significantly greater than the cost of creating or revoking a thread, because the system allocates and reclaims resources for it.

Line threads benefits for the process :

(Dispatch, Allocation: less time to generate deletes
Switching overhead between processes/threads (suspending a running process or thread, recovering a previously suspended process or thread): faster thread switching without recovering user address space
Communication mechanism: Communication is more efficient (shared address space, do not need to call the kernel to pass information)

Concurrency of
The principle of coding: the complexity of the creation process, and the control of the program.

(1) easy to dispatch.

(2) Improve concurrency. Concurrency can be easily and efficiently achieved through threading. A process can create multiple threads to perform different parts of the same program.

(3) less overhead. Creating a line turndown creates a process that is fast and requires little overhead.

(4) Facilitate the full use of multi-processor functions. By creating multithreaded processes, each thread runs on a single processor, which enables the concurrency of the application so that each processor is fully operational.

Process-to-thread state comparison

Process status

Status: Running, blocking, pending blocking, ready, pending

Transitions between states:

The ready process is executed by the CPU and becomes the running state;

A running process, making an I/O request or not getting the requested resource, into a blocking state;

Running process, process execution (or time slice has arrived), become ready state;

The blocked process hangs, becomes a suspended blocking state, when the I/O operation that causes the process to block is completed before the user restarts the process (called Wake-up), the suspend blocking state becomes the suspend-ready state, and when the user restarts the process before the I/O operation is completed, the suspend blocking state becomes the blocking state;

Suspends a process in readiness (or running) into a pending ready state, and when the process resumes, the pending ready state becomes the ready state;

Thread state

Differences between synchronization and mutex:

When there are multiple threads, it is often necessary to synchronize these threads to access the same data or resources. For example, suppose you have a program where one thread is used to read a file to memory, while another thread is used to count the number of characters in the file. Of course, it is meaningless to count the entire file before it is transferred into memory. However, because each operation has its own thread, the operating system executes each of the two threads as an unrelated task, which may count the number of words when the entire file is not loaded into memory. To work around this problem, you must synchronize two threads.

The so-called synchronization, refers to the walk in a number of different processes between the pieces of the program, their operation must be in strict accordance with the specified sequence of operation, this order depends on the specific task to be completed. If defined with access to resources, synchronization refers to the mutual exclusion (in most cases), through other mechanisms to achieve the visitor's orderly access to resources. In most cases, synchronization has been mutually exclusive, especially if all writes to the resource must be mutually exclusive. In rare cases, multiple visitors can be allowed to access resources at the same time.

The so-called mutex refers to a number of program fragments scattered between different processes, when a process runs one of the program fragments, other processes will not be able to run any of their program fragments, can only wait until the process runs out of this program fragment to run. If defined with access to a resource, it is unique and exclusive for a resource to allow only one visitor to access it at the same time. However, mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered.

********************************************************************

operation of the thread (based on the diagram of the life cycle)
Creation of Threads

#include <pthread.h>int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) ( void *), void *arg);//compile and link with-pthread
thread: Pointer to thread identifier attr: Setting the properties of a thread start_routine: Start address of thread run program Arg: Run the program's parameters

void* ARG: Cannot transfer large amounts of data through this parameter

Workaround:

Define a structure for each thread that includes all the data required for the same thread function (easy to implement multithreaded multiplexing of the same threading function);

Waits for the end of the specified thread

#include <pthread.h>int pthread_join (pthread_t thread, void **retval);

When a process is created, a main thread is created
The main thread holds process information, the parent-child relationship does not appear between the main thread and the newly created process
When the function returns, the resources of the thread are recycled (different from the process)

A thread cannot be waited on by multiple threads: If so, the first line that receives the signal Cheng back, the other error esrch;

Note Thread waits
End of Thread

#include <pthread.h>void pthread_exit (void *retval);

atomic manipulation of threads (e.g., operation of the bank's money-fetching system)
No atomic operations in the process
Asynchronous can be deleted (default is asynchronous and can be deleted at thread initialization)
Sync to delete

GCC compilation for a program that contains threads
Adding-pthread to the compilation parameters

Thread ID and Process ID

The process ID is portable and is the unsigned int type.The thread ID is a non-portable type, that is, in this system, it may be the unsigned int type, in other systems may be long,double, or even a struct. So, for the sake of accuracy, when printed, it is expressed in hexadecimal, that is:printf ("ThreadId is%x \ n", Threadid[i]), as our system obtains, pthread_t is a struct, we can think of this value directly as the hexadecimal address value.

Thread Application Instance
Web server
Single thread server (only one main thread)


Multi-threaded server (one main thread generates multiple worker threads)


A main thread corresponds to a port, so the program generally only occupies one main thread.
Thread pool Server (a main thread creates a thread pool that pulls threads from the thread pools to handle user requests)

The dynamic nature of the process
The life cycle of processes and signals


The time slice of the thread
Classroom Exercises:
The new thread shares the memory with the main thread, and the main thread waits for the i=0 state to be stopped, and four new threads are created.
The main thread may also remain in the i=0 state. Then a new thread prints out four 0.
WORKAROUND: Set the new thread to not share memory, each with a separate memory space.

The use of mutexes in multithreaded programming

Linux system programming @ multithreaded Programming

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.