Interprocess synchronization

Mutex mutex

Interprocess mutex can be used to achieve the purpose of synchronization. But it should be initialized before pthread_mutex_init, modify its properties is shared between processes. mutex attribute modification functions mainly in the following.

The main application functions:

       pthread_mutexattr_t mattr Type: used to define the mutex lock [Property]

       pthread_mutexattr_init function : initializes a mutex attribute object

              int pthread_mutexattr_init(pthread_mutexattr_t *attr);

       pthread_mutexattr_destroy function : to destroy mutex attribute objects (rather than destroy the lock)

              int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);

       pthread_mutexattr_setpshared function : to modify mutex attributes.

              int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);

              Reference 2: pshared values:

                     Thread lock: PTHREAD_PROCESS_PRIVATE (default attribute is the thread of the mutex lock, interprocess private)

                     Process lock: PTHREAD_PROCESS_SHARED

Interprocess mutex example

Interprocess synchronization is achieved using a mutex:

#include <fcntl.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/wait.h>

struct mt {
    int num;
    pthread_mutex_t mutex;
    pthread_mutexattr_t mutexattr;
};

int main(void)
{
    int fd, i;
    struct mt *mm;
    pid_t pid;

    fd = open("mt_test", O_CREAT | O_RDWR, 0777);
    ftruncate(fd, sizeof(*mm));
    mm = mmap(NULL, sizeof (* mm), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); 
    use Close (fd); 
    unlink ( " mt_test " );
     // mm = mmap (NULL, sizeof (* mm), PROT_READ | PROT_WRITE, MAP_SHARED | the MAP_ANON , -1, 0); 
    Memset (mm, 0 , the sizeof (* mm)); 

    to pthread_mutexattr_init ( & mm-> mutexattr);                                   // initialize mutex attribute object 
    pthread_mutexattr_setpshared (& mm-> mutexattr, PTHREAD_PROCESS_SHARED is);     // modify properties for the process shared between 
    pthread_mutex_init (& mm-> mutex, & mm-> mutexattr);                           // initialize a mutex Suo

    pid = fork();
    if (pid == 0) {
        for (i = 0; i < 10; i++) {
            pthread_mutex_lock(&mm->mutex);
            (mm->num)++;
            printf("-child----num++   %d\n", mm->num);
            pthread_mutex_unlock(&mm->mutex);
            sleep(1);
        }
    } else if (pid > 0) {
        for ( i = 0; i < 10; i++) {
            sleep(1);
            pthread_mutex_lock(&mm->mutex);
            mm->num += 2;
            printf("-parent---num+=2  %d\n", mm->num);
            pthread_mutex_unlock(&mm->mutex);
        }
        wait(NULL);
    }

    pthread_mutexattr_destroy(&mm->mutexattr);          //销毁mutex属性对象
    pthread_mutex_destroy(&mm->mutex);                //销毁mutex
    munmap(mm,the sizeof (* mm));                           // release mapping area 
    return  0 ; 
}        

operation result:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190821$ ./process_mutex

--------- ------- whether the parent + 2 2

-child-----------------num++   3

--------- ------- whether the parent + 2 = 5

-child-----------------num++   6

--------- ------- whether the parent 8 + 2

-child-----------------num++   9

--------- ------- whether the parent + 2 = 11

-child-----------------num++   12

--------- ------- whether the parent + 2 = 14

-child-----------------num++   15

--------- ------- whether the parent + 2 = 17

-child-----------------num++   18

--------- ------- whether the parent + 2 = 20

-child-----------------num++   21

--------- ------- whether the parent + 2 = 23

-child-----------------num++   24

--------- ------- whether the parent + 2 = 26

-child-----------------num++   27

--------- ------- whether the parent + 2 = 29

-child-----------------num++   30

 

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11389852.html