Title: Synchronization EE512 Project 2
1Synchronization- EE512 Project 2 -
TA Jong-Woon Yoo - jwyoo_at_core.kaist.ac.kr
- T. 5425 - EE Bldg. 3207 Issue Oct. 13 Due
Oct. 27 (235959 pm)
- Computer Engineering Research Laboratory
- KAIST
2Tasks Evaluation
- TASK 1 Implement semaphores (30)
-
- TASK 2 Solve synchronization problems using
semaphores (30) - 1) The Dining Philosophers Problem (10)
- 2) The Readers and Writers Problem (10)
- 3) The Professor and Students Problem (10)
- Report (30)
- 1) Algorithm explanation
- 2) Test methods Result analysis
- Comments Code readability (10)
3Semaphore
- Integer variable to count the number of wakeups
for future use - 0 no wakeups were pending
- Positive integer one or more wakeups were
pending - DOWN and UP
- DOWN Checks to see if the value is greater than
0. If so, decrements the value and just continue.
Otherwise, the process is put to sleep - UP Increments the semaphore value. If one or
more processes were sleeping, one of them is
chosen and allowed to complete its DOWN
4TASK 1 Implement Semaphores
- Implement the semaphore as a system server
- The semaphore should be a part of the boot image
and automatically start when the system is booted - It should support 10 different semaphores
- Semaphores have two states, active and inactive
states. Initially, all semaphores are inactive. - Implement the semaphore APIs
- int sema_init(int sema_id, int start_value)
- Initializes a semaphore (i.e. change the state of
the semaphore from inactive to active) specified
by sema_id, generates a SEMA_INIT message and
sends it to the semaphore server. - Returns an error if it tries to initialize an
active semaphore, otherwise, return success. - sema_id specifies the index of the semaphore that
should be initialized. Your system should support
10 different semaphores, thus, the sema_id must
be an integer between 0 and 9. - start_value specifies the initial value for the
semaphore, which can be an arbitrary integer
value.
5TASK 1 Implement Semaphores (cont)
- Implement the semaphore APIs (cont)
- int sema_down(int sema_id)
- It creates a SEMA_DOWN message and sends it to
the semaphore server. - This call can only be invoked for an active
semaphore. Calling it on an inactive semaphore
leads to an error. - This call decrements the counter of the semaphore
specified by sema_id by one. - When the counter (after the decrement) has a
value of lt 0, then the calling process should be
put to sleep (waiting in the queue that
corresponds to the semaphore) - int sema_up(int sema_id)
- It creates a SEMA_UP message and sends it to the
semaphore server. - This call can only be invoked for an active
semaphore. Calling it on an inactive semaphore
leads to an error. - This call increments the counter of the semaphore
specified by sema_id by one. - When there is at least one processes waiting
(sleeping) in the queue, the first process that
was put to the sleep should be woken up.
6TASK 1 Implement Semaphores (cont)
- Implement the semaphore APIs (cont)
- int sema_release(int sema_id)
- It creates a SEMA_RELEASE message and sends it to
the semaphore server. - sema_id specifies the index of the semaphore that
should be released. - This call can only be invoked for an active
semaphore. Calling it on an inactive semaphore
leads to an error. - This function fails when there are currently
processes waiting in the queue of the semaphore
specified by sema_id.
7TASK II - Solve Synchronization Problems
- The Dining Philosophers Problem
- Five philosophers are seated around a circular
table. - Each philosophers has a plate of spaghetti.
- Because the spaghetti is too slippery, a
philosopher needs two forks to eat it. - There is one fork between each pair of plates.
- The life of a philosopher consists of alternate
period of eating and thinking. - When a philosopher gets hungry, he tries to
acquire his left and right fork, one at a time,
in either order. - If successful in acquiring two forks, he eats for
a while, then puts down the forks and continues
to think. - Write a program for each philosopher that does
what it is supposed to do and never gets stuck.
8TASK II - Solve Synchronization Problems (cont)
- The Readers and Writers Problem
- There is an file.
- There are many completing processes whishing to
read and write the file. - It is acceptable to have multiple processes
reading the file at the same time. - But if one process is writing the file, no other
processes may have access to the file, not even
readers. - Write a program for the readers and writers.
Consider that a new reader arrives, say, every 2
seconds, and each reader takes 5 seconds to do
its work. Can a writer get it?
9TASK II - Solve Synchronization Problems (cont)
- The Professor and Students Problem
- You have been hired by the CS Division to write
code to help synchronize a professor and his/her
students during office hours. - The professor waits until there are students
around to ask questions if there are students
who want to ask questions, they must synchronize
with each other and with the professor so that
(i) only one person is speaking at any one time,
(ii) each student question is answered by the
professor, and (iii) no student asks another
question before the professor is done answering
the previous one. Each student asks a single
question. - Write the code for the professor and the student
programs, making sure that the necessary
synchronization code is in place to ensure the
required order of interaction.
10Implementation Hints
- Read the attached reference Modular System
Programming in MINIX3 - What is the system server?
- How semaphore can be added to MINIX?
11Implementation Hints
- Reuse the code of a simple system server such as
Data Store Server (DS) - Copy the files in /usr/src/servers/ds into a new
directory such as /usr/src/servers/ss
PUBLIC int main(int argc, char argv)
message m int result sigset_t sigset
init_server(argc, argv) while (TRUE)
get_work(m) switch (callnr) case
PROC_EVENT sig_handler()
continue case DS_PUBLISH result
do_publish(m) break case
DS_RETRIEVE result do_retrieve(m)
break case DS_SUBSCRIBE
result do_subscribe(m) break
case GETSYSINFO result
do_getsysinfo(m) break
default report("DS","warning, got
illegal request from", m.m_source)
result EINVAL if (result !
EDONTREPLY) m.m_type result
/ build reply message /
reply(who_e, m) / send it away /
return(OK)
/ shouldn't come here /
/usr/src/server/ds/main.c
12Implementation Hints
- How to add a new system call?
- Reference How To Add a New System Call for
MINIX 3 - Declare a prototype of the system-call handler in
/usr/src/servers/ss/proto.h - _PROTOTYPE(int sema_init(message m_ptr))
- Implement the detail in /usr/src/servers/ss/store.
c - Define the system-call number in
/usr/src/include/minix/callnr.h and
/usr/include/minix/callnr.h - define SEMA_INIT 69
- Create a new file /usr/src/lib/posix/_semaphore.c
- Add the name of the file (_semaphore.c) in the
/usr/src/lib/posix/Makefile.in - _semaphore.c \
- Go to /usr/src/lib/posix and issue command make
Makefile - Go to /usr/src and issue command make libraries
- Go to /usr/src/tools and issue commands make
hdboot and make install - Shutdown, reboot, and select the new boot-image
13Implementation Hints
- How to add a new system server?
- Create a new directory /usr/src/servers/ss
- Copy the files in /usr/src/servers/ds into the
new directory /usr/src/servers/ss. Make sure that
.depend file is copied - Update makefiles to make sure that your new
server is compiled - Open /usr/src/servers/ss/Makefile and change
SERVER from ds to ss - Add cd ./ss (MAKE) _at_ to /usr/src/servers/Ma
kefile - Add cd ./ss (MAKE) EXTRA_OPTS(EXTRA_OPTS)
build to /usr/src/servers/Makefile - Move to /usr/src/server and issue command make
image. - Check whether ss is compiled.
- Define semaphore server number (/usr/src/include/m
inix/com.h) - Add define SS_PROC_NR 7 and modify
INIT_PROC_NR from 7 to 8.
14Implementation Hints
- How to add a new system server? (cont)
- Define semaphore messages in /usr/src/include/mini
x/com.h - Add define SS_RQ_BASE 0x2000
- Add define SS_SEMA_INIT (SS_RQ_BASE 0)
- Add define SS_SEMA_DOWN (SS_RQ_BASE 1)
- Add define SS_SEMA_UP (SS_RQ_BASE 2)
- Add define SS_SEMA_RELEASE (SS_RQ_BASE 3)
- Add the semaphore server image to the kernel
(/usr/src/kernel/table.c) - Add define SS_C 0 (Find the meaning of this
value and change this value if necessary) - Find the struct boot_image image array
- Add SS_PROC_NR, 0, SRV_F, 4, 3, 0, SRV_T,
SYS_M, SS_C, ss (Find the meaning of each
value and change them if necessary) - Make sure that the permissions are set so that
user processes can send message to the semaphore
server.
15Implementation Hints
- How to add a new system server? (cont)
- Update makefiles and make sure that the semaphore
server is compiled - Add ../servers/ss/ss to /usr/src/tools/Makefile
- Move to /usr/src/tools and issue command make
install - Check whether ss is compiled
- Note down the name of the new boot-image (such as
/boot/image/3.1.2ar3) - shutdown
- In the boot prompt (such as ltd0p0s0gt), setup
the new image for booting by issuing the command
image/boot/image/ltnew-boot-image-namegt - boot
- Check whether ss is automatically activated
16Submission
- Due date Oct. 27 (Tue), 235959
- For code, follow the submission method in
handout - (!) Your own test code policy should be
included - With project report (studentnumber.doc or .hwp)
- Studentnumber.zip
- E-mail to jwyoo_at_core.kaist.ac.kr
- Title EE512-Project1 Studentnumber
- Ex) EE512-Project1 200931xx
- Penalty
- If copied, no points
- Delay 10/day (received time of e-mail)
17Producer Consumer Problem
18Producer Consumer Problem (cont)
19Producer Consumer Problem (cont)