Saturday, August 9, 2014

Creating, writing to and reading from shared memory program

This is an operating systems program to implement creating, writing to and reading from a shared memory. If you have any doubts please let me know.

#include<sys/mman.h>                                                                                                                                                                                                                           
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
    int fd;
    char mamname[20];
    printf(“\nEnter the shared memory name: “);
   scanf(“%s”,memname);
   fd=shm_open(memname,O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
   ftruncate(fd,100);
   if(fd>0)
   printf(“\nSUCCESS”);
   else
   printf(“\nFAIL”);
}



#include<sys/mman.h>                                                                                                                                                                                                                            
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
    int fd,i;
    char mamname[20];
    unsigned char *ptr;
    printf(“\nEnter the shared memory name: “);
   scanf(“%s”,memname);
   fd=shm_open(memname,O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
   ptr=mmap(NULL,100,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
   for(i=0;*(ptr+i)!=’\0’;i++)
    printf(“%c”,*(ptr+i));
}



#include<sys/mman.h>                                                                                                                                                                                                                            
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
    int fd,i;
    char mamname[20],s[100];
    unsigned char *ptr;
    printf(“\nEnter the shared memory name: “);
   scanf(“%s”,memname);
   fd=shm_open(memname,O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
   ptr=mmap(NULL,100,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
   if(fd>0)
   {
    printf(“\nEnter data:”);
   gets(s);
   for(i=0;s[i]!=’\0’;i++)
    *(ptr+i)=s[i];
}


No comments:

Post a Comment