0% found this document useful (0 votes)
25 views

COP 4600 Spring 2016 Project 2 - Semaphores: Implement Semaphores in Minix3

The document describes Project 2 which involves implementing semaphores in Minix3. It outlines the semaphore interface including functions for creating, downing, upping, and deleting semaphores. It specifies that semaphores can be binary or multivalued and describes the behavior of each function. It also discusses how processes can share semaphores and pass handles between programs.

Uploaded by

Andy Ortiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

COP 4600 Spring 2016 Project 2 - Semaphores: Implement Semaphores in Minix3

The document describes Project 2 which involves implementing semaphores in Minix3. It outlines the semaphore interface including functions for creating, downing, upping, and deleting semaphores. It specifies that semaphores can be binary or multivalued and describes the behavior of each function. It also discusses how processes can share semaphores and pass handles between programs.

Uploaded by

Andy Ortiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

COP 4600 Spring 2016

Project 2 - Semaphores

Implement semaphores in Minix3.

There is currently no shared memory in Minix3, so you may use the distributed shared variables
implemented in Project 1b to provide for critical sections if need be.

The semaphores shall be implemented using the following interface:


typedef int semaphore;
#define NULL 0
#define BINARY 0
#define MULTIVALUED 1
semaphore create_semaphore(int type, int initial_value);
int down(semaphore s);
int up(semaphore s);
int delete_semaphore(semaphore s);

create_semaphore() takes two non-negative integers as its arguments and returns a handle for
the semaphore created. A return value of NULL means that the call failed. If the value of type is
BINARY, then it is a binary semaphore and can only take a value of 0 and 1. If the value of type is
MULTIVALUED, then it is a multivalued semaphore and can take any non-negative value. For
binary semaphores, the initial value must be 0 or 1. For multivalued semaphores the initial value
can be any non-negative integer within the range supported by the integer type.
create_semaphore() must allocate space to store the value of the semaphore and its type, as
well as maintain a queue of processes that may be waiting on the semaphore. The value shall be
initialized to the initialize value given, provided that it is a legal value, and the queue shall initially
be empty.
create_semaphore() shall return an integer value that can be used to find the same semaphore
when subsequent calls are that use it, or NULL if the call failed for whatever reason.
down() shall take a semaphore handle value as its only parameter and determine the current value
of the semaphore, if it is a valid handle. If the value is positive, it shall decrement that value and
return immediately. If the value is zero, it shall enter the calling process into the queue associated
with that semaphore and mark the calling process as blocked so that it cannot be scheduled. It shall
return a value of 0 if the call failed for any reason (e.g., due to use of an invalid handle), and a value
of -1 if the call succeeded.
up() shall take a semaphore handle value as its only parameter and determine the current value of
the semaphore, if it is a valid handle. If the value is positive, it shall increment that value and return
immediately. If the value is zero, it shall determine whether the queue associated with that
semaphore is non-empty, and if it is, it shall remove the first process in the queue and mark it as
unblocked, so that it can return from the down() call and be scheduled. If the queue is empty, then
the semaphore's value shall be incremented. In all cases, the process calling up() shall return
immediately. It shall return a value of 0 if the call failed for any reason (e.g., due to use of an
invalid handle), and a value of -1 if the call succeeded.
delete_semaphore() shall take a semaphore handle value as its only parameter and determine
if it is a valid handle. If the handle value is valid, it shall release the resources associated with the
semaphore and allow them to be reused. It shall return a value of 0 if the call failed for any reason
(e.g., due to use of an invalid handle), and a value of -1 if the call succeeded.

USAGE
In order for two processes to share a semaphore, it must either be named in such a way that any
authorized process can find and access it, or it must be inherited from its parent across a fork() call.
We will take the latter approach, and require that two processes sharing one or more semaphore be
either the process that created the semaphore or one of its descendants. The semaphore must also
survive the exec() call, so that a child process can execute a new program, which in turn means that
the handle must be passed to the new program somehow. This may be done either implicitly (the
program expects that certain handle values will be operative), or explicitly (the values are passed in
as environment variables). We will take the latter approach, with the newly run program having to
look for the semaphore handle values in environment variables.
COMMENTS
Note that blocking a process that calls down() may be achieved by simply not sending it the reply
message it awaits.
Note that the queue associated with a semaphore can be implemented in a fashion similar to that
used by the message-passing system in Minix3 for its rendezvous operations. The normal data
structure queue linked list operations in which links are created dynamically is not desirable within
the OS, and in many cases is simply not feasible.
We will provide a program that spawns two children that uses the semaphore interface, but you may
want to augment this with your own modifications for testing purposes.

You might also like