CSAPP notes (Chapter XII concurrent programming 01)

p716 ~ p757, twice, 716 ~ 733, 733 ~ 757.

Summary

This chapter want to address network programming in the famous select mode, epoll mode.

Outline

Concurrent application-level role

  1. And slow I / O device. When the application waits for the slow I / O device to write data to memory, the CPU can perform other tasks to pay out, to maintain CPU usage.
  2. Human interaction. For example one can listen to songs while playing the game, you can also receive messages QQ ~
  3. By delaying work in order to reduce latency.
  4. Service multiple network clients
  5. On multicore parallel computing machine

Three kinds of concurrency

  1. Complicated process
  2. I / O multiplexer
  3. Thread concurrency

Complicated process

Use fork, exec, waitpid method, the client receives a connection request in the parent process, and then create a new child process to service the client. (Such as the barber shop, the customer reception, and then assign a free haircut new customers division.)

Advantages: clear and isolation, will not affect each other.
Cons: too much trouble to share information and communicate more cost resources, stand-alone reach very high concurrency.

I / O multiplexer

Select core functions, all of the descriptors in a set fd_set, the call to select the function, the kernel to perform a query operation on the query results fd_set, and then one by one to check the status of these descriptors, if opened (state 1 ), then it is a read operation.

#include <sys/select.h>

int select(int n, fd_set *fdset, NULL, NULL, NULL);

FD_ZERO(fd_set *fdset); //置空fdset
FD_CLR(int fd, fd_set *fdset); //置空fd所在的位
FD_SET(int fd, fd_set *fdset); //设置fd所在位为1
FD_ISSET(int fd, fd_set *fdset); //检查fd所在位是否为1

More on I / O multiplexing, you can reference this blog, blocking I / O, non-blocking I / O and I / O multiplexing

Thread concurrency

Each thread has its own thread context (thread context), including a unique integer thread ID (Thread ID), the stack, stack pointer, program counter, general purpose registers, and condition codes. All running threads within a process share the the entire virtual address space of the process.

Multi-threaded and multi-process is similar, there are several different points

  1. Thread context smaller, more lightweight switch
  2. The first thread is the main thread running between all the threads are peers, a thread can kill any peer threads, or wait for the peer thread to terminate.
  3. Easy to share data between threads

Guess you like

Origin www.cnblogs.com/winwink/p/CSAPP_Note_Chapter12_Concurrency_01.html