2. processes

118
作作 : 作作作 作作 : 作作作作作作作作作 Email: [email protected] URL : http://ccc.kmit.edu.tw 作作 : 111/06/17 2. Processes

Upload: misty

Post on 11-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

2. Processes. 2.1. Introduction to Processes. 2.1.1. The Process Model 2.1.2. Process Creation 2.1.3. Process Termination 2.1.4. Process Hierarchies 2.1.5. Process States 2.1.6. Implementation of Processes 2.1.7. Threads. 2.1 The Process Model. 2.1.2 Process Creation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 2. Processes

作者 : 陳鍾誠單位 : 金門技術學院資管系Email: [email protected] : http://ccc.kmit.edu.tw

日期 : 112/04/21

2. Processes

Page 2: 2. Processes

2.1. Introduction to Processes

2.1.1. The Process Model 2.1.2. Process Creation 2.1.3. Process Termination 2.1.4. Process Hierarchies 2.1.5. Process States 2.1.6. Implementation of Processes 2.1.7. Threads

2 陳鍾誠 - 112/04/21

Page 3: 2. Processes

2.1 The Process Model

3 陳鍾誠 - 112/04/21

Page 4: 2. Processes

2.1.2 Process Creation

4 陳鍾誠 - 112/04/21

Page 5: 2. Processes

2.1.3 Process Termination

5 陳鍾誠 - 112/04/21

Page 6: 2. Processes

2.1.4 Process Hierarchy

6 陳鍾誠 - 112/04/21

Page 7: 2. Processes

2.1.5 Process States

7 陳鍾誠 - 112/04/21

Page 8: 2. Processes

2.1.6 Implementation of Processes

8 陳鍾誠 - 112/04/21

Page 9: 2. Processes

9 陳鍾誠 - 112/04/21

Page 10: 2. Processes

2.1.7 Threads

10 陳鍾誠 - 112/04/21

Page 11: 2. Processes

11 陳鍾誠 - 112/04/21

Page 12: 2. Processes

12 陳鍾誠 - 112/04/21

Page 13: 2. Processes

2.2. Interprocess Communication

2.2.1. Race Conditions 2.2.2. Critical Sections 2.2.3. Mutual Exclusion with Busy Waiting 2.2.4. Sleep and Wakeup 2.2.5. Semaphores 2.2.6. Mutexes 2.2.7. Monitors 2.2.8. Message Passing

13 陳鍾誠 - 112/04/21

Page 14: 2. Processes

2.2.1 Race Conditions

14 陳鍾誠 - 112/04/21

Page 15: 2. Processes

2.2.2 Critical Section

15 陳鍾誠 - 112/04/21

Page 16: 2. Processes

2.2.3 Mutual Exclusion with Busy Waiting

Disabling Interrupts Lock Variables Strict Alternation Peterson’s Solution The TSL instruction

16 陳鍾誠 - 112/04/21

Page 17: 2. Processes

Disabling Interrupts

17 陳鍾誠 - 112/04/21

Page 18: 2. Processes

Lock Variables

18 陳鍾誠 - 112/04/21

Page 19: 2. Processes

Strict Alternation

19 陳鍾誠 - 112/04/21

Page 20: 2. Processes

Peterson’s Solution

20 陳鍾誠 - 112/04/21

Page 21: 2. Processes

The TSL instruction

21 陳鍾誠 - 112/04/21

Page 22: 2. Processes

2.2.4 Sleep and Wakeup

22 陳鍾誠 - 112/04/21

Page 23: 2. Processes

The Producer-Consumer Problem

23 陳鍾誠 - 112/04/21

Page 24: 2. Processes

2.2.5 Semaphores

24 陳鍾誠 - 112/04/21

Page 25: 2. Processes

Solving the Producer-Consumer Problem using Semaphores

25 陳鍾誠 - 112/04/21

Page 26: 2. Processes

2.2.6. Mutexes

26 陳鍾誠 - 112/04/21

Page 27: 2. Processes

2.2.7. Monitors

27 陳鍾誠 - 112/04/21

Page 28: 2. Processes

Solving the Producer-Consumer Problem using Monitor (1)

28 陳鍾誠 - 112/04/21

Page 29: 2. Processes

