Question: Hi I am studying threads and came accross this program. How would you write this? In this task you are required to create various threads
Hi I am studying threads and came accross this program.
How would you write this?
In this task you are required to create various threads using the POSIX Pthread API. These threads can receive data from the main thread via a struct.
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 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/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. 6 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. An extract of what the program should display, with user input: 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++ compiler to successfully compile your program with the Pthread library. You have to print verbose output to clearly show what task each thread performs. Make a makele for yourself. It will speed up your work-ow.
example output:

Device_data.txt :
FrontDoor,Closed,1532700000 LivingLight,Off,1532700000 LivingTemp,19,1532700000 LivingTemp,18.5,1532703600 FrontDoor,Open,1532707200 LivingTemp,18,1532707200 LivingLight,On,1532707203 FrontDoor,Closed,1532707205 LivingTemp,18.1,1532710800
user make run /threads Enter file name: device_data.txt Created thread 3077655360 Thread 1 with id 3077655360 received message: DeviceFrontDoor Value - Closed Time - 16:00: 00 27/07/2018 Created thread 3077655360 Thread 2 with id 3077655360 received message: Device = LivingLight Value = Off Time = 16:00:00 27/07/2018 Created thread 3077655360 Thread 3 with id 3077655360 received message: Device LivingTemp Value - 19 Time = 16:00:00 27/07/2018 Created thread 3077655360 Thread 4 with id 3077655360 received message: Device = Living Temp Value - 18.5 Time - 17:00: 00 27/07/2018
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
