Summary of Linux programming knowledge points

  • Makefile

1.1 Structure:

targets:prerequisites

  command

1.2 gcc compilation: -g, -o, -c, -D, -w, -W, -Wall, -O3

-c: source files are compiled and assembled, but not linked

Example:

gcc -c test.cpp -o test

-c followed by multiple source files

      When -c is followed by multiple source files, a .o file will be generated for each source file , but -o cannot be used at this time .

Reference link: Introduction to gcc compilation options -o and -c_gcc -o_chengqiuming's Blog-CSDN Blog

1.3 Automation variables

automation variable

illustrate

$@

Indicates the object file

$%

When the target file is a static library file, it represents a member name of the static library.

$<

Indicates the first dependent file.

$^

Represents all dependent files.

$?

A list of all dependent files newer than the target file.

$+

Like "$^", but it preserves duplicate occurrences of dependent files. It is mainly used in the cross-reference occasion of the library when the program is linked.

$*

In pattern rules and static pattern rules, stands for "stem". The "stem" is the part represented by the "%" in the target pattern (when a directory is present in the filename,

test:test.o test1.o test2.o

         gcc -o $@ $^

test.o:test.c test.h

         gcc -o $@ $<

test1.o:test1.c test1.h

         gcc -o $@ $<

test2.o:test2.c test2.h

         gcc -o $@ $<

Adding characters "D" or "F" to these variables in GNU make forms a series of variant automation variables that can operate on file names.

D represents the directory part, and F represents the file part. Such as: $(@D), $(@F)

1.4 Object file search

4.1 VPATH

VPATH := src car

test:test.o

    gcc -o $@ $^

The files in the src directory are searched first, and then the files in the car directory are searched.

4.2 vpath

vpath test.c src car

Search for test.c files in two paths

1.5 False targets

clean:

    rm -rf *.o test

Two, the shell

Reference link: https://www.jianshu.com/p/74e8739ddc01

2.1 shell script,

#!/bin/bash

echo "HelloWorld!"

2.2 Flow control statement

if – then – else – fi

if else fi

if elif fi

for loop

while statement

until loop (opposite of while condition)

case ... esac

-eq/-ne/-gt/-ge/-lt/le

greater than or equal greater than or equal

-e true if the file exists

-r True if the file exists and is readable

-w True if the file exists and is writable

-x True if the file exists and is executable

-s True if the file exists and has at least one character

-d true if the file is a directory

-f true if the file exists and is a normal file

-c true if the file exists and is a character special file

-b True if the file exists and is a block special file

2.3 Functions

Example:

#!/bin/bash

function timesFun(){

    echo " This function will multiply two numbers "

    echo " Please enter the first number "

    read num1

    echo " Please enter the second number "

    read num2

    return $(( ${num1} * ${num2} ))

}

timesFun

echo -n " The multiplication value of the two numbers entered is $?"

3. Pointer

Compared with other high-level programming languages, C language can operate computer hardware more efficiently, and the operation instructions of computer hardware depend on addresses to a large extent.

Pointers provide a way to operate on addresses. Therefore, using pointers can enable C language to more efficiently operate on the underlying hardware of the computer. In addition, arrays can be manipulated more conveniently through pointers. In a certain sense, it can be said that the pointer is the essence of the C language.

3.1 Memory and address

Reference link: http://c.biancheng.net/c/pointer/

(1) &: Get variable address

(2) Definition of pointer variable: such as int *p;

http://c.biancheng.net/uploads/allimg/180903/2-1PZ3145331D9.jpg

(3) "wild" pointer

int *pi,a; //pi is uninitialized, has no legal pointer, and is a "wild" pointer

*pi=3; // Runtime error! The storage operation cannot be performed on the space pointed to by the "wild" pointer. This statement attempts to store 3 in the random space pointed to by the "wild" pointer pi , and a runtime error will occur

(4) Pointers and arrays

Array pointer: example int (*p)[5]; can represent a two-dimensional array

#define M 3

#define N 4

int a[M][N],i,j;

int (*p)[N]=a; // equivalent to two statements int (*p)[N] ; p=a;

Pointer array: example int * a[5];

int a0,a1,a2,a3,a4;

a[0]=&a0;

a[1]=&a1;

...

a[4]=&a4;

3.2 Function pointers

Reference link: https://blog.csdn.net/Jacky_Feng/article/details/108953519

(1) Definition of function pointer

If a function is defined in the program, when compiling, the compiler will allocate a section of storage space for the function code, and the starting address of this space (also known as the entry address) is called the pointer of this function.

Example:

int func(int a,int b);// function declaration

int (*p) (int , int );// Define a parameter list as two variables of type int , and the return value type is a function pointer p of type int

p=func ; // The function pointer p points to the starting address of the function func

(2) Application of function pointers

Call the function pointed to by the function pointer p

*p(3, 5);

3.3 Callback function

The program passes the function pointer of this function to other functions through parameters, and calling this function pointer in that function is equivalent to calling this function. Such a process is called a callback, and the called function is called a callback function.

Example:

#include <iostream>

using namespace std;

 //1. Define the callback function

void print(int n)

{

      for (int i = 0; i < n; i++) {

             cout << "hello world" << endl;

      }

}

//2. Define the callback function prototype (function pointer)

typedef void (*CallbackFun)(int);

 //3. Define the registration callback function

void registCallback(CallbackFun callback,int n)

{

      callback(n);

}

 int main()

{

    //4. Pass the print function pointer as a parameter into the registration function registerCallback to call the callback , that is, the print function is executed .

      registCallback(print, 10);

}

4. Multithreading and multiprocessing

For threaded systems:

(1) A process is an independent unit of resource allocation

(2) Thread is an independent unit of resource scheduling

For threadless systems:

(1) A process is an independent unit of resource scheduling and allocation

4.1 Communication methods between processes and their advantages and disadvantages

Pipeline (PIPE )

(1) Well-known pipe: a half-duplex communication method that allows communication between unrelated processes.

①Advantages: Communication between processes of any relationship can be realized.

②Disadvantages: a. Long-term storage in the system, improper use is prone to errors; b. Limited buffer.

(2) Unnamed pipe: A half-duplex communication method that can only be used between processes with kinship (parent-child process).

①Advantages: Simple and convenient.

② Disadvantages: a. Limited to one-way communication; b. It can only be created between its process and its related processes; c. The buffer is limited.

Semaphore (Semaphore): A counter that can be used to control access to shared resources by multiple threads.

  • Pros: Can synchronize processes.

② Disadvantages: limited semaphore.

Signal (Signal ): A more complex communication method used to notify the receiving process that an event has occurred.

Message Queue (Message Queue ): It is a linked list of messages, stored in the kernel and identified by a message queue identifier.

  • Advantages: It can realize the communication between any processes, and realize the synchronization between message sending and receiving through the system call function, without considering the synchronization problem, which is convenient.

② Disadvantages: The copying of information requires extra CPU time, which is not suitable for occasions with large amount of information or frequent operations.

Shared Memory (Shared Memory ): Map a section of memory that can be accessed by other processes. This shared memory is created by one process, but multiple processes can access it.

  • Advantages: no need to copy, fast, large amount of information.

② Disadvantages: a. Communication is achieved by directly attaching the shared space buffer to the virtual address space of the process, so the synchronization of read and write operations between processes; b. Using the memory buffer to directly exchange information, the entity of the memory It exists in the computer and can only be shared by many processes in the same computer system, which is inconvenient for network communication.

Socket (Socket ): can be used for process communication between different computers

①Advantages:

a. The transmission data is at the byte level, the transmission data can be customized, and the data volume is small and the efficiency is high;

b. Short data transmission time and high performance;

c. Suitable for real-time information interaction between client and server;

d. It can be encrypted, and the data security is strong;

② Disadvantage: The transmitted data needs to be analyzed and converted into application-level data.

4.2 Communication methods between threads

4.2.1 Lock mechanism

Including mutex/quantity (mutex), reader-writer lock (reader-writer lock), spin lock (spin lock), condition variable (condition)

  1. Mutex/Volume (mutex): Provides methods to prevent data structures from being modified concurrently in an exclusive manner.

The mutex mechanism mainly includes the following basic functions:

  1. Mutex initialization: pthread_mutex_init()
  2. Mutex lock: pthread_mutex_lock()
  3. Mutex judgment lock: pthread_mutex_trylock()
  4. Mutex lock: pthread_mutex_unlock()
  5. Eliminate mutex: pthread_mutex_destroy()
  1. Reader-writer lock (reader-writer lock): Allows multiple threads to read shared data at the same time, while the write operation is mutually exclusive.

The read-write lock mechanism mainly includes the following basic functions:

  1. int pthread_rwlock_init(pthread_rwlock_t * rwlock, const pthread_rwlockattr_t * attr);
  2. int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
  3. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
  4. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
  5. int pthread_mutex_timedrdlock(pthread_rwlock_t * rwlock,const struct timespec * tsptr);
  6. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
  7. int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
  8. int pthread_mutex_timedwrlock(pthread_rwlock_t * rwlock,const struct timespec * tsptr);
  9. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
  1. A spin lock (spin lock) is similar to a mutex, both to protect shared resources. The mutex is when the resource is occupied, and the applicant enters the sleep state; while the spin lock loops to check whether the holder has released the lock.

The spinlock mechanism mainly includes the following basic functions:

  1. int pthread_spin_destroy(pthread_spinlock_t *lock);
  2. int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
  3. int pthread_spin_lock(pthread_spinlock_t *lock);
  4. int pthread_spin_trylock(pthread_spinlock_t *lock);
  5. int pthread_spin_unlock(pthread_spinlock_t *lock);
  1. Condition variable (condition): A process can be blocked atomically until a certain condition is true. The testing of the condition is done under the protection of a mutex. Condition variables are always used with mutexes.

Multithreaded semaphores include the following basic functions.

  1. int sem_init(sem_t *sem, int pshared, unsigned int val);
  2. int sem_wait(sem_t *sem);
  3. int sem_post(sem_t *sem);
  4. int sem_destory(sem_t *sem);

4.2.2 Semaphore mechanism (Semaphore)

(1) Unnamed thread semaphore

(2) Named thread semaphore

Signal mechanism (Signal): similar to signal processing between processes

Barrier: A barrier allows each thread to wait until all cooperating threads have reached a certain point, and then continue execution from that point.

The purpose of communication between threads is mainly for thread synchronization, so threads do not have a communication mechanism for data exchange like process communication

Private and shared resources between processes

Private: address space, heap, global variables, stack, registers

Shared: code segment, public data, process directory, process ID

Private and shared resources between threads

Private: thread stack, registers, program counter

Shared: heap, address space, global variables, static variables

4.3 Comparison, pros and cons and choice between multi-process and multi-thread

1. Contrast

2. Advantages and disadvantages

3. Select

(1) Priority threads that need to be frequently created and destroyed

(2) Priority use of threads that require a large amount of calculations

(3) Strongly related processing uses threads, and weakly related processing uses processes

(4) It may be extended to use processes for multi-machine distribution, and use threads for multi-core distribution

(5) When all requirements are met, use the method you are most familiar with and best at

Five, Linux network programming

5.1 TCP

5.1.1 TCP network model

https://img-blog.csdnimg.cn/2020020517240829.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dmZWFfbGZm,size_16,color_FFFFFF,t_70

To create a tcp server under Linux , there are six steps as follows:

socket - create a socket

bind - bind host and port

listen -- set the listening socket

accept - accept client connections and generate new sockets

read/write – send and receive data

close - close the socket

Creating a client is mainly divided into the following steps:

socket -- create a socket

connect -- Actively connect to the server

write/read -- send and receive data

close -- close the socket

  1. socket - create a socket

Function prototype: int socket(int domain, int type, int protocol);

Parameter Description:

domain: protocol family, specify the protocol family used for communication; common options are as follows:

AF_UNIX, AF_LOCAL : Local communication , used for communication between local processes / threads;

AF_INET:IPv4 Internet protocols

AF_INET6:IPv6 Internet protocols

type: socket type, commonly used options are as follows:

SOCK_STREAM: stream socket, uniquely corresponding to TCP;

SOCK_DGRAM: Datagram socket, uniquely corresponding to UDP;

SOCK_RAW: Raw (transparent) socket;

protocol: usually fill in 0, this parameter is required when the type type is SOCK_RAW.

Return value: socket (file descriptor) is returned on success, and -1 is returned on failure.

  1. bind - bind host and port

Function prototype: int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

General address structure:

struct sockaddr {

             sa_family_t sa_family; //address family

             char in_data[14];

             }

Internet address structure:

struct sockaddr_in   {          

             u_short sin_family; // address family, AF_INET, 2 bytes

             u_short sin_port; // port, 2 bytes

             struct in_addr sin_addr;  // IPV4地址,4 bytes    

             char sin_zero[8]; // 8 bytes unused, as padding

             };

IPv4 address structure

// internet address 

struct in_addr

{

       in_addr_t  s_addr;            // u32 network address

};

Local communication protocol address structure: can be used for inter-process communication

    struct sockaddr_un

    {

        sa_family_t sun_family; //protocol family

        char sun_path[108]; //socket file path

    }

  1. listen -- set the listening socket

Parameters: sockfd --- socket file descriptor

        backlog --- monitor queue length

  1. accept - accept client connections and generate new sockets

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

parameter:

      sockfd --- socket file descriptor

      addr --- client IP and port number

      addrlen --- client address length          

      Note: addr and addrlen are used to obtain the address of the client, if you do not need to know the client, these two parameters are set to NULL

  1. connect – Actively connect to the server (client)

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

parameter:

      sockfd --- socket file descriptor

      addr --- address of the server

      addrlen --- length of addr

  1. read/write – send and receive data

ssize_t send(int sockfd, const void *buf, size_t len, int flags);

parameter:

      sockfd --- socket file descriptor

      buf --- the first address of the sent data

      len --- number of bytes sent

      flags --- sending method(0)

size_t recv(int sockfd, void *buf, size_t len, int flags);

Parameters: sockfd --- socket file descriptor

             buf --- The first address of the space where the data is stored

             len --- the number of bytes you want to receive

             flags --- Receive method(0)

  1. close - close the socket
  2. setsockopt function

Reference link: https://blog.csdn.net/Mr_XJC/article/details/106788694

5.2 UDP

Steps (see TCP for function calls):

(1) Create a socket

(2) Bind IP and port bind

(3) Send and receive messages

Reference link: UDP communication under Linux environment_linux udp communication_workingwei's blog-CSDN blog

6. Data structure

Reference link: C language data structure_Rising Sun Chuyang's Blog-CSDN Blog

The difference between typedef struct and struct definition structure

Reference link: [C language] difference between typedef struct and struct usage_difference between typedef struct and direct struct_Mijie's Voice Blog-CSDN Blog

The syntax of struct is more complicated, so let's give examples one by one.

Example 1:

struct{

char a;

int b;

} x;

Here, a variable is created with two members,

One character, one integer.

Example 2:

struct STUDENT{

char name;

int age;

};

Here, a tag is created,

A STUDENT name was provided for the members list.

In the future, you can declare variables through struct STUDENT x;

Next is the combination of typedef and struct

Example three:

typedef struct{

char name;

int age;

}STUDENT;

The effect here is basically the same as in Example 2.

STUDENT is now the name of a data type.

Later statements can be written directly as STUDENT x;

Example four:

typedef struct NODE{

int data;

struct NODE* next;

}node;

This is a common way of creating linked list nodes, which can be divided into two steps:

first step

struct NODE{

int data;

struct NODE* next;

};

Created a structure type called NODE,

The second step typedef NODE node;

Name the data type NODE as node

It is worth noting that

When creating a linked list,

typedef struct NODE{

int data;

struct NODE* next;

}node;

The pointer is created using struct NODE* next;

In subsequent creation, insertion, deletion, lookup functions, and the main function,

The declaration pointer is unified with node* pointer;

problems encountered

  1. When compiling with multiple threads, there is a lack of library linking, and add –lpthread or -pthread when compiling

如:gcc -o test.c test -lpthread

  1. Scanf space problem, two solutions
  1. replace fgets();
  2. scanf(“%[^\n]”, msg);

Guess you like

Origin blog.csdn.net/qq_39825430/article/details/131982142