Thanks it compiles now.
I'm having some trouble as well with pipes and forks, so if I could borrow your brain again that be amazing ?
In this task you are required to create and terminate
various processes using process spawning. These processes need to communicate with
each other using pipes. You will need to read up on pipes and interprocess communica-
tion (IPC). You can consult the following link for some basic information about pipes
(
http://www.linfo.org/pipes.html).
For this task you will have to write a C/C++ program that simulates an IoT (Internet
of Things) controller. Any number of IoT devices can connect to this controller and
report their status. The controller will thus have a complete picture of all the IoT
devices that have reported to it, the current status of those devices and when last the
status changed/updated.
To simulate the messages sent by the various IoT devices, a text file called device data.txt
is provided that contains a number of device messages sent, with each line representing
a distinct message. Each message contains a device name, a status value and a Unix
epoch value which are separated by commas. Your C/C++ program will read this file
line by line and sent the contained message to a new child process. The child process will
separate the message into device name, status value and epoch value, convert the epoch
to a date and time string and send the data back to the parent process. The parent
process will use this data to create and maintain a IoT device list with associated status
value and time.
Here is my code so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
#include <unistd.h>
using namespace std;
main ()
{
string fileName;
int line=0;
string deviceData;
ifstream input;
pid_t pid;
cout<<"Enter the file name: ";
cin>>fileName;
input.open(fileName.c_str());
if (!input)
{
cout<<"Couldn't open file"<<endl;
}
else
{
pid = fork();
if (pid == 0)
{
//child process
cout<<"Child Process"<<endl;
}
else if (pid > 0)
{
//parent process
cout<<"Parent Process"<<endl;
while (input>>deviceData)
{
cout<<"Parent sent file line "<<line<<endl;
cout<<deviceData<<endl;
line++;
}
}
else
{
//fork failed
cout<<"Failed"<<endl;
return 1;
}
}
input.close();
return 0;
}
|
There is an example of what the output should be but its an image. If need be I can type out the example and post it. Just let me know ?