Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is a semaphore ?

Status
Not open for further replies.

atmaca

Full Member level 4
Joined
Jan 13, 2004
Messages
211
Helped
4
Reputation
8
Reaction score
2
Trophy points
1,298
Activity points
1,556
what is a semaphore

What is a semaphore and where can i find detailes information for that ?
 

binary semaphore, to halt a task

A very old device that this term is used for is a human with two flags, one in each hand. The different combinations of angles of the outstretched arms indicate different letters of the alphabet. This code was read by another human far away either directly or with a telescope. Later on a tower was used with two mechanical arms for the same purpose.

This is one of the ancient crofting skills young people learn in the scouting movement.
 

ipc semaphore max value c++

Or , do you mean a Sempahore as in a synchronization object used by an OS ?
 

what is a sephamore

DanPartelly said:
Or , do you mean a Sempahore as in a synchronization object used by an OS ?

Yes it is
 

what semaphore

YOu can find it in any book concerning pre-emptive OS or RTOS.

Semaphore can be considered as a simple variable with shared access and "atomic" check/increase/decrease operations.

One example: every task checks semaphore before getting access to shared resourse and if it is less than specified value, it will increase it (check/increase should be "atomic", in others words another task can't interrupt this sequence of operations). Otherwise, task will be suspended until resourse will become available. After finishing working with resourse, task will decrease semaphore.

Also, you can consider mutex - this is like binary semaphore (for shared resources with single port and tasks synchronization).
 

printer - semaphore concepts

A semaphore is a kernel object that one or more threads of execution can acquire or release for the purposes of synchronization or mutual exclusion.

When a semaphore is first created, the kernel assigns to it an associated semaphore control block, a unique ID, a value (binary or a count), and a task-waiting list, ...............



Please read chapter 6 of Real Time Concepts for Embedded system,
or use google search to collect info about this semaphore concept
 

ace semaphore example

Basicly a semaphore is a synchronization/mutual exclusion object offered by OS's. Programmers use them to synchronize multiple tasks/threads/processes etc and to limit access to resources.
There are lots of differnet types of semaphores and their function and implimentation is dependant on the OS, so read about them in your RTOS manual, or as faroo28 said in Real-Time Concepts for embedded systems for a detailed overview of them.
 

what is a semaphor

there is mainly two type of semaphore one is binary and second one is counting.Binary is used for where only one resource is available while counting is for more than one resource...One more type of semaphore is spin lock which is used for working on multiple processor......
 

semaphore
A flag variable that is used to govern access to shared system resources.

A semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore. When the count reaches zero, no more threads can successfully wait for the semaphore object state to become signaled. The state of a semaphore is set to signaled when its count is greater than zero, and nonsignaled when its count is zero.

The semaphore object is useful in controlling a shared resource that can support a limited number of users. It acts as a gate that limits the number of threads sharing the resource to a specified maximum number.
 

BINARY SEMAPHORE: It is like a Single-Key, where the task that needs to access a shared resource, like printer, gets the key and uses it! any other task that needs to access the resource waits for the key to be released by the first task!

Counting SEMAPHORE: It contains max. no. of values(tasks) that can access the resource at a time. This is used based on a particular hardware which can handle that certain number of tasks! :sm24:
 

A semaphore is really an integer variable used for interprocess communication inorder to achieve synchronization.They are share memory variables hence accesed
atomically(indivisibly).This means the when one process is using a particular semaphore,no other process has access to the same semaphore.
There are two types of semaphores:
(1)Counting Metaphore
This can take on a range of integer variables.But are non-negative
(2)Binary Semaphore
This can take on either a 0 or 1

Semaphores can be accesses by only two operations:
(1)Wait(S) or P(S)
This sent is used to detect if a semaphore is currently being used.

(2)Signal(S) or V(S)
This is used to signal the release of a semaphore
 

Semaphore is a technique used for IPC (Inter process communication)
where co-operating task communicate with each other with help of semaphore
when a particular process needs a resources it take a semaphore if avilable in case of MUTEX it can be taken only once before being given again
no other process can gain access to that resource(critical section) till the process which is holding it releases it

I guess you should read IPC it will clear you idea abt Semaphore
Signals and Mutex are other technique ues for same purposes
 

Lots have been told about what semaphore is? Can anyone put a light on how it is implemented and how is the atomicity ensured while operating upon a semaphore?

.. ~Kapil
 

Operative system is the master over all resources (memory, I/O, CPU) that a process can use. Operative system only multiplex this hardware resources, with OS multiple resources (Process state diagram, Synchronization, IPC, Signals. etc).
 

semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore. When the count reaches zero, no more threads can successfully wait for the semaphore object state to become signaled. The state of a semaphore is set to signaled when its count is greater than zero, and nonsignaled when its count is zero.

The semaphore object is useful in controlling a shared resource that can support a limited number of users. It acts as a gate that limits the number of threads sharing the resource to a specified maximum number.
 

u can find a details of semaphore & along with semaphore about the mutex in any operating system book.
in short semaphore is a protected variable used in the multiprogramming env. for sharing resources

A semaphore is a protected variable (or abstract data type) and constitutes the classic method for restricting access to shared resources (e.g. storage) in a multiprogramming environment

P(Semaphore s)
{
await s > 0, then s := s-1; /* must be atomic once s > 0 is detected */
}

V(Semaphore s)
{
s := s+1; /* must be atomic */
}

Init(Semaphore s, Integer v)
{
s := v;
}


this is one of the links with details http://en.wikipedia.org/wiki/Semaphore_(programming)
 

maui said:
Basicly a semaphore is a synchronization/mutual exclusion object offered by OS's. Programmers use them to synchronize multiple tasks/threads/processes etc and to limit access to resources.

yes, i think this is the short complete answer.
semaphore is an objec t (not reallya good term to use since it maybe confused with 'object' in OOP) / data which is used in RTOS for:

- task synchronization: to make sure that the tasks running in desired sequence/order.

- mutual exclusions: to prevent multiple usage of critical resource (resource, maybe only a variable or a hardware, that must not be used by more than limited task(s) at a time).

hope this helps,


best regards,
 

Hello

Under Unix/Linux...

IPC:Semaphores
Semaphores are a programming construct designed by E. W. Dijkstra in the late 1960s. Dijkstra's model was the operation of railroads: consider a stretch of railroad in which there is a single track over which only one train at a time is allowed. Guarding this track is a semaphore. A train must wait before entering the single track until the semaphore is in a state that permits travel. When the train enters the track, the semaphore changes state to prevent other trains from entering the track. A train that is leaving this section of track must again change the state of the semaphore to allow another train to enter. In the computer version, a semaphore appears to be a simple integer. A process (or a thread) waits for permission to proceed by waiting for the integer to become 0. The signal if it proceeds signals that this by performing incrementing the integer by 1. When it is finished, the process changes the semaphore's value by subtracting one from it.

Semaphores let processes query or alter status information. They are often used to monitor and control the availability of system resources such as shared memory segments.

Semaphores can be operated on as individual units or as elements in a set. Because System V IPC semaphores can be in a large array, they are extremely heavy weight. Much lighter weight semaphores are available in the threads library (see man semaphore and also Chapter 30.3) and POSIX semaphores (see below briefly). Threads library semaphores must be used with mapped memory . A semaphore set consists of a control structure and an array of individual semaphores. A set of semaphores can contain up to 25 elements.

In a similar fashion to message queues, the semaphore set must be initialized using semget(); the semaphore creator can change its ownership or permissions using semctl(); and semaphore operations are performed via the semop() function.

semaphore.c: Illustration of simple semaphore passing

/* semaphore.c --- simple illustration of dijkstra's semaphore analogy
*
* We fork() a child process so that we have two processes running:
* Each process communicates via a semaphore.
* The respective process can only do its work (not much here)
* When it notices that the semaphore track is free when it returns to 0
* Each process must modify the semaphore accordingly
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

union semun {
int val;
struct semid_ds *buf;
ushort *array;
};

main()
{ int i,j;
int pid;
int semid; /* semid of semaphore set */
key_t key = 1234; /* key to pass to semget() */
int semflg = IPC_CREAT | 0666; /* semflg to pass to semget() */
int nsems = 1; /* nsems to pass to semget() */
int nsops; /* number of operations to do */
struct sembuf *sops = (struct sembuf *) malloc(2*sizeof(struct sembuf));
/* ptr to operations to perform */

/* set up semaphore */

(void) fprintf(stderr, "\nsemget: Setting up seamaphore: semget(%#lx, %\
%#o)\n",key, nsems, semflg);
if ((semid = semget(key, nsems, semflg)) == -1) {
perror("semget: semget failed");
exit(1);
} else
(void) fprintf(stderr, "semget: semget succeeded: semid =\
%d\n", semid);


/* get child process */

if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}

