[操作系統(tǒng)]實(shí)現(xiàn)單處理機(jī)下的進(jìn)程調(diào)度程序

problem

實(shí)驗(yàn)內(nèi)容:

編寫(xiě)一個(gè)單處理機(jī)下的進(jìn)程調(diào)度程序抹恳,模擬操作系統(tǒng)對(duì)進(jìn)程的調(diào)度有额。

要求:

能夠創(chuàng)建指定數(shù)量的進(jìn)程,每個(gè)進(jìn)程由一個(gè)進(jìn)程控制塊表示慕蔚。
實(shí)現(xiàn)先來(lái)先服務(wù)調(diào)度算法:進(jìn)程到達(dá)時(shí)間可由進(jìn)程創(chuàng)建時(shí)間表示丐黄。
實(shí)現(xiàn)短作業(yè)優(yōu)先調(diào)度算法:可指定進(jìn)程要求的運(yùn)行時(shí)間。(說(shuō)明:對(duì)不可剝奪的短作業(yè)優(yōu)先算法孔飒,當(dāng)作業(yè)運(yùn)行時(shí)間相等時(shí)灌闺,優(yōu)先調(diào)度進(jìn)程號(hào)小的進(jìn)程執(zhí)行;對(duì)可剝奪式的短作業(yè)優(yōu)先算法坏瞄,即選最短剩余時(shí)間的進(jìn)程進(jìn)行運(yùn)行桂对,在剩余時(shí)間相同的情況下,選擇到達(dá)時(shí)間早的進(jìn)程進(jìn)行運(yùn)行)

實(shí)現(xiàn)時(shí)間片輪轉(zhuǎn)調(diào)度算法:可指定生成時(shí)間片大小鸠匀。(說(shuō)明:新進(jìn)程到來(lái)時(shí)插入到就緒隊(duì)列的隊(duì)尾蕉斜,當(dāng)進(jìn)程P運(yùn)行完一個(gè)時(shí)間片時(shí),若同時(shí)有進(jìn)程Q到達(dá),則先在就緒隊(duì)列隊(duì)尾插入新到達(dá)的進(jìn)程Q宅此,之后再插入進(jìn)程P)

實(shí)現(xiàn)動(dòng)態(tài)優(yōu)先級(jí)調(diào)度算法:可指定進(jìn)程的初始優(yōu)先級(jí)(優(yōu)先級(jí)與優(yōu)先數(shù)成反比机错,優(yōu)先級(jí)最高為0),優(yōu)先級(jí)改變遵循下列原則:進(jìn)程在就緒隊(duì)列中每停留一個(gè)時(shí)間片父腕,優(yōu)先級(jí)加1弱匪,進(jìn)程每運(yùn)行一個(gè)時(shí)間片,優(yōu)先級(jí)減3璧亮。(說(shuō)明:本算法在優(yōu)先級(jí)相同的情況下萧诫,選擇到達(dá)時(shí)間早的進(jìn)程進(jìn)行運(yùn)行)

測(cè)試用例格式如下:

輸入:調(diào)度算法

進(jìn)程號(hào)/到達(dá)時(shí)間/運(yùn)行時(shí)間/優(yōu)先級(jí)/時(shí)間片

輸出:調(diào)度順序/進(jìn)程號(hào)/開(kāi)始運(yùn)行時(shí)間/結(jié)束運(yùn)行時(shí)間/優(yōu)先級(jí)

其中調(diào)度算法選項(xiàng)為:1----先來(lái)先服務(wù),2----短作業(yè)優(yōu)先枝嘶,3----最短剩余時(shí)間優(yōu)先帘饶,4----時(shí)間片輪轉(zhuǎn),5----動(dòng)態(tài)優(yōu)先級(jí)

測(cè)試用例

測(cè)試輸入 期待的輸出
1
1/0/24/1/1
2/0/3/1/1
3/0/3/1/1
1/1/0/24/1
2/2/24/27/1
3/3/27/30/1
2
1/0/7/1/1
2/2/4/1/1
3/4/1/1/1
4/5/4/1/1
1/1/0/7/1
2/3/7/8/1
3/2/8/12/1
4/4/12/16/1
3
1/0/7/1/1
2/2/4/1/1
3/4/1/1/1
4/5/4/1/1
1/1/0/2/1
2/2/2/4/1
3/3/4/5/1
4/2/5/7/1
5/4/7/11/1
6/1/11/16/1
4
1/0/53/1/20
2/0/17/1/20
3/0/68/1/20
4/0/24/1/20
1/1/0/20/1
2/2/20/37/1
3/3/37/57/1
4/4/57/77/1
5/1/77/97/1
6/3/97/117/1
7/4/117/121/1
8/1/121/134/1
9/3/134/154/1
10/3/154/162/1
5
1/0/3/1/2
2/0/4/1/2
3/0/2/3/2
4/2/1/1/2
5/2/4/4/2
1/1/0/2/4
2/2/2/4/3
3/4/4/5/3
4/3/5/7/3
5/1/7/8/4
6/2/8/10/3
7/5/10/12/3
8/5/12/14/6

