HEADER FILE


/*
 * shmem.h
 *
 *  Created on: Nov 3, 2012
 *      Author: Pooja chahal
 */


#include <pthread.h>

#define MAX_TEXT_LEN    100



typedef struct {
	
    pthread_mutex_t  myshmemmutex;
    
    char    text[MAX_TEXT_LEN+1];
    
} shmem_t;





PROCESS 1:





/*
 * shmcreator.c
 *
 *  Created on: Nov 3, 2012
 *      Author: Pooja chahal
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>


#include "shmem.h"
#include <pthread.h>



char        *progname = "shmemcreator";
int main(int argc, char *argv[])
 {
	int     fd;
		shmem_t *ptr;
	char    *text = "Text by shmemcreator.c";
	
	pthread_mutexattr_t myattr;

	/* Create the shared memory object */
	fd = shm_open("CWD/entry", O_RDWR | O_CREAT | O_EXCL, 0777);

	
	if (fd == -1) {
		printf("%s: error creating the shared memory object. %s.\n",
				progname,  strerror(errno));
		exit(EXIT_FAILURE);
	}

	/* Set the size of the shared memory object */
	ftruncate(fd, sizeof(shmem_t));

	/* Get a pointer to the shared memory, map it into
	 * our address space */
	ptr = mmap(0, sizeof(shmem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	
	pthread_mutexattr_init(&myattr);
	pthread_mutexattr_setpshared(&myattr, PTHREAD_PROCESS_SHARED);
	pthread_mutex_init(&ptr->myshmemmutex, &myattr);

	
	pthread_mutex_lock(&ptr->myshmemmutex);

	strcpy(ptr->text, text); /* write to the shared memory */

	printf("%s: Shared memory created and semaphore initialized to 0.\n"
			"%s: Wrote text '%s' to shared memory.\n"
			"%s: Sleeping for 20 seconds.  While this program is sleeping\n"
			"%s: run 'example_shmem_user'.\n",
			progname, progname, ptr->text, progname, progname);

	
	sleep(20);

	printf("%s: Woke up.  Now unlocking the mutex.\n", progname);

	
	pthread_mutex_unlock(&ptr->myshmemmutex);

	close(fd); // Closing the file descriptor
	munmap(ptr, sizeof(shmem_t)); // removing the mapping

	shm_unlink("/entry");

	return(EXIT_SUCCESS);
}






PROCESS 2:







/*
 * shmuser.c
 *
 *  Created on: Nov 3, 2012
 *      Author: Pooja chahal
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>


#include "shmem.h"




char        *progname = "shmemuser";

int main(int argc, char *argv[])
{
	int     fd;
		shmem_t *ptr;


	
	fd = shm_open("CWD/entry", O_RDWR, 0777);
	if (fd == -1) {
		printf("%s: error opening the shared memory object: %s\n",
				progname,  strerror(errno));
		exit(EXIT_FAILURE);
	}

	/* Get a pointer to the shared memory object */
	ptr = mmap(0, sizeof(shmem_t),
			PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	
	printf("%s: Waiting on the mutex.  Run 'pidin'.  I should be MUTEX blocked.\n", progname);

	

	pthread_mutex_lock(&ptr->myshmemmutex);

	

	printf("%s: Got the mutex, now accessing shared memory\n",progname);
	printf("%s: The shared memory contains '%s'\n", progname, ptr->text);

	pthread_mutex_unlock(&ptr->myshmemmutex);

	
	close(fd);
	munmap(ptr, sizeof(shmem_t));

	return (EXIT_SUCCESS);
}
