Allocate N size array inside a Shared memory - c

I have a structure
struct {
int size;
char *data;
}tmp_buf;
Now i want to allocate memory to the structure in shared memory (mmap-ed location)
i have a pointer "tp" of type "tmp_buf" in my main()
When i try to use strncpy() something at location "tp->data" it gives a segmentation fault.
I have mmap-ed shared memory of size (sizeof(struct tmp_buf) + length_of_data)
This is the code i'm running:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/mman.h>
void * create_shared_memory(char *name, int size) {
int *ptr;
int ret;
int fd = shm_open (name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror ("shm_open error!");
exit (1);
}
ret = ftruncate (fd, sizeof (size));
if (ret == -1) {
perror ("ftruncate error!");
exit (2);
}
ptr = mmap(0, sizeof (size), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
perror ("shm-mmap error!");
exit (3);
}
return ptr;
}
typedef struct {
int size;
char *data;
}tmp_buf;
int main (int argc, char **argv)
{
tmp_buf *buf_ptr;
if(argc != 2)
{
perror("Error: Incorrect number of arguments passed\n");
exit(EXIT_FAILURE);
}
int max_buffers = atoi(argv[1]);
buf_ptr = (struct tmp_buf*)create_shared_memory("my_shm_buffer",sizeof(tmp_buf) + max_buffers*1024);
printf("Shared Memory Location: %p\n", buf_ptr);
printf("Shared Memory size: %d\n", buf_ptr->size);
printf("Shared Memory data: %s\n", buf_ptr->data);
buf_ptr->size =1;
printf("Shared Memory size: %d\n", buf_ptr->size);
printf("SIZEOF(int) =%d SIZEOF(char*) = %d\n",sizeof(int), sizeof(char*));
printf("Shared Memory size address: %p\n", (void*)&(buf_ptr->size));
printf("Shared Memory data address: %p\n", (void*)&(buf_ptr->data));
strncpy(buf_ptr->data,"Hello\n", 6);
printf("Shared Memory data: %s\n", buf_ptr->data);
return 0;
}
Output i am seeing is something like this :
Shared Memory Location: 0x7ffff7ff6000
Shared Memory size: 0
Shared Memory data: (null)
Shared Memory size: 1 (After t->size = 1)
Shared Memory size address: 0x7ffff7ff6000
Shared Memory data address: 0x7ffff7ff6008
Segmentation fault (core dumped) (After strncpy("Hello\n",tp->data, 6))

Have you tried
strncpy(buf_ptr->data, "Hello\n\0", 7);
You should use strncpy with (dest, src, len) and not (src, dest, len)
The C library function char *strncpy(char *dest, const char *src, size_t n) copies up to n characters from the string pointed to, by src to dest. In a case where the length of src is less than that of n, the remainder of dest will be padded with null bytes.

The most obvious problem is demonstrated with this line:
ret = ftruncate (fd, sizeof (size));
sizeof(size) is not going to be very much as it'll be whatever sizeof(int) gives you. To make it the size you want you just pass in size as is like so...
ret = ftruncate (fd, size);
You then repeat the same problem with this line which should also have size instead of sizeof(size)
ptr = mmap(0, sizeof (size), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
It's probably worth changing the function declaration to make size be of type size_t and have it return a tmp_buf *. Casting the void * returned won't automagically cause it to populate buf_ptr with valid values. End result should look like
tmp_buf* create_shared_memory(char *name, size_t size) {
tmp_buf *ptr;
int ret;
int fd = shm_open (name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror ("shm_open error!");
exit (1);
}
ret = ftruncate (fd, size);
if (ret == -1) {
perror ("ftruncate error!");
exit (2);
}
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
perror ("shm-mmap error!");
exit (3);
}
ptr->size = size;
ptr->data = ((char *) ptr) + sizeof(ptr->size);
return ptr;
}

Related

got bus error when writing to mmap'ed file

