## 4 Cooperative Resource Management Algorithm
### Cooperative Resource Management Algorithm  
The data cached from C and the computation offloading for each MT is managed by MEC-BS using Cooperative Resource Management Algorithm, which is responsible for monitoring and collecting the information (computation offloading requests and responses, data caching requests and responses, parameters of MTs, MEC-NBS, and C), running the optimization algorithm, and sending the optimization result to C and each MT. MEC-BS will send the selection probability in the optimization result to the MT and C to determine the place (C, MEC-BS, or MEC-NBS), which is selected to execute the task or cache the data. At initialization of the system, all MTs in \(\mathcal{M}\) upload their required parameters, including \(\forall i\in\mathcal{M},\forall j\in\mathcal{R},p_{i,j}\), \(s_{i}^{\text{MT2 BS}}\), \(s_{i}^{\text{BS2 MT}}\), \(e_{i}\), and the information of all its tasks (\(<c_{j},v_{j},v_{j},z_{j},q_{j}>\)), to MEC-BS. MEC-BS also collects the required parameters of MEC-NBS, including \(\forall k\in\mathcal{K},q_{k},s_{k}^{\text{BS2 NBS}}\), and \(s_{k}^{\text{NBS2 BS}}\). The required parameters of C need to be sent to MEC-BS, as well, including \(eta\), \(s_{i}^{\text{BS2C}}\), and \(s_{i}^{\text{C2 BS}}\), and parameters between C and MEC-NBS \(s_{k}^{\text{NN82C}}\) and \(s_{k}^{\text{C2 NBS}}\) should also be sent. During the system running, as for MTs, MEC-NBS, and C, once the value of any parameters of them has changed, they need to report the new value to MEC-BS. So, it can ensure the real-time and correctness of the parameter values in MEC-BS. MEC-BS monitors periodic observation of changes in all parameters. It will run Algorithm 1 in the next period, if any parameter has changed. Periodic monitoring reduces the execution frequency of the algorithm, while ensuring the timeliness of the optimization results. The flowchart of the Cooperative Resource Management Algorithm is shown in Figure 3. The details of the algorithm are described in Algorithm 2 for MT side, Algorithm 3 for C side, Algorithm 4 for MEC-NBS side, and Algorithm 5 for MEC-BS side. The four algorithms are composed of twelve loops, _L1-L12_; these loops are deployed into separate processes and executed in parallel during the running period of the system. Figure 3: Flowchart of the Cooperative Resource Management Algorithm. ```
Initial: set \(\mathbf{H}=0\), send \(p_{i,j}\), \(e_{i}\), \(s_{i}^{\text{MT2 BS}}\), \(s_{i}^{\text{BS2 MT}}\) and the profiles of all its tasks to MEC-BS. \(L1\): if any required parameters of \(m_{i}\) changes then \(m_{i}\) sends the new value of the parameter to MEC-BS; endif \(L2\): if\(m_{i}\) receives \(\mathbf{H_{i}}\) from MEC-BS then \(m_{i}\) updates the value of \(\mathbf{H_{i}}\) stored in \(\mathbf{H}\); endif \(L3\): if\(m_{i}\) has a new \(r_{j}\)then \(m_{i}\) generates a random number \(h\in[0,1]\) based on Uniform distribution; \(b1=0,b2=0\); for\(b=1\) to \((M*R+M*R*K)\)do if\(h\leq\mathbf{H_{n}}\)then if\(b\leq(M*R)\)then \(b1=b\); else\(b2=b\); endif break; endif if\((b1==0)\)&\((b2==0)\)then \(m_{i}\) executes \(r_{j}\) at C; elseif\((b1\;!=\;0)\)then \(m_{i}\) offloads \(r_{j}\) to MEC-BS; else \(m_{i}\) offloads \(r_{j}\) to \(n_{b2}\); endif \(L4\): if\(m_{i}\) has a new \(r_{j}\)then \(m_{i}\) generates a random number \(c\in[0,1]\) based on Uniform distribution; \(b3=0,b4=0\); for\(b{=}(M*R+M*R*K)\) to \((M*R+M*R*K+R+R*K)\)do if\(c\leq\mathbf{H_{b}}\)then if\(b\leq(M*R+M*R*K+R)\)then \(b3=b\); else\(b4=b\); endif break; endif endfor if\((b3==0)\)&\((b4==0)\)then  the data required by \(r_{j}\) does not be cached anywhere; elseif\((b3\;!=\;0)\)then  the data required by \(r_{j}\) is cached in MEC-BS from C; else  the data required by \(r_{j}\) is cached in \(n_{b4}\) from C; endif endif
```  
**Algorithm 2** Cooperative Resource Management Algorithm at \(m_{i}\) side
```
Initial: send \(\eta\), \(s_{i}^{\text{BS2C}}\), \(s_{i}^{\text{C2 BS}}\), \(s_{k}^{\text{NBS2C}}\) and \(s_{k}^{\text{C2 NBS}}\) to MEC-BS.