###### tags: `作業系統`
# *2022/10/11 作業_Version01*
## 題目:
Please write a program that has multiple threads, which two or more threads run at the same time.
## 程式設計:
建立兩個Process,然後輪流進入critical section,兩個Process是透過取亂數的方式,兩個Process都是讀取亂數(每次抓取1個->2個->3個......etc),然後加入critical section並且排序,結束後再把使用權交給另一個Process,然後最多超過預設的執行次數後結束並印出結果
### 程式碼
```
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <ctime>
#include <sys/types.h>
using namespace std;
/*This program virtual two processes and push different sequence number into a vector, which vector is sorted. Once only a process can sort this vector. If one process finish, this process need to release the control permission to the other.*/
void process_1 (void);
void process_2 (void);
int random_generate_function(void);
vector<int> critical_section;
int process_1_times=1;
int process_2_times=1;
int turn=1;
int times=1;//For random seed
int main(int argc, const char * argv[]) {
cout<< "Operation Start......"<<endl;
int execution_times =0;
int MAX =10;//The max execution times!
while (execution_times < MAX){
if (turn==1){//Process_1 is operating, so Process_2 is waitting.
cout<<"Process_2 is waitting!"<<endl;
cout<<"Process_1 start!"<<endl;
process_1();
cout<<"Process_1 finish!"<<endl;
cout<<"Current critical_section:"<<endl;
for (int i=0;i<critical_section.size();i++){
cout<<critical_section[i]<<" ";
}
cout<<"\n";
cout<<"Turn Process_2!"<<endl;
cout<<"\n";
}
else{//Process_2 is operating, so Process_1 is waitting.
cout<<"Process_1 is waitting!"<<endl;
cout<<"Process_2 start!"<<endl;
process_2();
cout<<"Process_2 finish!"<<endl;
cout<<"Current critical_section:"<<endl;
for (int i=0;i<critical_section.size();i++){
cout<<critical_section[i]<<" ";
}
cout<<"\n";
cout<<"Turn Process_1!"<<endl;
cout<<"\n";
}
execution_times++;
}
cout<<"=============================================="<<endl;
cout<<"\nFinish"<<endl;
cout<<"The final output:"<<endl;
for (int i=0;i<critical_section.size();i++){
cout<<critical_section[i]<<" ";
}
cout<<"\n"<<endl;
return 0;
}
void process_1(void){
int execution = 0;
while(execution < process_1_times){
int num = random_generate_function();//Generate random number
critical_section.push_back(num);
sort(critical_section.begin(), critical_section.end());
execution++;
times++;
}
process_1_times++;//Next time the random number will increase
turn = 2;//Release the control permission
return;
}
void process_2(void){
int execution = 0;
while(execution < process_2_times){
int num = random_generate_function();//Generate random number
critical_section.push_back(num);
sort(critical_section.begin(), critical_section.end());
execution++;
times++;
}
process_2_times++;//Next time the random number will increase
turn = 1;//Release the control permission
return;
}
int random_generate_function(void){
srand((unsigned)time(NULL));//Seed of random number
int num = rand()/times;//Generate random number
return num;
}
```
### 輸出結果:
```
Operation Start......
Process_2 is waitting!
Process_1 start!
Process_1 finish!
Current critical_section:
2129435188
Turn Process_2!
Process_1 is waitting!
Process_2 start!
Process_2 finish!
Current critical_section:
1064717594 2129435188
Turn Process_1!
Process_2 is waitting!
Process_1 start!
Process_1 finish!
Current critical_section:
532358797 709811729 1064717594 2129435188
Turn Process_2!
Process_1 is waitting!
Process_2 start!
Process_2 finish!
Current critical_section:
354905864 425887037 532358797 709811729 1064717594 2129435188
Turn Process_1!
Process_2 is waitting!
Process_1 start!
Process_1 finish!
Current critical_section:
236603909 266179398 304205026 354905864 425887037 532358797 709811729 1064717594 2129435188
Turn Process_2!
Process_1 is waitting!
Process_2 start!
Process_2 finish!
Current critical_section:
177452932 193585017 212943518 236603909 266179398 304205026 354905864 425887037 532358797 709811729 1064717594 2129435188
Turn Process_1!
Process_2 is waitting!
Process_1 start!
Process_1 finish!
Current critical_section:
133089699 141962345 152102513 163802706 177452932 193585017 212943518 236603909 266179398 304205026 354905864 425887037 532358797 709811729 1064717594 2129435188
Turn Process_2!
Process_1 is waitting!
Process_2 start!
Process_2 finish!
Current critical_section:
106471759 112075536 118301954 125260893 133089699 141962345 152102513 163802706 177452932 193585017 212943518 236603909 266179398 304205026 354905864 425887037 532358797 709811729 1064717594 2129435188
Turn Process_1!
Process_2 is waitting!
Process_1 start!
Process_1 finish!
Current critical_section:
85177407 88726466 92584138 96792508 101401675 106471759 112075536 118301954 125260893 133089699 141962345 152102513 163802706 177452932 193585017 212943518 236603909 266179398 304205026 354905864 425887037 532358797 709811729 1064717594 2129435188
Turn Process_2!
Process_1 is waitting!
Process_2 start!
Process_2 finish!
Current critical_section:
70981172 73428799 76051256 78867969 81901353 85177407 88726466 92584138 96792508 101401675 106471759 112075536 118301954 125260893 133089699 141962345 152102513 163802706 177452932 193585017 212943518 236603909 266179398 304205026 354905864 425887037 532358797 709811729 1064717594 2129435188
Turn Process_1!
==============================================
Finish
The final output:
70981172 73428799 76051256 78867969 81901353 85177407 88726466 92584138 96792508 101401675 106471759 112075536 118301954 125260893 133089699 141962345 152102513 163802706 177452932 193585017 212943518 236603909 266179398 304205026 354905864 425887037 532358797 709811729 1064717594 2129435188
Program ended with exit code: 0
```