Solving the Producer-Consumer Problem using Monitor (2)

29 陳鍾誠 - 112/04/21

Page 30: 2. Processes

2.2.8. Message Passing

30 陳鍾誠 - 112/04/21

Page 31: 2. Processes

2.3. Classical IPC Problems

2.3.1. The Dining Philosophers Problem 2.3.2. The Readers and Writers Problem

31 陳鍾誠 - 112/04/21

Page 32: 2. Processes

2.3.1. The Dining Philosophers Problem (DPP)

32 陳鍾誠 - 112/04/21

Page 33: 2. Processes

A nonsolution to DPP

33 陳鍾誠 - 112/04/21

Page 34: 2. Processes

A solution to DPP (1)

34 陳鍾誠 - 112/04/21

Page 35: 2. Processes

A solution to DPP (2)

35 陳鍾誠 - 112/04/21

Page 36: 2. Processes

2.3.2. The Readers and Writers Problem

36 陳鍾誠 - 112/04/21

Page 37: 2. Processes

2.4. Scheduling

2.4.1. Introduction to Scheduling 2.4.2. Scheduling in Batch Systems 2.4.3. Scheduling in Interactive Systems 2.4.4. Scheduling in Real-Time Systems 2.4.5. Policy versus Mechanism 2.4.6. Thread Scheduling

37 陳鍾誠 - 112/04/21

Page 38: 2. Processes

2.4.1. Introduction to Scheduling

Process Behavior When to Schedule Categories of Scheduling Algorithms Scheduling Algorithm Goals

38 陳鍾誠 - 112/04/21

Page 39: 2. Processes

Process Behavior

39 陳鍾誠 - 112/04/21

Page 40: 2. Processes

Scheduling Algorithm Goals

40 陳鍾誠 - 112/04/21

Page 41: 2. Processes

2.4.2. Scheduling in Batch Systems

First-Come First Served Shortest Job First Shortest Remaining Time Next Three-Level Scheduling

41 陳鍾誠 - 112/04/21

Page 42: 2. Processes

First-Come First Served

42 陳鍾誠 - 112/04/21

Page 43: 2. Processes

Shortest Job First

43 陳鍾誠 - 112/04/21

Page 44: 2. Processes

Shortest Remaining Time Next

44 陳鍾誠 - 112/04/21

Page 45: 2. Processes

Three-Level Scheduling

45 陳鍾誠 - 112/04/21

Page 46: 2. Processes

2.4.3. Scheduling in Interactive Systems

Round-Robin Scheduling Priority Scheduling Multiple Queues Shortest Process Next Guaranteed Scheduling Lottery Scheduling Fair-Share Scheduling

46 陳鍾誠 - 112/04/21

Page 47: 2. Processes

Round-Robin Scheduling

47 陳鍾誠 - 112/04/21

Page 48: 2. Processes

Priority Scheduling

48 陳鍾誠 - 112/04/21

Page 49: 2. Processes

Multiple Queues

49 陳鍾誠 - 112/04/21

Page 50: 2. Processes

Shortest Process Next

50 陳鍾誠 - 112/04/21

Page 51: 2. Processes

Guaranteed Scheduling

51 陳鍾誠 - 112/04/21

Page 52: 2. Processes

Lottery Scheduling

52 陳鍾誠 - 112/04/21

Page 53: 2. Processes

Fair-Share Scheduling

53 陳鍾誠 - 112/04/21

Page 54: 2. Processes

2.4.4. Scheduling in Real-Time Systems

54 陳鍾誠 - 112/04/21

Page 55: 2. Processes

2.4.5. Policy versus Mechanism

55 陳鍾誠 - 112/04/21

Page 56: 2. Processes

2.4.6. Thread Scheduling

56 陳鍾誠 - 112/04/21

Page 57: 2. Processes

2.5. Overview of Processes in MINIX 3

2.5.1. The Internal Structure of MINIX 3 2.5.2. Process Management in MINIX 3 2.5.3. Interprocess Communication in MINIX 3 2.5.4. Process Scheduling in MINIX 3

57 陳鍾誠 - 112/04/21

Page 58: 2. Processes

2.5.1. The Internal Structure of MINIX 3

58 陳鍾誠 - 112/04/21

Page 59: 2. Processes

