Различия между версиями 3 и 4
Версия 3 от 2020-03-21 17:22:40
Размер: 6824
Редактор: NataliaKrauze
Комментарий:
Версия 4 от 2020-03-22 12:22:50
Размер: 6785
Редактор: FrBrGeorge
Комментарий:
Удаления помечены так. Добавления помечены так.
Строка 1: Строка 1:
= 17 (conspect) Inter-process communications: messages and shared memory =

The plan:
1. Messages
2. Shared memory 
= 17.1 (conspect) Inter-process communications: messages and shared memory =
The plan:  1. Messages  2. Shared memory
Строка 10: Строка 7:
 * as opposed to a signal that delivers everything immediately.
 * a message of correct size as opposed to a signal is just a set of bytes which is transmitted by the processor through a virtual object which represents a queue of these messages.
 * so when we get a message, it has a header that stores the system memory.
  . We read the message from some queue, get some memory and there it is and will be read there as soon as we call the function.
Строка 11: Строка 12:
 * as opposed to a signal that delivers everything immediately.
 * a message of correct size as opposed to a signal is just a set of bytes which is transmitted by the processor through a virtual object which represents a queue of these messages.
 * so when we get a message, it has a header that stores the system memory.

 We read the message from some queue, get some memory and there it is and will be read there as soon as we call the function.
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
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
Строка 25: Строка 18:
Строка 28: Строка 20:
A queue is being created for us.  A queue is being created for us.
Строка 38: Строка 31:
    
Строка 45: Строка 38:

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. 
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.
Строка 51: Строка 43:
 Send a message using mq_send - with 4 pairs, queue to which to send, line with the message itself, message length, message priority
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.

 .
Send a message using mq_send - with 4 pairs, queue to which to send, line with the message itself, message length, message priority

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.
Строка 67: Строка 60:
    
Строка 81: Строка 74:

Reading messages: 
Reading messages:
Строка 97: Строка 89:
    
Строка 114: Строка 106:
== Shared memory ==
 . We can remove it ourselves - we can make a system call allowing you to work directly with file operations.
Строка 115: Строка 109:
== 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.
Строка 117: Строка 111:
 We can remove it ourselves - we can make a system call allowing you to work directly with file operations.
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.
 . !!!! About one funny thing - an alternative to reading from streaming input
Строка 121: Строка 113:
 !!!! About one funny thing - an alternative to reading from streaming input
In a longstanding lecture where we talked about v
irtual 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.
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.
Строка 126: Строка 115:
'Mmap' - parameters: place in memory where we want it to display, size, how exactly this memory to protect  'Mmap' - parameters: place in memory where we want it to display, size, how exactly this memory to protect
Строка 128: Строка 117:
The creation:
   'crt_shm' - is the place where I will communicate. With my comrades, put the objects together, and my comrades will read them.
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.
The creation:

 . 'crt_shm' - is the place where I will communicate. With my comrades, put the objects together, and my comrades will read them.

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.
Строка 146: Строка 134:
    
Строка 157: Строка 145:
Строка 169: Строка 156:
    

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

  • as opposed to a signal that delivers everything immediately.
  • a message of correct size as opposed to a signal is just a set of bytes which is transmitted by the processor through a virtual object which represents a queue of these messages.
  • so when we get a message, it has a header that stores the system memory.
    • We read the message from some queue, get some memory and there it is and will be read there as soon as we call the function.

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:

  • Send a message using mq_send - with 4 pairs, queue to which to send, line with the message itself, message length, message priority

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

  • We can remove it ourselves - we can make a system call allowing you to work directly with file operations.

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.

  • !!!! About one funny thing - an alternative to reading from streaming input

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:

  • 'crt_shm' - is the place where I will communicate. With my comrades, put the objects together, and my comrades will read them.

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)