Concurrency and Parallelism

0 Concurrency and Parallelism

When a CPU executes a thread, the other CPU can execute another thread, two threads and do not seize the CPU resources, may be performed simultaneously, we call such a manner parallel (Parallel). Difference: concurrency and parallelism are two similar but distinct concept i.e., parallel refers to two or more events occur at the same time; and refers to two or more concurrent events occurring at the same time interval.

1. Network Programming

1.1 TCP / IP network programming

Client:

  • socket
  • connect
  • write
  • recv

Server:

  • socket
  • bind
  • listen
  • accept
  • read
  • send

An example: Simple TCP/IP C/S.

a. socket

#include <sys/socket.h>
sockfd = socket(int socket_family, int socket_type, int protocol);

b. connect

#include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

c. bind

#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

d. listen

#include <sys/types.h>
#include <sys/socket.h>
int listen(int sockfd, int backlog);

Notes: The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.

e. accept

#include <sys/types.h>
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

2. Multi-threaded programming

  • pthread_create
  • pthread_exit
  • pthread_join
  • pthread_detach

2.1 pthread_create

create a new thread.

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine) (void *), void *arg);

Notes:

  • The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine().
  • Compile and link with -pthread.
  • On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.
  • After creating a thread, we also need to consider a problem: how to deal with the end of this thread? One way is to wait for the end of the thread, the calling thread instance in a suitable place to join () method, the caller's thread will always be waiting for the end of the target thread, when the caller's thread continues to run after the target thread; another way this thread is separated by its own ends, calling thread instance through detach () method to target thread placed in separate mode. After a thread after the join () method to detach () method can only be called once, can not be called join () in turn calls detach (), nor in calling detach () in turn calls join (), calling the join ( after) or the detach (), i.e. the thread id set to a default value (empty thread), indicates that thread can not continue further modification variations [2].

2.2 pthread_exit

terminate calling thread

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

Notes:

  • Compile and link with -pthread.
  • terminates the calling thread and returns a value via retval that (if the thread is joinable) is available to another thread in the same process that calls pthread_join.

2.3 pthread_join

join with a terminated thread

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

Notes:

  • waits for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately. The thread specified by thread must be joinable.
  • If retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the target thread supplied to pthread_exit(3)) into the location pointed to by retval. If the target thread was canceled, then PTHREAD_CANCELED is placed in the location pointed to by retval.

2.4 pthread_detach

detach a thread

#include <pthread.h>
int pthread_detach(pthread_t thread);

Notes:

  • marks the thread identified by thread as detached. When a detached thread terminates, its resources are automatically released back to the system without the need for another thread to join with the terminated thread.

3. Asynchronous Programming

4. IO multiplexing

Compared with multi-process and multi-threading technology, I / O multiplexing technology is the biggest advantage of small system overhead, the system does not have to create a process / thread, there is no need to maintain these processes / threads, thus greatly reducing the cost of the system.

4.1 synchronous IO

a. select

#include <sys/select.h>
#include <sys/time.h>

int select(int maxfdp1,fd_set *readset,fd_set *writeset,fd_set *exceptset,const struct timeval *timeout)
返回值:就绪描述符的数目,超时返回0,出错返回-1

select several major disadvantages:

(1) each call to select, from the set of fd need to copy the user mode to kernel mode, the overhead will be large when many fd (2) concurrently with the need to traverse all fd SELECT passed in the call to the kernel, the many overhead also great number of file descriptors fd (3) select support is too small, the default is 1024

b. poll

# include <poll.h>
int poll ( struct pollfd * fds, unsigned int nfds, int timeout);

Poll and select a similar mechanism, not much different in nature to select, manage a plurality of descriptors is polling, processing descriptor in accordance with the state, but there is no maximum number of file descriptors poll limit. Different ways to describe a collection of fd, poll pollfd structure rather than using select fd_set structure, others are similar. poll and select a drawback is also present, contains a large number of file descriptors between the array is copied in its entirety to the user mode and kernel address space, regardless of the file descriptor is ready to increase its spending with the number of file descriptors increases linearly.

c. epoll

#include <sys/epoll.h>
int epoll_create(int size);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);

epoll is presented in the 2.6 kernel, is an enhanced version of the previous select and poll. With respect to the select and poll for, the epoll more flexible, without limiting descriptor. epoll file descriptor management using a plurality of descriptors, file descriptors stored in the user relationship event an event table in the kernel, so that only one copy in the user space and kernel space.

d. Compare

(1) select, poll achieve continuously polls all need their own fd set until the device is ready, you may want to sleep and wake up several times during the alternate. And in fact, need to call epoll epoll_wait constantly polling the ready list, it may alternately repeatedly during sleep and wake, but it is when the device is ready, call the callback function, fd ready to put in the ready list, goes to sleep and wakes up in the epoll_wait process. Although every sleep and alternately, but select and poll in the "awake" time to traverse the entire collection fd, and epoll in the "awake" when just determine what the ready list is empty on the line, which saves a lot of CPU time. This is caused by a callback mechanism to enhance performance.

(2) select, poll each call must be set to fd from user mode to kernel mode Copy Once, current to the device and make a queue waiting to hang, and epoll just one copy, but the current queue waiting to be hung hanging only once (at the beginning of epoll_wait, Note that the device is not waiting queue waiting queue, but inside a defined waiting queue epoll). This can save a lot of overhead.

refers to [3].

4.2 Asynchronous IO

References

[1] man, man7.org.

[2] using C ++ 11 write Linux multi-threaded programs , ibm.com.

[3] the difference between the select, poll, epoll summary , cnblogs.com.

Original: Big Box  Concurrency and Parallelism


Guess you like

Origin www.cnblogs.com/petewell/p/11584807.html