西电软工操作系统作业题

Q1:关于进程创建的问题,进程创建的整个完整的历程是怎样的,就比如说,你用鼠标点开浏览器的过程,我觉得首先是鼠标作为输入设备,点击以后给系统输入了一个打开浏览器的信号,问题就是接下来系统创建浏览器这个进程经历了哪些步骤?如果鼠标点击的是桌面的快捷方式和直接在文件夹里面启动是不是一样的创建历程?

Q2:地址空间的物理位置是由操作系统决定的还是由硬件决定的?它的位置在UNIX和windows有区别吗?

Q3:由于库函数对文件的操作最终是通过系统调用实现的, 使用库函数也有系统调用的支持,为什么不直接使用系统调用呢? 比如编写C程序为什么不直接用write(),而用printf()?

Q1: What is race condition?

A: Where two or more processes are reading or writing some shared data and the final result depends on who runs precisely when, are called race conditions.

Q2:What is mutual exclusion?

A:some way of making sure that if one process is using a shared variable or file, the other processes will be excluded from doing the same thing.

Q3:What  is critical region?

A:That part of the program where the shared memory is accessed.

Q4:What are four conditions to hold to have a good solution for race condition/mutual exclusion?

A:

  1. No two processes may be simultaneously inside their critical regions.
  2. No assumptions may be made about speeds or the number of CPUs.
  3. No process running outside its critical region may block other processes.
  4. No process should have to wait forever to enter its critical region.    

Q5:Explain the asmcode of Fig 2-25 in section 2.3.3 to show that the code can provide mutual exclusion.

A:进程在进入临界区时,调用enter_region,进程会一直忙等待,直至琐空闲为止,随后它获得该锁并返回;进程从临界区返回时它调用leave_region,这把lock设置为0.进程在正确时间调用enter_region和leave_region,进程之间即可产生互斥。

Q6:Explain the code of Fig 2-28 in section 2.3.5 to show that the code can provide mutual exclusion.

A:通过两种不同的方式来使用信号量mutex,mutex用于互斥,它用于保证任一时刻只有一个进程读写缓冲区和相关的变量。

Q7:What is monitor and please show the connections between the code in Fig 2-34 and monitor.

A:进入monitor时的互斥有编译器负责,引入条件变量以及相关的两个操作:wait和signal。如果在一个条件变量上有若干进程正在等待,则在对该条件变量执行signal操作后,系统调度程序只能在其中选择一个使其恢复运行。

Q8:What is the difference between a semaphore and a mutex?

A:mutex由系统层控制,semaphore则是在用户层执行;mutex一定由获得锁的进程来释放,semaphore可以由其他进程释放;mutex只有两种计数,从而对共享资源加一保护。

Q9:Please explain the code in Fig 2-32 to show how conditional variables work. If we do not use conditional variable, only use mutexlock& unlock, what’s the difference?

A:当生产者填满缓冲区时,它在生产下一个数据项之前必须等待,直到消费者清空了它。类似的,当消费者一走一个数据项时,它必须等待,知道生产者生产了另外一个数据项。使一个线程睡眠的语句应该总是要检查这个条件,以保证线程在继续执行前满足条件,因为线程可能已经因为一个unix信号或其他原因而被唤醒。

Q10:What is barrier? 

A:Some applications are divided into phases and have the rule that no process may proceed into the next phase until all processes are ready to proceed to the next phase.

Q11:What is message passing?

A:使用两条原语send和receive,它们像信号量而不像管程,是系统调用而不是语言成分。

Q12:What is the  difference of these IPCs?

A:

忙等待的互斥:当一个进程进入临界区中更新共享内存时,其他进程不会进入临界区。

休眠与唤醒:几条进程间通信原语,它们在无法进入临界区时将阻塞,而不是忙等待。

信号量:使用一个整型变量来累计唤醒次数,供以后使用。

互斥量:信号量的一个简化版本,是一个可以处于两态之一的变量:解锁和加锁。

管程:一个管程是一个有过程、变量及数据结构等组成的一个集合,它们组成一个特殊的模块或软件包。

