On the evening of October 24, when I returned to the dorm to write this week's summary, I saw that the class was suddenly aware that it was the eighth week, and had to say that the time was really fast, compared with the relaxed, no-task course of the previous two years, this year really enriched too much! This may be the reason for the feeling of time too fast, but recently I also met a lot of problems, when the knowledge of contact more, found that they want to learn too much, only the lessons learned is not enough, so after class self-taught a number of courses, encountered problems one is their own ambitious, always want to be anxious, this look at a little, There is no systematic and comprehensive study of a book, question two: knowledge of the application of the hand quickly, but also quickly forget, to the application only remember some concepts. For these two issues, I have some thinking, the implementation of the following: In the study of the important points of knowledge recorded, I myself use the Youdao cloud notes, the newly learned knowledge or not the problem, through the access to data to solve all of them recorded. The results are good and can be reviewed every week. Recently opened its own blog, blogging is to summarize the daily learning, reflect on themselves. The ancients said "My day provinces" is still very reasonable. Make a summary of your knowledge in your own blog, as well as a weekly review of your monthly thoughts, and feel a lot. In addition, oneself also has some new sentiment, we often sigh many technical Daniel, blog celebrity How formidable, actually we can become the technical leader, one is the knowledge and the project experience unceasingly accumulates, second, I think is learns the knowledge to the detail grasp, its three, can persevere, Continue to learn new technology, usually constantly review, "warm so know new." Said a lot, practice is true, and then continue to go on their own journey of knowledge. Okay, here's a summary of this week's knowledge.
This week, two days of teaching, and three days of training, mainly on the Linux process and thread programming between the process of communication this aspect of knowledge. Summarized as follows:
Here are some of my understanding of these methods:
1, Nameless pipe (pipe): Pipeline is a half-duplex communication mode, the data can only be unidirectional flow, and only in the parent-child process communication. The buffer for the pipeline is limited (the pipeline exists in memory, a page size is allocated for the buffer when the pipeline is created), and the pipeline transmits the unformatted byte stream, which requires that the reader and writer of the pipeline must have agreed on the format of the data beforehand.
Process a fork () out of a process B, and process B is a copy of process A, which gets all the resources of process A, so process B also has a write pointer to pd[1], and a read pointer to Pd[0].
So the piping is designed like this:
Process A:
Close (FD [0]);
Write (fd[1],buffer,sizeof (buffer));
Process B:
Close (FD [1]);
Read (fd[0],buf,sizeof (BUF));
2. Named pipe (FIFO): A named pipe is also a half-duplex communication mode, but it allows communication between unrelated processes. Strict adherence to FIFO (first in initial out), the reading of pipelines and FIFO always returns data from the beginning, and the writing of them adds data to the end. File location operations such as Lseek () are not supported.
(1) A major limitation of piping applications is that it has no name, so it can only be used for inter-process communication with affinity, which is overcome when a named pipe (named pipe or FIFO) is presented. A FIFO differs from a pipe in that it provides a path name associated with it, which exists in the file system as a FIFO file. Thus, even processes that do not have affinity to the FIFO creation process, as long as they can access the path, can communicate with each other through the FIFO (the process that accesses the path and the creation process of the FIFO), so that processes that are not related to FIFO can also exchange data. It is important to note that FIFO strictly adheres to first-in, FIFO, which reads from the beginning of the pipeline, and is always returning data from the start, and writes the data to the end. They do not support file location operations such as Lseek ().
(2) Creation of famous pipelines
int Mkfifo (const char * pathname, mode_t mode);
The first parameter of the function is a normal pathname, which is the name of the FIFO after creation. The second parameter is the same as the mode parameter in the open () function that opens the normal file. If the first parameter of Mkfifo is an already existing pathname, the eexist error is returned, so the typical calling code first checks to see if the error is returned, and if it does return the error, simply call the function that opened the FIFO. I/O functions for general files can be used in FIFO, such as close, read, write, and so on.
(3) Open rules for famous pipelines
A famous pipe is more than a pipe. Open operation: Opening.
Open rules for FIFO:
If the current open operation is open FIFO for read, the current open operation will return successfully if the process has already opened the FIFO for write, otherwise, it may block until a corresponding process is written to open the FIFO (the current open operation has a blocking flag set); Successful return (the current open operation does not have a blocking flag set).
If the current open operation is to open a FIFO for write, the current open operation will return successfully if the corresponding process has opened the FIFO for reading, otherwise, it may block until the corresponding process has opened the FIFO for reading (the blocking flag is set for the current open operation); Returns a Enxio error (the current open operation does not have a blocking flag set).
Summary:
Pipelines are commonly used in two areas: (1) Pipelines are often used in the shell (as input input redirects), in which case the pipeline is created transparently to the user, (2) is used for inter-process communication with affinity, the user creates the pipeline himself, and reads and writes.
FIFO can be said to be the promotion of pipelines, overcome the pipe No name restrictions, so that the non-affinity process can also be used in first-out communication mechanism for communication.
Pipelines and FIFO data are byte streams, and applications must identify specific transport "protocols" in advance, using messages that propagate a particular meaning. To flexibly apply pipelines and FIFO, it is critical to understand their read and write rules.
3. Message queue: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Can be processed in a non-FIFO manner; Message Queuing is asynchronous communication; Message Queuing has a size limit, usually only for small data volumes, and only for interprocess communication on a single host;
Message Queuing:
Provides a way to send a block of data from one process to another
int Msgget (key_t key,int MSGFLG);
int msgctl (int magid,int cmd,struct msgid_ds *buf);
int msgsnd (int msgid,void *msg_ptr,size_t msg_sz,int msgflag);
int MSGRCV (int msgid,void *msg_ptr,size_t msg_sz,long int msg_type,int msgflag);
4. Shared memory: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest way to IPC. High efficiency, the process can read and write memory directly, without any copy of the data; In the shared memory segment, the default terminator of the string is the end of a message. Each process follows this rule when reading and writing, without destroying the integrity of the data.
Shared memory
Allow two processes that do not resemble a shutdown to access the same logical address
1, int shmget (key_t key,size_t size,int shmflag);
2, void *shmat (int shm_id,const void *shm_addr,int shm_flag);
3, int shmctl (int shm_id,int cmd,struct shmid_ds *buf);
4, int shmdt (const void *SHM_ADDR);
5. Semaphore (Semophore): Semaphore is a counter that can be used to control access to shared resources by multiple processes. Primarily as a means of synchronizing between processes and between different threads within the same process. Binary semaphore: Similar to a mutex.
Signal Volume:
int Semget (key_t key,int num_sems,int Sem_flgs);
int semctl (int sem_id,int sem_num,int command ...);
int semop (int sem_id,struct sembuf *sem_ops,size_t num_sem_ops);
The functions and parameters of each function can be found in the manual.
1. Semge create a semaphore or get a key for a known semaphore
Key: Unrelated processes can access the same semaphore through him
Num_sems: Number of semaphores required. Almost always 1
Sem_flags: A set of flags, such as ipc_creat;
The return value is the semaphore marker, which is used for other semaphore functions
2, Semctl:
SEM_ID: Semaphore designator, Semget return value
Sem_ops: Semaphore number, general take 0
Command: Do not explain, in the manual has the detailed explanation, I secretly lazy.
3, Semop
The value used to change the semaphore
struct sembuf{
Short sen_nums; Semaphore number, generally take 0
Short Sem_op; General-1 is P operation, 1 is v operation
Short Sem_flag; Usually set to undo
}
Learn the summary of inter-IPC interprocess communication (eighth week)