11th-day inter-process communication in UC =

Source: Internet
Author: User

 

========================= Inter-process communication ========================== ======

Review:
1. Timer
1. Struct itimerval {struct tiemval it_interval; // interval: returns a signal once at an interval of X. struct timeval xxx...; // start time: returns a signal after X}
2. Precise Time struct
Struct timeval {long TV _sec; long TV _usec;} *** if you want to use accurate time representation later, this is often used.

2. Reliable signal: 34--64 unreliable signal: 32 is in the middle of 1--32, and 33 null signals do not exist. Therefore, they are separated between reliable and unreliable signals.

3. Message queues, semaphores, and shared memory all use the IPC structure.

Class:
I. Shared Memory segments
1. shmctl (ID, command, depending on a struct of the command) function: A function used to control shared memory segments. Msgctl is used to control message queues.

* ***** Almost all important system functions usually need to determine whether the returned value is-1 to indicate whether the operation is successful or not. Therefore, after almost every function call, we need to determine if (xxx =-1), which is mandatory and unavoidable. ***
 
2. The shared memory segment has a corresponding struct that records some information about the memory segment: permissions, masters, etc. We cannot directly access the struct, we have to prepare the same struct for the assignment and replication. You can view the content in struct.

3. Shmctl (ID, ipc_rmid, 0) deletes the shared storage segment and does not immediately delete it. Just make a delete tag, because other processes may still use this memory segment. So you cannot delete it immediately. Other processes that want to be mounted cannot be mounted. It will not be deleted until all processes are disconnected (the printing is released.

II. IPC command: (these items are managed by the kernel, so you need to apply for deletion before the process ends. Otherwise, they still exist and can be viewed through the IPCS command .)
Display function: IPCS-a-m-Q-S
Delete function: ipcrm-m-Q-s ID

3. Message Queue (similar to the shared memory segment, but it is a queue made of a linked list: first-in-first-out)
Message: IPCS-Q (data of different types)
1. Function: msgget msgsnd msgrvc... msgctl
Step: 1. Confirm key 2. Create/obtain Message Queue 3. Send and receive messages (sent to the kernel-managed message queue area) 4. Delete A Message Queue

2. The deletion of a message queue is different from that of the shared memory. The message queue is deleted immediately instead of being marked. So programmers should note: whether there are other processes using this message queue

3. Message Type: type 2 (gets the first type of message 2)-type 2 (gets the <= (smaller than or equal to) Type 2 message) This is a negative value.

==================== Pm: ================
I. Message Type:
Msgrcv (ID, & structdata, sizeof (structdata), 1 (type), 0 (will wait, Nowait indicates not waiting ))
The size of the received and sent messages must correspond to each other so that the messages can be successfully received (if the received size is small, the messages will be truncated and discarded ). If the type is specified, only messages of this type can be received in the message pair column. 0 indicates any type.

The message format must be like this: struct MSG {long mtype; // must be> 0 renyitype mydata; // any data type, struct can also be };

II. Semaphores
Concept: it is used to control resource access, that is, the public counters of multiple processes. The deletion is also the same as that of the Message Queue. It is deleted immediately. Set a semaphore. Set a semaphore set with Serval and use setall.
-1 get + 1 release

3. Multi-Thread Programming
1. Difference: process and thread differences
0. A thread is a part of a process and has its own independent stack space, similar to a function.
1. Threads only occupy the stack area of the process, so other zones share with the process.
2. In a process, main () is the main thread, which can start the thread. Once the main thread ends, other threads will also end.
3. Threads compete for CPU resources.
2. In the eyes of the CPU, there are only threads and no processes.

3. -> The access efficiency is lower than. We can see from the generated assembly code. To supplement this defect, we can use restrict to improve efficiency.

4. All operations on threads are in the libpthread. So library. Therefore, gcc abc. C-l pthread must be used to reference threads in the program.

5. Advantages of multithreading: it consumes less resources and can process multiple tasks. For example, for a train ticket, each request starts a thread to serve it. One server provides services to multiple clients. Multi-task and multi-thread.

 

 

 

 

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.