Exercise 3/Source code/proc-common.c
/*
 * Create a shared memory area, usable by all descendants of the calling process.
 */
Create a shared memory area, usable by all descendants of the calling process.
Create a shared memory area usable by all descendants of the calling process .
void *create_shared_memory_area(unsigned int numbytes)
{
	int pages;
	void *addr;
	if (numbytes == 0) {
		fprintf(stderr, "%s: internal error: called for numbytes == 0\n", __func__);
		exit(1);
	}
	pages = (numbytes - 1) / sysconf(_SC_PAGE_SIZE) + 1;
	addr = mmap(NULL, pages * sysconf(_SC_PAGE_SIZE),
		PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	if (addr == MAP_FAILED) {
		perror("create_shared_memory_area: mmap failed");
		exit(1);
	}
	return addr;
}
void * create_shared_memory_area ( unsigned int numbytes ) { int pages ; void * addr ; if ( numbytes == 0 ) { fprintf ( stderr , " \n " , __func__ ) ; exit ( 1 ) ; } pages = ( numbytes - 1 ) / sysconf ( _SC_PAGE_SIZE ) + 1 ; addr = mmap ( NULL , pages * sysconf ( _SC_PAGE_SIZE ) , PROT_READ | PROT_WRITE , MAP_SHARED | MAP_ANONYMOUS , -1 , 0 ) ; if ( addr == MAP_FAILED ) { perror ( " " ) ; exit ( 1 ) ; } return addr ; }
Create a shared memory area, usable by all descendants of the calling process.
Create a shared memory area usable by all descendants of the calling process .
/* Determine the number of pages needed, round up the requested number of pages */ /* Create a shared, anonymous mapping for this number of pages */