#include <stdio.h>
#include <sys/types.h>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <string>
#include <semaphore.h>
using namespace std;
int main(int argc, char *argv[]){
int pshared = 1;
unsigned int value = 0;
sem_t sem_name;
sem_init(&sem_name, pshared, value);
int parentpid = getpid();
pid_t pid = fork();
if (parentpid == getpid()){
cout << "parent id= " << getpid() << endl;
sem_wait(&sem_name);
cout << "child is done." << endl;
}
if (parentpid != getpid()){
cout << "child id= " << getpid() << endl;
for (int i = 0; i < 10; i++)
cout << i << endl;
sem_post(&sem_name);
}
sleep(4);
return 0;
}
the result should be:
parent id 123456.
child id 123457.
0
1
2
3
4
5
6
7
8
9
child is done.
Program exits, but instead it never signals the semaphore.
To release or signal a semaphore, we use the sem_post function: int sem_post(sem_t *sem); A semaphore is initialised by using sem_init(for processes or threads) or sem_open (for IPC). sem_init(sem_t *sem, int pshared, unsigned int value);
You can check to see if a Semaphore is signaled by calling WaitOne and passing a timeout value of 0 as a parameter. This will cause WaitOne to return immediately with a true or false value indicating whether the semaphore was signaled.
Unnamed semaphores are either private, inherited through fork() , or are protected by access protections of the regular file in which they are allocated and mapped. Named semaphores are like process-shared semaphores, except that named semaphores are referenced with a pathname rather than a pshared value.
If the value of pshared is zero, then the semaphore cannot be shared between processes. If the value of pshared is nonzero, then the semaphore can be shared between processes. Multiple threads must not initialize the same semaphore. A semaphore must not be reinitialized while other threads might be using the semaphore.
From sem_init
's manpage:
If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)). (Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), etc.
POSIX semaphores are on-the-stack structs. They aren't reference-counted references to a kernel-maintained struct like filedescriptors are. If you want to share a POSIX semaphore with two processes, you need to take care of the sharing part yourself.
This should work:
#include <fstream>
#include <iostream>
#include <semaphore.h>
#include <stdio.h>
#include <string>
#include <sysexits.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]){
using namespace std;
sem_t* semp = (sem_t*)mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, 0, 0 );
if ((void*)semp == MAP_FAILED) { perror("mmap"); exit(EX_OSERR); }
sem_init(semp, 1 /*shared*/, 0 /*value*/);
pid_t pid = fork();
if(pid < 0) { perror("fork"); exit(EX_OSERR); }
if (pid==0){ //parent
cout << "parent id= " << getpid() << endl;
sem_wait(semp);
cout << "child is done." << endl;
}else { //child
cout << "child id= " << getpid() << endl;
for (int i = 0; i < 10; i++)
cout << i << endl;
sem_post(semp);
}
return 0;
}
Note: If you want just this behavior, then waitpid
is obviously the way to go. I'm assuming what you want is to test out POSIX semaphores.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With