LINUX knowledge sorting and linux knowledge
Socket (): Create a Socket
Bind (): Bind a socket. Assign a local Protocol address to a socket
Listen (): listening socket
Connect (): establish a connection with the server.
Accept (): receives connections,
Send (): sending Function
Recv (): receiving function
Close (): close the connection
Server order:
Socket (), bind (), listen (), accept (), recv (), close ()
Client sequence:
Socket (), connect (), send (), close ()
Socket ():Create a socket and return a small integer descriptor. All function calls will use this descriptor to identify the socket. If the returned value is less than 0, the socket fails to be created. AF_XX address family PF_XX protocol family
Socket (int af, int type, int protocol );
Af: An address description. Currently, only the AF_INET format is supported, that is, the ARPA Internet address format.
Type: Specifies the socket type. The type description type of The New Interface, such as TCP (SOCK_STREAM) and UDP (SOCK_DGRAM ). Common socket types include SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_PACKET, and SOCK_SEQPACKET.
Protocol: Specify the protocol as the name implies. The protocol used by the set of APIs. If the caller does not want to specify the value, the value 0 is available. Common protocols include IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP, and IPPROTO_TIPC. They correspond to TCP, UDP, STCP, and TIPC respectively.
INet_ton ():IP address conversion function, which can be used to convert an IP address between "dot decimal" and "binary integer"
Moreover, the inet_ton and inet_ntop functions can process ipv4 and ipv6.
Int inet_ton (int af, const char * src, void * dst );
This function converts the string to the network address. The first parameter af is the address family, the second parameter * src is the source address, and the third parameter * dst receives the converted data.
Inet_ton is an extension of inet_addr. The following multi-address families are supported:
Af = AF_INET
Src is the first address (ddd. ddd. ddd. the function converts the address to the in_addr struct and copies it in * dst.
Af = AF_INET6
Src is the IPV6 address. The function converts the address to an in6_addr struct and copies it in * dst.
If a function error occurs, a negative value is returned and errno is set to EAFNOSUPPORT. If the address family specified by the af parameter and src format are incorrect, the function returns 0.
Fork ():Pid_t fork (void); returns 0 in the child process, returns the ID of the child process in the parent process, and returns-1 if an error occurs.
Fork returns 0 in the child process instead of the ID of the parent process.The reason is:: Each sub-process has only one parent process. The sub-process can call getppid to obtain the ID of the parent process. On the contrary, a parent process can have many sub-processes and cannot obtain the ID of each sub-process. If the parent process wants to track the ID of all sub-processes, then it must record the returned value of each call to fork.
Creates a copy of the parent process,The two run at the same time, and the order is uncertain
Vfork ():
1.VforkEnsure that sub-processes run firstThe parent process can be scheduled to run only after it calls exec or exit. If the sub-process depends on the further action of the parent process before calling these two functions, a deadlock will occur.
2. fork needs to copy the process environment of the parent process, while vfork does not need to completely copy the process environment of the parent process. Before the child process does not call exec or exit, the sub-process shares the process environment with the parent process, which is equivalent to the thread concept. In this case, the parent process is blocked and waiting.
Why is vfork available?
Because the previous fork will create a new address space and copy the resources of the parent process when creating a sub-process, there will be two actions:
1. Execute the code segment copied from the parent process
2. Call an exec command to execute a new code snippet.
When a process calls the exec function, a new program replaces the body, Data, heap, and stack segments of the current process. In this way, the previous copy work is in vain. In this case, smart people come up with vfork. Vfork does not copy the process environment of the parent process. The child process runs in the address space of the parent process, so the child process cannot perform write operations, and when the son "occupies" Lao Tzu's house, I want to grieve the old man for a moment and let him rest (blocking) outside. Once the son executes exec or exit, it is equivalent to buying his own house, and then it is equivalent to dividing the house.
Therefore, if you create a sub-process to call exec to executeNew programYou should use vfork
Thread: A program has at least one process, and a process has at least one thread.
A thread is an entity of a process and the smallest unit of CPU scheduling.
Several functions:
Pthread_mutex_lock (& mutex); // lock
Pthread_mutex_unlock (& mutex); // release the lock
Pthread_cond_wait (& cond, & mutex); // The condition is waiting to enter this statement and the lock will be released.
Pthread_cond_signal (& cond); // activation condition
Pthread_cond_broadcast (& cond); // broadcast activation for all
Pthread_create (& tid [I-1], NULL, thread_fun, & I); // create a thread
Pthread_join (tid [I], NULL); // wait until the thread ends
Thread Synchronization and mutex
Multiple threads share the address space of a process. Although inter-thread communication is easy to implement, synchronization and mutex mechanisms must be introduced when multiple threads simultaneously access shared objects.
Synchronization means that multiple tasks work together to accomplish one thing in the agreed order. dijkstra proposes a synchronization mechanism based on the semaphore concept. The semaphore determines whether the thread continues to run or waits for blocking.
The mutex between threads is introduced to ensure the integrity of data operations on shared resources.
Mutex lock is mainly used to protect critical resources.
Each neighboring resource is protected by a mutex. At any time, only one thread can access the resource.
The thread must obtain a mutex lock before accessing the critical resource. The lock is released after the critical resource is accessed. If the lock cannot be obtained, the thread will block until the lock is obtained.
Mutex lock and Conditional Variable implement read/write lock: The read and write locks are mutually exclusive. They cannot be written during read and write operations. However, if a read/write lock is implemented, first, you must determine whether read or write is preferred. In addition, read processes can coexist, while write processes are completely mutually exclusive. If a process requires a write lock, but other processes are being written or read when applying for the write lock, wait, wait until the read or write locks are completely released. The read process applies for a read lock before reading. If a write process exists at this time, it waits until the write lock is released.
OSI Layer-7 model:
7: Application Layer
6: Presentation Layer
5: Session Layer
4: Transport Layer
3: Network Layer
2: Data Link Layer
1: Physical Layer
Internet protocol family:
Application Layer: OSI's 567
TCP | UDP: the OSI 4 network layer can be directly bypassed or the transport layer can directly use IPV4 or IPV6
IPV4 and IPV6: OSI 3
Device Drivers and hardware: OSI 12
UDP: A simple and unreliable datagram protocol.Non-connection protocol, the source end of data transmission does not establish a connection with the terminal. when it wants to transmit data at a speed that is only sent, it simply crawls data from the application, and throw it to the network as quickly as possible. On the sending end, the UDP data transmission application can generate data speed, the ability of the computer, and the transmission bandwidth limit. on the receiving end, UDP puts each message segment in the queue, the application reads a message segment from the queue each time.
TCP: a complex and reliable byte stream protocol.It is a connection-oriented protocol, that is, a reliable connection must be established with the other party before sending and receiving data.Three handshakes, four waves
Three-way handshake
1) first handshake: when A connection is established, client A sends the SYN Packet (SYN = j) to server B and enters the SYN_SEND status, waiting for confirmation from server B.
(2) second handshake: When server B receives the SYN packet, it must confirm the SYN (ACK = j + 1) of Customer A and send A SYN Packet (SYN = k) by itself ), that is, the SYN + ACK packet. At this time, server B enters the SYN_RECV state.
(3) third handshake: Client A receives the SYN + ACK package from server B and sends A confirmation package ACK (ACK = k + 1) to server B. The package is sent completely, client A and server B enter the ESTABLISHED status and complete three handshakes.
After three handshakes are completed, the client and the server start to transmit data.
Four Waves
(1) Client A sends a fin to disable data transmission from client A to server B.
(2) server B receives the FIN and sends back an ACK to confirm that the serial number is added to 1. Like SYN, a FIN occupies a sequence number.
(3) server B Closes the connection with client A and sends A FIN to Client.
(4) Client A sends back the ACK message for confirmation, and sets the confirmation sequence number to receive the serial number plus 1.
SmallDifference Between TCP and UDP:
1. Based on connection and no connection;
2. Requirements on system resources (more TCP and less UDP );
3. Relatively simple UDP program structure;
4. stream mode and datagram mode;
5. TCP ensures data correctness, UDP may cause packet loss, TCP ensures data order, and UDP does not.
SCTP: stream control transmission protocol, reliable
Several communication methods between processes:
# Pipe: a pipe is a half-duplex communication method. data can only flow in one direction and can only be used between unrelated processes. The kinship of a process usually refers to the parent-child process relationship.
# Named pipe: A famous pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
# Signal (sinal): a signal is a complex communication method used to notify the receiving process that an event has occurred.
# Semophore: a semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a lock mechanism to prevent other processes from accessing a shared resource. Therefore, it is mainly used for synchronization between processes and between different threads in the same process.
# Message queue: a message queue is a linked list of messages stored in the kernel and identified by the message queue identifier. The message queue has overcome the disadvantages of few signal transmission information, the pipeline can only carry unformatted byte streams, and the limited buffer size.
# Shared memory: the shared memory is the memory mapped to a segment that can be accessed by other processes. The shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest IPC method. It is specially designed for the low efficiency of communication between other processes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
# Socket: interprocess communication is also a mechanism for interprocess communication. Different from other communication mechanisms, it can be used for process communication between different machines.
Basic LINUX commands:
Ls displays file-l details
-A: All files, including hidden files
Mkdir create directory
Cd switch directory
Touch creates an empty file
Echo creates a file with content
Cat View File Content
Cp copy (source file)
Move or rename music videos
Rm delete file
-R can delete subdirectories and files.
-F force Delete
Rmdir Delete empty directory
Find searches for a file in the file system
Wc counts the number of rows, words, and characters in the text
Grep searches for a string in a text file.
Pwd displays the current directory
Ln create link file
System Management commands
Stat displays detailed information of the specified file, which is more detailed than ls.
Ps displays Transient Process status
Ifconfig
Ping test network connection
Clear Screen
Kill and kill Processes
Chown change owner or user group
Chmod: Change File ACL read/write execution