Rules | Recent posts | topic RSS | Search | Register  | Log in

What is a semaphore ?

 
Post new topic  Reply to topic    EDAboard.com Forum Index -> Embedded Systems and Real-Time OS
Author Message
atmaca



Joined: 13 Jan 2004
Posts: 352
Helped: 1


Post04 Feb 2005 23:39   What is a semaphore ?

What is a semaphore and where can i find detailes information for that ?
Back to top
flatulent



Joined: 19 Jul 2002
Posts: 4866
Helped: 292
Location: Middle Earth


Post04 Feb 2005 23:45   Re: What is a semaphore ?

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.
Back to top
DanPartelly



Joined: 06 Jan 2005
Posts: 11


Post04 Feb 2005 23:46   Re: What is a semaphore ?

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



Joined: 13 Jan 2004
Posts: 352
Helped: 1


Post05 Feb 2005 2:00   Re: What is a semaphore ?

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


Yes it is
Back to top
Ace-X



Joined: 25 Jan 2002
Posts: 594
Helped: 26


Post05 Feb 2005 2:15   What is a 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).
Back to top
faroo28



Joined: 18 Jan 2005
Posts: 34
Helped: 2


Post06 Feb 2005 3:06   Re: What is a semaphore ?

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
Back to top
maui



Joined: 19 Nov 2004
Posts: 54
Helped: 4
Location: Melbourne Victoria Australia


Post06 Feb 2005 9:24   What is a semaphore ?

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.
Back to top
raj_rohit10



Joined: 14 Jul 2004
Posts: 113
Helped: 1


Post07 Feb 2005 6:44   Re: What is a semaphore ?

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......
Back to top
tcsa35



Joined: 19 Mar 2002
Posts: 224
Helped: 2


Post07 Feb 2005 15:43   What is a semaphore ?

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.
Back to top
jith



Joined: 17 Feb 2005
Posts: 10


Post27 Feb 2005 13:11   Re: What is a semaphore ?

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:
Back to top
asemnor



Joined: 08 Mar 2005
Posts: 27


Post14 Mar 2005 15:51   Re: What is a semaphore ?

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
Back to top
devennan



Joined: 07 May 2004
Posts: 70
Helped: 4
Location: India


Post15 Mar 2005 13:32   Re: What is 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
Back to top
kapilhali



Joined: 08 Nov 2004
Posts: 11


Post04 May 2006 11:21   What is a semaphore ?

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
Back to top
teteamigo



Joined: 05 Mar 2005
Posts: 131
Helped: 14
Location: Portugal


Post04 May 2006 12:38   Re: What is a semaphore ?

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).
Back to top
bangash



Joined: 01 Apr 2006
Posts: 92
Helped: 4
Location: London


Post04 May 2006 16:02   Re: What is a semaphore ?

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.
Back to top
nikhil_damle



Joined: 15 Dec 2005
Posts: 83
Helped: 2


Post10 May 2006 7:31   Re: What is a semaphore ?

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)
Back to top
LBdgWgt



Joined: 06 Mar 2006
Posts: 83
Helped: 5


Post11 May 2006 13:39   Re: What is a semaphore ?

maui wrote:
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,
Back to top
lightning



Joined: 07 May 2006
Posts: 12


Post11 May 2006 17:49   Re: What is a semaphore ?

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;

}

}
}
Back to top
thendral



Joined: 03 May 2006
Posts: 34
Helped: 2


Post11 May 2006 19:14   Re: What is a semaphore ?

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
Back to top
Prakash



Joined: 22 Jun 2005
Posts: 21


Post25 May 2006 2:40   Re: What is a semaphore ?

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
Back to top
kavgoxin



Joined: 17 Sep 2004
Posts: 35
Helped: 1


Post01 Jun 2006 15:55   Re: What is a semaphore ?

Refer embedded system rajakamal for more detailed info.
Back to top
krishna410



Joined: 03 Sep 2006
Posts: 74
Helped: 2


Post18 Jun 2008 9:45   Re: What is a semaphore ?

please read operating system concepts textbook for detailed explanation by Galvin
Back to top
s.san2006



Joined: 30 Mar 2008
Posts: 12


Post19 Jun 2008 5:43   Re: What is a semaphore ?

Semaphore is a integer variable set to 1 by default in preventing modifications of the variables within of the function to be used by multiple processes which share this same function. Refer Galvin, Operating System Concepts.
Back to top
Post new topic  Reply to topic    EDAboard.com Forum Index -> Embedded Systems and Real-Time OS
Page 1 of 1 All times are GMT + 1 Hour


Abuse
Administrator
Moderators
topic RSS 
sitemap