william007
asked on
How to share semaphore between processes
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.
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.
ASKER
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.
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.
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.
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.
ASKER
>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"?
This gives me segmentation fault
>sem_t *sem = sem_open("semname", O_CREAT, 0644, 0);
This Works ok.
What is the reason"?
ASKER
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,ma xProcess);
if (count_sem==(void *)-1){
printf("Error create named semaphore\n");
exit(1);
}
while(1){
sem_getvalue(count_sem,&va lue);
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
====
#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
if (count_sem==(void *)-1){
printf("Error create named semaphore\n");
exit(1);
}
while(1){
sem_getvalue(count_sem,&va
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
ASKER
*What is the proper way to actually clean up the semaphore in the code?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks=)
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 ?