Dear All,
I am looking for a Sample Semaphore program on linux, Plz help me
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
More likely ashok449 is referring to the programming technique rather than the communication method.is this semaphore signalling? thats a cool little programming idea for a laugh, morse code translator would be good too..
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Now, Anyone can give me a small real-time example on semaphore
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...
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler