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);
Related
I am a complete rookie to programming in C and have been trying to program a system that will take an integer input, perform a calculation, and tack them onto a string that will then be passed to a shared memory. Apologies if I am being an idiot but I am getting an error about an incompatible pointer type. I dont know how I can fix this error.
Edit: I apologize for the bad initial question. Full code is included
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
/* The size (in bytes) of shared-memory object */
const int SIZE = 4096;
/* The name of shared-memory object */
const char *Obj = "Shm";
/* The shared-memory file descriptor */
int shm_fd;
/* The pointer to shared-memory object */
void *ptr;
/* Create the shared-memory object */
shm_fd = shm_open(Obj, O_CREAT | O_RDWR, 0666);
/* Configure the size of the shared-memory object */
ftruncate(shm_fd, SIZE);
/* Map the shared-memory object in the address space of the process */
ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED)
{
printf("Map failed\n");
return -1;
}
int cal;
char newStr[200];
char currentStr[200];
char *temp;
char value;
printf("Enter an integer");
scanf("%d", &cal);
/* Create a message and write it to the shared-memory object */
/*fgets(ptr, SIZE, stdin);*/
if (cal == 0) {
printf("0 is not valid");
return -1;
}
if (cal < 1) {
printf("Please enter a positive int");
return -1;
}
sprintf(newStr, "%d", cal);
while (cal != 1) {
if (cal % 2 == 0) {
cal = cal / 2;
}
else {
cal = 3 * cal + 1;
}
value = cal + '0';
sprintf(currentStr, " --- %d", value);
strcat(newStr, currentStr);
}
fgets(ptr, SIZE, newStr);
printf("Writing the message to the shared memory is done! \n");
return 0;
}
Due to nature of my coding environment testing and figuring out the exact nature of errors is particularly difficult.
Edit: Here is the exact error message
Collatz-Producer.c:84:2: warning: passing argument 3 of ‘fgets’ from incompatible pointer type [enabled by default]
And I had cut out the section above with ptr since I was confident it worked, though here is the specifics of what ptr equals
void *ptr;
ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
According to this documentation, the function fgets takes 3 parameters, in this order:
a pointer to the memory buffer to write to
the size of the memory buffer
the FILE * stream to read from, for example stdin
The array newStr is not a FILE * stream. Therefore, it is not valid as a third parameter.
If you do not intend to read from a FILE * stream (such as stdin or a file opened with fopen), then you should not be using the function fgets.
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;
}
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.
I am trying to create a shared memory area using examples and documentation I found online. My goal is IPC , so I can make different processes talk to each other.
This my C file
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
int main (int argc, char *argv[])
{
struct stat sb;
off_t len;
char *p;
int fd;
fd = shm_open("test", O_RDWR | O_CREAT); //,S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("open");
return 1;
}
if (fstat(fd, &sb)==-1){
perror("fstat");
return 1;
}
/*if (!S_ISREG(sb.st_mode)){
fprintf(stderr, "%s is not a file\n",fileName);
return 1;
}*/
p = mmap(0, sb.st_size, PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED){
perror("mmap");
return 1;
}
if (close(fd)==-1) {
perror("close");
return 1;
}
for (len = 0; len < sb.st_size; len++) {
putchar(p[len]);
}
if (munmap(p, sb.st_size) == -1) {
perror("munmao");
return 1;
}
fprintf(stderr,"\n");
return 0;
}
The problem is that I am getting a mmap: Invalid argument. I assume something is wrong with fd but have no clue how to fix it, any help would be appreciated. I am on Yosemite using latest XCODE .
You need to extend the size of the shared memory mapping, at least the first time when you create it. Right now its size is 0, and mmap is not going to allow you to make a zero length mapping.
So instead of your fstat() call, do e.g.:
size_t len = 4096;
if (ftruncate(fd, len) == -1) {
perror("ftruncate");
return 1;
}
And pass this len to mmap().
Your addr parameter is set to 0, which might be reserved. Did you mean to use NULL? This would be different than 0.
I have problem with creating shared memory between two processes.
I getting this error, and I don't know what to do because I think a have all libraries included.
Log...
g++ exc8.c -o exc8
exc8.c: In function ‘int main(int, char**)’:
exc8.c:29:37: error: ‘ltrunc’ was not declared in this scope
size = ltrunc(fd, B_SIZE, SEEK_SET);
Code...
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#define B_SIZE 4
char* memory = new char[4];
int main(int argc, char *argv[]) {
int size, fd;
char *buf;
char memory[4];
fd = shm_open(memory, O_RDWR|O_CREAT, 0774);
if (fd < -0) {
perror("open");
exit(-1);
}
size = ltrunc(fd, B_SIZE, SEEK_SET);
if(size < 0) {
perror("trunc");
exit(-1);
}
buf = (char *)mmap(0, B_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(buf == NULL) {
perror("map");
exit(-1);
}
}
I don't know where ltrunc() comes from, but
you can set the size of a shared memory object with ftruncate():
if (ftruncate(fd, B_SIZE) == -1) {
// Handle error
}
(from http://pubs.opengroup.org/onlinepubs/009695299/functions/shm_open.html).
ltrunc is not a standard function. It seems defined in QNX Platform using qcc as the compiler, which truncates a file at given position. Probably POSIX provides the truncate() and ftruncate() functions for the job.