Dear All,
I am looking for a Sample Semaphore program on linux, Plz help me
Printable View
Dear All,
I am looking for a Sample Semaphore program on linux, Plz help me
GIFY.
I have an Idea on Semaphores but dont know how to apply on a real time application.
is this semaphore signalling? thats a cool little programming idea for a laugh, morse code translator would be good too..
What sort of "real time" are we talking here?
Using Linux as a base for strict real-time applications is like using a Ford Transit to enter a Formula 3 race - it hasn't got a chance.
On the other hand, if you are just after some "decent human perceived realtime" then I suppose it would work.
What exactly is the problem with applying semaphores on a realtime application - it would be better if you discussed at a "bigger picture" level what you want to achieve, because that would allow us to give you sensible advice.
--
Mats
More likely ashok449 is referring to the programming technique rather than the communication method.Quote:
is this semaphore signalling? thats a cool little programming idea for a laugh, morse code translator would be good too..
Ok, here is some code I found on net. Plz tell me how this works.
thanks in advance ...Code:#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#define KEY (1492)
void main()
{
int id;
struct sembuf operations[1];
int retval;
id = semget(KEY, 1, 0666);
if(id < 0)
{
fprintf(stderr, "Program sema cannot find semaphore, exiting.\n");
exit(0);
}
/* Do a semaphore V-operation. */
printf("Program sema about to do a V-operation. \n");
operations[0].sem_num = 0;
/* Which operation? Add 1 to semaphore value : */
operations[0].sem_op = 1;
/* Set the flag so we will wait : */
operations[0].sem_flg = 0;
/* So do the operation! */
retval = semop(id, operations, 1);
if(retval == 0)
{
printf("Successful V-operation by program sema.\n");
}
else
{
printf("sema: V-operation did not succeed.\n");
perror("REASON");
}
}
I personally think that code is pretty clear [even tho' I've never seen that piece of code before] - can you perhaps clarify which part of the code you don't understand.
--
Mats
retval = semop(id, operations, 1);
if retval ==0 then semop is successfull which means I need to place my code here, If so how to aquire/release again.
if retval ==0 then semop is successfull which means I need to place my code here, If so how to aquire/release again.Code:
if(retval ===0)
{
// semop success
do some stuff .....
// how to release or aquire lock
operations[0].sem_num = 0;
/* Which operation? Add 1 to semaphore value : */
operations[0].sem_op = -1;
/* Set the flag so we will wait : */
operations[0].sem_flg = 0;
semop(id, operations, 1); // is this corrrect
}
That looks about right, yes.
--
Mats
Now, Anyone can give me a small real-time example on semaphore
Seriously?
Doesn't google work!? I'll have to quote brewbuck on this -- GIFY
zacs7 and brewbuck, you see in this thread I almost provide everything except a clarification which I require. Thats all I need and thats all about a forum is.
If getting sample code from google is enough then what is the requirement of a forum.
Forum means expressing yourself and getting advices.
Anyhow thanks for everything...