42
Operating Systems, 122 Practical Session 11 File Systems & Midterm 2012 1

Operating Systems, 122

Embed Size (px)

DESCRIPTION

Operating Systems, 122. Practical Session 11 File Systems & Midterm 2012. Quick recap. Files are an abstraction mechanism Several file types: User files (regular),Directory files, Special files (Block, Char) Access: sequentially (e.g. tapes) or random access (disk) - PowerPoint PPT Presentation

Citation preview

Page 1: Operating Systems, 122

Operating Systems, 122

Practical Session 11 File Systems & Midterm 2012

1

Page 2: Operating Systems, 122

Quick recap

โ€ข Files are an abstraction mechanismโ€ข Several file types: User files (regular),Directory

files, Special files (Block, Char)โ€ข Access: sequentially (e.g. tapes) or random

access (disk)โ€ข Data: structured (records) or unstructured (set

of bits and bytes)

2

Page 3: Operating Systems, 122

File system layout (Tanenbaum)

3

Page 4: Operating Systems, 122

Quick recap: Index-Nodes (i-nodes)

โ€ข The superblock object represents the entire file system and provides access to index-nodes.

โ€ข Each i-node is a data structure containing pointers to the disk blocks that contain the actual file contents.

โ€ข An i-node corresponds to a single file.โ€ข An i-node needs to be in the main memory only if the

correspondent file is open.โ€ข Besides the data blocks pointers, the i-node also

contains information on the file permissions, owner, etc

4

Page 5: Operating Systems, 122

Quick recap: i-Nodes

General file attributes

The number of hard-links to the file

Usually between 10

and 12

File Size

HardLink count

5

Page 6: Operating Systems, 122

Question 1: i-nodesHow many time will the disk be accessed when a user executes the following command:more /usr/tmp/a.txt

Assume that: 1. The size of 'a.txt' is 1 block. 2. The i-node of the root directory is not in the

memory. 3. Entries 'usr', 'tmp' and 'a.txt' are all located in

the first block of their directories.6

Page 7: Operating Systems, 122

Question 1: i-nodes

Accessing each directory requires at least 2 disk accesses: reading the i-node and the first block. In our case the entry we are looking for is always in the first block so we need exactly 2 disk accesses. According to assumption 2 the root directory's i-node is located on the disk so we need 6 disk accesses (3 directories) until we reach a.txt's i-node index. Since "more" displays the file's content, for a.txt we need its i-node + all the blocks of the file (1 block, according to assumption). Total disk accesses: 6 + 2 = 8.

7

Page 8: Operating Systems, 122

Question 1: i-nodesA similar problem

8

Page 9: Operating Systems, 122

Question 2: i-nodes

The Ofer2000 Operating Systems, based on UNIX, provides the following system call:rename(char *old, char *new)This call changes a fileโ€™s name from โ€˜oldโ€™ to โ€˜newโ€™. What is the difference between using this call, and just copying โ€˜oldโ€™ to a new file, โ€˜newโ€™, followed by deleting โ€˜oldโ€™? Answer in terms of disk access and allocation.

9

Page 10: Operating Systems, 122

Question 2: i-nodes

โ€ข rename - simply changes the file name in the entry of its directory.

โ€ข copy - will allocate a new i-node and the blocks for the new file, and copy the contents of the old file blocks to the new ones.

โ€ข delete - will release the i-node and blocks of the old file. โ€ข copy + delete - is a much more complicated operation for

the Operating System, note that you will not be able to execute it if you do not have enough free blocks or i-nodes left on your disk.

10

Page 11: Operating Systems, 122

Question 3: i-nodes

Write an implementation (pseudo code) of the system call: delete(i-node node)

Which deletes the file associated with node.Assume that: โ€ข node is associated with a regular file, and that delete is not

recursive. โ€ข The i-node has 10 direct block entries, 1 single indirect entry

and 1 double indirect entry. โ€ข You may use the system calls:

read_block(block b) which reads block b from the disk.free_block(block b) and free_i-node(i-node node).

11

Page 12: Operating Systems, 122

Question 3: i-nodesdelete(i-node node){

// remove the direct blocksfor each block b in node.direct do

free_block(b); // remove the single indirect blockssingle <-- read_block(node.single_indirect) for each entry e in single do

free_block(e); free_block(single); // remove the double indirect blocksdouble <-- read_block(node.double_indirect) for each entry e in double do

single <-- read_block(e) for each entry ee in single do

free_block(ee); free_block(single);

free_block(double); // remove the i-nodefree_i-node(node);

} 12

Page 13: Operating Systems, 122

Question 4: i-nodes

What would be the maximal size of a file in a UNIX system with an address size of 32 bits if :

1. The block size is 1K2. The block size is 4K

(The i-node has 10 direct block entries)

13

Page 14: Operating Systems, 122

Question 4: i-nodes

1. Block size: 1Kโ€“ Direct: 10ยท1Kโ€“ Single indirect: each address is 32 bit = 4 byte then

we have 256 pointers to blocks of size 1K (i.e. 256ยท1K)

โ€“ The same idea is applied for double and triple indirect.

In total: 10ยท1K+256ยท1K+256ยท256ยท1K+256ยท256ยท256ยท1K

14

Page 15: Operating Systems, 122

Question 4: i-nodes

1. Block size: 4Kโ€“ Direct: 10ยท4Kโ€“ Single indirect: each address is 32 bit = 4 byte then

we have 1024 pointers to blocks of size 4K (i.e. 1024ยท4K)

โ€“ The same idea is applied for double and triple indirect

In total: 10ยท4K+1024ยท4K+1024ยท1024ยท4K+1024ยท1024ยท1024ยท4K

15

Page 16: Operating Systems, 122

Question 5: i-nodes

Assuming that the size of each block is 1K and the address size is 32 bits (4 bytes). Convert byte address (offset) 1,515,000 in our file to the physical address.

16

Page 17: Operating Systems, 122

Question 5: I-Nodes

Byte number 1,515,000 is calculated as follows:โ€“ 1st byte of the double indirect block is 10k+256k =

272,384โ€“ byte number 1,515,000 is number 1,242,616 in the

double indirect blockโ€“ every single indirect block has 256k bytes --> byte

1,242,616 is in the 5th single indirect block (4*256k = 1,048,576)

โ€“ Every entry is 1k, so byte 194,040 is in the 189th block โ€“ assume that it points to block 123 on the disk

โ€“ within block 123 , it is byte #50417

Page 18: Operating Systems, 122

Operating SystemsMIDTERM 2012

Page 19: Operating Systems, 122

Question 1

(30( ) ืชื–ืžื•ืŸ (schedulingื ืงื•ื“ื•ืช

19

Page 20: Operating Systems, 122

Question 1. ืชืืจื• ื‘ืงืฆืจื” ืืš ื‘ืื•ืคืŸ ืžื“ื•ื™ื™ืง ื•ื‘ืจื•ืจ ืืช ืื•ืคืŸ ื ืง'(5ื. )โ€ข

Round robinืคืขื•ืœืชื ืฉืœ ืฉื ื™ ื”ืืœื’ื•ืจื™ืชืžื™ื ื”ื‘ืื™ื ืœืชื–ืžื•ืŸ: ( fairness. ื‘ืคืจื˜, ืชืืจื• ืื™ื–ื• "ื”ื’ื™ื ื•ืช" )Guaranteed schedulingื•-

ื‘ืชื–ืžื•ืŸ ืžื‘ื˜ื™ื—ื™ื ืฉื ื™ ื”ืืœื’ื•ืจื™ืชืžื™ื.

20

ืคืชืจื•ืŸ:Round robin ' ืืœื’ โ€“ ื”, preemptiveื”ื™ื ื• ืžืฉืš ืืช ื”ืžื’ื“ื™ืจ ืคืจืžื˜ืจ ืืฉืจ time sliceื‘ืขืœ

' . ื‘ืžืฆื‘ ืฉื”ื™ื ื ื”ืชื”ืœื™ื›ื™ื ืžื‘ื™ืŸ ืœืจื™ืฆื” ืชื”ืœื™ื›ื™ื ื‘ื•ื—ืจ ื”ืืœื’ ืชื”ืœื™ืš ื›ืœ readyื™ืงื‘ืœ. ืžืขื’ืœื™ ืกื“ืจ ืคื™ ืขืœ

ื‘ืžืฆื‘: ืœืชื”ืœื™ื›ื™ื ืฉื•ื•ื” ืจื™ืฆื” ื–ืžืŸ ืœืชืช ืฉื•ืืฃ ' readyื”ื•ื’ื ื•ืช ืฉืื™ืŸ. ืžื‘ื˜ื™ื— ื”ืืœื’starvation ื”ืžืžืชื™ื ื™ื ื”ืชื”ืœื™ื›ื™ื ื‘ืžืกืคืจ ื—ืกื•ื ืชื”ืœื™ืš ื›ืœ ืฉืœ ื”ื”ืžืชื ื” ืฉื–ืžืŸ ืžื›ื•ื•ืŸ

ื” ื’ื•ื“ืœ .time sliceื›ืคื•ืœGuaranteed scheduling ' ืืœื’ โ€“ ืœื›ืœ, preemptiveื”ื™ื ื• ืฉื•ื•ื” ืจื™ืฆื” ื–ืžืŸ ืœืชืช ื”ืฉื•ืืฃ

ืฉื”ืชื”ืœื™ืš. ื”ื–ืžืŸ ื‘ื™ืŸ ื”ื™ื—ืก ืืช ื”ืžืฆื™ื™ืŸ ืขืจืš ืฉืœ ืฉืžื™ืจื” ื™ื“ื™ ืขืœ ื–ืืช ืขื•ืฉื” ื”ืชื”ืœื™ื›ื™ื ' . ื”ืงื˜ืŸ ื”ืขืจืš ืขื ื”ืชื”ืœื™ืš ืืช ืœื”ืจื™ืฅ ื™ื‘ื—ืจ ื”ืืœื’ ืœื• ืฉืžื’ื™ืข ื”ื–ืžืŸ ืœื‘ื™ืŸ ื‘ืคื•ืขืœ ืงื™ื‘ืœ

ื‘ื™ื•ืชืจ. , " "- ' ื™ื”ื™ื”: ืฉืœื ืžื‘ื˜ื™ื— ื•ื‘ื›ืš ื‘ื™ื•ืชืจ ืžืงื•ืคื— ื” ื”ืชื”ืœื™ืš ืืช ืฉืœื‘ ื‘ื›ืœ ื‘ื•ื—ืจ ื”ืืœื’ ื”ื•ื’ื ื•ืช

starvation .ืœ ืฉื™ืฆืื• ื‘ื”ืชื”ืœื™ื›ื™ื ื’ื ืžืชื—ืฉื‘ ื”ื™ื—ืก ืขืœ I/Oื—ื™ืฉื•ื‘ ืื•ืชื ื™ืคืฆื” ื•ื‘ื›ืš. ืฉื™ืื‘ื“ื• ื”ื–ืžืŸ

Page 21: Operating Systems, 122

Question 1

kernel. ื ื ื™ื— ื›ื™ ื‘ืžืขืจื›ืช ืงื™ื™ืžื™ื ืžืกืคืจ ื—ื•ื˜ื™ ืงืจื ืœ ) ื ืง'(5ื‘. )โ€ขthreads ืฉื”ื )CPU-bound ื•ืžืกืคืจ ื—ื•ื˜ื™ ืงืจื ืœ ืฉื”ื I/O-bound .

ืื™ื–ื” ืžื‘ื™ืŸ ืฉื ื™ ื”ืืœื’ื•ืจื™ืชืžื™ื ืžืชืื™ื ื™ื•ืชืจ ืขื‘ื•ืจ ืžืขืจื›ืช ื›ื–ื•? ื ืžืงื• ื‘ืงื™ืฆื•ืจ ืืš ื‘ืื•ืคืŸ ืžื“ื•ื™ื™ืง ื•ื‘ืจื•ืจ.

21

ืคืชืจื•ืŸ:ืืœื’ื•ืจื™ืชื ืืช ื ืขื“ื™ืฃ ื–ื” ืžืคื ื™, Guaranteed schedulingื‘ืžืงืจื” ื•ื–ืืช

ื” ืฉืชื”ืœื™ื›ื™ ื™ื•ื•ื“ื ื‘ืคื—ื•ืช IO-boundืฉื”ืืœื’ื•ืจื™ืชื ื™ืฉืชืžืฉื• ืœืจื•ื‘ ืืฉืจ . ื›ื–ื” ื‘ืžื•ื‘ืŸ ืžื•ื›ื ื™ื ื”ื ื‘ืืฉืจ ืœืจื•ืฅ ืืคืฉืจื•ืช ื™ืงื‘ืœื• ืžืขื‘ื“ ื–ืžืŸ

ืœ ื™ื•ืชืจ ืงืจื•ื‘ ื™ื”ื™ื” ืฉืœื ื• ืืฉืจ SJFื”ืืœื’ื•ืจื™ืชื ืชื”ืœื™ื›ื™ื ืฉื™ืขื“ื™ืฃ ืžื›ื•ื•ืŸ , ืžื‘ื—ื™ื ืช ื™ื•ืชืจ ื”ื•ื’ืŸ ื™ื”ื™ื” ื•ื‘ื ื•ืกืฃ ื™ื•ืชืจ ืงืฆืจ ืžืขื‘ื“ ืœื–ืžืŸ ื–ืงื•ืงื™ื ื™ื”ื™ื•

. ืชื—ืช ืชื”ืœื™ืš ื›ืœ ืฉื™ืงื‘ืœ ื”ื›ื•ืœืœ , R.Rื”ื–ืžืŸ ื–ืžืŸ. ืœืื•ืจืš ื–ืืช ืœืขื•ืžืช ) ื” ) ืœืชื”ืœื™ื›ื™ ืžืขื‘ื“ ื–ืžืŸ ืžื‘ื—ื™ื ืช ื‘ืจื•ืจื” ื”ืขื“ืคื” .CPU-boundืชื”ื™ื”

