机器人工程的工作与考研之困惑→汇总篇←

举报
zhangrelay 发表于 2022/05/21 23:23:57 2022/05/21
【摘要】 有困惑,说明还在思考,麻木才是最恐怖的自我放弃。 如果在思想上不能做自己的主人,那么在身体上就只能做他人的奴仆。 还挺拗口的O(∩_∩)O ☞  机器人工程的工作与考研之困惑“卷” ☞  机器人工程的工作与考研之困惑“歧视” ☞  机器人工程的工作与考研之困惑“取舍” ☞&nbsp...

有困惑,说明还在思考,麻木才是最恐怖的自我放弃。

如果在思想上不能做自己的主人,那么在身体上就只能做他人的奴仆。

还挺拗口的O(∩_∩)O


☞  机器人工程的工作与考研之困惑“卷”

☞  机器人工程的工作与考研之困惑“歧视”

☞  机器人工程的工作与考研之困惑“取舍”

☞  机器人工程的工作与考研之困惑“学历与待遇”

☞  机器人工程的工作与考研之困惑“学历与待遇”补充

☞  机器人工程的工作与考研之困惑“阶段小结”

☞  机器人工程的工作与考研之困惑“要求越来越高”

☞  机器人工程的工作与考研之困惑“效果越来越差”

