Question: Operating systems using linux. Below is the skeleton and the output. // Standard includes #include #include #include // IPC includes #include // signals // message
Operating systems using linux. Below is the skeleton and the output.
// Standard includes #include
// IPC includes #include
// message queues #include
// named pipes #include
//shared memory #include
// mmap files #include
#include
using namespace std;
// Generates a string of ASCII characters for use as data // Takes in an integer that specifies how many bytes/chars the string contains char * GenerateData (int numberOfBytes) { char * result = new char[numberOfBytes];
char value = 65; for (int i = 0; i < numberOfBytes; i++) { result[i] = value++;
if (value > 126) value = 65; }
result[numberOfBytes-1] = '\0';
return result; }
// Represents the available IPC options. enum IPCModes {NAMED_PIPE = 0, SHARED_MEMORY = 1, EXIT = 2};
// Returns the string representation for mode. const char* StrForMode (int mode) { switch (mode) { case SHARED_MEMORY: return "shared memory"; case NAMED_PIPE: return "named pipes"; case EXIT: return "exit"; default: return "unknown"; } }
// Message sent from parent to child to communicate ipc mode and data size struct IPCModeMessage { long type; //TODO: Add two integers called ipc_mode and msg_size. };
// Message sent from child to parent to communicate transfer result struct IPCResponseMessage { long type; //TODO: Add a Boolean called success. //The child sets this to true when the data string it receives matches the expected result. };
// Constants #define FIFO_FILE "CPSC351-PIPE" #define MMAP_FILE "CPSC-351-MMAP" #define BUFFER_SIZE 10
// This is an implementation of a bounded buffer. // It needs to be placed in a shared memory segment // that both the parent and child processes have access to. struct BoundedBuffer { private: int in; int out; char buffer[BUFFER_SIZE]; public: BoundedBuffer() :in(0), out(0) { } // Initialize bounded buffer void init () { in = 0; out = 0; } // Transmits data through the bounded buffer. // Blocks until it has sent size quanity of bytes. void producer (char* data, int size) { //TODO: Implement the producer code of the bounded buffer. //Refer to the chapter 3 slides for the code. } // Receives data from the bounded buffer and stores it in recv_data. // Blocks until it has received size quanity of bytes. void consumer (char* recv_data, int size) { //TODO: Implement the consumer code of the bounded buffer. //Refer to the chapter 3 slides for the code. } };
// Stores the generated string. char * data_string;
void parent (int size) { // TODO: Setup two System-V message queues for the parent and child processes. // One is to send messages from parent to child. The other sends messages from // child to parent. Refer to these system calls: msgget // Process loops until this is false. bool go = true; // This is the type ofIPC transfer the parent wants to do. int p_state = NAMED_PIPE; struct IPCModeMessage msg; msg.type = 1; do { msg.ipc_mode = p_state; msg.msg_size = size; switch (p_state) { case NAMED_PIPE: {
//TODO: Create the named pipe on the file system. Its name is represented by the FIFO_FILE constant. //Refer to these system calls: mkfifo. //TODO: Initiate a data transfer by sending a message to the child process using //a message queue. The message should use a IPCModeMessage to communicate the size of the data being //transferred and the IPC mode the parent is using. Refer to these system calls: msgsnd //TODO: Begin the data transfer to the child process using the named pipe created earlier. //This consists of opening the pipe for writing, writing the data to the pipe, and finally, //closing the pipe. Use these system calls: open, write, close. } break; case SHARED_MEMORY: { //TODO: Begin the data transfer to the child process by using a bounded buffer //located in shared memory segment with the name defined by MMAP_FILE. Make the shared memory segment //the minimum capacity required to store a BoundedBuffer struct. Store a BoundedBuffer //instance in the shared memory segment and call its init method. Use these //system calls: shm_open, ftruncate, mmap.
//TODO: Begin the transfer by sending a message to the child process and calling the producer method //of the bounded buffer located in the shared memory segment. Close the shared memory file descriptor //after the transfer completes. Use these system calls: msgsnd, close. } break; case EXIT: go = false; // TODO: Terminate the child process by sending it another IPCModeMessage with // ipc_mode set to EXIT. Then proceed to cleanly // end the parent process by freeing the message queues and the // shared memory segment. Use these system calls: msgsnd, msgctl, shm_unlink; wait(); // Wait for the child to exit. break; } IPCResponseMessage resp_msg; if (go) { // TODO: Wait for a message from the child process. The message will // indicate if the transfer was successful. Display the result // to the terminal for the user to see. Use these system calls: msgrcv if (true) { printf("[Parent] Error receiving message. "); p_state = EXIT; } else { if (resp_msg.success) { printf ("[Parent] Data Verification: PASSED "); } else { printf ("[Parent] Data Verification: FAILED "); } p_state++; } } } while (go); }
void child () { // TODO: Setup two System-V message queues for the parent and child processes. // One is to send messages from parent to child. The other sends messages from // child to parent. Refer to these system calls: msgget
bool go = true; IPCModeMessage msg; while (go) { //TODO: Wait to receive a IPCModeMessage message from the parent process. //Store the message in 'msg'. Use these system calls: msgrcv. if (false) { printf("[Child ] Error receiving message. "); } else { int size = msg.msg_size; printf("[Child ] Message received, mode %d (%s). ", msg.ipc_mode, StrForMode(msg.ipc_mode)); char* recv_data = new char[size]; switch (msg.ipc_mode) { case NAMED_PIPE: { //TODO: Receive data from the parent process using the named pipe. //The process consists of opening the pipe for reading, reading the data from the pipe //and finally, closing the pipe. Use these system calls: open, read, close. } break; case SHARED_MEMORY: { //TODO: Receive data from the parent using a bounded buffer in shared memory. //Open the shared memory segment with the name defined by MMAP_FILE, call the consumer function //on the BoundedBuffer within it, and close the segment when done. //Use these system calls: shm_open, mmap, close. } break; case EXIT: return; break; default: printf("[Child ] Unknown request %d ", msg.ipc_mode); break; } IPCResponseMessage resp_msg; resp_msg.type = 1; if (strcmp(recv_data, /*reference_string*/ data_string) == 0) { resp_msg.success = true; } else { resp_msg.success = false; } delete[] recv_data; // TODO: Send a message to the parent process that indicates the // result of the string comparison in a IPCResponseMessage. Use these system calls: msgsnd. } } }
int main(int argc, char **argv) { // TODO: Take in a command line parameter that represents the size, // in bytes, of the string that will be sent to the child process // using IPC. Command line parameters are passed to main in argv.
// TODO: Generate a string of characters of length specified by // the passed in parameter value and assign output to provided data_string pointer. // See provided GenerateData function. pid_t pid_child = 0; // TODO: Launch the child process. Display the pid of the parent to the // to the terminal. The child process should also display its pid after // launch. The parent process calls the parent method and the // child calls the child method. Use these system calls: fork, getpid. return 0; }
Below is the output:
[Parent] Created string of 200000000 bytes. [Parent] pid: 11862 [Parent] Sent message to child, mode 0 (named pipes) [Child ] pid: 11866 [Child ] Message received, mode 0 (named pipes). [Parent] Data Verification: PASSED [Parent] Sent message to child, mode 1 (shared memory) [Child ] Message received, mode 1 (shared memory). [Parent] Data Verification: PASSED Press ENTER to continue...
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