ac code

# -*- coding: utf-8 -*-:
import Queue
class Thread:
    id,arrive, cost, priority, time, done  = 0, 0, 0, 0, 0, False
    def __init__(self, param):
        self.id = param[0]
        self.arrive = param[1]
        self.cost = param[2]
        self.priority = param[3]
        self.time = param[4]

def inputThread():
    threads,num = [], 0
    while True:
        try:
            threads.append(Thread(raw_input().split("/")))
            num += 1
        except:
            break
    return threads, num
def output(logidx, tid, tstart, tend, tpriority):
    print str(logidx) + '/' + str(tid) + '/' + str(tstart) + '/' + str(tend) + '/' + str(tpriority)
def cmp_arrive_id(x, y):
    if int(x.arrive) == int(y.arrive):
        return int(x.id) - int(y.id)
    return int(x.arrive) - int(y.arrive)
def cmp_cost_id(x, y):
    if int(x.cost) == int(y.cost):
        return int(x.id) - int(y.id)
    return int(x.cost) - int(y.cost)
def cmp_arrive_cost_id(x, y):
    if int(x.arrive) == int(y.arrive):
        if int(x.cost) == int(y.cost):
            return int(x.id) - int(y.id)
        return int(x.cost) - int(y.cost)
    return int(x.arrive) - int(y.arrive)
def fcfs():
    threads, num = inputThread()
    threads.sort(cmp_arrive_id)
    time,idx  = 0, 1
    for ech in threads:
        if time < int(ech.arrive):
            time = int(ech.arrive)
        endtime = time + int(ech.cost)
        output(idx, ech.id, time, endtime, ech.priority)
        time, idx = endtime, idx + 1

def sjf():
    threads, num = inputThread()
    threads.sort(cmp_arrive_cost_id)
    time, m, idx = int(threads[0].arrive), 0, 1
    while m < num:
        thds = [fl for fl in threads if int(fl.arrive) <= time and not bool(fl.done)]
        if len(thds) == 0:
            tmp = [fl for fl in threads if not bool(fl.done)]
            tmp.sort(cmp_arrive_id)
            time = int(tmp[0].arrive)
            continue
        thds.sort(cmp_cost_id)
        cur = thds[0]
        output(idx, cur.id, time, time + int(cur.cost), cur.priority)
        time,thds[0].done, m, idx = time + int(cur.cost), True, m + 1, idx + 1

def rr():
    threads, num = inputThread()
    threads.sort(cmp_arrive_id)
    tq = Queue.Queue()
    time, idx, m, i = int(threads[0].arrive), 1, 0, 0
    while m < num:
        if tq.empty():
            time = int(threads[i].arrive)
            while i < num and int(threads[i].arrive) <= time:
                tq.put(threads[i])
                i += 1
        ech = tq.get()
        if int(ech.time) < int(ech.cost):
            output(idx, ech.id, time, time + int(ech.time), ech.priority)
            idx, time = idx + 1, time + int(ech.time)
            ech.cost = int(ech.cost) - int(ech.time)
            while i < num and int(threads[i].arrive) <= time:
                tq.put(threads[i])
                i += 1
            tq.put(ech)
            continue
        output(idx, ech.id, time, time + int(ech.cost), ech.priority)
        idx += 1
        ech.done = True
        time = time + int(ech.cost)
        while i < num and int(threads[i].arrive) <= time:
            tq.put(threads[i])
            i += 1
        m += 1

        # 最短剩余時(shí)間優(yōu)先


def srt():
    threads, num = inputThread()
    threads.sort(cmp_arrive_cost_id)
    threads.append(Thread([0, 0, 0, 0, 0]))

    time = int(threads[0].arrive)

    execi = -1
    start = time
    m = 0
    idx = 1
    i = 0
    while m < num:
        while int(threads[i].arrive) <= time and i < num:
            i += 1

        min = 99999999999999999999999999999999999999999999
        mini = -1
        j = 0
        for j in range(i):
            if int(threads[j].cost) < min and not bool(threads[j].done):
                min = int(threads[j].cost)
                mini = j

        if mini == -1:
            time = int(threads[i].arrive)
            continue
        if execi == -1:
            execi = mini
            start = int(threads[execi].arrive)
            if int(threads[execi].arrive) < time:
                start = time

        if int(threads[mini].cost) < int(threads[execi].cost):
            output(idx, threads[execi].id, start, time, threads[execi].priority)
            idx += 1
            execi = mini
            start = time

        runnable = int(threads[i].arrive) - time

        if runnable >= int(threads[execi].cost) or runnable <= 0:
            time += int(threads[execi].cost)
            output(idx, threads[execi].id, start, time, threads[execi].priority)
            idx += 1
            threads[execi].cost = 0
            threads[execi].done = True
            m += 1
            execi = -1
            start = time
        else:
            time += runnable
            threads[execi].cost = int(threads[execi].cost) - runnable

def dp():
    threads, num = inputThread()
    threads.sort(cmp_arrive_id)
    time,last, m, idx = int(threads[0].arrive), int(threads[0].arrive), 0, 1
    while m < num:
        i = 0
        while i < num and int(threads[i].arrive) <= time:
            if last < int(threads[i].arrive) < time:
                if int(threads[i].priority) > 1:
                    threads[i].priority = int(threads[i].priority) - 1
                else:
                    threads[i].priority = 0
            i += 1
        min, mini = 99999999999999999999, -1
        for j in range(i):
            if int(threads[j].priority) < min and not bool(threads[j].done):
                min, mini = int(threads[j].priority), j
        if mini == -1:
            time = int(threads[i].arrive)
            continue
        last = time
        if int(threads[mini].cost) <= int(threads[mini].time):
            output(idx, threads[mini].id, time, time + int(threads[mini].cost), int(threads[mini].priority) + 3)
            idx, m, time, threads[mini].done = idx + 1, m + 1, time + int(threads[mini].cost), True
        else:
            output(idx, threads[mini].id, time, time + int(threads[mini].time), int(threads[mini].priority) + 3)
            idx, time, threads[mini].cost = idx + 1, time + int(threads[mini].time), int(threads[mini].cost) - int(threads[mini].time)
        for j in range(i):
            if not bool(threads[j].done):
                if j == mini:
                    threads[j].priority = int(threads[j].priority) + 3
                else:
                    if int(threads[j].priority) > 1:
                        threads[j].priority = int(threads[j].priority) - 1
                    else:
                        threads[j].priority = 0

if __name__ == '__main__':
    method = input()
    if method == 1:
        fcfs()
    elif method == 2:
        sjf()
    elif method == 3:
        srt()
    elif method == 4:
        rr()
    elif method == 5:
        dp()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末群扶,一起剝皮案震驚了整個(gè)濱河市及刻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌穷当,老刑警劉巖提茁,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異馁菜,居然都是意外死亡茴扁,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)汪疮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)峭火,“玉大人,你說(shuō)我怎么就攤上這事智嚷÷敉瑁” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵盏道,是天一觀的道長(zhǎng)稍浆。 經(jīng)常有香客問(wèn)我,道長(zhǎng)猜嘱,這世上最難降的妖魔是什么衅枫? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮朗伶,結(jié)果婚禮上弦撩,老公的妹妹穿的比我還像新娘。我一直安慰自己论皆,他們只是感情好益楼,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布猾漫。 她就那樣靜靜地躺著,像睡著了一般感凤。 火紅的嫁衣襯著肌膚如雪悯周。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 48,970評(píng)論 1 284
  • 那天俊扭,我揣著相機(jī)與錄音队橙,去河邊找鬼坠陈。 笑死萨惑,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的仇矾。 我是一名探鬼主播庸蔼,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼贮匕!你這毒婦竟也來(lái)了姐仅?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤刻盐,失蹤者是張志新(化名)和其女友劉穎掏膏,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體敦锌,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡馒疹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了乙墙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颖变。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖听想,靈堂內(nèi)的尸體忽然破棺而出腥刹,到底是詐尸還是另有隱情,我是刑警寧澤汉买,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布衔峰,位于F島的核電站,受9級(jí)特大地震影響蛙粘,放射性物質(zhì)發(fā)生泄漏垫卤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一组题、第九天 我趴在偏房一處隱蔽的房頂上張望葫男。 院中可真熱鬧,春花似錦崔列、人聲如沸梢褐。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)盈咳。三九已至耿眉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鱼响,已是汗流浹背鸣剪。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留丈积,地道東北人筐骇。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像江滨,于是被迫代替她去往敵國(guó)和親铛纬。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容