☞  机器人工程的工作与考研之困惑“以学生为中心”



  
  1. #include<stdio.h>
  2. #define n 4
  3. int compltedPhilo = 0,i;
  4. struct fork{
  5. int taken;
  6. }ForkAvil[n];
  7. struct philosp{
  8. int left;
  9. int right;
  10. }Philostatus[n];
  11. void goForDinner(int philID){ //same like threads concept here cases implemented
  12. if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
  13. printf("Philosopher %d completed his dinner\n",philID+1);
  14. //if already completed dinner
  15. else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
  16. //if just taken two forks
  17. printf("Philosopher %d completed his dinner\n",philID+1);
  18. Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he completed dinner by assigning value 10
  19. int otherFork = philID-1;
  20. if(otherFork== -1)
  21. otherFork=(n-1);
  22. ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks
  23. printf("Philosopher %d released fork %d and fork %d\n",philID+1,philID+1,otherFork+1);
  24. compltedPhilo++;
  25. }
  26. else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already taken, trying for right fork
  27. if(philID==(n-1)){
  28. if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
  29. ForkAvil[philID].taken = Philostatus[philID].right = 1;
  30. printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);
  31. }else{
  32. printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);
  33. }
  34. }else{ //except last philosopher case
  35. int dupphilID = philID;
  36. philID-=1;
  37. if(philID== -1)
  38. philID=(n-1);
  39. if(ForkAvil[philID].taken == 0){
  40. ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
  41. printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
  42. }else{
  43. printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
  44. }
  45. }
  46. }
  47. else if(Philostatus[philID].left==0){ //nothing taken yet
  48. if(philID==(n-1)){
  49. if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
  50. ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
  51. printf("Fork %d taken by philosopher %d\n",philID,philID+1);
  52. }else{
  53. printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
  54. }
  55. }else{ //except last philosopher case
  56. if(ForkAvil[philID].taken == 0){
  57. ForkAvil[philID].taken = Philostatus[philID].left = 1;
  58. printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
  59. }else{
  60. printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
  61. }
  62. }
  63. }else{}
  64. }
  65. int main(){
  66. for(i=0;i<n;i++)
  67. ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;
  68. while(compltedPhilo<n){
  69. /* Observe here carefully, while loop will run until all philosophers complete dinner
  70. Actually problem of deadlock occur only thy try to take at same time
  71. This for loop will say that they are trying at same time. And remaining status will print by go for dinner function
  72. */
  73. for(i=0;i<n;i++)
  74. goForDinner(i);
  75. printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);
  76. }
  77. return 0;
  78. }

 


  
  1. #include<iostream>
  2. #define n 4
  3. using namespace std;
  4. int compltedPhilo = 0,i;
  5. struct fork{
  6. int taken;
  7. }ForkAvil[n];
  8. struct philosp{
  9. int left;
  10. int right;
  11. }Philostatus[n];
  12. void goForDinner(int philID){ //same like threads concept here cases implemented
  13. if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
  14. cout<<"Philosopher "<<philID+1<<" completed his dinner\n";
  15. //if already completed dinner
  16. else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
  17. //if just taken two forks
  18. cout<<"Philosopher "<<philID+1<<" completed his dinner\n";
  19. Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he completed dinner by assigning value 10
  20. int otherFork = philID-1;
  21. if(otherFork== -1)
  22. otherFork=(n-1);
  23. ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks
  24. cout<<"Philosopher "<<philID+1<<" released fork "<<philID+1<<" and fork "<<otherFork+1<<"\n";
  25. compltedPhilo++;
  26. }
  27. else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already taken, trying for right fork
  28. if(philID==(n-1)){
  29. if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
  30. ForkAvil[philID].taken = Philostatus[philID].right = 1;
  31. cout<<"Fork "<<philID+1<<" taken by philosopher "<<philID+1<<"\n";
  32. }else{
  33. cout<<"Philosopher "<<philID+1<<" is waiting for fork "<<philID+1<<"\n";
  34. }
  35. }else{ //except last philosopher case
  36. int dupphilID = philID;
  37. philID-=1;
  38. if(philID== -1)
  39. philID=(n-1);
  40. if(ForkAvil[philID].taken == 0){
  41. ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
  42. cout<<"Fork "<<philID+1<<" taken by Philosopher "<<dupphilID+1<<"\n";
  43. }else{
  44. cout<<"Philosopher "<<dupphilID+1<<" is waiting for Fork "<<philID+1<<"\n";
  45. }
  46. }
  47. }
  48. else if(Philostatus[philID].left==0){ //nothing taken yet
  49. if(philID==(n-1)){
  50. if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
  51. ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
  52. cout<<"Fork "<<philID<<" taken by philosopher "<<philID+1<<"\n";
  53. }else{
  54. cout<<"Philosopher "<<philID+1<<" is waiting for fork "<<philID<<"\n";
  55. }
  56. }else{ //except last philosopher case
  57. if(ForkAvil[philID].taken == 0){
  58. ForkAvil[philID].taken = Philostatus[philID].left = 1;
  59. cout<<"Fork "<<philID+1<<" taken by Philosopher "<<philID+1<<"\n";
  60. }else{
  61. cout<<"Philosopher "<<philID+1<<" is waiting for Fork "<<philID+1<<"\n";
  62. }
  63. }
  64. }else{}
  65. }
  66. int main(){
  67. for(i=0;i<n;i++)
  68. ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;
  69. while(compltedPhilo<n){
  70. /* Observe here carefully, while loop will run until all philosophers complete dinner
  71. Actually problem of deadlock occur only thy try to take at same time
  72. This for loop will say that they are trying at same time. And remaining status will print by go for dinner function
  73. */
  74. for(i=0;i<n;i++)
  75. goForDinner(i);
  76. cout<<"\nTill now num of philosophers completed dinner are "<<compltedPhilo<<"\n\n";
  77. }
  78. return 0;
  79. }

 

 


  
  1. #include <stdio.h>
  2. int current[5][5], maximum_claim[5][5], available[5];
  3. int allocation[5] = {0, 0, 0, 0, 0};
  4. int maxres[5], running[5], safe = 0;
  5. int counter = 0, i, j, exec, resources, processes, k = 1;
  6. int main()
  7. {
  8. printf("\nEnter number of processes: ");
  9. scanf("%d", &processes);
  10. for (i = 0; i < processes; i++)
  11. {
  12. running[i] = 1;
  13. counter++;
  14. }
  15. printf("\nEnter number of resources: ");
  16. scanf("%d", &resources);
  17. printf("\nEnter Claim Vector:");
  18. for (i = 0; i < resources; i++)
  19. {
  20. scanf("%d", &maxres[i]);
  21. }
  22. printf("\nEnter Allocated Resource Table:\n");
  23. for (i = 0; i < processes; i++)
  24. {
  25. for(j = 0; j < resources; j++)
  26. {
  27. scanf("%d", &current[i][j]);
  28. }
  29. }
  30. printf("\nEnter Maximum Claim Table:\n");
  31. for (i = 0; i < processes; i++)
  32. {
  33. for(j = 0; j < resources; j++)
  34. {
  35. scanf("%d", &maximum_claim[i][j]);
  36. }
  37. }
  38. printf("\nThe Claim Vector is: ");
  39. for (i = 0; i < resources; i++)
  40. {
  41. printf("\t%d", maxres[i]);
  42. }
  43. printf("\nThe Allocated Resource Table:\n");
  44. for (i = 0; i < processes; i++)
  45. {
  46. for (j = 0; j < resources; j++)
  47. {
  48. printf("\t%d", current[i][j]);
  49. }
  50. printf("\n");
  51. }
  52. printf("\nThe Maximum Claim Table:\n");
  53. for (i = 0; i < processes; i++)
  54. {
  55. for (j = 0; j < resources; j++)
  56. {
  57. printf("\t%d", maximum_claim[i][j]);
  58. }
  59. printf("\n");
  60. }
  61. for (i = 0; i < processes; i++)
  62. {
  63. for (j = 0; j < resources; j++)
  64. {
  65. allocation[j] += current[i][j];
  66. }
  67. }
  68. printf("\nAllocated resources:");
  69. for (i = 0; i < resources; i++)
  70. {
  71. printf("\t%d", allocation[i]);
  72. }
  73. for (i = 0; i < resources; i++)
  74. {
  75. available[i] = maxres[i] - allocation[i];
  76. }
  77. printf("\nAvailable resources:");
  78. for (i = 0; i < resources; i++)
  79. {
  80. printf("\t%d", available[i]);
  81. }
  82. printf("\n");
  83. while (counter != 0)
  84. {
  85. safe = 0;
  86. for (i = 0; i < processes; i++)
  87. {
  88. if (running[i])
  89. {
  90. exec = 1;
  91. for (j = 0; j < resources; j++)
  92. {
  93. if (maximum_claim[i][j] - current[i][j] > available[j])
  94. {
  95. exec = 0;
  96. break;
  97. }
  98. }
  99. if (exec)
  100. {
  101. printf("\nProcess%d is executing\n", i + 1);
  102. running[i] = 0;
  103. counter--;
  104. safe = 1;
  105. for (j = 0; j < resources; j++)
  106. {
  107. available[j] += current[i][j];
  108. }
  109. break;
  110. }
  111. }
  112. }
  113. if (!safe)
  114. {
  115. printf("\nThe processes are in unsafe state.\n");
  116. break;
  117. }
  118. else
  119. {
  120. printf("\nThe process is in safe state");
  121. printf("\nAvailable vector:");
  122. for (i = 0; i < resources; i++)
  123. {
  124. printf("\t%d", available[i]);
  125. }
  126. printf("\n");
  127. }
  128. }
  129. return 0;
  130. }

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/124890695

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。