当前位置:网站首页>(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
2022-07-06 12:33:00 【Vigorous waist Nuo dance】
subject
1-4 Messaging interface (100 branch )
The teacher gave it T Share MPI Sample code for , Each code implements n Process communication . These processes are labeled from 0 To n − 1, Each process will execute its own send and receive instructions in sequence , Such as :“S x”,“R x”.“S x” To express to x Send data to process No ,“R x” From x No. process receives data . Each pair of send / receive commands must be executed to take effect , Otherwise “ Deadlock ”. for instance ,x Process No. 1 executes the send command first “S y”,y No. process must . Must be . Execute the transfer command “R x”, This pair of commands were successfully executed . otherwise x No. process will be waiting y No. process executes the corresponding receive command . conversely , if y Process No. 1 executes the receive command first “R x”, Will wait x No. process executes the send command “S y”, if x No. process has not executed the send command “S y”, be y No. process will be waiting x No. process executes the corresponding send command . The mismatching of the above sending and receiving commands will cause the whole program to appear “ Deadlock ”. in addition ,x No. process will not execute “S x” or “R x”, That is, they will not send or receive messages from their own processes .. Now the teacher asks you to judge whether each sample code will appear “ Deadlock ” The situation of . Each process has at least 1 strip , At most 8 strip , These instructions are executed sequentially , That is, the execution of Article 1 is completed , To implement Article 2 , Go to the last one .
Input format :
Reading data from standard input . Enter two positive integers on the first line T, n, Express T Sample code , Realized n Process communication . Next there is T × n That's ok , There are several in each line (1 − 8 individual ) character string , There is a space between the adjacent , Indicates the receiving and sending instructions of the corresponding process . There are no illegal instructions . For the first 2 + i, 0 ≤ i ≤ (T × n − 1) That's ok , It means the first one i ÷ n( merchant ) Of the code i KQ/ n( remainder ) Send and receive instructions for process No . ( such as ,“S1” To express to 1 Send message to process No ,“R1” From 1 Message received by process No . Please refer to the example for details .)
Output format :
Output to standard output . The output, T That's ok , One number per line , Indicates whether the corresponding sample code appears “ Deadlock ” The situation of .1 Indicates deadlock ,0 It means no dead lock .
sample input :
3 2
R1 S1
S0 R0
R1 S1
R0 S0
R1 R1 R1 R1 S1 S1 S1 S1
S0 S0 S0 S0 R0 R0 R0 R0
sample output :
0
1
0
author
Jingrong
Company
Yanshan University
Code length limit
16 KB
The time limit
400 ms
Memory limit
64 MB
Ideas
Simulation question
Refer to others' ideas
Because it involves the deletion and addition of a thread
use list Delete is more efficient
Code
// There are no illegal instructions
//x No. process will not execute S x
// Sequential execution
// At least one instruction Up to eight
// Input format i KQ/ n( remainder ) Number
using namespace std;
#include<unordered_set>
#include<bits/stdc++.h>
int main() {
int t, n;
cin >> t >> n;
getchar();
for (int z = 0; z < t; z++) {
//t Copy code
unordered_set<int>waiting;// Waiting for the
list < pair<queue<int>, int>>pool;// Thread pool
for (int i = 0; i < n; i++) {
//n Threads
pool.push_back({
queue<int>(),INT_MAX });// Make room for new threads
string temp;// A whole line of instructions
getline(cin, temp);// Each thread corresponds to a string of instructions
// Split instruction
// When there is only one instruction , Finally, there are no spaces
for (int j = 0,c; j < temp.size(); j = c + 1) {
c = temp.find(" ", j);
if (c== string::npos) {
c = temp.size();
}
if (temp[j] == 'S') {
// send out
pool.back().first.push(i*10000+stoi(string(temp.begin() + j + 1, temp.begin() + c)));// The scope may
}
else {
// Accept
pool.back().first.push(-i - 10000*stoi(string(temp.begin() + j + 1, temp.begin() + c)));// The scope may be wrong
}
}
}
bool flag = true;
while (flag && !pool.empty()) {
// There are still
for (auto i = pool.begin(); i != pool.end(); i++) {
// There is at least one match in a trip , Otherwise, it will be locked
if (waiting.find(i->second) != waiting.end()) {
// The process is blocking , next
continue;
}// Pay attention to the first two if The order of
else if ((i->first).empty()) {
// The process has no instructions to continue
i->second = 0;
continue;
}
else {
while (!(i->first).empty()) {
// The instruction is not empty , Just out of the team , Until no match is found or the instruction is gone
int getout =( i->first).front();
(i->first).pop();
if (waiting.find(-getout) != waiting.end()) {
// Find a match
waiting.erase(-getout);
flag = false;
}
else {
i->second = getout;
waiting.insert(getout);
break;
}
}
}
}
if (flag == true)break;
else flag = true;
}
bool pass = true;
if (waiting.empty()) {
for (auto i = pool.begin(); i != pool.end(); i++) {
if (i->second != 0) {
pass = false;
break;
}
}
}
else pass = false;
if (pass)cout << "0" << endl;
else cout << "1" << endl;
}
return 0;
}
边栏推荐
- MySQL takes up too much memory solution
- [leetcode622] design circular queue
- 2021.11.10汇编考试
- 燕山大学校园网自动登录问题解决方案
- ES6语法总结--上篇(基础篇)
- Générateur d'identification distribué basé sur redis
- [Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
- E-commerce data analysis -- salary prediction (linear regression)
- open-mmlab labelImg mmdetection
- Imgcat usage experience
猜你喜欢
Kconfig Kbuild
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
NRF24L01 troubleshooting
SVN更新后不出现红色感叹号
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
Types de variables JS et transformations de type communes
ES6语法总结--上篇(基础篇)
The dolphin scheduler remotely executes shell scripts through the expect command
随机推荐
Arduino uno R3 register writing method (1) -- pin level state change
Page performance optimization of video scene
[leetcode19]删除链表中倒数第n个结点
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
2021.11.10 compilation examination
VSCode基础配置
Unity场景跳转及退出
Kconfig Kbuild
如何给Arduino项目添加音乐播放功能
dosbox第一次使用
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
NRF24L01 troubleshooting
[Offer18]删除链表的节点
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
(1) Introduction Guide to R language - the first step of data analysis
[offer18] delete the node of the linked list
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
ORA-02030: can only select from fixed tables/views