Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Experiment with the programs in Example 46-1 (svmsg_create.c), Example 46-2 (svmsg_send.c), and Example 46-3 (svmsg_receive.c) to confirm your understanding of the msgget(), msgsnd(), and msgrcv() system calls.
Recode the sequence-number client-server application of Section 44.8 to use System V message queues. Use a single message queue to transmit messages from both client to server and server to client. Employ the conventions for message types described in Section 46.8.
In the client-server application of Section 46.8, why does the client pass the identifier of its message queue in the body of the message (in the clientId field), rather than in the message type (mtype)?
Make the following changes to the client-server application of Section 46.8:
Replace the use of a hard-coded message queue key with code in the server that uses IPC_PRIVATE to generate a unique identifier, and then writes this identifier to a well-known file. The client must read the identifier from this file. The server should remove this file if it terminates.
In the serveRequest() function of the server program, system call errors are not diagnosed. Add code that logs errors using syslog() (Section 37.5).
Add code to the server so that it becomes a daemon on startup (Section 37.2).
In the server, add a handler for SIGTERM and SIGINT that performs a tidy exit. The handler should remove the message queue and (if the earlier part of this exercise was implemented) the file created to hold the server’s message queue identifier. Include code in the handler to terminate the server by disestablishing the handler, and then once more raising the same signal that invoked the handler (see Section 26.1.4 for the rationale and steps required for this task).
The server child doesn’t handle the possibility that the client may terminate prematurely, in which case the server child would fill the client’s message queue, and then block indefinitely. Modify the server to handle this possibility by establishing a timeout when calling msgsnd(), as described in Section 23.3. If the server child deems that the client has disappeared, it should attempt to delete the client’s message queue, and then exit (after perhaps logging a message via syslog()).
The client shown in Example 46-9 (svmsg_file_client.c) doesn’t handle various possibilities for failure in the server. In particular, if the server message queue fills up (perhaps because the server terminated and the queue was filled by other clients), then the msgsnd() call will block indefinitely. Similarly, if the server fails to send a response to the client, then the msgrcv() call will block indefinitely. Add code to the client that sets timeouts (Section 23.3) on these calls. If either call times out, then the program should report an error to the user and terminate.
Write a simple chat application (similar to talk(1), but without the curses interface) using System V messages queues. Use a single message queue for each client.