⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lockmanagern.cpp

📁 模拟实现锁管理器
💻 CPP
字号:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;


void main()
{
   std::string ReqType,Obj1;
   int TrID,Label;
   char a[10];
   string XL("z"),SL("z"),T("z"),W("z"); //XL is XLock ltable,SL is SLock table

 
 /*  request  "Start",   "SLock",  "XLock",  "End", "UnLock"     */

   do 
   {  
       cout<<"Type Request: "<<endl;
       cin>>ReqType;
	   cin>>TrID;
	   cin>>Obj1;
       itoa(TrID,a,10);
     //  cout<<ReqType<<"  "<<a<<"  "<<Obj1<<endl;
   
	 /*Start transaction */
      if  (ReqType.compare("Start")==0)  //if request is Start ,output Transactio Started
	   {
	       	cout<<ReqType<<" "<<a<<"  : "<<"Transaction  "<<a<<"  started"<<endl;
	        T.append(a);
	   }

	  if  (T.find(a)!=-1)  // transaction a started
	  {	        
     	
		 /* SLock begin */

	     if  (ReqType.compare("SLock")==0) //if request is SLock 
		 {
   	        if(XL.find(Obj1)==-1)  // if Obj1 is not in XL[],so Obj1 add SL[]
			{
		        SL.append(ReqType+a+Obj1); //so Obj1 add SL[]			
	            cout<<ReqType<<" "<<a<<" "<<Obj1<<" : "<<" Lock granted "<<endl;
			}
            else
			{
			    W.append(ReqType+a+Obj1);
	            cout<<ReqType<<" "<<a<<" "<<Obj1<<" : "<<" Waiting for lock (X-lock held by:  " <<XL.substr((XL.find(Obj1))-3,3)<<") "<<endl;
            }
		 }
      /* SLock end */

     /*Xlock begin*/
 	     if  (ReqType.compare("XLock")==0)  //if request is XLock
		 {             
   	       if (XL.find(Obj1)==-1) // if Obj1 is not in XL[]
		   {			   
			   if(SL.find(Obj1)==-1)
			   {
		           XL.append(ReqType+a+Obj1); //so Obj1 add SL[]		    
	              cout<<ReqType<<" "<<a<<" "<<Obj1<<" : "<<" Lock granted "<<endl;
			   }
               else
			   {
			     if (SL.substr(SL.find(Obj1)-3,3).compare(a)==-1)
				 {
					 W.append(ReqType+a+Obj1);
                     cout<<ReqType<<" "<<a<<" "<<Obj1<<" : "<<" Waiting for lock (S-lock held by:  " <<SL.substr((SL.find(Obj1))-3,3)<<") "<<endl;
                 }
				 else
				 {
                    XL.append(ReqType+a+Obj1);				    
					SL.erase(SL.find(ReqType+a+Obj1),9);  //SLock erase transaction Obj1
                    cout<<ReqType<<" "<<a<<" "<<Obj1<<" : "<<Obj1<<" Upgrade to XL granted " <<endl;
				 } 
			   }
		   }
		   else
		   {
			   W.append(ReqType+a+Obj1);			   
			   cout<<ReqType<<" "<<a<<" "<<Obj1<<" : "<<" Waiting for lock (X-lock held by:  " <<XL.substr(XL.find(Obj1)-3,3)<<") "<<endl;
           }
		 }
	   /*Xlock end*/

       /*END begin*/
       if  (ReqType.compare("End")==0) //if request is SLock 
		 {
   	        if(XL.find(a)!=-1 || SL.find(Obj1)!=-1 )   // if a is in XL[]
			{
               if(XL.find(a)!=-1)  
			   {
				   Label=XL.find(a);
				   Obj1=XL.substr(Label+3,1);
                   XL.erase(Label-5,9); 	//erase transaction a  in XL
			   }
			   else
			   {
				   Label=SL.find(a);
				   Obj1=SL.substr(Label+3,1);
				   SL.erase(Label-5,9);    //erase transaction a  in sL
			   }
				cout<<ReqType<<" "<<a<<" "<<" : "<<" Transaction "<<a<<" Ended; "<<endl;
			   if (W.find(a)!=-1)
			        W.erase(W.find(a)-5,9);
               if(W.find(Obj1)!=-1) 
			   {
				   Label=W.find(Obj1);
				   if((W.substr(Label-8,5)).compare("XLock")==0)
				   {
                     XL.append(W.substr(Label-8,9));
                     cout<<"Released  X_Lock on "<<W.substr(Label,1)<<endl; 
			         cout<<" X_Lock on "<<W.substr(Label,1)<<" granted to "<<W.substr(Label-3,3)<<endl;
				     W.erase(Label-8,9); 
				   }
				   else
				   {
				     SL.append(W.substr(Label-5,9));
                     cout<<"Released  S_Lock on "<<W.substr(Label+2,1)<<endl; 
			         cout<<" S_Lock on "<<W.substr(Label,1)<<" granted to "<<W.substr(Label-3,3)<<endl;
				     W.erase(Label-8,9); 
				   }
			   } 
			}
		   

           
		 }

       /*END end*/

	   /*Unlock begin*/
         if  (ReqType.compare("Unlock")==0) //if request is SLock 
		 {
   	        if(XL.find(a)!=-1 || SL.find(a)!=-1 )   // if a is in XL[]
			{
               if(XL.find(a)!=-1)  
                   XL.erase(XL.find(a)-5,9); 	//erase transaction a  in XL
			   if(SL.find(a)!=-1)
				   SL.erase(SL.find(a)-5,9);    //erase transaction a  in sL
			  	  cout<<ReqType<<" "<<a<<" "<<Obj1<<" : "<<" Lock released"<<endl;
               if(W.find(Obj1)!=-1) 
			   {
				   //(SL.substr(SL.find(Obj1)-3,3).compare(a)==-1)
				   Label=W.find(Obj1);
				   if((W.substr(Label-8,5)).compare("XLock")==0)
				   {
                     XL.append(W.substr(Label-8,9));
                     cout<<"Released  X_Lock on "<<Obj1<<endl; 
			         cout<<" X_Lock on "<<Obj1<<" granted to "<<W.substr(Label-3,3)<<endl;
                     W.erase(Label-8,9); 
				   }
				   else
				   {
				     SL.append(W.substr(Label-8,9));
                     cout<<"Released  S_Lock on "<<Obj1<<endl; 
			         cout<<" S_Lock on "<<Obj1<<" granted to "<<W.substr(Label-3,3)<<endl;
					 W.erase(Label-8,9); 
				   }
			   } 
			}
           
		 }
  
        




       /*Unlock end*/

	  }
	  // a is in T?
   } while(ReqType.compare("Exit")!=0);
   
  

  
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -