• Post Reply Bookmark Topic Watch Topic
  • New Topic

using semaphore for synchronization and mutual exclusion  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code parent will create two child processes using fork. These children will read a queue. If queue is empty they will wait for it to be filled. The child which comes first will read from this queue and perform the task. Thus I need to synchronise them and also ensure mutual exclusion for the queue. Here is the draft.

ppid=getpid();  //get parent pid
create a semaphore s
while(two times)//run two times to create two children
{
if(getpid()==ppid);    
a=fork();
if(a==0)
{
while(1)

lock the semaphore s
while(queue empty)
{do nothing}
read the current element of queue
increment queue pointer
release semaphore s
process the element which is read
}
}
Thus synchronization is needed to decide which of two children will run next. And mutual exclusion is needed to ensure queue is read properly. How do I implement this?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravindra koranga wrote:
Thus synchronization is needed to decide which of two children will run next. And mutual exclusion is needed to ensure queue is read properly. How do I implement this?


You actually have quite a few options here...

First, the Linux SysV IPC libraries actually have a message queue implementation that you can use -- and it is designed to be used by multiple processes.

Second, if you already have your own queue, and just wants to synchronize it, then the same IPC library also supports Semaphores.

And Third, the POSIX threading library also supports a few options. I know that you stated that these are different processes, but that doesn't mean that it won't work. It is possible to use shared memory (also an SysV IPC) and with it share the POSIX structures for synchronization purposes.

With POSIX, you can have Semphores ... but since, you seem to only want them for mutual exclusion, the POSIX library also support mutexes too.... so, lots of options...

Henry
 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose I have an array filled with some integers. I want to share this array among children. Can I do so using shmget?
for eg
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravindra koranga wrote:suppose I have an array filled with some integers. I want to share this array among children. Can I do so using shmget?


Sure, but it may be a good idea to allocated the space directly in the shared memory instead... meaning declare the variable as a pointer, get it assigned via shmget() and shmat(), and then, with the pointer pointing at the share memory, fill in the array.

Henry
 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats exactly what I want to do. I need to store data as a 2d array of pointer with dimensions 10x10 for e.g


But this gives me segmentation fault error during execution. Why does it show error?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravindra koranga wrote:
But this gives me segmentation fault error during execution. Why does it show error?


Hmmm.... it looks like you duplicated your question across more than one topic...

https://coderanch.com/t/669910/Linux-UNIX/pointer-type-array-character-pointers

Please don't do that. It causes confusion, and even wasted effort. Please continue this discussion in your other topic.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!