Unix/Linux System Programming Chapter 4 learning notes
Author: 20191322wyl
Summary of knowledge points
Introduction to parallel computing
There are many applications and processes in the Linux environment, the most important of which is the client network / server. Multi process server means that when the client sends a request, the server uses sub processes to process the client's request. The parent process continues to wait for requests from other clients. The advantage of this method is that the server can manage the client when requested by the client, especially in the interactive client / server system. For a TCP server, the connection between the client and the server may not close immediately. The client can close the connection after sending data. During this period, the server-side process is blocked, and the operating system may set other schedules. Customer service process at this time. Compared with circular server, the performance of this service has been significantly improved.
Sequential algorithm and parallel algorithm
Parallelism and concurrency
Usually, parallel algorithms only recognize tasks that can be executed in parallel, but it is not. However, for example, a multiprocessor or multicore system. In a single CPU system, only one task can be executed at a time. In this case, different tasks can only be executed concurrently, that is, logically in parallel. In a single CPU system, concurrency is achieved by multitasking.
thread
Principle of thread
A thread is an independent execution unit of a process in the same address space.
Advantages and disadvantages of threads
Compared with processes, threads have many advantages.
- Faster thread creation and switching
- Threads respond faster
- Threads are more suitable for parallel computing
Disadvantages of threads
- Due to address space sharing, threads need explicit synchronization from users.
- Many library functions may be thread unsafe. Generally, any function that uses global variables or depends on static memory content is not thread safe.
- On a single CPU system, using threads to solve problems is actually slower than using sequential programs
Thread operation
The execution trajectory of a thread is similar to that of a process. Threads can execute in kernel mode or user mode. In user mode, line
Procedures execute in the same address space of the process, but each thread has its own execution stack. Threads are independent execution orders
According to the scheduling policy of the operating system kernel, the system call to the kernel can be suspended and activated to continue execution
Wait. In order to make use of the shared address space of threads, the scheduling strategy of the operating system kernel may give priority to those in the same process
Threads, not threads in different processes. Up to now, almost all operating systems support posixthread,
A series of standard application programming interfaces (API s) are defined to support thread programming. Next, we will discuss and demonstrate
Pthread concurrent programming in Linux (Goldt et al., 1995; IBM; Love2005; Linux manpageprogect)
2017).
Thread management function
pthread_create(thread, attr, function, arg): create thread pthread_exit(status) : terminate thread pthread_cancel(thread) : cancel thread pthread_attr_init(attr) : initialize thread attributes pthread_attr_destroy(attr): destroy thread attribute
practice
Title:
Use thread to calculate the sum of all elements in each N*N integer matrix
Process:
code:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #define N 4 int A[N][N],sum[N]; void *func(void *arg) { int j,row ; pthread_t tid = pthread_self(); row = (int)arg; printf("Thread %d [%lu] computes sum of row %d\n",row,tid,row); for(j=0;j<N; j++) sum[row] += A[row][j]; printf("Thread %d [%lu] done:sum [%d] =%d\n",row,tid,row,sum[row]); pthread_exit ((void*)0); } int main(int argc, char *argv[]) { pthread_t thread[N]; int i,j,r,total = 0; void *status; printf("Main: initialize A matrix\n"); for(i=0; i<N;i++){ sum[i] = 0; for(j=0;j<N;j++){ A[i][j]=i*N+j+1; printf("%4d ",A[i][j]); } printf( "\n" ); } printf ("Main: create %d threads\n",N); for(i=0;i<N;i++) { pthread_create(&thread[i],NULL,func,(void *)i); } printf("Main: try to join with thread\n"); for(i=0; i<N; i++) { pthread_join(thread[i],&status); printf("Main: joined with %d [%lu]: status=%d\n",i,thread[i], (int)status); } printf("Main: compute and print total sum:"); for(i=0;i<N;i++) total += sum[i]; printf ("tatal = %d\n",total ); pthread_exit(NULL); }
result:
Problems and Solutions
problem
How to detect whether a thread has a lock?
solve
In java.lang.Thread, there is a method called holdsLock(), which returns true if and only if the current thread owns the lock of a specific object.