信息传递:使用两条原语send和receive,它们像信号量而不像管程,是系统调用而不是语言成分。

屏障:除非所有的进程都就绪准备着手下一个阶段,否则任何进程都不能进入下一个阶段。

Q13:Please read section 2.3 and write down your questions.

1)P135 figure2-32中,main函数中使用的pthread_create ();我上网查的参数列表为int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void * (*func)(void *), void *arg);

其中第二第四参数应该为指针,为什么这里传递的参数是0?

Exercise1:

P1

P1

P2

P3

P1

P3

P3

P4

P5

P4

P6

P7

0        20         40        55         75      85        105    110    120       130           160       190         210

P1

P2

P3

P4

P5

P6

P7

Response time

0

10

25

110

0

100

60

Turnaroundtime

85

25

80

160

10

130

80

Exercise2:

(a)Round robin (quantum=1s).

A

A

B

C

D

A

B

C

B

C

B

C

B

0         1          2          3          4          5          6          7          8          9          10          11          12     13

Tmean=(6+11+9+1)/4=27/4=6.75

(b) Priority scheduling (NonPreemptive).

  A

     C

         B

  D

0          3                      7                                       12        13

Tmean=(3+5+4+1)/4=13/4=3.25

(c) Priority scheduling (preemptive).

     A

  B

         C

     B

  A

   D

0                        2            3                                      7                        11           12           13

Tmean=(12+9+4+1)/4=26/4=6.5

(d) FCFS.

     A

             B

           C

     D

0                        3                                                   8                                               12                     13

Tmean=(3+5+4+1)/4=13/4=3.25

(e) Shortest job first (Preemptive).

       A

 C

  D

        B

0                                3                           7             8                                    13

Tmean=(3+5+4+1)/4=13/4=3.25

(f) Shortest job first (Nonpreemptive).

      A

       C

   D

C

      B

0                           3                        4                    5                              8                                        13

Tmean=(3+5+5+1)/4=14/4=3.5

题目1:page fault = 7

题目2:page fault = 8

(1)

当前状态是安全的

过程如下:

E(3568)

P(3467)

A(0101)

其中

       Max                        Has

       R1 R2 R3 R4        R1 R2 R3 R4

P1     1   2    3    6           1   1    2    4

P2     1   1    2    2           0   1    2    2

P3     1   2    1    1           1   1    1    0

P4     1   1    2    3           1   1    1    1

分配给P3,P3结束后,A(1211)

       Max                        Has

       R1 R2 R3 R4        R1 R2 R3 R4

P1     1   2    3    6           1   1    2    4

P2     1   1    2    2           0   1    2    2

P3     1   2    1    1           0   0    0    0

P4     1   1    2    3           1   1    1    1

分配给P2,P2结束后,A(1333)

       Max                        Has

       R1 R2 R3 R4        R1 R2 R3 R4

P1     1   2    3    6           1   1    2    4

P2     1   1    2    2           0   0    0    0

P3     1   2    1    1           0   0    0    0

P4     1   1    2    3           1   1    1    1

分配给P4,P4结束后,A(2444)

       Max                        Has

       R1 R2 R3 R4        R1 R2 R3 R4

P1     1   2    3    6           1   1    2    4

P2     1   1    2    2           0   0    0    0

P3     1   2    1    1           0   0    0    0

P4     1   1    2    3           0   0    0    0

 分配给P1,P1结束后,A(3568),完成

       Max                        Has

       R1 R2 R3 R4        R1 R2 R3 R4

P1     1   2    3    6            0   0    0    0

P2     1   1    2    2           0   0    0    0

P3     1   2    1    1           0   0    0    0

P4     1   1    2    3           0   0    0    0

(2)

不能,如果系统分配R2给P1的话,会是如下状态:

A(0001)

       Max                        Has

       R1 R2 R3 R4        R1 R2 R3 R4

P1     1   2    3    6           1   2    2    4

P2     1   1    2    2           0   1    2    2

P3     1   2    1    1           1   1    1    0

P4     1   1    2    3           1   1    1    1

没有足够的资源分配给任意一个进程,该状态将陷入死锁状态。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值