Trying to use mmap to write to a file. Unfortunately the first write in the loop map[i] = i; will cause a bus error. Not sure why.
The PC runs Ubuntu 14.04 and the file /tmp/mmapped.bin has 12 bytes and the program is invoked with ./a.out 3.
Thanks
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#define FILEPATH "/tmp/mmapped.bin"
//#define NUMINTS (1000)
#define FILESIZE 0x400000000
int main(int argc, char *argv[])
{
int i;
int fd;
int *map; /* mmapped array of int's */
int size = atoi(argv[1]);
fd = open(FILEPATH, O_RDWR| O_CREAT | O_TRUNC);
if (fd == -1) {
perror("Error opening file for reading");
exit(EXIT_FAILURE);
}
map = mmap(0, 4 * size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
for (i = 1; i <= size; ++i) {
map[i] = i;
}
if (munmap(map, FILESIZE) == -1) {
perror("Error un-mmapping the file");
}
close(fd);
return 0;
}
In c you need to start at index 0. Because it will simply increment the pointer by the amount i and then dereference it. Your code dereferences the pointer beyond the allowed bound.
It should be,
for (i = 0; i < size; ++i) {
map[i] = i;
}
because it's equivalent to
for (i = 0; i < size; ++i) {
*(map + i) = i;
}
Also, use
map = mmap(0, size * sizeof *map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
to ensure that enough space is allocated and that *(map + i) will be inside bounds. Don't use magic numbers.
According to the mmap man page a bus error (SIGBUS) happens when you read/write outside the bounds of the file.
The length of the mapping is separate from the length of the file. If your file is newly created its size will be 0, even if you specify a length with mmap. Resize the file with ftruncate after opening it.

c - writing to shared memory segment causes segmentation fault

I'm having problems with writing to shared memory segment. Here's the code:
EDIT: after I removed that == (mistake), now I'm getting Bus Error (Core Dumped), here's the edited code:
// Struct for data from shared memory
typedef struct {
pthread_mutex_t shared_mutex;
int last_used_job_id;
} shared1;
static void *job_generator(void *param)
{
int J = *((int *) param);
shared1 *shd;
int shm;
int job_id;
// Open shared memory, don't create it if doesn't exist
shm = shm_open("/lab5", O_RDWR, 00600);
// Check
if (shm == -1) {
// Open shared memory, create it if doesn't exist (O_CREAT)
shm = shm_open(q_name, O_RDWR | O_CREAT, 00600);
// Map space for struct
shd = mmap(NULL, sizeof(shared1), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0);
if (shd == (void *) -1) {
perror ( "mmap" );
exit(1);
}
// Initialize mutex
if (pthread_mutex_init(&(shd->shared_mutex), NULL) != 0)
{
printf("Mutex initialization failed!\n");
exit(1);
}
}
else
{
// Map space for struct
shd = mmap(NULL, sizeof(shared1), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0);
if (shd == (void *) -1) {
perror ( "mmap" );
exit(1);
}
}
// Lock mutex
pthread_mutex_lock(&(shd->shared_mutex));
job_id = shd->last_used_job_id + 1;
shd->last_used_job_id = job_id + J;
printf("a: %d\n", shd->last_used_job_id);
return NULL;
}
it's caused by any of the instructions which are using shd, so any of these:
// Lock mutex
pthread_mutex_lock(&(shd->shared_mutex));
job_id = shd->last_used_job_id + 1;
shd->last_used_job_id = job_id + J;
printf("a: %d\n", shd->last_used_job_id);
I think this is where your problem lies:
shd == mmap(NULL, sizeof(shared1), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0);
You're comparing shd to the return value of mmap with '=='. I think you meant to use a single '=' which would assign the return value to shd.

Creating Shared Memory Segments

I have this struct in C:
struct first {
struct list *buf;
struct pointers *ptr;
};
A function to create a shared memory segment:
void * create_shared_memory(char *name, int size){
int *ptr;
int ret;
int fd = shm_open (name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror ("shm_open error!");
exit (1);
}
ret = ftruncate (fd, sizeof (size));
if (ret == -1) {
perror ("ftruncate error!");
exit (2);
}
ptr = mmap(0, sizeof (size), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
perror ("shm-mmap error!");
exit (3);
}
}
and a function to create a shared memory segment for that structure:
void shared_memory_structure(){
create_shared_memory("struct", sizeof(struct first));
}
However I get an error. I found the problem was that the pointers inside the structure are not being pointed to the shared memory segment I just created. How can I do this ?
try out this code first. it's a demo code taken from http://blog.csdn.net/liuzhanchen1987/article/details/7455208 , which is in Chinese:
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct{
char name[4];
int age;
}people;
int
main(int argc, char** argv)
{
int i;
people *p_map;
char temp;
p_map=(people*)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS,-1,0);
if(fork() == 0)
{
sleep(2);
for(i = 0;i<5;i++)
printf("child read: the %d people's age is %d\n",i+1,(*(p_map+i)).age);
(*p_map).age = 100;
munmap(p_map,sizeof(people)*10);
exit();
}
temp = 'a';
for(i = 0;i<5;i++)
{
temp += 1;
memcpy((*(p_map+i)).name, &temp,2);
(*(p_map+i)).age=20+i;
}
sleep(5);
printf( "parent read: the first people,s age is %d\n",(*p_map).age );
printf("umap\n");
munmap( p_map,sizeof(people)*10 );
printf( "umap ok\n" );
return 0;
}
expected result:
child read: the 1 people's age is 20
child read: the 2 people's age is 21
child read: the 3 people's age is 22
child read: the 4 people's age is 23
child read: the 5 people's age is 24
parent read: the first people,s age is 100
umap
umap ok
in which
mmap(NULL,size,PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS,-1,0);
is all what you need to do before forking(and its return value checking code), and its return value will be the allocated pages starting address(if it's valid).
if all processes are forked. it's very handy to just use mmap to allocate anonymous pages, and additionally, there is no side effect after the processes get collected.
if you use shm_open, then in somewhere there will be a share memory object gets created, and it will be the side effect remains in your system, even after all your processes get collected. however, it's necessary when you plan to have two irrelevant processes talk to each other.

use mmap in C to write into memory. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to use mmap() to create a file containing some integers. I want to write to this file by writing to memory. I know that the data in memory is binary format and hence the data in file will also be in binary.
Can I use mmap for this purpose? where can I find good resources on how to use mmap? I didn't find a good manual to start with.
Here is an example:
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h> /* mmap() is defined in this header */
#include <fcntl.h>
#include <stdio.h>
void err_quit(char *msg)
{
printf(msg);
return 0;
}
int main (int argc, char *argv[])
{
int fdin, fdout;
char *src, *dst;
struct stat statbuf;
int mode = 0x0777;
if (argc != 3)
err_quit ("usage: a.out <fromfile> <tofile>");
/* open the input file */
if ((fdin = open (argv[1], O_RDONLY)) < 0)
{printf("can't open %s for reading", argv[1]);
return 0;
}
/* open/create the output file */
if ((fdout = open (argv[2], O_RDWR | O_CREAT | O_TRUNC, mode )) < 0)//edited here
{printf ("can't create %s for writing", argv[2]);
return 0;
}
/* find size of input file */
if (fstat (fdin,&statbuf) < 0)
{printf ("fstat error");
return 0;
}
/* go to the location corresponding to the last byte */
if (lseek (fdout, statbuf.st_size - 1, SEEK_SET) == -1)
{printf ("lseek error");
return 0;
}
/* write a dummy byte at the last location */
if (write (fdout, "", 1) != 1)
{printf ("write error");
return 0;
}
/* mmap the input file */
if ((src = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0))
== (caddr_t) -1)
{printf ("mmap error for input");
return 0;
}
/* mmap the output file */
if ((dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fdout, 0)) == (caddr_t) -1)
{printf ("mmap error for output");
return 0;
}
/* this copies the input file to the output file */
memcpy (dst, src, statbuf.st_size);
return 0;
} /* main */
From Here
Another Linux example
Windows implementation of memory mapping.
Ressources -> mmap man 2
Examples :
Linux's cp by fahmy
if ((dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fdout, 0)) == (caddr_t) -1)
err_sys ("mmap error for output");
/* this copies the input file to the output file */
memcpy (dst, src, statbuf.st_size);
And the mmap wiki example
#include <sys/types.h>
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Does not work on OS X, as you can't mmap over /dev/zero */
int main(void)
{
const char str1[] = "string 1";
const char str2[] = "string 2";
int parpid = getpid(), childpid;
int fd = -1;
char *anon, *zero;
if ((fd = open("/dev/zero", O_RDWR, 0)) == -1)
err(1, "open");
anon = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
zero = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);
if (anon == MAP_FAILED || zero == MAP_FAILED)
errx(1, "either mmap");
strcpy(anon, str1);
strcpy(zero, str1);
printf("PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero);
switch ((childpid = fork())) {
case -1:
err(1, "fork");
/* NOTREACHED */
case 0:
childpid = getpid();
printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero);
sleep(3);
printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero);
munmap(anon, 4096);
munmap(zero, 4096);
close(fd);
return (EXIT_SUCCESS);
}
sleep(2);
strcpy(anon, str2);
strcpy(zero, str2);
printf("PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero);
munmap(anon, 4096);
munmap(zero, 4096);
close(fd);
return (EXIT_SUCCESS);
}
Try using both and adapt them for your goal.

