The program attached below is an example of shared memory inter-process communic
ID: 3597321 • Letter: T
Question
The program attached below is an example of shared memory inter-process communication. Study and run the code carefully and explain the program and its function.
#include #include #include #include unistd.h> #include sys/file.h> #include Sysimmanh> nclude void error_and_die(const char *msg)i exit(EXIT_FAILURE) int main int argc, char argv int r const char *memname - "sample"; const size t region size=sysconf( SC PAGE SIZE) TRUNC O RDWR 0666); int fd= shin open memname. O CREAT if (fd--1)Explanation / Answer
#include<stdio.h> /* for standard i/o functions*/
#include<string.h>/* for string functions*/
#include<stdlib.h>/* for standard library functions*/
#include<unistd.h>/* for unix i/o functions*/
#include<sys/file.h>/* for file related operations*/
#include<sys/mman.h>/* for memory manipulation functions*/
#include<sys/wait.h>/* for memory management declarations*/
void error_and_die(const char *msg)/* for displaying the error messge*/
{
perror(msg);/*for displaying the error messge*/
exit(EXIT_FAILURE);/*to exit from the progrma with */
int main(int argc,char *argv[])/* argc for argument count,&argv[] for storing values passed at run time to the main function*/
{
int r; /* decalred a variable name d r */
const char *memname="sample";/* to name the memory location*/
const size_t region_size=sysconf(_SC_PAGE_SIZE);
/* size_t is an unsigned integer type of at least 16 bit
Portable applications should use sysconf(_SC_PAGESIZE) instead of getpagesize():
getpagesize() is a Linux system call depends on the underlying system architecture. If it returns the kernel symbol PAGE_SIZE, whose value depends on the architecture and machine model we use.*/
int fd=shm_open(memname,O_CREAT|O_TRUNC|O_RDWR,0666);
/* to create a shared if it does not exist we use O_CREAT
O_TRUNC If the sharememory with memname exist then it will be opened
O_RDWR Open for reading and writing.
0666 for setting the read and write permissions*/
if (fd==-1)/* if sharememory is not created then it returns -1*/
error_and_die("shm_open");/*call the userdefined function*/
r=ftruncate(fd,region_size);/*truncate a sharememory to a specified length */
if(r!=0)/* if ftruncate failed to truncate the sharememory*/
error_and_die("ftruncate");/*call the userdefined function*/
void *ptr=mmap(0,region_size,PROT_READ|PROT_WRITE|MAP_SHARED,fd,0);
/* mmap a method of memory-mapped file I/O
0 --The starting address for the new mapping
region_size memory size
PROT_READ Pages may be read.
PROT_WRITE Pages may be written.
MAP_SHARED for sharing the MAP created
fd is shared memory discriptor
if (ptr==MAP_FAILED)/* if memory map failed */
error_and_die("mmap");/* calling the userdefined function*/
close(fd);/* closing the sharememory discriptor */
if(pid==0)
{
u_long *d=(u_long *)ptr;/* performing the type conversion using u_long with value in ptr */
*d=0xdbeebee;/* to assign the address */
exit(0);/* to exit from the program execution with passing value zero */
}else
{
int status;/* declaring a variable */
waitpid(pid,&status,0);
/*it is called by the parent process to wait for a specified child process to exit
$status indicates status of the child process
printf("child wrote %#lx ",*(u_long)ptr);//to display the data written by child process in the sharememory*/
}
r=munmap(ptr,region_size);/* to clear/delete the map created previously */
if(r!=0)/* if memory clear/delete failed */
error_and_die("munmap");/* display the error message*/
r=shm_unlink(memname);/*If references to the shared memory object exist when the object is unlinked, the name shall be removed before shm_unlink() returns */
if(r!=0)/* if memory unlinking failed */
error_and_die("munmap");/* display the error message*/
return 0;/* to return 0 to the called function*/
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.