473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

semaphores question

EARNEST
128 100+
hey guys,
let's say there is a while loop, that checks if it is allowed to write to a memory segment. if it is not allowed, it will wait a couple of ns, then it will check again and so on. what if semaphores unlock and let's say 2 processes at the same time will read it as unlocked and will try to perform action. how can i eliminate that?
Apr 9 '10 #1
4 1421
jkmyoung
2,057 Expert 2GB
One of them will get to the semaphore first, and will lock it. The other process will try to lock it but fail. The semaphore code is written so that the part that locks it is mutually exclusive (synchronized), requires some sort of resource so that it can't be done at the same time. This way, only the smallest amount of code possible is synchronized.

If it is not working for you, the semaphore is not written correctly.
Apr 9 '10 #2
EARNEST
128 100+
Here is my semaphores, not sure abotu their correctness tho.
Expand|Select|Wrap|Line Numbers
  1. key_t sem_key;
  2.     sem_key = 2;
  3.  
  4.     struct sembuf sbuf[1];
  5.  
  6.     union semun {
  7.         int val;
  8.         struct semid_ds *buf;
  9.         ushort * array;
  10.     } arg;
  11.  
  12.     arg.val = 0;
  13.  
  14.     segm_id = semget(sem_key, 1, IPC_CREAT | 0666);
  15.     if (segm_id < 0)
  16.     {
  17.         perror("sem id, semget");
  18.         exit(1);
  19.     }
  20.  
  21.     if (semctl(segm_id, 0, SETVAL, arg) < 0)
  22.     {
  23.         //mvprintw(0,1,"cannot set");
  24.     }
  25.     else
  26.     {
  27.         //mvprintw(0,1,"initialized sems");
  28.     }
  29.  
  30.     sbuf[0].sem_num = 0;
  31.     sbuf[0].sem_op = 1;
  32.     sbuf[0].sem_flg = 0;
  33.  
  34. /* DO IPC, SHARED MEM, WRITING or READINING */
  35.  
  36. //when finished
  37.  
  38.     sbuf[0].sem_num = 0;
  39.     sbuf[0].sem_op = -1;
  40.     sbuf[0].sem_flg = 0;    
  41.  
Apr 9 '10 #3
donbock
2,426 Expert 2GB
Are these semaphore functions (semget and semctl) from a standard library or did you write them yourself?
Apr 9 '10 #4
EARNEST
128 100+
standard library, C, unix
Apr 9 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nish | last post by:
Hi, I have some code, to which I added usage of posix semaphores. I am working on a solaris platform. After doing that, my process becomes unkillable, that is it will not terminate if sent a...
1
by: Mehmet Turker | last post by:
Hi; In a research project, I want to use shared memory with semaphores. First I have to create an environment for interprocess messaging. I'm thinking of having a main process which creates a big...
2
by: schneider jp | last post by:
Hi everybody For a project of system programming, we need to use semaphores, but i got some pbs... when I compile, i get perror with sem_open("machin", O_CREAT, 0666, 0); and perror says...
11
by: FiLH | last post by:
Hello, I would like to know if posix semaphores are inter processes or just semaphores for threads inside the same process. I have not seen it defined in the posix specification I have found,...
1
by: jinksk | last post by:
I have a C# application and a VB6 application that need to share a dialup connection to the internet. Don't ask why, this is one of those corporate "do or die" things. The only way I can see to...
1
by: Edwin New | last post by:
I have a requirement to run two separate postmasters on the one machine. It looks like they compete for resources (semaphores) so won't run concurrently. I compiled twice, specifying different...
5
by: Steven Spencer | last post by:
Hi, I'm doing an IT degree at university, and my lecturer's do not teach m$ technologies. I was wondering if anyone could point me to the implementation of mutex's and semaphores in .net? Or the...
0
arne
by: arne | last post by:
Hi, I am using Perl ithreads. The threads I create shall access a shared hash. In order to control access to the hash, I would like to use a semaphore. (BTW, does the hash need the protection at...
0
by: The Advocate | last post by:
Hello, how can we implement semaphores using monitors. I got how we can do it the way round. I did alot of search but couldnt find any implementation of semaphores using monitors,. Can anyone help me...
5
by: davidcollins001 | last post by:
Hi, Firstly please excuse the long ramblingness. I need some conceptual help, I am trying to make two programs that are exactly the same but take it in turns to execute something, so the output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.