## 5 The TEMS Algorithm  
The heuristic of the scheduling algorithm for time and energy minimizing was developed to execute a dynamic cost minimization model with reduced computational cost. Algorithm 1 exhibits the steps of TEMS. Step 1 is the configuration detection round that forms a data set of IoT devices, MEC servers, and configuration of communication channels. The battery levels of the IoT devices are collected, and the LSL is established. The algorithm regards the number of cores into CPU available in each IoT device and MEC server, the operating frequency, and operating voltages. This process could also occur in CC data centers, but the number of CPUs is expected to be unlimited. A loop from step 2 to step 4 repeats until all tasks are distributed across the processing infrastructure. The scheduler observes hardware conditions, energy consumption, and performance of application execution. ```
Result: Task mapping to the processing nodes
1 execute Step 1: Task of information collection and system setup
2 repeat
3 execute Step 2: Task allocations
4 execute Step 3: Task conclusion monitor
5 execute Step 4: New tasks and device battery level monitor
6 until user decides to keep running
```  
**Algorithm 1**TEMS. Step 3 monitors the task completion status. When one task was completed, the CPU core resources are released to turn available for new allocations in step 2. However, tasks that use CC resources do not need to release them since CC is supposed to have unlimited resources, absorbing any number of tasks. Task cancellation may occur if the elapsed time is higher than the deadline or if the IoT device runs out of battery. Finally, in step 4, the battery level from each IoT device is collected, and after that, it creates new tasks again. Execution continues as long as tasks are being created. Algorithm 2 details step 2 from task allocations, which is the task allocation decision-making process of the scheduler. Here, tasks are first classified between critical and regular. ```
1 foreachtask\(A_{i}\)from list of critical tasksdo
2 foreachfreeCPUcore\(pl_{j,k}\in PL_{j}\)do
3 policy 1: it calculates IoT device execution time
4 foreachfreeCPUcore\(ps_{j,k}\in PS_{j}\)do
5 policy 2: it calculates MEC server execution time and transmission times
6 policy 3: it calculates CC execution time and transmission times
7 it evaluates IoT battery level and offload task to the CPU core with minimal total time
8 foreachtask\(A_{i}\)from list of regular tasksdo
9 foreachfreeCPUcore\(pl_{j,k}\in PL_{j}\)do
10 policy 1: it calculates energy consumption, execution time, and cost for the IoT device
11 foreachfreeCPUcore\(ps_{j,k}\in PS_{j}\)do
12 policy 2: it calculates energy consumption for dynamic processing and data transmission
13 policy 2: it calculates execution time and transmission times
14 policy 2: it calculates MEC server cost
15 policy 3: it calculates energy consumption for dynamic processing and data transmission
16 policy 3: it calculates execution time and transmission times
17 it evaluates IoT battery level and offload task to the CPU core with minimal cost
```  
**Algorithm 2**Task allocations  
Three policies are defined for task execution: Policy 1, the tasks are executed in local IoT devices; Policy 2, the tasks are offloaded from IoT devices to the MEC server and executed in the MEC server; Policy 3, the tasks are executed in the cloud. The time and energy consumption for task processing on the different CPU cores of the network is calculated, as well as the time and energy consumption of the data transmissions for MEC servers and CC data centers. Critical tasks are the first to be scheduled due to the execution deadline. The tasks are launched from the lower to higher deadline and allocated to produce the lowest total elapsed time considering latency and the channel bandwidth availability. The regular tasks are ordered by creation time and allocated by the minimum total cost. The battery level of IoT devices is continuously evaluated in lines 7 and 18 to check if the energy constraints are respected.