Upload
burke-sanford
View
27
Download
2
Tags:
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
Operating Systems, 122
Practical Session 11 File Systems & Midterm 2012
1
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
File system layout (Tanenbaum)
3
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
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
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
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
Question 1: i-nodesA similar problem
8
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
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
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
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
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
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
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
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
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
Operating SystemsMIDTERM 2012
Question 1
(30( ) ืชืืืื (schedulingื ืงืืืืช
19
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ืืืฉืื ืืืชื ืืคืฆื ืืืื. ืฉืืืืื ืืืื
Question 1
kernel. ื ื ืื ืื ืืืขืจืืช ืงืืืืื ืืกืคืจ ืืืื ืงืจื ื ) ื ืง'(5ื. )โขthreads ืฉืื )CPU-bound ืืืกืคืจ ืืืื ืงืจื ื ืฉืื I/O-bound .
ืืืื ืืืื ืฉื ื ืืืืืืจืืชืืื ืืชืืื ืืืชืจ ืขืืืจ ืืขืจืืช ืืื? ื ืืงื ืืงืืฆืืจ ืื ืืืืคื ืืืืืืง ืืืจืืจ.
21
ืคืชืจืื:ืืืืืจืืชื ืืช ื ืขืืืฃ ืื ืืคื ื, Guaranteed schedulingืืืงืจื ืืืืช
ื ืฉืชืืืืื ืืืืื ืืคืืืช IO-boundืฉืืืืืืจืืชื ืืฉืชืืฉื ืืจืื ืืฉืจ . ืืื ืืืืื ืืืื ืื ืื ืืืฉืจ ืืจืืฅ ืืคืฉืจืืช ืืงืืื ืืขืื ืืื
ื ืืืชืจ ืงืจืื ืืืื ืฉืื ื ืืฉืจ SJFืืืืืืจืืชื ืชืืืืืื ืฉืืขืืืฃ ืืืืื , ืืืืื ืช ืืืชืจ ืืืื ืืืื ืืื ืืกืฃ ืืืชืจ ืงืฆืจ ืืขืื ืืืื ืืงืืงืื ืืืื
. ืชืืช ืชืืืื ืื ืฉืืงืื ืืืืื , R.Rืืืื ืืื. ืืืืจื ืืืช ืืขืืืช ) ื ) ืืชืืืืื ืืขืื ืืื ืืืืื ืช ืืจืืจื ืืขืืคื .CPU-boundืชืืื
Question 1
. ืืืฉืจ ืขืกืงื ื ืืืื ืืืืจืื, ืชืืืจื ื ืืช ืืืืืืฉืื ื ืง'(8ื. )โข )ืื ืืืืื ืื Hoare( ืืืืืฉ ืืื ืืืืจ ืืกืื ืืืงื ืฉื 1ืืืืื.
( 2ืืืื ืืืงืื ืืช ืืกืืื ื ืืื ืืื ืืจืืฅ ืืืื ืืืืจ(, ื-, ืื ืื ืืชืงืืืืช ืืืืื ืฉืืื ืืืื ืคืขื Javaืืื ืืืืจ ืฉื
( ืืืขืื ืืื ืื ืืืืื ืก ืืืื ืื ืืฆืืื ืืืฆื threadsืืืืืื )ready.ืืชืืจืื ืขื ืืื ืืกื
ื ื ืื ืื ืืืขืจืืช ืืฉื ื ืืกืคืจ ืืืืื ืืืฉืชืืฉืื ืืืื ืืืืจ. ืืื ืืืืืจื ืืื ืฉื ื ืืืืืืจืืชืืื ืื "ื ืืชืืืื ืชืฉืคืืข ืืืชืจ
ืื ืขื ืืื ืืืืจ Hoareืขื ืืืืืฉ ืืืฉืชืืฉ ืืืื ืืืืจ ืืืืคืืก ? ื ืืงื ืชืฉืืืชืื ืืงืืฆืืจ ืื ืืืืคื ืืืืืืง ืืืจืืจ.Javaืฉื
22
Question 1
ืคืชืจืื:
, ืืืืช ืืืืื Javaืืืืืจื ืชืฉืคืืข ืืืชืจ ืขื ืืื ืืืืจ ืฉื ืฉืืืฆื ืื ืืืืืจืืชื ืืชืืืื ืืืืื ืืืขืฉื ืื
, ืื Round robinืืชืืืื ืฉืืื ืก ืืืื ืืืืจ. ืื ืืืื ืื ืืืื ืื ืืชืืืื ืืืขืื ืืื ืืื ืฉื ืืชืงื ืื ืืืขืืจ
, ืืื Guaranteed schedulingืืืขืืื. ืื ืืืื ืื ืืชืืืื ืฉืืื ืก ืืืื ืืืืจ ืืืื ืืชืืืื ืืืขืื ืืื ืฉืงืืื
, Hoareืืช ืืืก ืืืื ืื ืืื ืืืืชืจ. ืืืื ืืืืจ ืืืืคืืก ืืืื ืืืืจ ืืืขืฉื ืืืืจ ืื ืืชืืืื ืืื ืฉืืืื ืืืื ืก,
ืืืื ืืืื' ืืชืืืื ืื ืชืืื ืืฉืคืขื ืจืื ืขื ืืจืืฆื.
23
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
Question 1
ืคืชืจืื:.I ืืืงืจื ืืQ ืืกืจ ืืฉืืขืืช. ืื ืชืืืื ืืจืืฅ ืืื T ืืื ื ืืืื S ืืื ื
C.S :ืืื ื ืืฆืืืช ืืืขืื ืชืืื .
.II:ืืชืงืืื ืชืฉืืืืช ืืื ืืื ื ืฉ . ืืืฆื ืืื ื ืงืื ื ืืฆืืืช ืืขืื
.III:ืืืฉืจ ื ืงืื ืขื ืคื ืื ืืกืื ืืืกืขืืฃ ืืงืืื
25
Question 2
(35 ) ืืืืืื ืชืืืืืื ื ืงืืืืช
26
Question 2
. ืืื ืืคืืืื ืืืคืฉืจืืื ืฉื ืืชืืื ืืช ืืืื, ืขื ื ืง'(15ื. )โข? ืขืืืจ ืื ืคืื, ืฆืืื ื ืืื ืืื ืืืืคืก ืขื a.txtืืืกื ืืืงืืืฅ
ืืื ืืชืืืื ืืื, ืชืืืื ืื ืื ืืื. ืื ืืื ืื ืืชืืื ืืช ืจืฆื ืขื ืืขืจืืช ืขื ืืขืื ืืืื, ืืื ืคืชืืืช ืืงืืืฅ ืืฆืืืื. ืื ืืื ืื
ืืื ืชืืืื ืืืืืื ืฉื ืืืืืื ื ืงืืขืื ืืกืืจ ืขืืื, ืืื . ืื ืืกืฃ, ืื ืืื ืื ืืืฉืจ ืชืื ืืช ืืจืืืช ืืืืื ืืืฆืขืช 1ื-
ืจืง ืืืื ืืฉืจ ืืืฆืข ืืช ืืงืจืืื ืืฉืชืืคื. ืื ืืื forkืคืขืืืช ืื ืื ืื ืคืขืืืช ืืืคืกื ืืื ืืืืืืช )ืืืืืจ, ืืื ืื ืชืืคืจืข
(.context switchืืืืฆืข ืขื ืืื
27
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}
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
ืคืชืจืื:
Question 2. ืื ืืื ืื ืืจืฉืืชืื ืฉืจืช ืืื ืืจื ื ืืืกืคืง ืืงืฉืืช ื ืง'(10ื. )โข
ืืฉืชืืฉืื. ืืฉืจืช ืืื ื ื ืชืื ืื ืื ืืื ืฉืืืจ ืขืจืืื ืฉืืืืื ืขืืืจ ืืงืฉืืช ืฉืืชืืฆืขื ืืืืจืื ื. ืืื ืคืขื ืฉืืชืงืืืช ืืงืฉื, ืืชืืฆืขืช ืคืขืืืช ืืืคืืฉ ืืืืจื ืืืื ื ืื ืชืื ืื. ืืืืื ืืืืืืข ืื ืืจืฉ ืืื ื ืืฆืื ืืืื ื ืื ืชืื ืื,
(. ืื ืืื ืื ืืฉืจืช ืืขืื ืืืื. ืืื I/Oืืชืืฆืข ืชืืืื ืืืืืจ ืืชืื ืืืกืง ) ืืช h ืืืืกืง ืืืงืืื. ื ืกืื ื-I/Oืื ืื ืืื ืื ืื ื ืืชื ืืืฆืข ืฉืชื ืคืขืืืืช
ืืช ืืืื ืื ืืจืฉ cืืืกืชืืจืืช ืืืฆืืืช ืืืืืข ืื ืืจืฉ ืืืื ื ืื ืชืื ืื, ื- ืืช ืืฉื ืืืื ืื ืืจืฉ ืืืืืืจ ืืืืข tืืืืืืจ ืืืืข ืืืื ื ืื ืชืื ืื ืื-
(. ืืคืจื, ืืืฉืจ ืืืืืข ืื ืืจืฉ ืื ื ืืฆื ืืืื ื t>cืืืืืกืง )ืืจืืจ ืื )ืจืืฉืืช ืืฉ ืืงืจืื ืืช ืืืืืข ืื t+cืื ืชืื ืื, ืืฉื ืืืืืืืจ ืืืืื ืืื
ืืืืกืง ืืืื ื ืื ืชืื ืื ืืื ืืงืจืื ืืืื ื ืื ืชืื ืื(. ื ืชืื ืื ืฉืชื ืืงืฉืืช ืืืชื ืชืืืืืช ืื ืืื, ืื ืืืช ืืื ืืืืืข ืฉืื ื, ืืืืขืืช ืื ืืื ืืช ืืืขืจืืช.
ืืื ืืฉื ืืืื ืืฆืคืื ืขื ืืืฉืืืช ืืืงืฉืืช ืืืขืจืืช ืืืืกืกืช ืืืื ืืฉืชืืฉโ(user space threads?)
ืืื ืืฉื ืืืื ืืฆืคืื ืขื ืืืฉืืืช ืืืงืฉืืช ืืืขืจืืช ืืืืกืกืช ืืืื ืงืจื ื โ(kernel threads?)
30
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ืืืื ื
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
Question 2
ืืืกืืืช signal_unblock ืืื signal_blockืืืืข ืื ืืคืงืืืืช ืืืฉืืจืจืืช ืืกืืื ืืกืืื ืืื. ืฉืจืืื ืืจืฃ ืืืืื ืืืชืืจ ืืช
ืืคืืืื ืืืคืฉืจืืื ืืงืื ืื. ืื ืฆืืืช ืืืจืฃ ืชืกืื ืืืคืกื ืืื ืงืฉืช ืืืืื ืช ืชืืืฆื ืืืก ืกืืจ ืืชืืืื ืืื ืืืคืกืืช. ืืืืืื, ืื
" X" ืืื ืืืืคืกื ืฉื "Z" ื- "X", "Yืขืค"ื ืงืื ืืกืืื ืืืืข ืื ืืืืคืกื "" ืืืื ืืืืคืืข ืืคื ื ืื Z" )ืื "Yืชืืคืืข ืืืืจื ืืคื ื ืืืืคืกื ืฉื "
ืืืจื ืื ืืืช ืื ืืืืคืกืืช ืืืืจืืช(, ืืชืงืื ืืืจืฃ ืืื:
33
YX Z
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โ); }}
Question 3
(35 ) ืกืื ืืจืื ืืืฆืื ื ืงืืืืชืืืืืช ืื ืืขื
35
Question 3
. ืืืืชื ืืืืจื ื ืืช ืืชื ืืื ืืืคืฉ ืืงืืคืืื ื ืง'(5ื. )โข(deadlock freedom( ืืืืคืฉ ืืืจืขืื )starvation freedom )
ืขืืืจ ืืืืืจืืชืืื ืืื ืืขื ืืืืืช. ืืชืื ืืช ืืืืืจืืช ืฉื ืชื ืืื ืืื.ืืืืืืืงืืช
36
ืคืชืจืื:: ืืงืืคืืื , ืืืคืฉ ืืงืจืืื ืืงืืข ืืืืื ืก ืื ืกื ืชืืืืืื ืงืืืฆืช ืื
. ืืืื ืืืืื ืก ืืฆืืื ืืชืืืืืื ืืื ืฆืขืืื ืฉื ืกืืคื ืืกืคืจ ืืืืจ ืืื: ืืืจืขืื , ืืืคืฉ ืืื ืืงืจืืื ืืงืืข ืืืืื ืก ืื ืกื ืชืืืื ืื
. ืืืื ืืืืื ืก ืืฆืืื ืืื ืฆืขืืื ืฉื ืกืืคื ืืกืคืจ ืืืืจ
Question 3
( ืขืืืจ ืืขืืืช fairness). ืืืืชื ืืืืจื ื ืชื ืื ืืืื ืืช ื ืง'(10ื. )โข.first-in-first-out (FIFO)ืืื ืืขื ืืืืืืช ืืงืจืื
ืฉื ืชื ืื ืื.ืืืืืืืงืชืืชืื ืืช ืืืืืจื โืืืืืืจืืชื ืืื ืืขื ืืืืืช ืฉื ืคืืจืกืื ืืฉื ื ืชืืืืืื ืืืื ืืกืืฃ โ
? ื ืืงื ืืงืฆืจื ืื FIFOืฉืืื ืื. ืืื ืืืืืืจืืชื ืืงืืื ืชื ืื ืืืืืืืง.
37
ืคืชืจืื:ืชืืืื โ -Aืื ื ) ื ืืฆื ืฉืชืืืื( waiting sectionืืืชืื ืืชืืื Bืืคื ื
- ื ืืช ืืคื ื Bืืื doorwayืืืฆืข ืืื ืก Aืืืืช. โ ืืืฆืข ืืื ืฉืืฉื ื ืืคื ื ืืืืชื ื ื ืื ืก ืืชืืืืืื ืืื ืื ืื
, ืืงืจืืื ืืงืืข ืจืืฉืื ืืื ืก ืืจืืฉืื ืืชืืื ืืื ืืื ืืกื ืงืืข
Question 3. ืืฉืืื ืื ืขืืืื ืืืืฉ ืืืืืจืืชื ืืื ืืขื ืืืืืช ืขืืืจ ื ืง'(10ื. )โข
. ืขื ืืืืืืจืืชื ืืงืืื ืื ืืขื ืืืืืช r ื-p, qืฉืืืฉื ืชืืืืืื โ ืืืืคืฉ ืืงืืคืืื. ืื ืืกืฃ, ืขืืื ืืงืืื ืืช ืืืจืืฉื ืืืื ืืืืืืื
: pืขืืืคืืช ืืชืืืื ืืชืืื ืืืืชืื ืืื ืขืื ืืจื ืฉืืชืืืื ืืืืืืง ืืื ืขืื pืื
ืืื ืืชืืืื p( ืฉืื, ืืื exit sectionืืื ืืืฆืข ืืช ืงืื ืืืฆืืื )ืืื ืฉืืื ืก ืืงืืข ืืงืจืืื.
ืืฆืืจื ืืืืืืฉ ืืืชืจ ืืืฉืชืืฉ ืื ืืจืง ืืืฉืชื ืื ืืชืืืืื ืืงืจืืืืช )ืืคืจื, ืืื ืืื ืืืฉืชืืฉ ืืกืืืื ืืืจืื ืฉื ืืฉืชื ืืืืืืชืืืืช.
(. ื ืืชื ืื test-and-setืืืฉืชืืฉ ืืกืืคืืจืื ืื ืืคืขืืืืช ืืืื ืืืฉืชืืฉ ืืืืืืจืืชื ืฉื ืคืืจืกืื ืืื ืืขื ืืืืืช ืขืืืจ ืฉื ื ืชืืืืืื ืืืื ืื ืืื. ืืกืืืจื ืืงืฆืจื ืืช ืืืืืืจืืชื ืฉืืชืืชื ืื ืืงื ืืงืฆืจื
ืืืืข ืืื ืืงืืื ืืช ืฉื ืืจืฉ )ืืื ืฆืืจื ืืืืืื ืคืืจืืืืช(.
38
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
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
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
Question 3
. ืฉืืืืฆืืื ื ืืืขื ืช ืื ืืืืืจืืชื ืืืงืืื ืืช ื ืง'(10ื. )โข ืืืคืฉ ืืืจืขืืืืื ื ืืงืืืืืืจืืฉืืช ืฉื ืกืขืืฃ ื. ืืืืจื
(starvation freedom ืืื ืืขื ืชื ืฆืืืงืช? ื ืืงื ืชืฉืืืชืื .)ืืงืฆืจื, ืื ืืืืคื ืืืืืืง ืืืจืืจ.
42
ืคืชืจืื: , . ื, ืกืขืืฃ ืฉื ืืืจืืฉืืช ืืงืื ืืืื ืืขืื ืืืืืืจืืชื ืืืจืืื ืื
. ืืืจืขืื ืืืคืฉ ืืชืงืืื ืฉืื ืืืจืจืืช ืืื ื