Do different Linux message queues have the same identifier?

I open the queue for mesage in the .c file, and if successful, it says that the message queue identifier is 3. While this program is still running, in another terminal I run another program (from another .c file) that creates a new message queue with another mqd_t. But its id also appears as 3. Is this a problem?

The server file is as follows:

void server(char* req_mq) { struct mq_attr attr; mqd_t mqdes; struct request* msgptr; int n; char *bufptr; int buflen; pid_t apid; //attr.mq_maxmsg = 300; //attr.mq_msgsize = 1024; mqdes = mq_open(req_mq, O_RDWR | O_CREAT, 0666, NULL); if (mqdes == -1) { perror("can not create msg queue\n"); exit(1); } printf("server mq created, mq id = %d\n", (int) mqdes); 

and the client is as follows:

 void client(char* req_mq, int min, int max, char* dir_path_name, char* outfile) { pid_t pid; /* get the process id */ if ((pid = getpid()) < 0) { perror("unable to get client pid"); } mqd_t mqd, dq; char pfx[50] = DQ_PRFX; char suffix[50]; // sprintf(suffix, "%d", pid); strcat(pfx, suffix); dq = mq_open(pfx, O_RDWR | O_CREAT, 0666, NULL); if (dq == -1) { perror("can not open data queue\n"); exit(1); } printf("data queue created, mq id = %d\n", (int) dq); mqd = mq_open(req_mq, O_RDWR); if (mqd == -1) { perror("can not open msg queue\n"); exit(1); } 

mqdes and dq seem to have the same identifier 3.

+4
source share
3 answers

You may not have closed the first message queue. because in this situation, os gives the same identifier (index) to the new one.

0
source

Think of the descriptor as an array index. An index into an array stored in the operating system, one for each process in the system.

When you open a descriptor (be it a file, message queue, socket, etc.), the operating system writes the parameters for this object to an array unique to your process. Then, the operating system returns the index into this array in your program.

Every time your program uses this "descriptor", the operating system really looks for a structure in this private array, it continues to figure out how to handle the object associated with this "descriptor".

Linux typically reserves descriptors 0, 1, and 2 for STDIN, STDOUT, and STDERR, respectively. But from then on, all open handles will be numbered 3, 4, and so on. And your handle 3 may refer to the file /tmp/foo.txt, and another handle to process 3 may refer to the file /tmp/bar.txt. Therefore, if two processes use similar file handles, it does not matter.

By the way, you do not need to know or care about what the descriptor actually contains. Theoretically, it could be anything - a magic number, a pointer, an integer, it does not matter. The handle is really a secret token that you simply pass to the operating system anytime you want to access the system object.

+3
source

Message queues differ in the name you give them when you open the message queue using mq_open(3) . The message queue descriptors returned by mq_open(3) make sense only within the scope of the process. In other words, what other process matters to the message queue descriptor is completely irrelevant to another process. This is completely analogous to file paths and file descriptors. Indeed, Linux is special in the sense that message queue descriptors are actually file descriptors - see the Linux mq_overview(7) page mq_overview(7) . This is an explanation of the total value of 3 that you see when you open the message queue in two processes (in the most typical case, 0, 1, 2 were already opened with the goal of providing STDIN , STDOUT and STDERR as PP. Already mentioned in his answer).

0
source

Source: https://habr.com/ru/post/1303396/


All Articles