分叉的子进程使用相同的信号量吗?

问题描述 投票:0回答:3

假设我创建了一个信号量。如果我分叉了一堆子进程,它们仍然会使用相同的信号量吗?

另外,假设我创建一个内部有信号量并分叉的结构。所有子进程是否仍然使用相同的信号量?如果不是,将结构+信号量存储在共享内存中是否允许子进程使用相同的信号量?

我真的很困惑我的分叉子进程如何使用相同的信号量。

c linux semaphore shared-memory fork
3个回答
13
投票

假设我创建了一个信号量。如果我分叉了一堆子进程,它们仍然会使用相同的信号量吗?

如果您使用 SysV IPC 信号量 (

semctl
),那么可以。如果您使用 POSIX 信号量 (
sem_init
),那么可以,但是 仅当您在创建时为 pshared 参数传递一个真值并将其放置在共享内存中。

另外,假设我创建一个内部有信号量并分叉的结构。所有子进程是否仍然使用相同的信号量?如果不是,将结构+信号量存储在共享内存中是否允许子进程使用相同的信号量?

“内部信号量”是什么意思?对 SysV IPC 信号量的引用将被共享,因为信号量不属于任何进程。如果您使用 POSIX 信号量,或者从 pthreads 互斥体和 condvars 构建某些内容,则需要使用共享内存和 pshared 属性(pthreads 也有 condvarsmutexes 的 pshared 属性)

请注意,出于这些目的,使用

MAP_SHARED
标志创建的匿名 mmap 算作(匿名)共享内存,因此无需实际创建命名共享内存段。 普通堆内存在fork后不会被共享


8
投票

假设我创建了一个信号量。如果我分叉一堆子进程, 他们仍然会使用相同的信号量吗?

这取决于您如何创建信号量,要使用 IPC 信号量来执行此操作,请参阅 semaphore.c:简单信号量传递的说明 作为示例。

另外,假设我创建一个内部有信号量并分叉的结构。做 所有子进程仍然使用相同的信号量?如果没有的话,会 将结构+信号量存储在共享内存中允许子进程 进程使用相同的信号量?

为此,您的信号量需要存储在父进程和子进程之间共享的区域(如共享内存)中,而不仅仅是在堆栈或堆上创建,因为当进程分叉时它会被复制。

我真的很困惑我的分叉子进程如何使用 相同的信号量。

信号量可以跨线程或进程共享。跨进程共享是在操作系统层面实现的。两个或多个不同的进程可以共享相同的信号量,即使这些进程不是通过分叉单个父进程创建的。

请参阅此示例,了解在父进程与其子进程之间共享未命名的 UNIX 信号量(要使用 gcc 进行编译,您需要

-pthread
标志):

#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

int main(void)
{
  /* place semaphore in shared memory */
  sem_t *sema = mmap(NULL, sizeof(*sema), 
      PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,
      -1, 0);
  if (sema == MAP_FAILED) {
    perror("mmap");
    exit(EXIT_FAILURE);
  }

  /* create/initialize semaphore */
  if ( sem_init(sema, 1, 0) < 0) {
    perror("sem_init");
    exit(EXIT_FAILURE);
  }
  int nloop=10;
  int pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid == 0) { 
    /* child process*/
    for (int i = 0; i < nloop; i++) {
      printf("child unlocks semaphore: %d\n", i);
      if (sem_post(sema) < 0) {
          perror("sem_post");
      }
      sleep(1);
    }
    if (munmap(sema, sizeof(sema)) < 0) {
      perror("munmap");
      exit(EXIT_FAILURE);
    }
      exit(EXIT_SUCCESS);
  }
  if (pid > 0) {
    /* back to parent process */
    for (int i = 0; i < nloop; i++) {
      printf("parent starts waiting: %d\n", i);
      if (sem_wait(sema) < 0) {
        perror("sem_wait");
      }
      printf("parent finished waiting: %d\n", i);
    }
    if (sem_destroy(sema) < 0) {
      perror("sem_destroy failed");
      exit(EXIT_FAILURE);
    }
    if (munmap(sema, sizeof(*sema)) < 0) {
      perror("munmap failed");
      exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
  }
}

输出将是:

parent starts waiting: 0
child unlocks semaphore: 0
parent finished waiting: 0
parent starts waiting: 1
child unlocks semaphore: 1
parent finished waiting: 1
...

您可能还想阅读 Linux 中的信号量,但请注意,给出的跨分支的 UNIX 信号量示例不起作用,因为作者忘记在

MAP_ANONYMOUS
中使用
mmap
标志。


-1
投票

试试这个

孩子和父母会交替增加共享变量

#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

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) < 0)
    {
      perror("semaphore initilization");
      exit(0);
    }
  /* create, initialize semaphore */
  if( sem_init(&(testptr->mutex2),1,0) < 0)
    {
      perror("semaphore initilization");
      exit(0);
    }
  if (fork() == 0) { /* child process*/
    for (i = 0; i < nloop; i++) {
      sem_wait(&(testptr->mutex2));
      printf("child: %d\n", testptr->temp++);
      sem_post(&(testptr->mutex1));
    }
    exit(0);
 /* back to parent process */
  for (i = 0; i < nloop; i++) {
    sem_wait(&testptr->mutex1);
    printf("parent: %d\n", testptr->temp++);
    sem_post(&(testptr->mutex2));
  }
  exit(0);
}
© www.soinside.com 2019 - 2024. All rights reserved.