pthread_mutex实现进程间同步

   前面实验室的师兄们在讨论pthread这套接口能不能实现进程间同步,自己对这个不是十分地了解,但是对这个比较感兴趣,于是就在实验室师兄的指导下去实现了这个同步机制。

   测试的结果就是可以用pthread_mutex_t来实现进程间的同步。
   需要注意的是初始化 mutex 时需要指定 PTHREAD_PROCESS_SHARED 这个属性,代码如下

pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);

pthread_mutex_init(&mutex, &attr);



   其中 pthread_mutexattr_setpshared 这个 API 在 open group 的标准中定义 http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_getpshared.html

不过这个 feature 不是所有的平台都支持,compile time 用 _POSIX_THREAD_PROCESS_SHARED 检查,runtime 用 sysconf(_SC_THREAD_PROCESS_SHARED) 检查所在平台是否支持。



另外我是通过在 mmap 调用指定 MAP_SHARED 实现在父子进程间共享内存的,并没有使用 System V 的共享内存 API。

这是示例代码:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#ifndef _POSIX_THREAD_PROCESS_SHARED	
#error "This platform does not support process shared mutex"	
#endif

/* Define globally accessible variables and a mutex */

#define NUMTHRDS 4

int* shared_sum = NULL; 

pthread_mutex_t *sum_mutex;


int process_fn()
{
    int i;       
    for ( i=0; i < 100000000; i++)
    {
        pthread_mutex_lock(sum_mutex);
        *shared_sum += 1;
        pthread_mutex_unlock(sum_mutex);
    }
    return 0;
}

int main(int argc, char** argv)
{

  int i;
  pthread_mutex_t *p_map;
  int* temp;
  int cpid;
  p_map=(pthread_mutex_t*)mmap(NULL,sizeof(pthread_mutex_t)*10,PROT_READ|PROT_WRITE,
       MAP_SHARED|MAP_ANONYMOUS,-1,0);
 
  sum_mutex = p_map;
  shared_sum = (int*) (p_map+3);
  *shared_sum = 0;
 
  pthread_mutexattr_t mutex_shared_attr;

  /* Set pthread_mutex_attr to process shared */
  pthread_mutexattr_init(&mutex_shared_attr);
  pthread_mutexattr_setpshared(&mutex_shared_attr, PTHREAD_PROCESS_SHARED);
 
  pthread_mutex_init(sum_mutex, &mutex_shared_attr);

  cpid = fork();
  if(cpid == 0)
  {
      process_fn();
      return 0;
  } else {
      process_fn();
      waitpid(cpid, NULL, 0);
  }
  
  temp = (int*) (p_map+3);
  printf("in the end, the shared sum: %d\n",*temp);
  return 0;
}



这是进程中有线程的示例代码。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#ifndef _POSIX_THREAD_PROCESS_SHARED	
#error "This platform does not support process shared mutex"	
#endif

/* Define globally accessible variables and a mutex */

#define NUMTHRDS 4

int* shared_sum = NULL; 

pthread_t callThd[NUMTHRDS];
pthread_mutex_t *sum_mutex;

void *thread_fn(void *arg)
{
    int i;
    int id = (int)((long)arg);
    for(i = 0; i < 1000000; i++)
    {
        pthread_mutex_lock(sum_mutex);
        *shared_sum += 1;
        /*printf("Thread #%d:  mysum=%d\n",id, *shared_sum);*/
        pthread_mutex_unlock(sum_mutex);
    }
}

int process_fn()
{

    pthread_attr_t attr;

  
    int i;       
    /* Create threads to perform the dotproduct  */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for(i=0;i<NUMTHRDS;i++)
    {
        pthread_create(&callThd[i], &attr, thread_fn, (void *)((long)i)); 
    }
  
    /* Wait on the other threads */
    for(i=0;i<NUMTHRDS;i++) {
        pthread_join(callThd[i], NULL);
    }
    /* After joining, print out the results and cleanup */
    printf("Sum =  %d\n",  *shared_sum);
    return 0;
}

int main(int argc, char** argv)
{

    int i;
    pthread_mutex_t *p_map;
    int cpid;
    p_map=(pthread_mutex_t*)mmap(NULL,sizeof(pthread_mutex_t)*10,PROT_READ|PROT_WRITE,
           MAP_SHARED|MAP_ANONYMOUS,-1,0);
 
    sum_mutex = p_map;
    shared_sum = (int*) (p_map+3);
    *shared_sum = 0;
 
    pthread_mutexattr_t mutex_shared_attr;

    /* Set pthread_mutex_attr to process shared */
    pthread_mutexattr_init(&mutex_shared_attr);
    pthread_mutexattr_setpshared(&mutex_shared_attr, PTHREAD_PROCESS_SHARED);
 
    pthread_mutex_init(sum_mutex, &mutex_shared_attr);

    cpid = fork();
    if(cpid == 0)
    {
        process_fn();
        return 0;
    } else {
        process_fn();
    }
  
    printf("In the end, the shared sum: %d\n",*shared_sum);
    return 0;
}

你可能感兴趣的:(pthread,mutex,进程间同步)