Question

In: Computer Science

Write a c++ Program To simulate the messages sent by the various IoT devices, a text...

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.

Solutions

Expert Solution

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:


Related Solutions

Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
Question 6 (1 point) The number of text messages sent by a random sample of students...
Question 6 (1 point) The number of text messages sent by a random sample of students at a local university was collected and the following histogram was created: Which of the following statements is likely true: Question 6 options: The distribution of number of text messages sent is approximately symmetric, so the mean is approximately the same as the median. The distribution of number of text messages sent is skewed to the left, so the mean is likely less than...
3. In order to approximate the number of text messages sent daily by Swedish teenagers, Neu...
3. In order to approximate the number of text messages sent daily by Swedish teenagers, Neu Star Communications took a random sample of Swedish youth, finding the number of text messages they sent on a given day in 2018: HOURS 78 86 86 79 84 79 89 86 86 84 79 81 What is the point estimate of the population mean? (Round your answer to 2 decimal places.) ESTIMATED POPULATION MEAN: …..............? Develop a 98% confidence interval for the population...
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling...
In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling algorithm. It should implement threads for the algorithm plus the main thread using a linked list to represent the list of jobs available to run. Each node will represent a Job with the following information: int ProcessID int time time needed to finish executing The thread representing the scheduler algorithm should continue running until all jobs end. This is simulated using the time variable...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT