Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program is in C . In this assignment you are to simulate the notion of several \

ID: 3707418 • Letter: P

Question

Program is in C. In this assignment you are to simulate the notion of several "distributed" temperature sensors that feed data to a single source. Each sensor (really a thread) is responsible for updating a place in a linked list of temperatures initialized by the parent program (the single source). The temperatures can be positive, negative or zero positive.

The main thread creates a global linked list (you create the struct for each node, initialize a pointer to this list with the value NULL and allow the thread to add a node to the shared growing linked list), then creates all the sensor threads (the number of sensors, and thus threads, can be an argument passed on the command line to main or read in by main from the user) one by one and passes to each sensor thread a struct that contains an integer seed for the random number and an ID number. When all the threads complete a cycle (which is placing the temperature in the linked list), the main thread prints each temperature in the linked list of temperatures and the average temperature. Each thread does this 10 times (we will call each time a cycle, thus there are 10 cycles) and terminates. However, each thread can only put a number in the shard linked list only ONCE during each of the 10 cycles. The critical section of this problem is the modification of the shared linked list as each thread will need to create a new node, fill it with a temperature value and insert the node into the shared linked list. The functions to create a node, insert a node and any other node functions are all global functions that each thread can access (hopefully safely with mutual exclusion).

However, there can be race conditions as each thread attempts to access the shared linked list. You will need to use some mechanism (perhaps a mutex, or better yet a conditional variable/mutex mix) to ensure that the linked list is updated correctly. Each sensor thread is responsible for acquiring a temperature from its sensor. We will emulate this by taking the using a seed integer value from the parent thread, use it in srand() in the main thread and then calling rand() % 100 in the sensor thread (you can refer to the examples how to do this). The sensor thread then must place its temperature in the global linked list, and then wait. Remember, each thread can only put a number in ONCE during each of the 10 cycles and needs to wait until the cycle completes, which is when all the threads have entered a value. So, you need to somehow make sure that after a thread updates the linked list, it does not do it again until the next cycle and it waits until the cycle is complete. Also, remember to protect this linked list from each sensor.Also, after the cycle, the list needs to reset to NULL and the process starts over.

Explanation / Answer

#include "contiki.h"

#include "dev/temperature-sensor.h"

#include "board.h"

#include <stdio.h> /* For printf() */

/*---------------------------------------------------------------------------*/

PROCESS(temp_process, "Temperature process");

AUTOSTART_PROCESSES(&temp_process);

/*---------------------------------------------------------------------------*/

PROCESS_THREAD(temp_process, ev, data)

{

static struct etimer etimer;

  

PROCESS_BEGIN();

boardPrintStringDescription();

printf("Starting measuring temperature ");

while(1) {

etimer_set(&etimer, CLOCK_SECOND);

  

PROCESS_WAIT_UNTIL(etimer_expired(&etimer));

  

unsigned int temp = temperature_sensor.value(0);

printf("Temp: %d.%d °C ",temp/10,temp-(temp/10)*10);

}

  

  

PROCESS_END();

}