17.1 (conspect) Inter-process communications: messages and shared memory

The plan: 1. Messages 2. Shared memory

Both in the case of the standard user interface, these processes work almost like files - we create a special object associated with these things can then communicate with them - use different file operations.

messages

Since the size is arbitrary, message passing is a much more flexible mechanism and messages are prioritized. We can queue the messages and give them priority by ourselves. And the operation will take the highest priority and then others by priority (which are lower). To create an object in the system that will work with the message queue call the system export - shell mq_open

We read the mq_open documentation.

Send it 4 parameters - name (in fact, nowhere in reality is this name reflected), flags (to create a queue it must be created, the mode by which it opens, and fourth parameters are all attributes - an additional parameter we do not look at them.

As for rights, the queue must be r w for the user (readable and writable)

A queue is being created for us.

   1 #include <mqueue.h>
   2 #include <stdio.h>
   3 #include <fcntl.h>
   4 #include <sys/stat.h>
   5 
   6 int main(int argc, char *argv[]) {
   7     mqd_t mqd;
   8     struct mq_attr attr;
   9 
  10     attr.mq_maxmsg = 10;
  11     attr.mq_msgsize = 2048;
  12     mqd = mq_open(argv[1], O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, NULL);
  13     return 0;
  14 }

To create a message you need to call the system call - there is a virtual file system, that is something that can be mounted that in the form of files shows the system status.

If we look at him, we can see his statistics - queue size and so on.

Open a queue:

What's the length for? To transmit bytes that aren't strings, for example. Pass the line, and we're very comfortable... A priority is a certain number that is higher for all topics.

   1 #include <mqueue.h>
   2 #include <stdio.h>
   3 #include <fcntl.h>
   4 #include <sys/stat.h>
   5 
   6 int main(int argc, char *argv[]) {
   7     mqd_t mqd;
   8     unsigned int prio;
   9     void *buf;
  10     struct mq_attr attr;
  11     ssize_t n;
  12 
  13     mqd = mq_open(argv[1], O_RDONLY);
  14 
  15     /* We need to know the 'mq_msgsize' attribute of the queue
  16        in order to determine the size of the buf for mq_receive() */
  17 
  18     mq_getattr(mqd, &attr);
  19     buf = malloc(attr.mq_msgsize);
  20     n = mq_receive(mqd, buf, attr.mq_msgsize, &prio);
  21     printf("Read %ld bytes; priority = %u\n", (long) n, prio);
  22 
  23     return 0;
  24 }

Reading messages:

   1 #include <mqueue.h>
   2 #include <stdio.h>
   3 #include <fcntl.h>
   4 #include <sys/stat.h>
   5 
   6 int main(int argc, char *argv[]) {
   7     mqd_t mqd;
   8     unsigned int prio;
   9     void *buf;
  10     struct mq_attr attr;
  11     ssize_t n;
  12     char *s;
  13 
  14     mqd = mq_open(argv[1], O_RDONLY);
  15 
  16     /* We need to know the 'mq_msgsize' attribute of the queue
  17        in order to determine the size of the buf for mq_receive() */
  18 
  19     mq_getattr(mqd, &attr);
  20     buf = malloc(attr.mq_msgsize);
  21     n = mq_receive(mqd, buf, attr.mq_msgsize, &prio);
  22     printf("Read %ld bytes; priority = %u\n", (long) n, prio);
  23     s = strndup(buf, attr.mq_msgsize);
  24     printf("/%s/\n", s);
  25     frees(s);
  26 
  27     return 0;
  28 }

Shared memory

In fact, in addition to just transmitting the reported message we can sign up to receive the signal code the message will be available - we can take and tell the dear OS send me a plise this sigevent signal when in this queue there is a message and the signal handler say - we can process the message when remembering the signal. sigevent - the structure is described as notifying itself.

In a longstanding lecture where we talked about virtual memory, particularly swap and shared memory. Swap - too much memory is squeezed in OP then a part of this memory can be unloaded by OS to a special place on disks - swap area - but a part of this memory in OS is not located but unloaded on disk. We said that if we use files on the disk as the quality of the memory to be loaded - binary - this binary already exists on the disk, it does not need to be shipped. There is no summary, but there is a clear correspondence between the memory area and the disk area - map and mapping.

'Mmap' - parameters: place in memory where we want it to display, size, how exactly this memory to protect

The creation:

Unlike the messages that were put in line - memory is just five - we opened something, put something in it and passed. Otherwise, the process of creating such an object is very similar to that of a repository. OS I want to create a repository for writable memory, this repository will be called like this - I want it to open both read and write. Truncate - open a file on record, cut it to a certain size - when we just open a file on record as open We order a file object, we tell him to listen, in this window it will be size.

   1 #include <unistd.h>
   2 #include <stdio.h>
   3 #include <fcntl.h>
   4 #include <sys/stat.h>
   5 #include <sys/mman.h>
   6 
   7 int main(int argc, char *argv[]) {
   8     int fd;
   9     char *addr;
  10     struct stat sb;
  11 
  12     fd = shm_open(argv[1], O_RDONLY, 0);
  13     fstat(fd, &sb);
  14     addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  15     close(fd);
  16 
  17     fwrite(adds, 1, sb.st_size, stdout);
  18     printf("\n... Done");
  19     return 0;
  20 }

   1 #include <unistd.h>
   2 #include <stdio.h>
   3 #include <fcntl.h>
   4 #include <sys/stat.h>
   5 #include <sys/mman.h>
   6 
   7 int main(int argc, char *argv[]) {
   8     int fd;
   9     char *addr;
  10     void *addr;
  11 
  12     fd = shm_open(argv[1], )_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
  13     size = atol(argv[2]);
  14     truncate(fd, size);
  15 
  16     addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  17     return 0;
  18 }

HSE/ProgrammingOS/17_IPC2/Conspect_en (последним исправлял пользователь FrBrGeorge 2020-03-22 12:22:50)