AempOS

An educational multiprocessor Operating System

Lab4 Interprocess communication

Lab4-1 Message queue

1. Principle

The message queue communication mechanism in AempOS is modeled on the System V communication mechanism. The System V message queue was added to the System V kernel in the early 1980s. The message queue overcomes the disadvantage that pipeline communication can only transmit unformatted byte streams, and can be used for processes with or without kinship on the same host between.

In AempOS, processes can add new messages to the message queue according to certain rules; other processes can read messages from the message queue. The message queue has one of the most important identifiers, the key value. Two processes that are about to communicate establish a communication relationship between them by looking for a message queue with a common key value. Processes using the same key value share a message queue, which can be used to receive messages or send messages. The key value of a process is not unique, and a process can share multiple message queues. The message queue model in AempOS is shown in Figure below.

In Figure above, the sender can call the Send procedure to send a message with a length of size bytes to the message queue, and indicate whether to use blocking sending or non-blocking sending. The receiver can call the Recieve procedure to receive bytes of length size from the message queue. The two sides of the communication match the message queue and the other process through the key value, and the processes using the same key share a message queue.

2. To do

In AempOS, users are provided with 4 message queue system call interfaces, see Table below.

函数名称 函数参数 函数功能
msgget key 获取一个消息队列,分配成功时返回消息队列的id(msqid),分配失败时返回-1
msgctl msqid,cmd 回收一个消息队列,回收成功时返回0,回收失败时返回-1
msgsnd msqid,*ptr,size,flag 向msqid消息队列发送长度为size字节的消息,flag指明是采用阻塞发送还是采用不阻塞发送。ptr指针指向存放消息的缓冲区
msgrcv msqid,*ptr,size,flag 接收类型为type的消息,flag指明采用阻塞发送还是采用不阻塞发送。接收缓冲区的长度为size字节。ptr指针指向接收消息的缓冲区

In terms of data structure design, the message queue consists of four core data structures.

  1. Message queue structure

    Each message queue contains four queues:

    1. Idle buffer queue: use the last-in-first-out method to manage the idle buffer in the message queue.
    2. Occupy the buffer queue: use the first-in first-out method to manage the messages that the process needs to send.
    3. Process queue waiting for free buffer.
    4. Process queues waiting to occupy the buffer.
  2. The structure of the message node

    Figure above is the structure of the message node. Each message node has four elements: type (the type of message sent), size (the length of the message sent), buffer (pointing to the buffer used to store the message, the maximum number of bytes is MSGMAX), next (pointing to The next node, used to implement the linked list).

  3. Waiting for the structure of the process node

    Figure above is the structure of the waiting process node. Each waiting process node has four elements: type (if it is the node of the process queue waiting to occupy the buffer, it indicates the type of message it is waiting for), data (pointing to the PCB of the sleeping process), next (pointing to the next node point, used to implement a linked list).

  4. The structure of the management message queue in the kernel

    Figure above is the structure of the management message queue in the kernel. It consists of an array of management blocks, and each management block contains four elements: Alloc (whether the message queue has been allocated), Key (the Key represented by the message queue), Lock (spinlock, used for mutual exclusion operations), MsgQueue ( pointer to the message queue). The total number of all message queues managed by the kernel is MSGMNI.

Lab4-2 Mailbox communication

1. Principle

Mailbox communication means that a process uses an independent mailbox, and the child thread shares the mailbox of the parent process, and can only receive operations on the mailbox it owns. The principle of mailbox communication in AempOS is shown in Figure below.

In Figure above, the sender can call the Send procedure to send an email with a length of size bytes to the other party’s mailbox, and indicate whether to use blocking or non-blocking sending. The receiver can call the Recieve procedure to receive bytes of length size from its own mailbox.

2. To do

In AempOS, 4 mailbox communication system call interfaces are provided for users, see Table below.

函数名称 函数参数 函数功能
boxget 生成该进程的邮箱,只有目的进程调用此函数之后,其它进程才可以向目的进程发送信件
boxdel 删除该进程的邮箱,目的进程删除邮箱之后,其它进程不可以向目的进程发送信件
boxsnd pid,*ptr,size,flag 将ptr所指的长度为size字节的消息发送给进程pid,flag用于指明采用阻塞方式还是采用不阻塞方式
boxrcv pid,*ptr,size,flag 接收pid进程所发送的信件,将信件内容拷贝到ptr所指的缓冲区中,缓冲区的长度为size字节,flag用于指明采用阻塞方式还是采用不阻塞方式