Page 22: Operating Systems, 122

Question 1

. ื›ืืฉืจ ืขืกืงื ื• ื‘ืžื•ื ื™ื˜ื•ืจื™ื, ืชื™ืืจื ื• ืืช ื”ืžื™ืžื•ืฉื™ื ื ืง'(8ื’. )โ€ข )ื‘ื• ืžื•ื‘ื˜ื— ื›ื™ Hoare( ืžื™ืžื•ืฉ ืžื•ื ื™ื˜ื•ืจ ื‘ืกืžื ื˜ื™ืงื” ืฉืœ 1ื”ื‘ืื™ื.

( 2ื”ื—ื•ื˜ ื”ืžืงื‘ืœ ืืช ื”ืกื™ื’ื ืœ ื”ื•ื ื”ื‘ื ืœืจื•ืฅ ื‘ืžื•ื ื™ื˜ื•ืจ(, ื•-, ื‘ื• ืœื ืžืชืงื™ื™ืžืช ื”ื‘ื˜ื—ื” ืฉื›ื–ื• ื•ื‘ื›ืœ ืคืขื Javaืžื•ื ื™ื˜ื•ืจ ืฉืœ

( ื”ืžืขื•ื ื™ื™ื ื™ื ืœื”ื™ื›ื ืก ืืœื™ื• ื•ื ืžืฆืื™ื ื‘ืžืฆื‘ threadsื”ื—ื•ื˜ื™ื )ready.ืžืชื—ืจื™ื ืขืœ ื”ื›ื ื™ืกื”

ื ื ื™ื— ื›ื™ ื‘ืžืขืจื›ืช ื™ืฉื ื ืžืกืคืจ ื—ื•ื˜ื™ื ื”ืžืฉืชืžืฉื™ื ื‘ืžื•ื ื™ื˜ื•ืจ. ื”ืื ื”ื‘ื—ื™ืจื” ื‘ื™ืŸ ืฉื ื™ ื”ืืœื’ื•ืจื™ืชืžื™ื ื”ื "ืœ ืœืชื–ืžื•ืŸ ืชืฉืคื™ืข ื™ื•ืชืจ

ืื• ืขืœ ืžื•ื ื™ื˜ื•ืจ Hoareืขืœ ืžื™ืžื•ืฉ ื”ืžืฉืชืžืฉ ื‘ืžื•ื ื™ื˜ื•ืจ ืžื˜ื™ืคื•ืก ? ื ืžืงื• ืชืฉื•ื‘ืชื›ื ื‘ืงื™ืฆื•ืจ ืืš ื‘ืื•ืคืŸ ืžื“ื•ื™ื™ืง ื•ื‘ืจื•ืจ.Javaืฉืœ

22

Page 23: Operating Systems, 122

Question 1

ืคืชืจื•ืŸ:

, ื•ื–ืืช ืžื›ื•ื•ืŸ Javaื”ื‘ื—ื™ืจื” ืชืฉืคื™ืข ื™ื•ืชืจ ืขืœ ืžื•ื ื™ื˜ื•ืจ ืฉืœ ืฉื‘ืžืฆื‘ ื–ื” ืืœื’ื•ืจื™ืชื ื”ืชื–ืžื•ืŸ ื™ื—ืœื™ื˜ ืœืžืขืฉื” ืžื™

, ืื– Round robinื”ืชื”ืœื™ืš ืฉื™ื›ื ืก ืœืžื•ื ื™ื˜ื•ืจ. ืื ื™ื”ื™ื” ื–ื” ื™ื”ื™ื” ื–ื” ื”ืชื”ืœื™ืš ื”ืžืขื•ื ื™ื™ืŸ ื”ื‘ื ืฉื ื™ืชืงืœ ื‘ื• ื‘ืžืขื‘ืจ

, ืื–ื™ Guaranteed schedulingื”ืžืขื’ืœื™. ืื ื™ื”ื™ื” ื–ื” ื”ืชื”ืœื™ืš ืฉื™ื›ื ืก ืœืžื•ื ื™ื˜ื•ืจ ื™ื”ื™ื” ื”ืชื”ืœื™ืš ื”ืžืขื•ื ื™ื™ืŸ ืฉืงื™ื‘ืœ

, Hoareืืช ื™ื—ืก ื”ื–ืžืŸ ื”ื ืžื•ืš ื‘ื™ื•ืชืจ. ื‘ืžื•ื ื™ื˜ื•ืจ ืžื˜ื™ืคื•ืก ื”ืžื•ื ื™ื˜ื•ืจ ืœืžืขืฉื” ื™ื‘ื—ืจ ืžื™ ื”ืชื”ืœื™ืš ื”ื‘ื ืฉื™ื•ื›ืœ ืœื”ื›ื ืก,

ื•ืœื›ืŸ ืœืืœื’' ื”ืชื–ืžื•ืŸ ืœื ืชื”ื™ื” ื”ืฉืคืขื” ืจื‘ื” ืขืœ ื”ืจื™ืฆื”.

23

Page 24: Operating Systems, 122

Question 1

( ื™ื—ื™ื“ ื™ืฉื ื” CPU). ื ื ื™ื— ื›ื™ ื‘ืžืขืจื›ืช ื‘ืขืœืช ืžืขื‘ื“ ื ืง'(12ื“. )โ€ข ืœืคื ื™ Tืงื‘ื•ืฆืช ื—ื•ื˜ื™ื ืืฉืจ ื›ืœ ืื—ื“ ืžื”ื ืจืฅ ืชืžื™ื“ ืžืฉืš ื–ืžืŸ

. ื ื ื™ื— S ืื•ืจืš ื–ืžืŸ context switch ื•ื›ื™ I/Oืฉื”ื•ื ืขื•ื‘ืจ ืœื”ืžืชื ื” ืœ-ื’ื ื›ื™ ืžืขืจื›ืช

ื‘ืื•ืจืš time-quantum ืขื round robinืžื•ืคืขืœ ืืœื’ื•ืจื™ืชื ืชื–ืžื•ืŸ Q ื ื™ืชืŸ ืœื”ื ื™ื— ื›ื™ ืชืžื™ื“ ื™ืฉื ื ื‘ืžืขืจื›ืช ื—ื•ื˜ื™ื ื”ืžื•ื›ื ื™ื ืœืจื•ืฅ .

ื‘ืžืขืจื›ืช CPU(. ื—ืฉื‘ื• ืืช ื ื™ืฆื•ืœืช ื”-I/O)ืื™ื ื ืžืžืชื™ื ื™ื ืœ-ื›ืคื•ื ืงืฆื™ื” ืฉืœ ืคืจืžื˜ืจื™ื ืืœื• ืขื‘ื•ืจ ื”ืžืงืจื™ื ื”ื‘ืื™ื:

.IQ>T.IIS<Q<T

.IIIQืฉื•ืืฃ ืœืืคืก

24

Page 25: Operating Systems, 122

Question 1

ืคืชืจื•ืŸ:.I ื‘ืžืงืจื” ื–ื”Q ื—ืกืจ ืžืฉืžืขื•ืช. ื›ืœ ืชื”ืœื™ืš ื™ืจื•ืฅ ื–ืžืŸ T ื•ืื– ื ื‘ื–ื‘ื– S ื–ืžืŸ ื‘

C.S :ืœื›ืŸ ื ื™ืฆื•ืœืช ื”ืžืขื‘ื“ ืชื”ื™ื” .

.II:ื”ืชืงื‘ืœื• ืชืฉื•ื‘ื•ืช ื‘ื”ืŸ ื”ื•ื ื— ืฉ . ื‘ืžืฆื‘ ื›ื–ื” ื ืงื‘ืœ ื ื™ืฆื•ืœืช ืžืขื‘ื“

.III:ื›ืืฉืจ ื ืงื‘ืœ ืขืœ ืคื™ ื”ื ื•ืกื—ื” ืžื”ืกืขื™ืฃ ื”ืงื•ื“ื

25

Page 26: Operating Systems, 122

Question 2

(35 ) ื•ื—ื•ื˜ื™ื ืชื”ืœื™ื›ื™ื ื ืงื•ื“ื•ืช

26

Page 27: Operating Systems, 122

Question 2

. ืžื”ื ื”ืคืœื˜ื™ื ื”ืืคืฉืจื™ื™ื ืฉืœ ื”ืชื•ื›ื ื™ืช ื”ื‘ืื”, ืขืœ ื ืง'(15ื. )โ€ข? ืขื‘ื•ืจ ื›ืœ ืคืœื˜, ืฆื™ื™ื ื• ื”ืื ื”ื•ื ืžื•ื“ืคืก ืขืœ a.txtื”ืžืกืš ื•ื‘ืงื•ื‘ืฅ

ื™ื“ื™ ื”ืชื”ืœื™ืš ื”ืื‘, ืชื”ืœื™ืš ื‘ืŸ ืื• ื—ื•ื˜. ื”ื ื™ื—ื• ื›ื™ ื”ืชื•ื›ื ื™ืช ืจืฆื” ืขืœ ืžืขืจื›ืช ืขื ืžืขื‘ื“ ื™ื—ื™ื“, ื•ื›ื™ ืคืชื™ื—ืช ื”ืงื•ื‘ืฅ ืžืฆืœื™ื—ื”. ื”ื ื™ื—ื• ื›ื™

ื‘ื›ืœ ืชื”ืœื™ืš ื”ืžื–ื”ื™ื ืฉืœ ื”ื—ื•ื˜ื™ื ื ืงื‘ืขื™ื ื‘ืกื“ืจ ืขื•ืœื”, ื”ื—ืœ . ื‘ื ื•ืกืฃ, ื”ื ื™ื—ื• ื›ื™ ื›ืืฉืจ ืชื›ื ื™ืช ืžืจื•ื‘ืช ื—ื•ื˜ื™ื ืžื‘ืฆืขืช 1ืž-

ืจืง ื”ื—ื•ื˜ ืืฉืจ ื‘ื™ืฆืข ืืช ื”ืงืจื™ืื” ืžืฉืชื›ืคืœ. ื”ื ื™ื—ื• forkืคืขื•ืœืช ื’ื ื›ื™ ื›ืœ ืคืขื•ืœืช ื”ื“ืคืกื” ื”ื™ื ืื˜ื•ืžื™ืช )ื›ืœื•ืžืจ, ื”ื™ื ืœื ืชื•ืคืจืข

(.context switchื‘ืืžืฆืข ืขืœ ื™ื“ื™

27

Page 28: Operating Systems, 122

Question 2int main() { int fd; pthread_t tid[2]; printf("STARTED\n"); fd = dup(STDOUT_FILENO); close(STDOUT_FILENO); open("a.txt", O_WRONLY | O_CREAT | O_TRUNC);

pthread_create(&tid[0], NULL, foo, NULL); fork(); pthread_create(&tid[1], NULL, foo, NULL); sleep(10); // sleep for 10 seconds printf("after fork x=%d\n", x);

close(STDOUT_FILENO); dup(fd); printf("FINISHED\n");

return 0;}

28

int x = -1;

void foo() { printf("x=%d\n", x); x = pthread_self(); // get thread ID}

Page 29: Operating Systems, 122

Question 2On screen: STARTEDFINISHEDFINISHED

a.txt: x = -1 (parent)x = -1/1/2 (parent)x = -1/1 (child)after fork x = 1/2 (parent)after fork x = 1 (child) * first 3 lines can come in any order* last 2 lines could come in any order

29

ืคืชืจื•ืŸ:

Page 30: Operating Systems, 122

Question 2. ื”ื ื™ื—ื• ื›ื™ ื‘ืจืฉื•ืชื›ื ืฉืจืช ืื™ื ื˜ืจื ื˜ ื”ืžืกืคืง ื‘ืงืฉื•ืช ื ืง'(10ื‘. )โ€ข

ืžืฉืชืžืฉื™ื. ืœืฉืจืช ืžื‘ื ื” ื ืชื•ื ื™ื ื‘ื• ื”ื•ื ืฉื•ืžืจ ืขืจื›ื™ื ืฉื”ื•ื‘ืื• ืขื‘ื•ืจ ื‘ืงืฉื•ืช ืฉื”ืชื‘ืฆืขื• ืœืื—ืจื•ื ื”. ื‘ื›ืœ ืคืขื ืฉืžืชืงื‘ืœืช ื‘ืงืฉื”, ืžืชื‘ืฆืขืช ืคืขื•ืœืช ื—ื™ืคื•ืฉ ืžื”ื™ืจื” ื‘ืžื‘ื ื” ื”ื ืชื•ื ื™ื. ื‘ืžื™ื“ื” ื•ื”ืžื™ื“ืข ื”ื ื“ืจืฉ ืื™ื ื• ืžืฆื•ื™ ื‘ืžื‘ื ื” ื”ื ืชื•ื ื™ื,

(. ื”ื ื™ื—ื• ื›ื™ ืœืฉืจืช ืžืขื‘ื“ ื™ื—ื™ื“. ื›ืžื• I/Oืžืชื‘ืฆืข ืชื”ืœื™ืš ืื—ื–ื•ืจ ืžืชื•ืš ื“ื™ืกืง ) ืืช h ืœื“ื™ืกืง ื‘ืžืงื‘ื™ืœ. ื ืกืžืŸ ื‘-I/Oื›ืŸ ื”ื ื™ื—ื• ื›ื™ ืœื ื ื™ืชืŸ ืœื‘ืฆืข ืฉืชื™ ืคืขื•ืœื•ืช

ืืช ื”ื–ืžืŸ ื”ื ื“ืจืฉ cื”ื”ืกืชื‘ืจื•ืช ืœืžืฆื™ืืช ื”ืžื™ื“ืข ื”ื ื“ืจืฉ ื‘ืžื‘ื ื” ื”ื ืชื•ื ื™ื, ื‘- ืืช ืžืฉืš ื”ื–ืžืŸ ื”ื ื“ืจืฉ ืœืื—ื–ื•ืจ ืžื™ื“ืข tืœืื—ื–ื•ืจ ืžื™ื“ืข ืžืžื‘ื ื” ื”ื ืชื•ื ื™ื ื•ื‘-

(. ื‘ืคืจื˜, ื›ืืฉืจ ื”ืžื™ื“ืข ื”ื ื“ืจืฉ ืœื ื ืžืฆื ื‘ืžื‘ื ื” t>cืžื”ื“ื™ืกืง )ื‘ืจื•ืจ ื›ื™ )ืจืืฉื™ืช ื™ืฉ ืœืงืจื•ื ืืช ื”ืžื™ื“ืข ืžืŸ t+cื”ื ืชื•ื ื™ื, ืžืฉืš ื”ืื™ื—ื–ื•ืจ ื”ื›ื•ืœืœ ื”ื•ื

ื”ื“ื™ืกืง ืœืžื‘ื ื” ื”ื ืชื•ื ื™ื ื•ืื– ืœืงืจืื• ืžืžื‘ื ื” ื”ื ืชื•ื ื™ื(. ื ืชื•ืŸ ื›ื™ ืฉืชื™ ื‘ืงืฉื•ืช ื‘ืœืชื™ ืชืœื•ื™ื•ืช ื–ื• ื‘ื–ื•, ื›ืœ ืื—ืช ืžื”ืŸ ืœืžื™ื“ืข ืฉื•ื ื”, ืžื’ื™ืขื•ืช ื‘ื• ื–ืžื ื™ืช ืœืžืขืจื›ืช.

ืžื”ื• ืžืฉืš ื”ื–ืžืŸ ื”ืฆืคื•ื™ ืขื“ ืœื”ืฉืœืžืช ื”ื‘ืงืฉื•ืช ื‘ืžืขืจื›ืช ืžื‘ื•ืกืกืช ื—ื•ื˜ื™ ืžืฉืชืžืฉโ€“(user space threads?)

ืžื”ื• ืžืฉืš ื”ื–ืžืŸ ื”ืฆืคื•ื™ ืขื“ ืœื”ืฉืœืžืช ื”ื‘ืงืฉื•ืช ื‘ืžืขืจื›ืช ืžื‘ื•ืกืกืช ื—ื•ื˜ื™ ืงืจื ืœ โ€“(kernel threads?)

30

Page 31: Operating Systems, 122

Question 2

ืคืชืจื•ืŸ:ื—ื•ื˜ื™ ืžืฉืชืžืฉ:โ€“

2ch^2+2(c+t)(1-h)^2+h(1-h)(4c+2t)

ื—ื•ื˜ื™ ืงืจื ืœ: โ€“2ch^2+ (c+2t)(1-h)^2+h(1-h)(2c+t+c+t)

31

ืžืชื—ืœืง ืœืฉื ื™ ืžืงืจื™ื ืกื™ืžื˜ืจื™ื™ื ื›ืœ ืื—ื“ 2c+t

2c+tื’ื ืžืชื—ืœืง ืœืฉื ื™ ืžืงืจื™ื: ื”ืฆืœื—ื” + ื›ื™ืฉืœื•ืŸ ื ื•ืชืŸ c<t ื›ื™ c+t ื›ื™ืฉืœื•ืŸ + ื”ืฆืœื—ื” ื ื•ืชืŸ

c+tืคืขืžื™ื™ื ื’ื™ืฉื” ืฉืœ

c ื”ืฉื ื™ ืžืชื—ื™ืœ cืฉืื—ื“ ืžืกื™ื™ื ืฉืœ ื”ืจืืฉื•ืŸtื‘ื–ืžืŸ ื”

Page 32: Operating Systems, 122

Question 2. ื ืชื•ืŸ ืงื˜ืข ื”ืงื•ื“ ื”ื‘ื: ื ืง'(10ื’. )โ€ข

void sigchld_handler(int s) { printf(โ€œSโ€);}int main(){ signal(SIGCHLD, sigchld_handler); signal_block(SIGCHLD); if (fork() != 0) { printf(โ€œAโ€); signal_unblock(SIGCHLD); printf(โ€œBโ€); wait (); printf(โ€œCโ€); } else { printf(โ€œDโ€); }}

32

Page 33: Operating Systems, 122

Question 2

ื—ื•ืกืžื•ืช signal_unblock ื•ื›ืŸ signal_blockื™ื“ื•ืข ื›ื™ ื”ืคืงื•ื“ื•ืช ื•ืžืฉื—ืจืจื•ืช ื—ืกื™ืžื” ืœืกื™ื’ื ืœื™ื. ืฉืจื˜ื˜ื• ื’ืจืฃ ืžื›ื•ื•ืŸ ื”ืžืชืืจ ืืช

ื”ืคืœื˜ื™ื ื”ืืคืฉืจื™ื™ื ืœืงื•ื“ ื–ื”. ื›ืœ ืฆื•ืžืช ื‘ื’ืจืฃ ืชืกืžืœ ื”ื“ืคืกื” ื•ื›ืœ ืงืฉืช ืžื›ื•ื•ื ืช ืชื™ื™ืฆื’ ื™ื—ืก ืกื“ืจ ืžืชื—ื™ื™ื‘ ื‘ื™ืŸ ื”ื“ืคืกื•ืช. ืœื“ื•ื’ืžื, ืื

" X" ื•ื›ื™ ื”ื”ื“ืคืกื” ืฉืœ "Z" ื•- "X", "Yืขืค"ื™ ืงื•ื“ ืžืกื•ื™ื ื™ื“ื•ืข ื›ื™ ื™ื•ื“ืคืกื• "" ื™ื›ื•ืœ ืœื”ื•ืคื™ืข ืœืคื ื™ ืื• Z" )ืืš "Yืชื•ืคื™ืข ื‘ื”ื›ืจื— ืœืคื ื™ ื”ื”ื“ืคืกื” ืฉืœ "

ืื—ืจื™ ื›ืœ ืื—ืช ืžืŸ ื”ื”ื“ืคืกื•ืช ื”ืื—ืจื•ืช(, ื™ืชืงื‘ืœ ื”ื’ืจืฃ ื”ื‘ื:

33

YX Z

Page 34: Operating Systems, 122

Question 2

34

A

S

B

C

D

ืคืชืจื•ืŸ:

void sigchld_handler(int s) { printf(โ€œSโ€);}int main(){ signal(SIGCHLD, sigchld_handler); signal_block(SIGCHLD); if (fork() != 0) { printf(โ€œAโ€); signal_unblock(SIGCHLD); printf(โ€œBโ€); wait (); printf(โ€œCโ€); } else { printf(โ€œDโ€); }}

Page 35: Operating Systems, 122

Question 3

(35 ) ืกื™ื ื›ืจื•ื ื™ื–ืฆื™ื” ื ืงื•ื“ื•ืชื”ื“ื“ื™ืช ืžื ื™ืขื”

35

Page 36: Operating Systems, 122

Question 3

. ื‘ื›ื™ืชื” ื”ื’ื“ืจื ื• ืืช ื”ืชื ืื™ื ื—ื•ืคืฉ ืžืงื™ืคืื•ืŸ ื ืง'(5ื. )โ€ข(deadlock freedom( ื•ื—ื•ืคืฉ ืžื”ืจืขื‘ื” )starvation freedom )

ืขื‘ื•ืจ ืืœื’ื•ืจื™ืชืžื™ื ืœืžื ื™ืขื” ื”ื“ื“ื™ืช. ื›ืชื‘ื• ืืช ื”ื”ื’ื“ืจื•ืช ืฉืœ ืชื ืื™ื ืืœื•.ื”ืžื“ื•ื™ื™ืงื•ืช

36

ืคืชืจื•ืŸ:: ืžืงื™ืคืื•ืŸ , ื—ื•ืคืฉ ื”ืงืจื™ื˜ื™ ืœืงื˜ืข ืœื”ื™ื›ื ืก ืžื ืกื” ืชื”ืœื™ื›ื™ื ืงื‘ื•ืฆืช ืื

. ืืœื™ื• ืœื”ื™ื›ื ืก ื™ืฆืœื™ื— ื”ืชื”ืœื™ื›ื™ื ืื—ื“ ืฆืขื“ื™ื ืฉืœ ืกื•ืคื™ ืžืกืคืจ ืœืื—ืจ ืื–ื™: ืžื”ืจืขื‘ื” , ื—ื•ืคืฉ ืื–ื™ ื”ืงืจื™ื˜ื™ ืœืงื˜ืข ืœื”ื™ื›ื ืก ืžื ืกื” ืชื”ืœื™ืš ืื

. ืืœื™ื• ืœื”ื™ื›ื ืก ื™ืฆืœื™ื— ื”ื•ื ืฆืขื“ื™ื ืฉืœ ืกื•ืคื™ ืžืกืคืจ ืœืื—ืจ

Page 37: Operating Systems, 122

Question 3

( ืขื‘ื•ืจ ื‘ืขื™ื™ืช fairness). ื‘ื›ื™ืชื” ื”ื’ื“ืจื ื• ืชื ืื™ ื”ื•ื’ื ื•ืช ื ืง'(10ื‘. )โ€ข.first-in-first-out (FIFO)ื”ืžื ื™ืขื” ื”ื”ื“ื“ื™ืช ื”ืงืจื•ื™

ืฉืœ ืชื ืื™ ื–ื”.ื”ืžื“ื•ื™ื™ืงืชื›ืชื‘ื• ืืช ื”ื”ื’ื“ืจื” โ€“ื”ืืœื’ื•ืจื™ืชื ืœืžื ื™ืขื” ื”ื“ื“ื™ืช ืฉืœ ืคื˜ืจืกื•ืŸ ืœืฉื ื™ ืชื”ืœื™ื›ื™ื ืžื•ื‘ื ื‘ืกื•ืฃ โ€“

? ื ืžืงื• ื‘ืงืฆืจื” ืืš FIFOืฉืืœื” ื–ื•. ื”ืื ื”ืืœื’ื•ืจื™ืชื ืžืงื™ื™ื ืชื ืื™ ื‘ืžื“ื•ื™ื™ืง.

37

ืคืชืจื•ืŸ:ืชื”ืœื™ืš โ€“ -Aืื ื‘ ) ื ืžืฆื ืฉืชื”ืœื™ืš( waiting sectionืžืžืชื™ืŸ ื”ืชื—ื™ืœ Bืœืคื ื™

- ื” ืืช ืœืคื ื™ Bืื–ื™ doorwayืœื‘ืฆืข ื™ื›ื ืก Aืœืืืช. โ€“ ืœื‘ืฆืข ื”ื—ืœ ืฉื”ืฉื ื™ ืœืคื ื™ ืœื”ืžืชื ื” ื ื›ื ืก ื”ืชื”ืœื™ื›ื™ื ืื—ื“ ืื ื›ืŸ

, ื”ืงืจื™ื˜ื™ ืœืงื˜ืข ืจืืฉื•ืŸ ื™ื›ื ืก ื”ืจืืฉื•ืŸ ื”ืชื”ืœืš ืื–ื™ ื”ื›ื ื™ืกื” ืงื˜ืข

Page 38: Operating Systems, 122

Question 3. ื‘ืฉืืœื” ื–ื• ืขืœื™ื›ื ืœืžืžืฉ ืืœื’ื•ืจื™ืชื ืœืžื ื™ืขื” ื”ื“ื“ื™ืช ืขื‘ื•ืจ ื ืง'(10ื’. )โ€ข

. ืขืœ ื”ืืœื’ื•ืจื™ืชื ืœืงื™ื™ื ืžื ื™ืขื” ื”ื“ื“ื™ืช r ื•-p, qืฉืœื•ืฉื” ืชื”ืœื™ื›ื™ื โ€“ ื•ื—ื•ืคืฉ ืžืงื™ืคืื•ืŸ. ื‘ื ื•ืกืฃ, ืขืœื™ื• ืœืงื™ื™ื ืืช ื”ื“ืจื™ืฉื” ื”ื‘ืื” ื”ืžื‘ื˜ื™ื—ื”

: pืขื“ื™ืคื•ืช ืœืชื”ืœื™ืš ืžืชื—ื™ืœ ืœื”ืžืชื™ืŸ ืœืžื ืขื•ืœ ื˜ืจื ืฉื”ืชื”ืœื™ืš ื”ืžื—ื–ื™ืง ื‘ืžื ืขื•ืœ pืื

ื”ื•ื ื”ืชื”ืœื™ืš p( ืฉืœื•, ืื–ื™ exit sectionื”ื—ืœ ืœื‘ืฆืข ืืช ืงื•ื“ ื”ื™ืฆื™ืื” )ื”ื‘ื ืฉื™ื›ื ืก ืœืงื˜ืข ื”ืงืจื™ื˜ื™.

ืœืฆื•ืจืš ื”ืžื™ืžื•ืฉ ืžื•ืชืจ ืœื”ืฉืชืžืฉ ืืš ื•ืจืง ื‘ืžืฉืชื ื™ื ื”ืชื•ืžื›ื™ื ื‘ืงืจื™ืื•ืช )ื‘ืคืจื˜, ืื™ืŸ ืื™ืŸ ืœื”ืฉืชืžืฉ ื‘ืกื•ื’ื™ื ืื—ืจื™ื ืฉืœ ืžืฉืชื ื™ืื•ื‘ื›ืชื™ื‘ื•ืช.

(. ื ื™ืชืŸ ื’ื test-and-setืœื”ืฉืชืžืฉ ื‘ืกืžืคื•ืจื™ื ืื• ื‘ืคืขื•ืœื•ืช ื›ื’ื•ืŸ ืœื”ืฉืชืžืฉ ื‘ืืœื’ื•ืจื™ืชื ืฉืœ ืคื˜ืจืกื•ืŸ ืœืžื ื™ืขื” ื”ื“ื“ื™ืช ืขื‘ื•ืจ ืฉื ื™ ืชื”ืœื™ื›ื™ื ื›ืื‘ืŸ ื‘ื ื™ื™ืŸ. ื”ืกื‘ื™ืจื• ื‘ืงืฆืจื” ืืช ื”ืืœื’ื•ืจื™ืชื ืฉื›ืชื‘ืชื ื•ื ืžืงื• ื‘ืงืฆืจื”

ืžื“ื•ืข ื”ื•ื ืžืงื™ื™ื ืืช ืฉื ื“ืจืฉ )ืื™ืŸ ืฆื•ืจืš ื‘ื”ื•ื›ื—ื” ืคื•ืจืžืœื™ืช(.

38

Page 39: Operating Systems, 122

Question 3

ืœืžื ื™ืขื” ื”ื“ื“ื™ืช ืขื‘ื•ืจ ืฉื ื™ ื”ืืœื’ื•ืจื™ืชื ืฉืœ ืคื˜ืจืกื•ืŸ ืœื”ืœืŸ . false ืžืื•ืชื—ืœื™ื ืœืขืจืš b[1] ื•-b[0]ืชื”ืœื™ื›ื™ื. ืฉื ื™ ื”ื“ื’ืœื™ื

)ืื™ืŸ ื–ื” ืžืฉื ื” ืžื‘ื—ื™ื ืช 1 ืื• 0 ืžืื•ืชื—ืœ ืœืขืจืš turnื”ืžืฉืชื ื” ื ื›ื•ื ื•ืช ื”ืืœื’ื•ืจื™ืชื(.

39

Algorithm for p0

b[0]=trueturn=0await (b[1]=false OR turn=1)CSb[0]=false

Algorithm for p1

b[1]=trueturn=1await (b[0]=false OR turn=0)CSb[1]=false

Page 40: Operating Systems, 122

Question 3

ืคืชืจื•ืŸ ื–ื” ืขื•ืฉื” ืฉื™ืžื•ืฉ ื‘ืื‘ืŸ ื‘ื ื™ื™ืŸ ืžืกื•ื’ ืคื˜ืจืกื•ืŸ:ืคืชืจื•ืŸ:

ื‘ืงื˜ืข ื”ืงืจื™ื˜ื™ ืื–ื™ ื”ืชื”ืœื™ืš ื”ืฉื ื™ ืœื r ืื• qืื ืื—ื“ ืžื”ืชื”ืœื™ื›ื™ื ื›ื‘ืจ p ื˜ืจื ืฉื”ืจืืฉื•ืŸ ื™ืฆื ื•ืื–ื™ ืื Peterson(p,qr)ื™ื—ืœ ืœื‘ืฆืข ืืช

ื”ื•ื ื”ื‘ื ืฉื™ื›ื ืก. ืื•ืคืŸ ืื—ืจ ืœื”ืกืชื›ืœ ืขืœ ื›ืš Peterson(p,qr)ืžื—ื›ื” ื‘ โ€“p .ืžืชื—ืจื”" ืชืžื™ื“ ืจืง ืขื ืื—ื“ ืžืฉื ื™ ื”ืชื”ืœื™ื›ื™ื ื”ืื—ืจื™ื"

ื”ืืœื’ื•ืจื™ืชื ื”ื•ื ื’ื ื—ืกืจ ื”ืจืขื‘ื”.40

Algorithm for q

Peterson)q,r(.lock // as p0Peterson)p,qr(.lock // as p1CSPeterson)p,qr(.unlock // as p1Peterson)q,r(.unlock // as p0

Algorithm for r

Peterson)q,r(.lock // as p1Peterson)p,qr(.lock // as p1CSPeterson)p,qr(.unlock // as p1Peterson)q,r(.unlock // as p1

Algorithm for p

Peterson)p,qr(.lock // as p0CSPeterson)p,qr(.unlock // as p0

Page 41: Operating Systems, 122

Question 3

ื”ืคืขื ืžื™ืžื•ืฉ ืœืœื ืื‘ื ื™ ื‘ื ื™ื™ืŸ:ืคืชืจื•ืŸ:

41

void enter)i( { // for i = 0 || 1 intrested[i] = true; turn[0] = 1- i; while )intrested[1-i] = true && turn[0] = 1-i(; turn[1] = 2; while )Intrested[2] = true && turn[1] = 2(; }

void enterForProcess2)( { intrested[2] = true; turn[1] = 0; while ))intrested[0] = true || intrested[1] = true( && turn[1] = 0(; }

void leave)i( { intrested[i] = false;}

// We set for each process an id. q as 0, r as 1, and p as 2.intrested[1..3] = {false, false, false}turn[1..2] // init values are not important

Page 42: Operating Systems, 122

Question 3

. ืฉืœื•ืžืฆื™ื•ื ื” ื˜ื•ืขื ืช ื›ื™ ืืœื’ื•ืจื™ืชื ื”ืžืงื™ื™ื ืืช ื ืง'(10ื“. )โ€ข ื—ื•ืคืฉ ืžื”ืจืขื‘ื”ืื™ื ื• ืžืงื™ื™ืื”ื“ืจื™ืฉื•ืช ืฉืœ ืกืขื™ืฃ ื’. ื‘ื”ื›ืจื—

(starvation freedom ื”ืื ื˜ืขื ืชื” ืฆื•ื“ืงืช? ื ืžืงื• ืชืฉื•ื‘ืชื›ื .)ื‘ืงืฆืจื”, ืืš ื‘ืื•ืคืŸ ืžื“ื•ื™ื™ืง ื•ื‘ืจื•ืจ.

42

ืคืชืจื•ืŸ: , . ื’, ืกืขื™ืฃ ืฉืœ ื”ื“ืจื™ืฉื•ืช ืžืงื•ื ื•ืžื›ืœ ืœืขื™ืœ ื”ืืœื’ื•ืจื™ืชื ื•ืœืจืื™ื” ืœื

. ืžื”ืจืขื‘ื” ื—ื•ืคืฉ ืžืชืงื™ื™ื ืฉืœื ื’ื•ืจืจื•ืช ืื™ื ืŸ