29. in-depth understanding of computer system notes, concurrent programming (1)

Source: Internet
Author: User

1. If logical control flows overlap in time, they are concurrent. This phenomenon is called concurrency ( concurrency ).

2. To allow the server to serve a large number of clients at the same time, it is better to create a concurrent server and create separate logical streams for each client. ModernOSThe following methods are commonly used to construct concurrency:

Process and thread.

1) Each logical flow is a process and is scheduled and maintained by the kernel. Each process has an independent virtual address space, and the control flow passes throughIPCMechanism for communication.

2) thread: the logical stream running in a single process context is scheduled by the kernel to share the virtual address space of the same process.

due to process control and IPC high overhead, so process-based design ratio Thread-based design is slow.

CommonIPCMPs queue,FIFO, Shared memory, signal.

3. Thread-Based Concurrent Programming

threads are automatically scheduled by the kernel, each thread has its own thread context ( thread context ), contains a unique integer thread id ( thread ID, tid ), stack, stack pointer, Program counter, general purpose register, and condition code. Each thread shares the remaining part of the process context with other threads, including the Virtual Address Space of the entire user, it consists of read-only text ( Code ), read / write data, heap, and all the shared library code and data areas, the thread also shares the same set of open files.

1) thread execution model

threads are not like processes, it is not organized according to the strict parent-child hierarchy. A peer thread pool ( A pool of peers ), threads created independently from other threads ( the threads associated with a process form a pool of peers, independent of which threads were created by which other Threads. in my personal understanding, this sentence is a thread independent from the thread pool of other processes ). The first running thread in the process is called the main thread. The main influence of the concept of peer-to-peer (thread) pool is that a thread can kill any of its peer-to-peer threads or wait for any of its peer-to-peer threads to terminate. Further, each peer thread can read and write the same shared data.

2) aboutPOSIXThread example and related functions, see the original book13.3.2Section. The examples in this section are classic and worth reading.

3) Separate threads

At any point in time, threads can be combined (Joinable), Or separated (Detached). A combined thread can be reclaimed and killed by other threads. Before being recycled by other threads, its memory resources (such as stacks) are not released. On the contrary, a separate thread cannot be recycled or killed by other threads, and its memory resources are automatically released by the system when it is terminated.

Sample program

/** Echoservert. c-a concurrent echo server using threads * // * $ begin echoservertmain */# include "csapp. H "Void echo (INT connfd); void * thread (void * vargp); int main (INT argc, char ** argv) {int listenfd, * connfdp, port, clientlen = sizeof (struct sockaddr_in); struct sockaddr_in clientaddr; pthread_t tid; If (argc! = 2) {fprintf (stderr, "Usage: % S <port> \ n", argv [0]); exit (0 );} port = atoi (argv [1]); listenfd = open_listenfd (port); While (1) {connfdp = malloc (sizeof (INT); * connfdp = accept (listenfd, (Sa *) & clientaddr, & clientlen); pthread_create (& tid, null, thread, connfdp) ;}/ * thread routine */void * thread (void * vargp) {int connfd = * (int *) vargp); pthread_detach (pthread_self (); free (vargp); echo (connfd); close (connfd); return NULL ;} /* $ end echoservertmain */

Related Article

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.