Golang Simple Implementation Banker algorithm

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This week the course design, thought will be very fun, the experiment book sent down, incredibly just realized banker algorithm, disappointed ... Used a small half a day (forgive me on the operating system incredibly even banker algorithm is what forget, can only go back to read a book again), with Golang simple implementation of the banker algorithm, is the code:

 PackageMainImport("FMT"    "Log"    "OS")varAvailable []int    //can use resource vectorsvarMax []int        //maximum demand matrixvarAllocation []int //Assigned matrixvarNeed []int       //Demand MatrixvarRequest []int      //Request Vectorvarwork []int         //working vectorvarFinish []BOOL      //finish VectorvarPidint            //Request Resource Process numbervarserial []int       //For storing secure sequencesvarwork []int//InitializefuncInit () {//Number of resources is 10,5,7    //Number of resources is 10,5,7}//Banker algorithmfuncBank () { forI: =0; I <Len(Request); i++ {ifRequest[i] <= Need[pid][i] {Continue}Else{log. Fatalln ("The required resources exceed the declared maximum value")        }    } forI: =0; I <Len(Request); i++ {ifRequest[i] <= Available[i] {Continue}Else{log. Fatalln ("There is not enough resources to wait")        }    } forI: =0; I <Len(Available); i++ {Available[i] = Available[i]-request[i]} forI: =0; I <Len(Allocation[0]); i++ {Allocation[pid][i] = Allocation[pid][i] + request[i]} forI: =0; I <Len(Need[0]); i++ {Need[pid][i] = Need[pid][i]-request[i]}ifSafe () {fmt. Println ("Complete Assignment")    }Else{FMT. Println ("The system is in an insecure state, waiting")} FMT. Println ("Max:", Max) fmt. Println ("Allocation:", Allocation) fmt. Println ("Need:", need) fmt. Println ("Available:", Available) fmt. Println ("Work:", work) FMT. Println ("Safe sequence:", serial)}//Security checkfuncSafe ()BOOL{work = Available forI: =0; I <Len(Max); i++ {Finish =Append(Finish,false)} Temp: =0Step2: forI: = temp; I <Len(Finish); i++ {ifFinish[i] = =false{ forJ: =0; J <Len(work); J + + {ifNEED[I][J] <= Work[j] {ifj = =Len(Work)-1{serial =Append(Serial, i) Work =Append(Work and work) forJ: =0; J <Len(work); J + + {Work[j] = Work[j] + allocation[i][j]} Fini Sh[i] =true                        ifi = =Len(Finish)-1{temp =0}Else{temp = i +1}GotoSTEP2}}Else{ Break}            }        }Else{Continue}ifi = =Len(Finish)-1{return false}    }return true}funcMain () {lable:varSelintFmt. Println ("******************** banker algorithm ********************") FMT. Println () fmt. Println ("\T1, test data One") FMT. Println ("\T2, test Data II") FMT. Println ("\T3, test data three") FMT. Println ("\t4, Exit") FMT. Println () fmt. Println ("************************************************") FMT. Print ("Please make your choice:") FMT. SCANLN (&sel)SwitchSEL {//The total number of resources is 10,5,7     Case1://test data 1Available = []int{3,3,2} Max = []int{{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}} Allocation = []int{{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}} need = []int{{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}} Request = []int{1,0,2} PID =1     Case2://test Data 2Available = []int{2,3,0} Max = []int{{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}} Allocation = []int{{0,1,0},{3,0,2},{3,0,2},{2,1,1},{0,0,2}} need = []int{{7,4,3},{0,2,0},{6,0,0},{0,1,1},{4,3,1}} Request = []int{3,3,0} PID =4     Case3://test Data 3Available = []int{2,3,0} Max = []int{{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}} Allocation = []int{{0,1,0},{3,0,2},{3,0,2},{2,1,1},{0,0,2}} need = []int{{7,4,3},{0,2,0},{6,0,0},{0,1,1},{4,3,1}} Request = []int{0,2,0} PID =0     Case4: OS. Exit(0)default: FMT. Println ("No this option")    }//Execution banker algorithmBank ()Gotolable}

Very humble, only tested three sets of data, if found a bug, look treatise, thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.