C - fork() and sharing memory

后端 未结 3 758
甜味超标
甜味超标 2021-02-19 15:30

I need my parent and child process to both be able to read and write the same variable (of type int) so it is \"global\" between the two processes.

I\'m assuming this wo

相关标签:
3条回答
  • 2021-02-19 16:19

    A variant that I used recently of shared memory is to open a mmap before forking. This avoids certain restrictions of the shared memory api. You don't have a size limit (the address range is limit), you don't need to generate the key from that absolute file. Here an example how I did it (I left out the error checking for the sake of brevity)

    ppid = getpid();
    shm_size  = ...;
    
    char *tmpFile = tempnam(NULL, "SHM_");    /* Generate a temp file which is virtual */
    
    /* Before we fork, build the communication memory maps */
    mm = open(tmpFile, O_RDWR|O_CREAT|O_TRUNC, 0664));    /* Create the temp file */
    ftruncate(mm, shm_size);                              /* Size the file to the needed size, on modern Unices it's */
                                                          /* a sparse file which doesn't allocate anything in the file system */
    
    /* The exact type of comm_area left to the implementer */
    comm_area *pCom = (comm_area *)mmap(NULL, shm_size, PROT_READ|PROT_WRITE, MAP_SHARED, mm, 0);
    if(pCom == (comm_area*)MAP_FAILED) handle_error();
    close(mm);                                /* We can close the file, we won't access it via handle */
    unlink(tmpFile);                          /* We can also remove the file so even if we crash we won't let corpses lying */
    free(tmpFile);
    
    /* Initialise some shared mutexes and semaphores */
    pthread_mutexattr_t mattr;
    pthread_mutexattr_init(&mattr);
    pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(&pCom->stderr_mutex, &mattr);         
    
    /* nSonAsked, global variable with the number of forked processes asked */
    for(nSon=0; nSon<nSonsAsked; nSon++) {
    
      printf("Setup son #%.2u ",nSon+1);
      /* Initialize a semaphore for each child process */
      sem_init(&pCom->sem_ch[nSon], USYNC_PROCESS, 0);
      if(fork() == 0 {
         ... /* do child stuff*/
         return;
      }
      /* Father, cleans up */
      pthread_mutexattr_destroy(&mattr);
      ...
      return;
    
    0 讨论(0)
  • 2021-02-19 16:27

    Since you are mentioning using fork(), I assume that you are living on a *nix-System

    From Unix.com

    The primary way to share data between processes using UNIX IPCs are:

    (1) Shared memory;

    (2) Sockets:

    There are other UNIX IPCs including

    (3) Message Queues.

    (4) Semaphores;

    (5) Signals.

    Your best bet (for IPCs) is to use shared memory segments, based on your post. You might need to use semaphores to insure that the shared memory operations are atomic.

    A tutorial on forking and shared memory is on dev shed:

    http://forums.devshed.com/c-programming-42/posix-semaphore-example-using-fork-and-shared-memory-330419.html

    another more in-depth description of using multithreading (if appilcable for your application) can be found here:

    https://computing.llnl.gov/tutorials/pthreads/

    0 讨论(0)
  • 2021-02-19 16:31

    If you need to share memory, perhaps using threads instead of processes would be a better solution?

    0 讨论(0)
提交回复
热议问题