Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to share semaphore between processes

Posted on 2006-10-29
11
Medium Priority
?
1,643 Views
Last Modified: 2008-01-09
How to share semaphore between processes?
eg
static sem_t count_sem;
sem_init(&count_sem,1,3);

How to shared this  &count_sem between processes? So we can synchronise between processes.
0
Comment
Question by:william007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 17829577
Take a look at this wiki article about IPC (InterProcess Communication) :

http://en.wikipedia.org/wiki/Inter-process_communication

There's a few ways to achieve this. Shared memory could be a good option for you :

http://en.wikipedia.org/wiki/Shared_memory

What system/OS are you developing for ?
0
 
LVL 9

Author Comment

by:william007
ID: 17829598
I am using Linux fedora core 5.
I just researching for semaphore.h, there is a system call called sem_open to create named semaphore, I think it is possible that we can use that.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 17829669
If all you want to share between the processes is that semaphore, then that's indeed an option.

More info for that on the man page for sem_open :

http://www.penguintutor.com/man/man.php?topic=sem_open§ion=3


    sem_t *sem = sem_open("/tmp/semname", O_CREAT, 0644, 0);
    if (sem == SEM_FAILED) {
      perror("sem_open() failed ");
    }

Chec kthe man pages for your system to get the implementation you'll use.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Author Comment

by:william007
ID: 17829699
>sem_t *sem = sem_open("/tmp/semname", O_CREAT, 0644, 0);
This gives me segmentation fault
>sem_t *sem = sem_open("semname", O_CREAT, 0644, 0);
This Works ok.

What is the reason"?
0
 
LVL 9

Author Comment

by:william007
ID: 17829727
Code
====
#include <semaphore.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
static sem_t *count_sem;
int main(int argc, char* argv[]){
char* semName=argv[1];
mode_t mode=0777;
unsigned int maxProcess=3;
unsigned int i=O_CREAT|O_EXCL;
int value=0;
count_sem=sem_open(semName,i,mode,maxProcess);
if (count_sem==(void *)-1){
printf("Error create named semaphore\n");
exit(1);
}
while(1){
      sem_getvalue(count_sem,&value);
      printf("value=%d\n",value);
      sem_wait(count_sem);
}


}

Several Test Results
================
[root@localhost Desktop]# ./sem "/tmp/name1"
Segmentation fault
[root@localhost Desktop]# ./sem "/tmp/name2"
Segmentation fault
[root@localhost Desktop]# ./sem "/tmp/name3"
Segmentation fault
[root@localhost Desktop]# ./sem "sem1"
Segmentation fault
[root@localhost Desktop]# ./sem "sem2"
value=3
value=2
value=1
value=0

[root@localhost Desktop]# ./sem "sem2"
Segmentation fault
[root@localhost Desktop]# ./sem "sem3"
value=3
value=2
value=1
value=0
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 17829728
I guess your implementation doesn't support slashes other than the first. Try :

    sem_t *sem = sem_open("/semname", O_CREAT, 0644, 0);

The leading slash is to make sure that other processes will refer to the same named semaphore when using "/semname".
Although your implementation might not need the leading slash either. For that you'll have to check the man pages on your system.
0
 
LVL 9

Author Comment

by:william007
ID: 17829734
Now I have faced a big problem...How to clean up the named semaphore that I have created and forget to clear? What is the proper way to actually clean up with code?
0
 
LVL 9

Author Comment

by:william007
ID: 17829735
*What is the proper way to actually clean up the semaphore in the  code?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 2000 total points
ID: 17829737
And about your last post (with the code)

From the man pages :

    "If O_EXCL and O_CREAT are set, sem_open() fails if the semaphore name exists."

Don't set both :)

Also, don't compare with (void *)-1 to check for errors, use SEM_FAILED for that as in my earlier example - it's a lot more portable.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 2000 total points
ID: 17829767
>> *What is the proper way to actually clean up the semaphore in the  code?
Use sem_close to close the semaphore in each process, and sem_unlink in the process that actually created it (to remove it from the file system).

    sem_close(sem);
    sem_unlink("/semname");

Don't forget to check the return codes !
0
 
LVL 9

Author Comment

by:william007
ID: 17853551
Thanks=)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question