模拟分页存储算法

这是一次操作系统的实验,写的很垃圾,留在这等以后回过头来看自己的垃圾代码

package main
import (
    "container/list"
    "fmt"
)
type page struct {
    pg int
    flag int 
    pgnum int
    modify int
    loc int
}
var   (
    ProMemory int = 4
    PG            = 0
    ORDER         = 0
    OFFSET        = 0
    countOld      = 1
)
func main() {
    useList := list.New()
    for i := 0; i <= 4; i++{
        fifo(useList)
    }
}
func initPage() map[int]page {
    pages := map[int]page{
        1 : {
            pg:     0,
            flag:   1,
            pgnum:  5,
            modify: 0,
            loc:    11,
        },
        2: {
            pg:     1,
            flag:   1,
            pgnum:  8,
            modify: 0,
            loc:    12,
        },
        3: {
            pg:     2,
            flag:   1,
            pgnum:  9,
            modify: 0,
            loc:    13,
        },
        4: {
            pg:     3,
            flag:   0,
            pgnum:  1,
            modify: 0,
            loc:    21,
        },
    }
    return pages
}
func searchPage(userlist *list.List,pg int) bool  {
    for item := userlist.Front(); nil != item ;item = item.Next()  {
        if item.Value == pg{
            return true
        }
    }
    return false
}
func setMod(s map[int]page,pg int)  {
    for _,v := range s{
        if v.pg == pg{}
        v.modify = 1
    }
}
func fifo(uselist *list.List)  {
    pages := initPage()
    uselist.PushBack(0)
    uselist.PushBack(1)
    uselist.PushBack(2)
    uselist.PushBack(3)
    fmt.Println("请输入要访问的页号:")
    _, _ = fmt.Scanln(&PG)
    fmt.Println("请输入地址偏移量:")
    _, _ = fmt.Scanln(&OFFSET)
    fmt.Println("请输入要执行的操作:1 存指令 2 非存指令")
    _, _ = fmt.Scanln(&ORDER)
    if searchPage(uselist,PG) == true{
        countOld = countOld % ProMemory
        fmt.Printf("页号%d已在内存中 \n",PG)
        fmt.Printf("%d \n",pages[countOld].pgnum*1024+pages[countOld].loc)
        if ORDER == 1 {
            setMod(pages,countOld)
        }
        countOld++;
    }else {
        fmt.Printf("*%d \n",PG)
        uselist.PushBack(PG)
    }
}

你可能感兴趣的:(模拟分页存储算法)