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

The Operating System Tutorials

The document provides an introduction to using semaphores in C programming on UNIX systems, explaining how to create a semaphore using semget(), control a semaphore using semctl() including setting, getting, and removing its value, and perform up and down operations on a semaphore using semop(). It includes examples of code implementing these semaphore functions and instructs the reader to modify provided code snippets to experiment with and better understand using semaphores for inter-process synchronization.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

The Operating System Tutorials

The document provides an introduction to using semaphores in C programming on UNIX systems, explaining how to create a semaphore using semget(), control a semaphore using semctl() including setting, getting, and removing its value, and perform up and down operations on a semaphore using semop(). It includes examples of code implementing these semaphore functions and instructs the reader to modify provided code snippets to experiment with and better understand using semaphores for inter-process synchronization.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The University of the West Indies, St.

Augustine
Department
The Department ofComputing
The Department
of Mathematics
of Computing andand Computer
Information
and Information Science
Technology
Technology
CS31ACOMP- Semester I, 2005/2006
2604 - Operating Systems
Tutorial #7
Lab 7 - Semaphores

Required reading: Chapter 8 of UNIX System Programming (Haviland et al)


(this may be done after performing the tutorial)

Semaphores
The C programming language in UNIX systems provides library functions that implement
semaphores. These functions are more complex in nature than the basic down and up oper-
ations.
This tutorial will introduce semaphores in UNIX using simplified examples.
Source code files for this tutorial should include <sys/types.h>, <sys/ipc.h> and <sys/sem.h>.

Creating a Semaphore

To create a semaphore you need to use the semget() function. This function takes three
(3) parameters. The first is an integer key. This is simply a special unique number that you
must come up with to identify your semaphore.
The second parameter is an integer representing the number of semaphores you want to
create. For this tutorial, use 1.
The third parameter is an integer representing properties of the semaphore to be created.
For the purposes of this tutorial, simply use 0600 | IPC CREAT | IPC EXCL. This basically
means that you want to create a semaphore with read and write permissions and that it
must be exclusive, i.e. the semaphore should only be created if no other semaphore in the
system was previously created with the same key.
The semget() function returns an integer which will be used as a descriptor for the semaphore.
If there was an error in creating the semaphore, the value -1 will be returned.
Example

#define SEMKEY 12345

int semid;

semid = semget(SEMKEY, 1, 0600 | IPC_CREAT | IPC_EXCL);

will create one semaphore, and store the descriptor for it in the variable semid.
. Create a C program called semaphores.c.
. Add the code shown above to your program.
. Add a printf() statement to display the return value of the semget() function.
. Compile and run the program.
. Run the program a second time. What do you notice about the return value of semget()?

Controlling a Semaphore

The function semctl() is used to control a semaphore. Specifically, we will use it to:

• Set the value of a semaphore


• Get the value of a semaphore
• Remove a semaphore from the system.

semctl() takes four parameters. The first is the descriptor for the semaphore to be con-
trolled.
The second is the index number of the semaphore (for this tutorial, since we are only creating
one semaphore at a time, this index will be 0).
The third parameter is an integer representing a command. The ones which are of interest
in this tutorial are:

• IPC RMID : Remove the semaphore from the system.


• GETVAL : Get the current value of the semaphore.
• SETVAL : Set the value of the semaphore.

The fourth parameter is a C union. This will not be considered in detail in this tutorial, but
you should read about it afterward.

Setting the value of a semaphore

When using semctl() to set the value of a semaphore, the fourth parameter can simply be
set to the value that is desired for the semaphore.
Example

semctl(semid, 0, SETVAL, 5)
will set the value of a semaphore (whose id is stored in the integer variable semid) to 5.
Getting the value of a semaphore

When using semctl() to get the value of a semaphore, the fourth parameter can simply be
set to NULL. The return value of semctl() will be the value of the semaphore.
Example

int semval;
semval = semctl(semid, 0, GETVAL, NULL);

can be used to obtain the value of a semaphore (whose id is stored in the integer variable
semid).
. Change the semaphore key in your program to 123456
. Add code to set the value of the semaphore to 5.
. Add code to get the value of the semaphore and save it in an integer variable semval.
. Add a printf() statement to print the value of semval.
. Compile and run the new program.
. Run the program again. Explain the results that you obtain.

Removing a semaphore

If a process creates a semaphore, then even after that process terminates, the semaphore
continues to exist within the operating system.
It is important to remove it from the system, otherwise when you try to run your program
again, the semaphore operations will fail since the old semaphore key will still be in the
operating system.
When using semctl() to remove a semaphore, the fourth parameter can simply be set to
NULL.
. Change the semaphore key in your program to 1234567
. Add code at the end of the program to remove the semaphore.
. Compile and run the new program.
. Try running the program several times. You should not get any errors.

Performing up and down operations

The semop() function is capable of performing the roles of the up and down operations.
One call to semop() is capable of performing several operations on several semaphores. But
again for the sake of simplicity, we will use semop() for the purpose of performing only one
operation on one semaphore at a time.
semop() takes three (3) parameters. The first is the descriptor of the semaphore.
The second is a pointer to an array of type struct sembuf. The third is an integer repre-
senting the number of operations to be performed.
Each struct sembuf structure in the array represents an operation to be performed on a
given semaphore. Since we will only be performing one operation on one semaphore at a
time, the second second parameter to semop() can be the address of a variable of type
struct sembuf, and the third parameter will be 1.
The members of the struct sembuf structure that are of interest are:

• sem num : the index of the semaphore.

• sem op : an integer representing the operation to be performed.

• sem flg : set this to SEM UNDO for now.

The sem op member can be set to 1, indicating an up operation (i.e. increase the value of
the semaphore by 1), or to -1, indicating a down operation (i.e. decrease the value of the
semaphore by 1).
. Add a variable sb of type struct sembuf to your program.
. Set the sem num field of sb to 0.
. Set the sem fld field of sb to SEM UNDO.
. Set the sem op field of sb to 1.
. Just before the code to remove the semaphore, make a call to semop() to perform
the operation indicated in sb.
. After the call to semop(), get and print the value of the semaphore.
. Compile and run the new program.
. Modify the code so that the sem op field of sb is set to -1 instead.
. Compile and run the new program.
. Move the lines code that call semop() and get and print the value of the semaphore
into a for loop. Set the loop to iterate 6 times.
. Compile and run the new program. Explain what happens.
. Run the program once more. Explain what happens.

You might also like