1

I am trying to compile a code using pointer in a shared memory. I'd like to use mutex variable to examine whether interprocess synchronization is possible. But Xcode gives me the error "Parse Issue "Expected Expression" and highlights the line
*(pthread_mutex_t*)shm_addr = PTHREAD_MUTEX_INITIALIZER; in red.
Here is the code.

#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define KEY_NUM 9527
#define MEM_SIZE 4096

pthread_mutex_t test;
int main(int argc, char * argv[])
{
    int     shm_id;
    void    *shm_addr;

    if( (shm_id = shmget((key_t)KEY_NUM, MEM_SIZE, IPC_CREAT | 0666)) == -1)
    {
        printf("fail to allocate a shared memory.\n");
        return -1;
    }

    if((shm_addr = shmat(shm_id, (void*)0,0)) == (void*)-1)
    {
        printf("fail to attach shared memory.\n");
        return -1;
    }

    *(pthread_mutex_t*)shm_addr     = PTHREAD_MUTEX_INITIALIZER; // error.


    test                            = PTHREAD_MUTEX_INITIALIZER; 
    // this statement works well.
    *(int*)(shm_addr+64) = 10000; // this statement also works well.

    // information useful to you.
    // sizeof(pthread_mutex_t*) : 64
    // OS X Mountain Lion 64bits
    return 0;
}

I have no idea why. Can anyone help?

Thank you.

inherithandle
  • 2,614
  • 4
  • 31
  • 53
  • Once you get this to compile, I expect you will have other issues with what you're doing. See comments below by Jens Gustedt @JensGustedt. –  May 25 '13 at 15:04
  • I've deleted my previous answer, but why are you casting shm_addr to a pthread_mutex? You should create a separate pthread_mutex and init it, it's a different thing than the shared memory address. `pthread_mutex_t myMutex; pthread_mutex_init(&myMutex, 0); pthread_mutex_lock(&myMutex); etc...` You lock the mutex, not the memory pointer itself, and do your work while the mutex is locked. If every thread uses the mutex before accessing the memory, then it is mutually exclusive access. (It's not like Java where you synchronize on the object itself.) –  May 25 '13 at 16:10
  • Also see http://stackoverflow.com/questions/2584678/how-do-i-synchronize-access-to-shared-memory-in-lynxos-posix –  May 25 '13 at 16:10
  • Have you seen my comment on your your deleted answer? Here is my comment again : Six process are going to use a shared memory. one process only reads the shared memory and the other 5 process only write it. there may be race-condition problem with writing a value into the shared memory and reading the value in the shared memory. So, I have decided to use a part of shared memory as mutex variable. I have expected the statement `pthread_mutex_lock((pthread_mutex_t*)shm_addr)` before writing and reading value in the shared memory. the statement `pthread_mutex_unlock((pthread_mutex_t*)shm_addr)` – inherithandle May 25 '13 at 23:55
  • after writing and reading value in the shared memory.; – inherithandle May 25 '13 at 23:58
  • Yes I saw your comment. My comment above addresses it. You don't lock shm_addr itself, you create a mutex and lock the mutex. See above. –  May 26 '13 at 00:20

1 Answers1

1

To use mutexes in shared memory you'd first of all need a system that supports this (you didn't tell us) and then you'd have to initialize the mutex accordingly. The macro PTHREAD_MUTEX_INITIALIZER is not appropriate for this, you'd have to use pthread_mutex_init dynamically with the correct set of parameters.

The relevant text for this is always the POSIX standard, which you easily find by searching for name_of_the_function_you_are_interested_in and "opengroup". Here for pthread_mutex_init it states:

In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.

Whereas the relevant section for the function pthread_mutexattr_setpshared states:

The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be operated upon by any thread that has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes. If the process-shared attribute is PTHREAD_PROCESS_PRIVATE, the mutex shall only be operated upon by threads created within the same process as the thread that initialized the mutex; if threads of differing processes attempt to operate on such a mutex, the behavior is undefined. The default value of the attribute shall be PTHREAD_PROCESS_PRIVATE.

Also, on modern systems and with new code, don't use the ancient shmat calls. The modern replacement for this is a combination of shm_open and mmap.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Could you explain why `tempMutex` in above code can't be used in a shared memory in more detail? OR any page would be awesome. Also, I have commented what program I write on above answer. – inherithandle May 25 '13 at 15:45