Code:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

pid_t pid;
pthread_mutex_t imutex;
pthread_mutexattr_t attr;
int main(int argc, char **argv)
{
pthread_mutexattr_settype(&attr ,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&imutex,&attr);

pid = fork();
printf("pid %d",pid);

 if (pid == 0){
   while(1){
      pthread_mutex_lock(&imutex);
      printf("\ni am the child\n");
      pthread_mutex_unlock(&imutex);
   }
 }
 else{
   while(1){
      pthread_mutex_lock(&imutex);
      printf("\ni am the parent\n");
      pthread_mutex_unlock(&imutex);
   }
 }
return 0;
}

Can anyone explain to me why this works but when the mutex is allocated in the shared memory fails to do its job ?