if (pid == 0)
{ /* child */
i = 0;


while (i < 3) {/* allow for 3 semaphore sets */

nsops = 2;

/* wait for semaphore to reach zero */

sops[0].sem_num = 0; /* We only use one track */
sops[0].sem_op = 0; /* wait for semaphore flag to become zero */
sops[0].sem_flg = SEM_UNDO; /* take off semaphore asynchronous */


sops[1].sem_num = 0;
sops[1].sem_op = 1; /* increment semaphore -- take control of track */
sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT; /* take off semaphore */

/* Recap the call to be made. */

(void) fprintf(stderr,"\nsemop:Child Calling semop(%d, &sops, %d) with:", semid, nsops);
for (j = 0; j < nsops; j++)
{
(void) fprintf(stderr, "\n\tsops[%d].sem_num = %d, ", j, sops[j].sem_num);
(void) fprintf(stderr, "sem_op = %d, ", sops[j].sem_op);
(void) fprintf(stderr, "sem_flg = %#o\n", sops[j].sem_flg);
}

/* Make the semop() call and report the results. */
if ((j = semop(semid, sops, nsops)) == -1) {
perror("semop: semop failed");
}
else
{
(void) fprintf(stderr, "\tsemop: semop returned %d\n", j);

(void) fprintf(stderr, "\n\nChild Process Taking Control of Track: %d/3 times\n", i+1);
sleep(5); /* DO Nothing for 5 seconds */

nsops = 1;

/* wait for semaphore to reach zero */
sops[0].sem_num = 0;
sops[0].sem_op = -1; /* Give UP COntrol of track */
sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT; /* take off semaphore, asynchronous */


if ((j = semop(semid, sops, nsops)) == -1) {
perror("semop: semop failed");
}
else
(void) fprintf(stderr, "Child Process Giving up Control of Track: %d/3 times\n", i+1);
sleep(5); /* halt process to allow parent to catch semaphor change first */
}
++i;
}

}
else /* parent */
{ /* pid hold id of child */

i = 0;


while (i < 3) { /* allow for 3 semaphore sets */

nsops = 2;

/* wait for semaphore to reach zero */
sops[0].sem_num = 0;
sops[0].sem_op = 0; /* wait for semaphore flag to become zero */
sops[0].sem_flg = SEM_UNDO; /* take off semaphore asynchronous */


sops[1].sem_num = 0;
sops[1].sem_op = 1; /* increment semaphore -- take control of track */
sops[1].sem_flg = SEM_UNDO | IPC_NOWAIT; /* take off semaphore */

/* Recap the call to be made. */

(void) fprintf(stderr,"\nsemop:parent Calling semop(%d, &sops, %d) with:", semid, nsops);
for (j = 0; j < nsops; j++)
{
(void) fprintf(stderr, "\n\tsops[%d].sem_num = %d, ", j, sops[j].sem_num);
(void) fprintf(stderr, "sem_op = %d, ", sops[j].sem_op);
(void) fprintf(stderr, "sem_flg = %#o\n", sops[j].sem_flg);
}

/* Make the semop() call and report the results. */
if ((j = semop(semid, sops, nsops)) == -1) {
perror("semop: semop failed");
}
else
{
(void) fprintf(stderr, "semop: semop returned %d\n", j);

(void) fprintf(stderr, "Parent Process Taking Control of Track: %d/3 times\n", i+1);
sleep(5); /* Do nothing for 5 seconds */

nsops = 1;

/* wait for semaphore to reach zero */
sops[0].sem_num = 0;
sops[0].sem_op = -1; /* Give UP COntrol of track */
sops[0].sem_flg = SEM_UNDO | IPC_NOWAIT; /* take off semaphore, asynchronous */

if ((j = semop(semid, sops, nsops)) == -1) {
perror("semop: semop failed");
}
else
(void) fprintf(stderr, "Parent Process Giving up Control of Track: %d/3 times\n", i+1);
sleep(5); /* halt process to allow child to catch semaphor change first */
}
++i;

}

}
}
 

Semaphore is a mechanism mainly used to avoid shared data problem.

You can find more info abt semaphore in "Embedded software primer" by David. E. Simon
 

Semaphores let processes query or alter status information. They are often used to monitor

and control the availability of system resources such as shared memory segments

A semaphore is a protected variable (or abstract data type) and constitutes the classic

method for restricting access to shared resources (e.g. storage) in a multiprogramming

environment

Two types

(1)Counting Metaphore
(2)Binary Semaphore
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top