Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 进程间通信--在共享内存中锁定互斥_C++_Posix_Ipc_Mutex_Shared Memory - Fatal编程技术网

C++ 进程间通信--在共享内存中锁定互斥

C++ 进程间通信--在共享内存中锁定互斥,c++,posix,ipc,mutex,shared-memory,C++,Posix,Ipc,Mutex,Shared Memory,我有两个进程将执行相同的代码: #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <semaphore.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wa

我有两个进程将执行相同的代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <pthread.h>


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

struct shm_content
{
      pthread_mutex_t   mutex;
};

pthread_mutex_t    *mptr; //Mutex Pointer
pthread_mutexattr_t matr; //Mutex Attribute

int   shared_mem_id;      //shared memory Id
int   *mp_shared_mem_ptr; //shared memory ptr -- pointing to mutex

int main (void)
{
    int  rtn;
    size_t shm_size;

    /* initialize shared memory segment */
    shm_size = 1*sizeof(pthread_mutex_t);

    if ((shared_mem_id = shmget(IPC_PRIVATE, shm_size, 0660)) < 0)
    {
        perror("shmget"), exit(1) ;
    }
    if ((mp_shared_mem_ptr = (int *)shmat(shared_mem_id, (void *)0, 0)) == NULL)
    {
        perror("shmat"), exit(1);
    }

    //Offset to find the location of the mutex variable in the shared memory
    shm_content* pcontent = reinterpret_cast<shm_content*>(mp_shared_mem_ptr);

    mptr = &(pcontent->mutex);

    // Setup Mutex
    if (rtn = pthread_mutexattr_init(&matr))
    {
        fprintf(stderr,"pthreas_mutexattr_init: %s",strerror(rtn)),exit(1);
    }
    if (rtn = pthread_mutexattr_setpshared(&matr,PTHREAD_PROCESS_SHARED))
    {
        fprintf(stderr,"pthread_mutexattr_setpshared %s",strerror(rtn)),exit(1);
    }
    if (rtn = pthread_mutex_init(mptr, &matr))
    {
        fprintf(stderr,"pthread_mutex_init %s",strerror(rtn)), exit(1);
    }

        // Lock mutex and then wait for signal to relase mutex
    printf("child mutex lock \n");
    pthread_mutex_lock( mptr );
    printf("child mutex locked\n");

    int i = 0; // :)

    //busy wait
    while (i<10)
    {
        printf("Busy Wait!!! I AM PROCESS 1\n");
        //in the second process will change this line to :
        //printf("Busy Wait!!! I AM PROCESS 2\n");
        sleep(2);
    }

    printf("child mutex unlock\n");
    pthread_mutex_unlock( mptr );
    printf("child mutex unlocked\n");

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#ifndef\u POSIX\u线程\u进程\u共享
#错误此系统不支持进程共享互斥
#恩迪夫
结构shm_内容
{
pthread_mutex_t mutex;
};
pthread_mutex_t*mptr//互斥指针
pthread_mutextatr_t matr//互斥属性
int共享内存id//共享内存Id
int*mp_共享_内存_ptr//共享内存ptr——指向互斥体
内部主(空)
{
int rtn;
尺寸(t shm)尺寸;;
/*初始化共享内存段*/
shm_size=1*sizeof(pthread_mutex_t);
如果((共享内存id=shmget(IPC_私有,shm_大小,0660))<0)
{
perror(“shmget”),出口(1);
}
if((mp_shared_mem_ptr=(int*)shmat(shared_mem_id,(void*)0,0))==NULL)
{
perror(“shmat”),出口(1);
}
//偏移量以查找共享内存中互斥变量的位置
shm_内容*P内容=重新解释(mp_共享_成员ptr);
mptr=&(pcontent->mutex);
//设置互斥
if(rtn=pthread\u mutexattr\u init(&matr))
{
fprintf(stderr,“pthreas_mutexattr_init:%s”,strerror(rtn)),出口(1);
}
if(rtn=pthread\u mutexattr\u setpshared(&matr,pthread\u PROCESS\u SHARED))
{
fprintf(stderr,“pthread_mutexttr_setpshared%s”,strerror(rtn)),退出(1);
}
if(rtn=pthread\u mutex\u init(mptr和matr))
{
fprintf(stderr,“pthread_mutex_init%s”,strerror(rtn)),退出(1);
}
//锁定互斥锁,然后等待信号释放互斥锁
printf(“子互斥锁”);
pthread_mutex_lock(mptr);
printf(“子互斥锁\n”);
int i=0;/:)
//忙等
而(i
if((共享内存id=shmget(IPC\u PRIVATE,shm\u size,0660))<0)
^^^^^^^^^^^
共享内存对每个进程都是私有的,因此其中的互斥量对每个进程都是私有的

内存和互斥锁可以跨fork继承,但这与您当前的设计无关


您需要非私有共享内存。这将是放弃SysV样式的共享内存接口(
shmget()
等)并采用更简单的POSIX接口()的好时机.

上面的代码不会运行,例如,我没有定义,我认为它的目的是让进程1和进程2共享信息。此外,您是否有可能是前C代码员?上面的代码非常C-ish(因为没有更好的术语),我只是问,因为我也经常返回到类型,并且在编写C++时使用C风格的代码修复了代码:)上面的代码将运行(g++-o temp.cpp-pthread)。但我仍然没有得到行为?线程是在哪里创建的?
if ((shared_mem_id = shmget(IPC_PRIVATE, shm_size, 0660)) < 0)
                            ^^^^^^^^^^^