fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5. void P(int &s)
  6. {
  7. while(s==0);
  8. s= s-1;
  9. }
  10. void V(int &s)
  11. {
  12. s= s+1;
  13. }
  14. int main()
  15. {
  16. int pp=0,cp=0,ps=10,cs=0,pps=1,ccs=1,pcs=1;
  17. int buff[ps]={0},x=0;
  18. cout<<"before any edit:"<<endl;
  19. for(int i=0;i<10;i++)cout<<buff[i]<<"\t";
  20. cout<<endl;
  21. cout<<x<<endl;
  22. //-----------Producer-1------------------------------
  23. P(pps);
  24. P(pcs);
  25. P(ps);
  26. buff[pp]=1;
  27. cout<<"after producing:"<<endl;
  28. for(int i=0;i<10;i++)cout<<buff[i]<<"\t";
  29. cout<<endl;
  30. cout<<x<<endl;
  31. pp++;
  32. if(pp>=10)
  33. pp=0;
  34. V(cs);
  35. /*
  36.   //-----------Producer-2------------------------------
  37.   P(pps);
  38.   P(pcs);
  39.   P(ps);
  40.   buff[pp]=1;
  41.   cout<<"after producing:"<<endl;
  42.   for(int i=0;i<10;i++)cout<<buff[i]<<"\t";
  43.   cout<<endl;
  44.   cout<<x<<endl;
  45.   pp++;
  46.   if(pp>=10)
  47.   pp=0;
  48.   V(cs);
  49.   V(pcs);
  50.   V(pps);*/
  51. V(pcs);
  52. V(pps);
  53. //-----------Consumer-1------------------------------
  54. P(ccs);
  55. P(pcs);
  56. P(cs);
  57. x=buff[cp];
  58. buff[cp]=0;
  59. cout<<"after consuming:"<<endl;
  60. for(int i=0;i<10;i++)cout<<buff[i]<<"\t";
  61. cout<<endl;
  62. cout<<x<<endl;
  63. cp++;
  64. if(cp>=10)
  65. cp=0;
  66. V(ps);
  67. /*//-----------Consumer-2------------------------------
  68.   P(ccs);
  69.   P(pcs);
  70.   P(cs);
  71.   x=buff[cp];
  72.   buff[cp]=0;
  73.   cout<<"after consuming:"<<endl;
  74.   for(int i=0;i<10;i++)cout<<buff[i]<<"\t";
  75.   cout<<endl;
  76.   cout<<x<<endl;
  77.   cp++;
  78.   if(cp>=10)
  79.   cp=0;
  80.   V(ps);
  81.   V(pcs);
  82.   V(ccs);*/
  83. V(pcs);
  84. V(ccs);
  85. return 0;
  86. }
  87.  
Success #stdin #stdout 0s 4348KB
stdin
Standard input is empty
stdout
before any edit:
0	0	0	0	0	0	0	0	0	0	
0
after producing:
1	0	0	0	0	0	0	0	0	0	
0
after consuming:
0	0	0	0	0	0	0	0	0	0	
1