Do forked child processes use the same semaphore?

If you are using a SysV IPC semaphore ( semctl ), then yes. If you are using POSIX semaphores ( sem_init ), then yes, but only if you pass a true value for the pshared argument on creation and place it in shared memory.

Let's say I create a semaphore. If I fork a bunch of child processes, will they all still use that same semaphore? If you are using a SysV IPC semaphore (semctl), then yes.

If you are using POSIX semaphores (sem_init), then yes, but only if you pass a true value for the pshared argument on creation and place it in shared memory. Also, suppose I create a struct with semaphores inside and forked. Do all the child processes still use that same semaphore?

If not, would storing that struct+semaphores in shared memory allow the child processes to use the same semaphores? What do you mean be 'semaphores inside'? References to SysV IPC semaphores will be shared, because the semaphores don't belong to any process.

If you're using POSIX semaphores, or constructing something out of pthreads mutexes and condvars, you will need to use shared memory, and the pshared attribute (pthreads has a pshared attribute for condvars and mutexes as well) Note that anonymous mmaps created with the MAP_SHARED flag count as (anonymous) shared memory for these purposes, so it's not necessary to actually create a named shared memory segment. Ordinary heap memory will not be shared after a fork.

To clarify, I created a struct and inside the struct I put my POSIX semaphores. Then I put this struct into shared memory. In doing so, can I assume that all my child processes are accessing the same semaphores properly?

– canistr Jul 27 at 18:17 I created my semaphore with sem_init(&p. Mysem, 1, 1). Where p is a struct and mysem is a sem_t inside of the struct.

Does this look correct? – canistr Jul 27 at 18:19 @canistr, the struct must be placed in shared memory (memory mapped using mmap's MAP_SHARED flag), and the semaphores must be created with sem_init passing 1 for the pshared flag. If these conditions are satisfied, the child processes will share the semaphore properly.

– bdonlan Jul 27 at 18:20 So how do I use mmap with shared memory? – canistr Jul 27 at 18:44 @canistr, this is a bit beyond the scope of this question. There are a lot of resources on using mmap for shared memory if you search a bit, and if you still have trouble feel free to open another question on the topic – bdonlan Jul 27 at 18:47.

Let's say I create a semaphore. If I fork a bunch of child processes, will they all still use that same semaphore? Only if that is an IPC semaphore.

See semaphore. C: Illustration of simple semaphore passing for an example. Also, suppose I create a struct with semaphores inside and forked.Do all the child processes still use that same semaphore?

If not, would storing that struct+semaphores in shared memory allow the child processes to use the same semaphores? That is probably not going to work for in-process semaphores. Even if it does, that is probably a bad idea.

I'm really confused about how my forked child processes can use the same semaphores. The semaphore can be shared across threads or processes. Cross-process sharing is implemented on the operating system level.

Two or more different processes can share the same semaphore even if those processes were not created by forking a single parent process. You may want to read Semaphores in Linux as well.

So if I want forked children to share a semaphore, they need to be IPC semaphores. Can I store these semaphores in a struct and pass it to different functions? I'm confused about your second point because I want to use shared memory to pass information between child processes and protect them with those semaphores.

– canistr Jul 27 at 17:29 If I wanted to specifically use unnamed semaphores, would this differ greatly from IPC semaphores? – canistr Jul 27 at 17:36 @canistr: You can indeed use shared memory to share information between processes, just don't put a semaphore into that memory. Just go trough the first example I have provided - it does fork () and both child and parent work with the same semaphore without shared memory.

Throw shared memory with the information you want to share on top of this example and you will be OK. – Vlad Lazarenko Jul 27 at 17:37 @canistr: use sem_init to initialize unnamed semaphores, that should do the job. See the man page for a nice description - kernel.Org/doc/man-pages/online/pages/man3/sem_init.3.

Html – Vlad Lazarenko Jul 27 at 17:40 @Vlad, semaphores can be shared in shared memory if you pass the proper pshared arguments; see my answer for references – bdonlan Jul 27 at 18:00.

Try this child and parent would increment the shared variable alternatively #include #include #include #include #include #include #include #include #include struct test { sem_t mutex1; sem_t mutex2; int temp; }test1; int main(int argc, char **argv) { int fd, i,count=0,nloop=10,zero=0,*ptr; struct test *testptr; //open a file and map it into memory sem_t mutex; fd = open("log. Txt",O_RDWR|O_CREAT,S_IRWXU); write(fd,&zero,sizeof(int)); ptr = mmap(NULL, sizeof(struct test),PROT_READ |PROT_WRITE,MAP_SHARED,fd,0); close(fd); memcpy(ptr, &test1, sizeof(test1)); testptr = (struct test *)ptr; // testptr = (struct test *)&test1; /* create, initialize semaphore */ if( sem_init(&(testptr->mutex1),1,1) mutex2),1,0) mutex2)); printf("child: %d\n", testptr->temp++); sem_post(&(testptr->mutex1)); } exit(0); /* back to parent process */ for (i = 0; I mutex1); printf("parent: %d\n", testptr->temp++); sem_post(&(testptr->mutex2)); } exit(0); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions