In: Computer Science
Write a c++ Program
To simulate the messages sent by the various IoT devices, a text le 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++
program will read this le line by line. It will separate the message into device name,
status value and epoch value and store it in a suitable struct. For each line, a new
thread will be created. The ID of the created thread will be added to the struct which
will then be sent to the new thread. The thread will convert the epoch value to a date
and time string and print all the elements of the struct.
Implementation:
The following steps show how your program should work:
1. Your program should rst request the name of a IoT device data input le.
2. It should then open the provided IoT device data input le and read the input le
line by line.
3. Thereafter it should separate the line into values and store the data in a suitable
struct.
4. For each input line, it should create a new thread with the pthread create()
function.
5. The program then should send the data to the newly created thread via the struct.
6. The thread then should transform the epoch value and print its own thread id as
well as all the data values from the struct.
IMPORTANT
-You have to dene a struct that contains at least the following elements: device
name, status value and epoch time.
-You are not allowed to send the thread id via the struct. Use the pthread self()
function to determine the thread id.
-You have to use the pthread create() function to create threads.
-You will need to use the command line option -pthread with the gcc/g++ com-
piler to successfully compile your program with the Pthread library.
-You have to print verbose output to clearly show what task each thread performs.
PROGRAM :
#include<iostream>
#include<pthread.h>
#include<fstream>
#include<sstream>
#include<stdlib.h>
#include<string.h>
#include<time.h>
using namespace std;
/*Structure definition*/
typedef struct deviceInfo{
string devicename;
string status;
string epoch;
int id;
}deviceInfo;
//function prototype of subroutine which will be passed to
pthread_create()
void *handler(void* info);
//Main function definition
int main(){
string filename,line;
fstream file;
int i=0;
deviceInfo info,*info_ptr;
pthread_t th_id;
cout<<"Enter file name: ";
cin>>filename;
file.open(filename.c_str()); //Opening the file
//Error handling of file open operation
if(!file.is_open()){
cout<<"Error in opening the file
"<<filename.c_str()<<endl;
return 0;
}
//Reading file line by line
while(getline(file,line)){
stringstream ss(line);
getline(ss,info.devicename,',');
getline(ss,info.status,',');
getline(ss,info.epoch,',');
info.id = ++i;
//thread creation and it's error handling
if(pthread_create(&th_id,NULL,handler,(void*)&info) <
0){
perror("Thread creation failed!!\n");
return 1;
}else{
cout<<"Created thread
"<<pthread_self()<<endl;
}
pthread_join(th_id,NULL); //Wait for completion of the thread id
th_id
}
return 0;
}
/*Definition of subroutine which will be responsible to convert
epoch
to date and time formate and display ech device info */
void *handler(void* info){
deviceInfo dInfo;
time_t tt =0;
unsigned long epoch =0;
char timestamp[64] = "";
dInfo = *(deviceInfo *)info;
stringstream (dInfo.epoch) >> epoch; //converting string to
unsigned long
tt = epoch;
strftime(timestamp,64,"%c",localtime(&tt)); //converting epoch
to date and time
cout<<"Thread "<<dInfo.id<<" with id
"<<pthread_self()<<" received message:
"<<endl;
cout<<"Device: "<<dInfo.devicename<<endl;
cout<<"Status: "<<dInfo.status<<endl;
cout<<"Epoch: "<<timestamp<<endl;
}
ScreenShots:
Output: