操作系统实验1 实现两种进程调度

操作系统实验1 实现两种进程调度

优先权法和轮转法

code:

#coding=utf-8
import sys,random,time
#produce a random
def randomTime():
    return random.randint(1,20)

def randomValue():
    return random.randint(60,100)


class pcb:
    def __init__(self,ctime,cvalue,cname):
        self.name=cname
        self.time=ctime
        self.value=cvalue

class myPriorityQueue:
    def __init__(self):
        self.q=[]

    def length(self):
        return len(self.q)

    def insert(self,e):
        i=0
        if (len(self.q)!=0):
            for i in range(len(self.q)):#i from 0 --- n-1
                if e.value>self.q[i].value:
                    break

        self.q.insert(i,e)
        #print 'length=%d'%len(self.q),
        #print "insert (time=%d,value=%d) into pq"%(e.time,e.value)


    def get(self):
        t=self.q[0]
        del self.q[0]
        return t

    def display(self):
        print 'current myPriorityQueue:'
        for i in range(len(self.q)):
            print 'name='+str(self.q[i].name),'value='+str(self.q[i].value),'time='+str(self.q[i].time)
        print '---------------------------------'




class dispatchProcess:
    def __init__(self,n):#瑙e喅鏂规n
        if n==1:
            self.solution=1
            self.changetime=1
            self.changevalue=3
            #self.fuc={}
        elif n==2:
            pass
        else:
            print '[-]ERROR solutions'


    def ProcessOneSecond(self):
        global pq,runtime
        runtime+=1
        print 'run for %d senconds'%runtime,
        e=pq.get()
        if not e.time==1:
            pq.insert(pcb(ctime=e.time-self.changetime,cvalue=e.value-self.changevalue,cname=e.name))
        print '%s(%d,%d) run 1s ---> %s(%d,%d) '%(e.name,e.time,e.value,e.name,e.time-self.changetime,e.value-self.changevalue)
        return e

    def start(self):
        global pq
        if pq.length()==0:
            print 'no process!'
        else:
            m=raw_input('Single-step debugging?(y/n):')
            print 'process start running!'
            if m=='y':
                print 'enter n for next second'
                print 'enter d for display status'
                print 'enter e run to end!'
                print 'enter q for exit'

            while pq.length()!=0:
                if m=='y':
                    t=raw_input('>')
                    if t=='n':
                        e=self.ProcessOneSecond()
                        #print '(%d,%d) run 1s ---> (%d,%d) '%(e.time,e.value,e.time-self.changetime,e.value-self.changevalue)
                    elif t=='d':
                        pq.display()
                    elif t=='q':
                        break
                    elif t=='e':
                        m='n'
                    else:
                        print '[-]ERROR INPUTS!'
                else:
                #while pq.length()!=0:
                    self.ProcessOneSecond()
                    time.sleep(1)



def init():
    global n,pq,runtime
    runtime=0;
    n=raw_input('How many process do you want?(4~~8 suggested)\n>')
    n=int(n,10)
    print '[+]producing %d processes ...'%n
    #produce
    l={}
    for i in range(n):
        l[i]=pcb(cname=chr(ord('a')+i),ctime=randomTime(),cvalue=randomValue())
        print 'have produced %d process (name=%s,time=%d,value=%d)'%(i,l[i].name,l[i].time,l[i].value)
    c=raw_input('OK?(y/n):');
    if c=='n':
        sys.exit()
    for i in range(n):
        pq.insert(l[i])
#---------------------------------------------------------------------------next for second method
class pcb2:
    def __init__(self,cname):
        self.name=cname
        self.timeSlice=random.randint(5,10)
        self.usedTime=0
        self.needTime=random.randint(10,30)


class RotateTimeSliceDispatch:
    def __init__(self):
        self.l=[]
        n=raw_input('How many process do you want?(4~~8 suggested)\n>')
        self.n=n=int(n,10)
        print '[+]producing %d processes ...'%n
        #produce
        for i in range(n):
            e=pcb2(cname=chr(ord('a')+i))
            self.l.append(e)
            print 'produce a process %s(timeSlice=%d,needTime=%d)'%(e.name,e.timeSlice,e.needTime)
        self.runtime=0

    def goOneSecond(self):
        self.runtime+=1
        self.l[0].needTime-=1
        self.l[0].usedTime+=1
        print '%d s.process %s run 1s. '%(self.runtime,self.l[0].name),
        print 'timeSlice=%d,usedTime=%d,needTime=%d'%(self.l[0].timeSlice,self.l[0].usedTime,self.l[0].needTime)
        if self.l[0].needTime==0:
            print 'and it ends'
            del self.l[0]
            return 

        if self.l[0].usedTime==self.l[0].timeSlice:
            t=self.l[0]
            del self.l[0]
            t.usedTime=0
            self.l.append(t)
            print 'and it goes to the last'
        print 

    def start(self):
        if len(self.l)==0: return
        m=raw_input('Single-step debugging?(y/n):')
        print 'process start running!'
        if m=='y':
            print 'enter n for next second'
            print 'enter d for display status'
            print 'enter e run to end!'
            print 'enter q for exit'

        while len(self.l):
            time.sleep(1)
            if m=='y':
                t=raw_input('>')
                if t=='n':
                    self.goOneSecond()
                elif t=='d':
                    #show the queue
                    for i in range(len(self.l)):
                        print 'process %d is %s,timeSlice=%d,usedTime=%d,needTime=%d'%(i,self.l[i].name,self.l[i].timeSlice,self.l[i].usedTime,self.l[i].needTime)
                elif t=='e':
                    m='n'
                elif t=='q':
                    break
                else:
                    print 'ERROR!'
                    continue            
            else:
                self.goOneSecond()



if __name__=="__main__":
    global f,pq,dp 
    pq=myPriorityQueue()
    print '[+]1.优先权方法'
    print '[+]2.时间片轮转法'
    f=raw_input('>')
    f=int(f,10)
    if f==1:
        dp=dispatchProcess(f)
        init()
        dp.start()
    elif f==2:
        rts=RotateTimeSliceDispatch()
        rts.start()
    else:
        print '[-]ERROR!'    
    print 'All done'


运行结果:

[+]1.优先权方法
[+]2.时间片轮转法
1
How many process do you want?(4~~8 suggested)
5
[+]producing 5 processes …
have produced 0 process (name=a,time=2,value=71)
have produced 1 process (name=b,time=15,value=87)
have produced 2 process (name=c,time=19,value=87)
have produced 3 process (name=d,time=13,value=91)
have produced 4 process (name=e,time=15,value=97)
OK?(y/n):y
Single-step debugging?(y/n):y
process start running!
enter n for next second
enter d for display status
enter e run to end!
enter q for exit
n
run for 1 senconds e(15,97) run 1s —> e(14,94)
n
run for 2 senconds e(14,94) run 1s —> e(13,91)
d
current myPriorityQueue:
name=d value=91 time=13
name=e value=91 time=13
name=b value=87 time=15
name=c value=87 time=19
name=a value=71 time=2
e
run for 3 senconds d(13,91) run 1s —> d(12,88)
run for 4 senconds e(13,91) run 1s —> e(12,88)
run for 5 senconds d(12,88) run 1s —> d(11,85)
run for 6 senconds e(12,88) run 1s —> e(11,85)
run ……….
run for 64 senconds a(1,68) run 1s —> a(0,65)
All done

[+]1.优先权方法
[+]2.时间片轮转法
2
How many process do you want?(4~~8 suggested)
5
[+]producing 5 processes …
produce a process a(timeSlice=7,needTime=14)
produce a process b(timeSlice=8,needTime=20)
produce a process c(timeSlice=7,needTime=13)
produce a process d(timeSlice=5,needTime=11)
produce a process e(timeSlice=8,needTime=27)
Single-step debugging?(y/n):y
process start running!
enter n for next second
enter d for display status
enter e run to end!
enter q for exit
n
1 s.process a run 1s. timeSlice=7,usedTime=1,needTime=13
d
process 0 is a,timeSlice=7,usedTime=1,needTime=13
process 1 is b,timeSlice=8,usedTime=0,needTime=20
process 2 is c,timeSlice=7,usedTime=0,needTime=13
process 3 is d,timeSlice=5,usedTime=0,needTime=11
process 4 is e,timeSlice=8,usedTime=0,needTime=27
n
2 s.process a run 1s. timeSlice=7,usedTime=2,needTime=12
d
process 0 is a,timeSlice=7,usedTime=2,needTime=12
process 1 is b,timeSlice=8,usedTime=0,needTime=20
process 2 is c,timeSlice=7,usedTime=0,needTime=13
process 3 is d,timeSlice=5,usedTime=0,needTime=11
process 4 is e,timeSlice=8,usedTime=0,needTime=27
e
3 s.process a run 1s. timeSlice=7,usedTime=3,needTime=11
4 s.process a run 1s. timeSlice=7,usedTime=4,needTime=10
5 s.process a run 1s. timeSlice=7,usedTime=5,needTime=9
6 s.process a run 1s. timeSlice=7,usedTime=6,needTime=8
7 s.process a run 1s. timeSlice=7,usedTime=7,needTime=7
and it goes to the last
8 s.process b run 1s. timeSlice=8,usedTime=1,needTime=19
9 s.process b run 1s. timeSlice=8,usedTime=2,needTime=18
10 s.process b run 1s. timeSlice=8,usedTime=3,needTime=17
11 s.process b run 1s. timeSlice=8,usedTime=4,needTime=16
12 s.process b run 1s. timeSlice=8,usedTime=5,needTime=15
13 s.process b run 1s. timeSlice=8,usedTime=6,needTime=14
14 s.process b run 1s. timeSlice=8,usedTime=7,needTime=13
15 s.process b run 1s. timeSlice=8,usedTime=8,needTime=12
and it goes to the last
16 s.process c run 1s. timeSlice=7,usedTime=1,needTime=12
17 s.process c run 1s. timeSlice=7,usedTime=2,needTime=11
18 s.process c run 1s. timeSlice=7,usedTime=3,needTime=10
19 s.process c run 1s. timeSlice=7,usedTime=4,needTime=9
20 s.process c run 1s. timeSlice=7,usedTime=5,needTime=8
21 s.process c run 1s. timeSlice=7,usedTime=6,needTime=7
22 s.process c run 1s. timeSlice=7,usedTime=7,needTime=6
and it goes to the last
23 s.process d run 1s. timeSlice=5,usedTime=1,needTime=10
24 s.process d run 1s. timeSlice=5,usedTime=2,needTime=9
25 s.process d run 1s. timeSlice=5,usedTime=3,needTime=8
26 s.process d run 1s. timeSlice=5,usedTime=4,needTime=7
27 s.process d run 1s. timeSlice=5,usedTime=5,needTime=6
and it goes to the last
28 s.process e run 1s. timeSlice=8,usedTime=1,needTime=26
29 s.process e run 1s. timeSlice=8,usedTime=2,needTime=25
30 s.process e run 1s. timeSlice=8,usedTime=3,needTime=24
31 s.process e run 1s. timeSlice=8,usedTime=4,needTime=23
32 s.process e run 1s. timeSlice=8,usedTime=5,needTime=22
33 s.process e run 1s. timeSlice=8,usedTime=6,needTime=21
34 s.process e run 1s. timeSlice=8,usedTime=7,needTime=20
35 s.process e run 1s. timeSlice=8,usedTime=8,needTime=19
and it goes to the last
36 s.process a run 1s. timeSlice=7,usedTime=1,needTime=6
37 s.process a run 1s. timeSlice=7,usedTime=2,needTime=5
38 s.process a run 1s. timeSlice=7,usedTime=3,needTime=4
39 s.process a run 1s. timeSlice=7,usedTime=4,needTime=3
40 s.process a run 1s. timeSlice=7,usedTime=5,needTime=2
41 s.process a run 1s. timeSlice=7,usedTime=6,needTime=1
42 s.process a run 1s. timeSlice=7,usedTime=7,needTime=0
and it ends
43 s.process b run 1s. timeSlice=8,usedTime=1,needTime=11
44 s.process b run 1s. timeSlice=8,usedTime=2,needTime=10
45 s.process b run 1s. timeSlice=8,usedTime=3,needTime=9
46 s.process b run 1s. timeSlice=8,usedTime=4,needTime=8
47 s.process b run 1s. timeSlice=8,usedTime=5,needTime=7
48 s.process b run 1s. timeSlice=8,usedTime=6,needTime=6
49 s.process b run 1s. timeSlice=8,usedTime=7,needTime=5
50 s.process b run 1s. timeSlice=8,usedTime=8,needTime=4
and it goes to the last
51 s.process c run 1s. timeSlice=7,usedTime=1,needTime=5
52 s.process c run 1s. timeSlice=7,usedTime=2,needTime=4
53 s.process c run 1s. timeSlice=7,usedTime=3,needTime=3
54 s.process c run 1s. timeSlice=7,usedTime=4,needTime=2
55 s.process c run 1s. timeSlice=7,usedTime=5,needTime=1
56 s.process c run 1s. timeSlice=7,usedTime=6,needTime=0
and it ends
57 s.process d run 1s. timeSlice=5,usedTime=1,needTime=5
58 s.process d run 1s. timeSlice=5,usedTime=2,needTime=4
59 s.process d run 1s. timeSlice=5,usedTime=3,needTime=3
60 s.process d run 1s. timeSlice=5,usedTime=4,needTime=2
61 s.process d run 1s. timeSlice=5,usedTime=5,needTime=1
and it goes to the last
62 s.process e run 1s. timeSlice=8,usedTime=1,needTime=18
63 s.process e run 1s. timeSlice=8,usedTime=2,needTime=17
64 s.process e run 1s. timeSlice=8,usedTime=3,needTime=16
65 s.process e run 1s. timeSlice=8,usedTime=4,needTime=15
66 s.process e run 1s. timeSlice=8,usedTime=5,needTime=14
67 s.process e run 1s. timeSlice=8,usedTime=6,needTime=13
68 s.process e run 1s. timeSlice=8,usedTime=7,needTime=12
69 s.process e run 1s. timeSlice=8,usedTime=8,needTime=11
and it goes to the last
70 s.process b run 1s. timeSlice=8,usedTime=1,needTime=3
71 s.process b run 1s. timeSlice=8,usedTime=2,needTime=2
72 s.process b run 1s. timeSlice=8,usedTime=3,needTime=1
73 s.process b run 1s. timeSlice=8,usedTime=4,needTime=0
and it ends
74 s.process d run 1s. timeSlice=5,usedTime=1,needTime=0
and it ends
75 s.process e run 1s. timeSlice=8,usedTime=1,needTime=10
76 s.process e run 1s. timeSlice=8,usedTime=2,needTime=9
77 s.process e run 1s. timeSlice=8,usedTime=3,needTime=8
78 s.process e run 1s. timeSlice=8,usedTime=4,needTime=7
79 s.process e run 1s. timeSlice=8,usedTime=5,needTime=6
80 s.process e run 1s. timeSlice=8,usedTime=6,needTime=5
81 s.process e run 1s. timeSlice=8,usedTime=7,needTime=4
82 s.process e run 1s. timeSlice=8,usedTime=8,needTime=3
and it goes to the last
83 s.process e run 1s. timeSlice=8,usedTime=1,needTime=2
84 s.process e run 1s. timeSlice=8,usedTime=2,needTime=1
85 s.process e run 1s. timeSlice=8,usedTime=3,needTime=0
and it ends
All done

难度不大,重在理解。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值