2.5.2. Process Management in MINIX 3

59 陳鍾誠 - 112/04/21

Page 60: 2. Processes

Minix Startup

60 陳鍾誠 - 112/04/21

Page 61: 2. Processes

Some Important MINIX 3 system components

61 陳鍾誠 - 112/04/21

Page 62: 2. Processes

Initialization of the Process Tree

62 陳鍾誠 - 112/04/21

Page 63: 2. Processes

2.5.3. Interprocess Communication in MINIX 3

63 陳鍾誠 - 112/04/21

Page 64: 2. Processes

2.5.4. Process Scheduling in MINIX 3

Round-Robin

64 陳鍾誠 - 112/04/21

Page 65: 2. Processes

2.6. Implementation of Processes in MINIX 3

2.6.1. Organization of the MINIX 3 Source Code 2.6.2. Compiling and Running MINIX 3 2.6.3. The Common Header Files 2.6.4. The MINIX 3 Header Files 2.6.5. Process Data Structures and Header Files 2.6.6. Bootstrapping MINIX 3 2.6.7. System Initialization 2.6.8. Interrupt Handling in MINIX 2.6.9. Interprocess Communication in MINIX 3 2.6.10. Scheduling in MINIX 3 2.6.11. Hardware-Dependent Kernel Support 2.6.12. Utilities and the Kernel Library

65 陳鍾誠 - 112/04/21

Page 66: 2. Processes

2.6.1. Organization of the MINIX 3 Source Code

66 陳鍾誠 - 112/04/21

Page 67: 2. Processes