mmap and double pointer

Context:
Taking advantage of my holidays to fiddle with some pointers :)
The code below is an intellectual challenge to myself more than anything else. it helps me working on pointers and so on.
And I fail.
I didn't enforce the coherence with error management, I confess.
Debian64.
The problem :
I make my way with mmap and I litteraly plundge with a double pointer assignation. here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
static int mmap_create(const char ** restrict map, const char * restrict path, const unsigned int * restrict size)
{
int fd;
int result;
fd = open(path, O_RDWR | O_CREAT,(mode_t)0777);
if (fd == -1)
{
printf("fail3\n");
close(fd);
return -1;
}
result = lseek(fd, *size-1, SEEK_SET);
if (result == -1)
{
printf("fail4\n");
close(fd);
return -1;
}
result = write(fd, "", 1);
if (result != 1)
{
printf("fail0\n");
close(fd);
return -1;
}
/* Here is my problem since map is a pointer to pointer */
map = mmap(0, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
{
printf("fail\n");
close(fd);
return -1;
}
printf("pointing to %p\n",map);
return 0;
}
static void second_function(const char * restrict path, const char ** restrict handle)
{
printf("pointing to %p\n",handle);
/* CREATE MMAP */
unsigned int value = 100;
mmap_create(handle,path,&value);
}
static void write_to(char ** map)
{
printf("pointing to %p\n",map);
}
int main(int argc, char * argv[])
{
const char path[] = "/my/path/";
char ** handle_a;
printf("pointing to %p\n",handle_a);
second_function(path,handle_a);
printf("pointing to %p\n",handle_a);
write_to(handle_a);
/*munmap*/
return 0;
}
Question:
How could I do to be able to retrieve the right address of the mapped file up to the write_to function ?
The first two are nil (normal) the third is assigned but the last two ones are nil. Not good.
I think it all goes wrong in the mmap call since it gives a pointer but I have a pointer to pointer.
Thereafter, the addresses are not the same anymore.
And then, I am lost..
Any "pointer" please?
Thanks
handle_a has no memory allocated to store the pointer
change
char ** handle_a;
to
char * handle_a;
and then use as
second_function(path,&handle_a);
and assign it like;
*map = mmap(0, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

Resources