c - Init中的C错误中的共享互斥锁

c - Init中的C错误中的共享互斥锁,第1张

我正在尝试使用共享内存创建一个程序,并在同一个程序中执行多次以增加一个值,所有这些都使用一个共享的互斥锁。

问题在于,在我的第二次执行中,功能mutex_init失败。

任何想法?

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

#define SM_SIZE sizeof (struct shared)
#define min 4
#define max 10

struct shared
{
    pthread_mutex_t mutex;
    int i;

};

char *shared1_name="asdf";


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

int id_sharedMem1;
int i,j,loops;
struct shared *memoriaC;

loops = atoi(argv[1]);

id_sharedMem1 = shm_open ( shared1_name , O_RDWR, 644 );    

if(id_sharedMem1==-1)
    {    
    id_sharedMem1 = shm_open ( shared1_name , O_CREAT|O_RDWR, 644 );
    if ( id_sharedMem1 == -1 || ftruncate ( id_sharedMem1, SM_SIZE ) == -1) 
        {
        printf("open %s\n",strerror(errno));
        exit(1);
        }

    memoriaC = mmap ( NULL, SM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, id_sharedMem1, 0 );
    if ( memoriaC == (void *) -1)
        {
        perror ( "mmap" );
        exit(1);
        }
    memoriaC->i=0;
    if (pthread_mutex_init(&(memoriaC->mutex), NULL)!=0)
            printf("mutex init 1 %s\n",strerror(errno));

    }else
        {

        if (pthread_mutex_init(&(memoriaC->mutex), NULL)!=0)
                printf("mutex init 2 %s\n",strerror(errno));

        }

    if(pthread_mutex_lock ( &(memoriaC->mutex))!=0)
            printf("mutex lock %s\n",strerror(errno));

    i = memoriaC->i   1;
    memoriaC->i = i loops;
    close ( id_sharedMem1 );
    for(j=0;j<loops;j  )
        i  ;

    printf("i= %d\n",i);

    if(pthread_mutex_unlock ( &(memoriaC->mutex))!=0)
        printf("mutex unlock %s\n",strerror(errno));


//shm_unlink ( shared1_name );                      
}

最佳答案:

1 个答案:

答案 0 :(得分:0)

正确使用共享内存互斥锁需要几个“技巧”:

1)您必须保证只有一个进程/线程创建/初始化互斥锁。如果在创建共享内存时使用[2015-06-11T21:18:57.799 0100] [glassfish 4.1] [FINE] [] [org.eclipse.persistence.session.file:/OBFUSCATED/Larrys/out/artifacts/Larrys/WEB-INF/classes/_LarrysPU.sql] [tid: _ThreadID=26 _ThreadName=http-listener-1(2)] [timeMillis: 1434053937799] [levelValue: 500] [[ INSERT INTO larrys.customer (id, address, cc_number, county, email, name, phone, postcode, town) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) bind => [9 parameters bound]]] [2015-06-11T21:18:57.800 0100] [glassfish 4.1] [FINEST] [] [org.eclipse.persistence.session.file:/OBFUSCATED/Larrys/out/artifacts/Larrys/WEB-INF/classes/_LarrysPU.query] [tid: _ThreadID=26 _ThreadName=http-listener-1(2)] [timeMillis: 1434053937800] [levelValue: 300] [[ Execute query InsertObjectQuery(entity.CustomerOrder@1f3cec0)]] [2015-06-11T21:18:57.801 0100] [glassfish 4.1] [FINE] [] [org.eclipse.persistence.session.file:/OBFUSCATED/Larrys/out/artifacts/Larrys/WEB-INF/classes/_LarrysPU.sql] [tid: _ThreadID=26 _ThreadName=http-listener-1(2)] [timeMillis: 1434053937801] [levelValue: 500] [[ INSERT INTO larrys.customer_order (id, amount, confirmation_number, date_created, customer_id) VALUES (?, ?, ?, ?, ?) bind => [5 parameters bound]]] [2015-06-11T21:18:57.802 0100] [glassfish 4.1] [FINE] [] [org.eclipse.persistence.session.file:/OBFUSCATED/Larrys/out/artifacts/Larrys/WEB-INF/classes/_LarrysPU.sql] [tid: _ThreadID=26 _ThreadName=http-listener-1(2)] [timeMillis: 1434053937802] [levelValue: 500] [[ SELECT 1]] [2015-06-11T21:18:57.802 0100] [glassfish 4.1] [WARNING] [] [org.eclipse.persistence.session.file:/OBFUSCATED/Larrys/out/artifacts/Larrys/WEB-INF/classes/_LarrysPU] [tid: _ThreadID=26 _ThreadName=http-listener-1(2)] [timeMillis: 1434053937802] [levelValue: 900] [[ Local Exception Stack: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`larrys`.`customer_order`, CONSTRAINT `fk_customer_order_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) Error Code: 1452 ,则可以提高获得此权限的机会,然后使成功创建负责初始化互斥锁的共享内存的过程成为可能。这可能是也可能不是一个充分的策略。

2)您必须启用使用pthread_class="superseo">mutexattr_setpshared`和相关功能共享互斥锁。将互斥锁放在共享内存中是不够的。

3)您应该也理解O_EXCL并知道何时以及为何使用不同的类型。

4)如果您的操作系统支持,适当地使用pthread_mutexattr_settype。否则,如果进程在按住锁定时退出,则会出现死锁。

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复