I'm trying to finish up some programs I didn't get finished earlier in the semester and this multiprocessing program is stumping me.
"Instructions:
Set random delay times to be between 25 and 100 ms
This assignment will be to create a series of 3 processes that "pass" information from one to another. The producer program will read data from a file and periodically (based on a random delay) send it to the next program (the filter) using a message queue. The filter program will do a transformation to the data (implement the Caesar cipher with a shift of 5) and then periodically (based on a random delay) pass the data on to the consumer program through message queue 2. The consumer will read the data from the second message queue, perform a reverse transformation on the data, and store the results in an output file. If the program is being run through remote login (such as an SSH or PuTTY session), then only the transformed information (generated in the filter) should be printed to the console.
The idea behind the random delays is to make the process somewhat random and slow enough so that you can watch the transformations. See discussion below on generating delays.
To simplify the use of the message queues, and to manage the overall program, a fourth program (the main program) should be used to create the message queues and start up the above processes. The main program will take as a parameter the number of seconds that this program should run. The input file can be any regular text file (like something created in nano or Notepad, or even source code for a program). The input file should be named "input.txt" and the output file should be named "output.txt" . (Your main program could also take other command line parameters such as input and output file names, however you may hardcode these or query the user. ...) Your main program will then create the message queues and the processes.
It will use an alarm timer to determine when to shut down. (You may use the kill() or killall() functions to close down the created processes. )
The example programs that we used in our discussions of message queues (Linux Synchronization) might be useful in seeing how message queues might be used for this lab.
To make the assignment a little more manageable, the following is a pseudocode implementation of the filter program .
/*********************************************
*
* File Name: filter.c
*
* Author: Cotter
*
* This is part of exercise #5 from CS431
* This program will
* receive and transmit asynchronously by
* reading information in from a shared memory segment a
* character at a time, changing the case of
* the letter if it is alphabetic, and then
* outputting the character to an output SM.
*
**********************************************/
#include <iostream.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main ()
{
// Local variables
char input[25];
const int timeToProduce = 3;
// Announce existence.
cout << "Filter: Alive and running\n" << endl;
// Open connection to MQ 1, and to MQ 2
// Processing loop
while(1)
{
// Be sure to use sync mechanisms! (semaphore)
if (input = get a string from MQ1) == EOF)
jump out of loop
Sleep(rand( ) % timeToProduce);
// For each character "c" in "input"...
//Implement Caesar cipher (shift of 5)
//a ->f, b->g, c->h, etc.
c = c + 5;
// output results to console
// output results to MQ2
The idea behind introducing random waits in each of the processes is to use different random numbers (perhaps in the range of 0.025 to 0.1 seconds - use usleep()) in each program to simulate the asynchronous nature of the operation. It also slows down the process enough that you can observe the changes in the data.
You are to create separate producer, consumer, and filter programs, as well as the main program that creates the message queues and processes. Be sure to document your code (internally or externally) well enough that it is easy to follow what you are trying to do. I will use the assignment evaluation form for grading.
Extra Credit: (10 Points)
If all of the required features of the program work correctly, then you can get an additional 10 points by using signals (SIGUSR1 ?) to terminate the processes gracefully ( close all MQs and files before exiting).
Items to turn in:
Source code for main program, producer, consumer and filter programs. You MUST have documentation (either embedded as comments or external) to explain what you did and how you did it.
Your source files should be named: CS431_yourLastName_producer.cpp, CS431_yourLastName_filter.cpp, and CS431_yourLastName_consumer.cpp, CS431_yourLastName_proj5.cpp. For this assignment, you should build 4 separate files, not a single big file."
I read over the entire think multiple times and just making 4 programs that communicate still confuses me. I know how to make 2 communicate but not 4. Any suggestions of pseudo code for how to do/start these programs?
The main program (#4) is responsible for creating the two message queues, and launching the other 3 programs. Main prog pass the filename to publisher. The publisher will read the file, push a char onto the message queue 1. The filter watches msg queue 1, reads, encrypts/processes, and pushes it onto msg queue 2. Consumer watches msg queue 2, decrypts/processes, writes to output file.
Ahh I see that makes it make more sense. I was looking up some code that might assist me in making message queues and I found this which looked helpful. http://linuxfocus.berlios.de/English/March2003/article287.shtml Is this sorta what I'm supposed to be trying to do or is this completely wrong?