Linux Network Programming Learning notes four-----multi--threaded Server

Source: Internet
Author: User

for concurrency during use. By implementing a more lightweight thread.

Each thread is a separate logical stream.

The topic is the smallest independent unit in which the CPU executes the schedule, which is the resource allocation unit. Of course, this is said in the micro-kernel operating system. In short, this is the only one operating system kernel provides the most important OS service, many people watch click Open link


Each thread has its own thread context. Contains a unique thread ID (implemented on Linux as unsigned long), stack, and stack pointer. program counters, general purpose registers and condition codes, as well as their own signal masks and precedence. Threads in the same process share the entire virtual address space of the process, including the executable program text, the program's global memory, heap memory, stack, and file descriptor descriptors. As a result, the context switch of a thread is much faster than the process, but it also brings synchronization problems between threads at the same time.


Few tips:

1, the process ID is unique throughout the system, but the thread ID is only valid in the thread environment in which it belongs.

2, each thread has a copy of errno, regardless of its synchronization problem.

3, arbitrary a thread called the Exit, _exit, _exit will cause the entire process to terminate.

4. Because of the sharing of descriptive words, the newly created thread does not affect the reference count of the opened descriptive narrative word. So, don't just close.


Several properties of a thread

Separation and binding: If a thread can be withdrawn by another thread and killed, it can be combined, otherwise it is separate. By default. Threads are created to be associative. At this point, it is necessary for an existing thread to call Pthread_join () to block waiting for the process to end and reclaim its resources (since this function can only wait for a particular thread to cause the coding to be complicated, Steven has a lovely spat in the UNP for a while, arguing with the standard designer).

The disconnected thread does not need to wait for other threads to end. The resource is immediately reclaimed by the operating system and can be implemented when it is created by calling Pthread_detach ().


Many other attributes, such as inheritance. Scheduling policies, schedules, scopes, and stack sizes can be seen in APUE12.3

You can call Pthread_attr_init settings for the property variable pthread_attr_t, but remember to destroy it with pthread_attr_destory after creating the thread



Common methods for process synchronization

1) Mutual rejection lock UNP26.7

2) Condition variable UNP26.8. It is important to note that pthread_cond_wait () assumes no signal. The thread will clog until there is a signal. But the amount of mutual repulsion will be released immediately. Conversely, when it returns, the mutual repulsion amount is locked again.

3) Signal Volume CSAPP12.5


The above is simply a list and notes. The line output too deep. I will continue to study.

Practice. The last simple server-side program:

#include "simon_socket.h" #define Serv_port 12345typedef struct arg_tag{int connectfd;struct sockaddr_in  addr_info ;} Arg;void *start_routine (void *arg) {Pthread_detach (Pthread_self ());p rocess_client (((arg*) arg), CONNECTFD, & ((arg*) arg), Addr_info), Close ((ARG *) arg-CONNECTFD),//free (ARG);p thread_exit (NULL);} int main () {int sockfd, acfd;size_t sin_len = sizeof (struct sockaddr); ARG *arg;struct sockaddr_in client_addr;pthread_t thread;sockfd = Init_tcp_psock (Serv_port); while (1) {if (ACFD = Accept _request (SOCKFD, (struct sockaddr *) &client_addr, &sin_len)) <= 0) Continue;arg = malloc (sizeof (ARG)); Arg- > connectfd = acfd;arg, Addr_info = Client_addr;if (Pthread_create (&thread, NULL, Start_routine, (void*) arg)) {printf ("Create thread error!\n"); continue;} Free (arg);} Close (SOCKFD); return 0;}

The implementation of some calling functions in the code. To my GITHUB:HTTPS://GITHUB.COM/SIMON-XIA/LNP.

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Linux Network Programming Learning notes four-----multi--threaded Server

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.