MINIX 的原始檔列表 (1) boot/ : 啟動相關的程式碼 (addaout.c, boot.c/h, bootlock.s, boothead.s, bootimage.c, doshead.s, image.h, installboot.c, jumpboot.s, masterboot.s, mkfhead.s, mkfile.c, rawfs.c/h kernel/ : 行程相關的程式碼 (clock.c, config.c/h, debug.c/h, exception.c, glo.h, i8259.c, ipc.h, kernel.h, klib88.s, klib386.s, klib.s, main.c, mpx88.s, mpx386.s, mpx.s, priv.h, proc.c/h, protect.c/h, proto.h, sconst.h, start.c, system.c, table.c, type.h, utility.c, system/ : do_xx 的程式 , (do_abort.c, do_copy.c, dodevio.c, do_endksig.c, do_exec.c, do_exit.c, do_fork.c, do_getinfo.c, do_getksig.c, do_int86.c,

do_iopenable.c, do_irqctl.c, dodo_exec.c, do_fork.c, do_kill.c, do_memset.c,

do_newmap.c, do_nice.c, do_privctl.c, do_sdevio.c, do_segctl.c, do_setalarm.c, do_sigreturn.c, do_sigend.c, do_times.c, do_trace.c, do_umap.c, do_unused.c, do_vcopy.c, do_vdevio.c)

67 陳鍾誠 - 112/04/21

Page 68: 2. Processes

MINIX 的原始檔列表 (2) servers/ : 訊息傳遞相關的程式碼 (No file) ds/ : Data Store Server, (main.c, store.c/h, glo.h, inc.h, proto.h) fs/ : File System, (buf.h, cache.c, cache2.c, cdprobe.c, device.c, dmap.c, file.h, filedes.c, fproc.h, fs.h, glo.h, inode.c/h, link.c, lock.c/h, main.c, misc.c, mount.c, open.c, param.h, path.c, pipe.c, protect.c, proto.h, read.c, select.c/h, stadir.c, super.c/h, table.c, time.c, timers.c, type.h, utility.c, write.c) inet/ : Networking, (buf.c, clock.c, inet.c, inet_config.c, mnx_eth.c, mq.c, qp.c, sha2.c, sr.c, stacktrace.c, version.c) generic/: 網路通用程式 , *arp.c/h, assert.h, buf.h, clock.h, eth.c/h, eth_int.h, event.c/h, icmp.c/h, icmp_lib.h, io.c/h, ip.c/h, ip_eth.c/h, ip_int.h, ip_ioctl.c/h, ip_lib.c, ip_ps.c, ip_read.c, ip_write.c, ipr.c/h, psip.c/h, rand256.c/h, sr.h, tcp.c/h, tcp_int.h, tcp_lib.c, tcp_recv.c, tcp_send.c, type.h, udp.c, udp.h, udp_int.h) minix3/ : 只有 queryparam.c, .h

68 陳鍾誠 - 112/04/21

Page 69: 2. Processes

MINIX 的原始檔列表 (3) init/ : 只有 init.c , 是所有 Process 的 Father Process. is/ : information server, 除錯資訊的 dump.(dump.c, dmp_ds.c, dmp_fs.c, dmp_kernel.c, dmp_pm.c, dmp_rs.c, glo.h, inc.h, is.h, main.c, proto.h) pm/ : Process Management, 包含 (alloc.c, break.c, const.h, exec.c, forkexit.c, getset.c, glo.h, main.c, misc.c, mproc.h, param.h, pm.h, proto.h, signal.c, table.c, time.c, timers.c,trace.c, type.h, utility.c rs/ : Reincarnation( 輪迴轉世 ) Server, (inc.h, main.c, manager.c/h, proto.h, service.c) sm/ : System Process Manager, ( 沒有 *.c 只有一個 makefile)

69 陳鍾誠 - 112/04/21

Page 70: 2. Processes

MINIX 的原始檔列表 (4) drivers/ : IO 驅動程式 (drivers.h) at_wini/ : driver for the IBM-AT winchester controller. bios_wini/: hard disk driver that uses the ROM BIOS. cmos/ : access the CMOS chip to get or set the system time. dp8390/ : Ethernet device driver for NS dp8390 based ethernet cards. (dp8390.c, ne2000.c, rtl9029.c, wdeth.c) dpeth/ : Ethernet device driver for 3Com Etherlink (3c501) boards, 包含 3c501.c, 3c503.c, 3c509.c, 8390.c, devio.c, dp.c, ne.c, netbuff.c, wd.c, floppy/ : Floppy Disk Driver, 包含 floppy.c fxp/ : Ethernet device driver for Intel 82557, 82558, 82559, 82550, and 82562 fast ethernet controllers. 包含 : fxp.c ( 與裝置相關 ), mii.c ( 與裝置無關 ) lance/ : Ethernet driver for AMD LANCE based ethernet cards. (lance.c) libdriver/: Device Independent Device Driver Interface. (driver.c, drvlib.c) libpci/ : PCI 插槽 (pci.c, pci_table.c, pci_amd.h, pci_intel.h, pci_via.h, pci.h) log/ : system log device (log.c, diag.c, kput.c) memory/ : (memory.c) printer/ : printer driver. It is a fairly simple driver,supporting only one printer. Characters that are written to the driver are written to the printer without any changes at all. random/ : random number generator, (main.c, random.c, sha2.c) rescue/ : rescue device driver (rescue.c) rtl8138/ : device driver for Realtek rtl8139 based ethernet cards. sb16/ : driver for a DSP (Digital Sound Processor) on a SoundBlaster 16 soundcard. tty/ : terminal driver (tty.c, rs232.c, pty.c, keyboard.c, console.c, vidcopy.c)

70 陳鍾誠 - 112/04/21

Page 71: 2. Processes

MINIX 的原始檔列表 (5) tools/ : 工具軟體 (Makefile, mkboot, tell_config) include/ : *.h 的表頭檔 (a.out.h, alloca.h, ansi.h, assert.h, configfile.h, ctype.h, courses.h, dirent.h, env.h, errno.h, fcntl.h, float.h, grp.h, inttype.h, lib.h, libgen.h, libutil.h, limits.h, locale.h, math.h, mathconst.h, netdb.h, pwd.h, regex.h, regexp.h, setjmp.h, sgtty.h, signal.h, stdarg.h, stddef.h, stdint.h, stdio.h, stdlib.h, string.h, tar.h, termcap.h, termios.h, time.h, timers.h, tools.h, ttyent.h, unistd.h, utime.h, utmp.h) arpa/ : inet.h ibm/ : bios.h, cmos.h, cpu.h, diskparm.h, int86.h, interrupt.h, memory.h, partition.h, portio.h, ports.h minix/ : bitmap.h, callnr.h, cdrom.h, com.h, config.h, const.h, devio.h, dl_eth.h, dmap.h, fslib.h, ioctl.h, ipc.h, jmp_buf.h, keymap.h, minlib.h, partition.h, paths.h, sound.h, swap.h, sys_config.h, syslib.h, sysutil.h, type.h, u64.h,

71 陳鍾誠 - 112/04/21

Page 72: 2. Processes

MINIX 的原始檔列表 (6) net/ : hton.h, if.h, ioctl.h, netlib.h gen/ : arp_io.h, dhcp.h, eth_hdr.h, eth_io.h, ether.h, icmp.h, icmp_hdr.h, if_ether.h, in.h, inet.h, ip_hdr.h, ip_io.h, nameser.h, netdb.h, oneCsum.h, psip_hdr.h, psip_io.h, resolv.h, rip.h, route.h, socket.h, tcp.h, tcp_hdr.h, tcp_io.h, udp.h, udp_hdr.h, udp_io.h, vjhc.h netinet/ : in.h, tcp.h sys/ : asynchio.h, dir.y, file.h, ioc_cmos.h, ioc_disk.h, ioc_file.h, ioc_memory.h, ioc_scsi.h, ioc_sound.h, ioc_tape.h, ioc_tty.h, ioctl.h, mtio.h, param.h, ptrace.h, resource.h, select.h, sigcontext.h, socket.h, stat.h, statfs.h, svrctl.h, time.h, types.h, uio.h, un.h, utsname.h, wait.h

72 陳鍾誠 - 112/04/21

Page 73: 2. Processes

2.6.2. Compiling and Running MINIX 3

Rebuild MINIX Operating System cd /usr/src/tools/ make

Make Bootable image su make clean time make image

Make bootable floppy make fdboot

type fd0 when you are asked to complete the path.

73 陳鍾誠 - 112/04/21

Page 74: 2. Processes

Memory Layout

74 陳鍾誠 - 112/04/21

Page 75: 2. Processes

2.6.3. The Common Header Files

75 陳鍾誠 - 112/04/21

0~ 6200 行都是 Header File 不太重要,可先忽略,看內容時再回頭參考

Page 76: 2. Processes

Data Type Size

76 陳鍾誠 - 112/04/21

Page 77: 2. Processes

2.6.4. The MINIX 3 Header Files

77 陳鍾誠 - 112/04/21

Page 78: 2. Processes

Seven Message Type used in MINIX

78 陳鍾誠 - 112/04/21

Page 79: 2. Processes

2.6.5. Process Data Structures and Header Files

Discriptor table Define how system resources are used and prevent process from accessing

memory assigend to other processes.

GDT : Global Descriptor Table

LDT : Local Descriptor Table

IDT : Interrupt Descriptor Table

79 陳鍾誠 - 112/04/21

Page 80: 2. Processes

Privilege Table

80 陳鍾誠 - 112/04/21

Page 81: 2. Processes

2.6.6. Bootstrapping MINIX 3

Boot Process : SEC 2.6, page 155.

When the system is started, the hardware (actually, a program in ROM) reads the first sector of the boot disk, copies it to a fixed location in memory, and executes the code found there.

On an unpartitioned MINIX3 diskette, the first sector is a bootblock which loads the boot program, as in Fig. 2-36(a).

Hard disk are partitioned, and the program on the first sector (called master boot on MINIX sytems) first relocates itself to a different memory region, the reads the partition table, loaded with it from the first sector. Then it load and executes the first sector of the active partition, as shown in Fig. 2-36(b).

Boot block : The first sector (512 byte).

81 陳鍾誠 - 112/04/21

Page 82: 2. Processes

Disk Structure for Bootstrapping

82 陳鍾誠 - 112/04/21

Page 83: 2. Processes

Making boot program The MINIX 3 boot sector is modified at the time it is written to the

disk by a special program called installboot which writes the boot sector and patches into it the disk address of a file named boot on its partition or subpartition

In MINIX 3, the standard location for the boot program is in a directory of the same name, that is, /boot/boot. But it could be anywhere – the patching of the boot sector just mentioned locates the disk sectors from which it is to be loaded. This is necessary because previous to loading boot there is no way to use directory and filenames to find a file.

83 陳鍾誠 - 112/04/21

Page 84: 2. Processes

Boot Parameters

84 陳鍾誠 - 112/04/21

Page 85: 2. Processes

Reference for MINIX booter Boot Sequence Overview

http://www.os-forum.com/minix/boot/bootsequence.php

85 陳鍾誠 - 112/04/21

Page 86: 2. Processes

2.6.7. System Initialization

86 陳鍾誠 - 112/04/21

Page 87: 2. Processes

8088 or 80386 ?

87 陳鍾誠 - 112/04/21

Page 88: 2. Processes

2.6.8. Interrupt Handling in MINIX

88 陳鍾誠 - 112/04/21

Page 89: 2. Processes

Interrupt V.S. System Call

89 陳鍾誠 - 112/04/21

Page 90: 2. Processes

8259 Interrupt Controller

90 陳鍾誠 - 112/04/21

Page 91: 2. Processes

System Startup - Restart

91 陳鍾誠 - 112/04/21

Page 92: 2. Processes

2.6.9. Interprocess Communication in MINIX 3

92 陳鍾誠 - 112/04/21

Page 93: 2. Processes

93 陳鍾誠 - 112/04/21

Page 94: 2. Processes

2.6.10. Scheduling in MINIX 3

94 陳鍾誠 - 112/04/21

Page 95: 2. Processes

Process Queue

95 陳鍾誠 - 112/04/21

Page 96: 2. Processes

2.6.11. Hardware-Dependent Kernel Support

96 陳鍾誠 - 112/04/21

Page 97: 2. Processes

2.6.12. Utilities and the Kernel Library

97 陳鍾誠 - 112/04/21

Page 98: 2. Processes

The format of an Intel Segment Descriptor

98 陳鍾誠 - 112/04/21

Page 99: 2. Processes

2.7. The System Task in MINIX 3

2.7.1. Overview of the System Task 2.7.2. Implementation of the System Task 2.7.3. Implementation of the System

Library

99 陳鍾誠 - 112/04/21

Page 100: 2. Processes

2.7.1. Overview of the System Task

100 陳鍾誠 - 112/04/21

Page 101: 2. Processes

Message types accepted by the system task. (1)

101 陳鍾誠 - 112/04/21

Page 102: 2. Processes

Message types accepted by the system task. (2)

102 陳鍾誠 - 112/04/21

Page 103: 2. Processes

2.7.2. Implementation of the System Task

103 陳鍾誠 - 112/04/21

Page 104: 2. Processes

2.7.3. Implementation of the System Library

104 陳鍾誠 - 112/04/21

Page 105: 2. Processes

How many message passing per read required ?

105 陳鍾誠 - 112/04/21

Page 106: 2. Processes

2.8. The Clock Task in MINIX 3

2.8.1. Clock Hardware 2.8.2. Clock Software 2.8.3. Overview of the Clock Driver in

MINIX 3 2.8.4. Implementation of the Clock Driver

in MINIX 3

106 陳鍾誠 - 112/04/21

Page 107: 2. Processes

2.8.1. Clock Hardware

107 陳鍾誠 - 112/04/21

Page 108: 2. Processes

108 陳鍾誠 - 112/04/21

Page 109: 2. Processes

2.8.2. Clock Software

109 陳鍾誠 - 112/04/21

Page 110: 2. Processes

110 陳鍾誠 - 112/04/21

Page 111: 2. Processes

111 陳鍾誠 - 112/04/21

Page 112: 2. Processes

2.8.3. Overview of the Clock Driver in MINIX 3

The Clock Task The Clock Interrupt Handler Watchdog Timers Millisecond Timing Summary of Clock Services

112 陳鍾誠 - 112/04/21

Page 113: 2. Processes

The Clock Task

113 陳鍾誠 - 112/04/21

Page 114: 2. Processes

The Clock Interrupt Handler

114 陳鍾誠 - 112/04/21

Page 115: 2. Processes

Watchdog Timers

115 陳鍾誠 - 112/04/21

Page 116: 2. Processes

Millisecond Timing

116 陳鍾誠 - 112/04/21

Page 117: 2. Processes

Summary of Clock Services

117 陳鍾誠 - 112/04/21

Page 118: 2. Processes

2.8.4. Implementation of the Clock Driver in MINIX 3

118 陳鍾誠 - 112/04/21