named mmap without Disk IO - c

I wanted to use the POSIX standard mmap as if shmat had an id, they share the same shared memory.
mmap seems to normally use fd=-1 when using MAP_ANON, and in this case it is said to be valid when inheriting from a child process.
I wanted to ensure the same behavior in the spawn method. Then I can use named shared memory, but I wish it didn't do DISK IO at all.
I think MAP_ANON is essential because I want to always guarantee the speed of RAM without leaving any files on the DISK in case of an unexpected shutdown.
So is it reasonable to provide fd!=-1 while being MAP_ANON|MAP_SHARED?
#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;
char *p;
int fd;
fd = shm_open("test", O_RDWR | O_CREAT, 0600);
if (fd == -1) {
perror("open");
return 1;
}
size_t len = 128;
if (ftruncate(fd, len) == -1) {
perror("ftruncate");
return 1;
}
p = (char*)mmap(0, len, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED, fd, 0);
if (p == MAP_FAILED){
perror("mmap");
return 1;
}
if (close(fd)==-1) {
perror("close");
return 1;
}
for (int i = 0; i < len; i++) {
putchar(p[i]);
}
if (munmap(p, len) == -1) {
perror("munmap");
return 1;
}
if(shm_unlink("test")) {
perror("unlink");
return 1;
}
fprintf(stderr,"\n");
return 0;
}

Related

Cant Share the dynamically allocated memory block created in parent to child process [duplicate]

In fork child, if we modify a global variable, it will not get changed in the main program.
Is there a way to change a global variable in child fork?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int glob_var;
main (int ac, char **av)
{
int pid;
glob_var = 1;
if ((pid = fork()) == 0) {
/* child */
glob_var = 5;
}
else {
/* Error */
perror ("fork");
exit (1);
}
int status;
while (wait(&status) != pid) {
}
printf("%d\n",glob_var); // this will display 1 and not 5.
}
You can use shared memory (shm_open(), shm_unlink(), mmap(), etc.).
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
static int *glob_var;
int main(void)
{
glob_var = mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*glob_var = 1;
if (fork() == 0) {
*glob_var = 5;
exit(EXIT_SUCCESS);
} else {
wait(NULL);
printf("%d\n", *glob_var);
munmap(glob_var, sizeof *glob_var);
}
return 0;
}
Changing a global variable is not possible because the new created process (child)is having it's own address space.
So it's better to use shmget(),shmat() from POSIX api
Or You can use pthread , since pthreadsare sharing the globaldata and the changes in global variable is reflected in parent.
Then read some Pthreads tutorial.
Here is an alternative solution.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
typedef struct
{
int id;
size_t size;
} shm_t;
shm_t *shm_new(size_t size)
{
shm_t *shm = calloc(1, sizeof *shm);
shm->size = size;
if ((shm->id = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)) < 0)
{
perror("shmget");
free(shm);
return NULL;
}
return shm;
}
void shm_write(shm_t *shm, void *data)
{
void *shm_data;
if ((shm_data = shmat(shm->id, NULL, 0)) == (void *) -1)
{
perror("write");
return;
}
memcpy(shm_data, data, shm->size);
shmdt(shm_data);
}
void shm_read(void *data, shm_t *shm)
{
void *shm_data;
if ((shm_data = shmat(shm->id, NULL, 0)) == (void *) -1)
{
perror("read");
return;
}
memcpy(data, shm_data, shm->size);
shmdt(shm_data);
}
void shm_del(shm_t *shm)
{
shmctl(shm->id, IPC_RMID, 0);
free(shm);
}
int main()
{
int var = 1;
shm_t *shm = shm_new(sizeof var);
int pid;
if ((pid = fork()) == 0)
{ /* child */
var = 5;
shm_write(shm, &var);
printf("child: %d\n", var);
return 0;
}
/* Wait for child to return */
int status;
while (wait(&status) != pid);
/* */
shm_read(&var, shm);
/* Parent is updated by child */
printf("parent: %d\n", var);
shm_del(shm);
return 0;
}
Build with:
$ gcc shm.c -o shm && ./shm

ioctl failed: Operation not permitted - block layout of my files

I want to view the contents of a file at block level.
Given a file, i want to know how many blocks it has and what is the size if each one.
my problem is when i run it i get this error:
FIBMAP ioctl failed: Operation not permitted
Also when i'm comliling i get the following warning:
warning: implicit declaration of function ‘ioctl’ [-Wimplicit-function-declaration]
if (ioctl(fd, FIGETBSZ, &blocksize)) {
I am using the following code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <assert.h>
#include <linux/fs.h>
int main(int argc, char **argv)
{
int fd, i, block, blocksize, blkcnt;
struct stat st;
assert(argv[1] != NULL);
fd = open(argv[1], O_RDONLY);
if (fd <= 0) {
perror("error opening file");
exit(EXIT_FAILURE);
}
if (ioctl(fd, FIGETBSZ, &blocksize)) {
perror("FIBMAP ioctl failed");
exit(EXIT_FAILURE);
}
if (fstat(fd, &st)) {
perror("fstat error");
exit(EXIT_FAILURE);
}
blkcnt = (st.st_size + blocksize - 1) / blocksize;
for (i = 0; i < blkcnt; i++) {
block = i;
if (ioctl(fd, FIBMAP, &block)) {
perror("FIBMAP ioctl failed");
}
printf("%3d %10d\n", i, block);
}
close(fd);
return 0;
}
Can anyone please explain me what is the problem in this code.
You should run that program as root.
ioctl(FIBMAP) needs the CAP_SYS_RAWIO capability.

How to mmap character device file?

How can I use mmap to map a character device file to my application? I've tried the following code which fails because the size of the file is zero (sb.st_size == 0). Is there another way or is this impossible all together? Thanks.
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int init_module(void *module_image, unsigned long len,
const char *param_values);
int main() {
int res = 0;
void *buf = 0;
struct stat sb;
int rc = 0;
int fd = open("/dev/nvidia0", O_RDWR);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
res = fstat(fd, &sb);
if (res == -1) {
perror("fstat");
exit(EXIT_FAILURE);
}
buf = mmap(0, sb.st_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
munmap(buf, sb.st_size);
close(fd);
return EXIT_SUCCESS;
}

Read Only shared memory segmentation fault [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm struggling with shared mem on linux paltform.
Cosider the following code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <semaphore.h>
#define SEM_NAME "mysem"
int main (int argc, char *argv[])
{
int fd, zero = 0;
int *ptr;
sem_t *mutex;
pid_t PID = getpid();
int mmap_prot = PROT_WRITE;
if (argc < 2)
{
printf(" Usage: Test [OPTION]\n\tW = Write Only\n\tR = Read Only\n");
return 1;
}
if (*argv[1] == 'W')
{
fd = open("Test_SHM", O_RDWR | O_CREAT, -1);
if (fd == -1)
perror("open");
write(fd, &zero, sizeof(int));
}
else
{
fd = open("Test_SHM", O_RDONLY| O_CREAT, -1);
if (fd == -1)
perror("open");
mmap_prot = PROT_READ;
}
ptr = mmap(NULL, sizeof(int), mmap_prot, MAP_SHARED, fd, 0);
close(fd);
if (ptr == MAP_FAILED)
{
perror("mmap");
return 1;
}
// create, initialize, and unlink semaphore
mutex = sem_open(SEM_NAME, O_CREAT | O_EXCL, -1, 1);
sem_unlink(SEM_NAME);
setbuf(stdout, NULL); /* stdout is unbuffered */
printf("Shared Mem ready..\n");
while(1)
{
sem_wait(mutex);
printf("PID %d Count: %d\n", PID, (*ptr)++);
sem_post(mutex);
sleep(1);
}
return 0;
}
If I launch the app for a read only shared memory I get, as expected, segmentation fault the first time *ptr is incremented inside main loop.
I'm working on a lib that abstract Linux shared memory.
This lib will be deployed to third part developers that will implement some processes for my application on an embedded target.
This lib will implement "global variables" between processes. I was wondering if I can avoid to develop get and set function and simply return address of allocated memory.
In case of wrong permission access I want to give to caller infos about what was wrong in its code. Read segmentation fault on terminal and process termination does not give user a good information.
EDIT2
After #Ctx answer I tried the following solution but it works the first segmentation fault. The second trigger standard segmentation fault and pogram terminate.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <signal.h>
#include <stdbool.h>
#include <setjmp.h>
#define SEM_NAME "mysem"
#define TEST 1
jmp_buf env;
void segvhandler(int arg) {
longjmp(env, 1);
}
bool Test ( int *ptr, sem_t *mutex, pid_t PID)
{
#if (TEST == 1)
signal(SIGSEGV, segvhandler);
#elif (TEST == 2)
sig_t segvhandler_OLD = signal(SIGSEGV, segvhandler);
#endif
int val = setjmp(env);
if (val != 0)
{
printf("Segmentation fault catched.\n");
sem_post(mutex);
#if (TEST == 1)
signal(SIGSEGV, SIG_DFL);
#elif (TEST == 2)
signal(SIGSEGV, segvhandler_OLD);
#endif
return false;
}
sem_wait(mutex);
printf("PID %d Count: %d\n", PID, (*ptr)++);
sem_post(mutex);
#if (TEST == 1)
signal(SIGSEGV, SIG_DFL);
#elif (TEST == 2)
signal(SIGSEGV, segvhandler_OLD);
#endif
return true;
}
int main (int argc, char *argv[])
{
int fd, zero = 0;
int *ptr;
sem_t *mutex;
pid_t PID = getpid();
int mmap_prot = PROT_WRITE;
if (argc < 2)
{
printf(" Usage: Test [OPTION]\n\tW = Write Only\n\tR = Read Only\n");
return 1;
}
if (*argv[1] == 'W')
{
fd = open("Test_SHM", O_RDWR | O_CREAT, -1);
if (fd == -1)
perror("open");
write(fd, &zero, sizeof(int));
}
else
{
fd = open("Test_SHM", O_RDONLY| O_CREAT, -1);
if (fd == -1)
perror("open");
mmap_prot = PROT_READ;
}
ptr = mmap(NULL, sizeof(int), mmap_prot, MAP_SHARED, fd, 0);
close(fd);
if (ptr == MAP_FAILED)
{
perror("mmap");
return 1;
}
// create, initialize, and unlink semaphore
mutex = sem_open(SEM_NAME, O_CREAT | O_EXCL, -1, 1);
sem_unlink(SEM_NAME);
setbuf(stdout, NULL); /* stdout is unbuffered */
printf("Shared Mem ready..\n");
while(1)
{
Test (ptr, mutex, PID);
sleep(1);
}
return 0;
}
Per the mmap() man page:
Use of a mapped region can result in these signals:
SIGSEGV
Attempted write into a region mapped as read-only.
If you want to proceed if the modification doesn't work, you can install a signal handler for SIGSEGV and use (sig)setjmp/longjmp to continue execution at a defined point:
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
jmp_buf env;
void segvhandler(int arg) {
siglongjmp(env, 1);
}
void somefunc(void) {
char *ptr = NULL;
signal(SIGSEGV, segvhandler);
if (!sigsetjmp(env, 1)) {
// Direct invocation, try the memory access
*ptr++;
}
signal(SIGSEGV, SIG_DFL);
}
int main (void) {
while (1) {
somefunc();
printf("One more iteration...\n");
}
exit(EXIT_SUCCESS); // Never reached
}
sigsetjmp(env, 1) also saves the blocked signals in env, when it's second argument is non-zero and siglongjmp() then restores these. Otherwise, the signal will still be blocked after longjmp(), since it is not a real return from the signal handler.
Keep in mind that you should only have the handler installed directly before you make the memory access in question and deinstall it afterwards.
a few minutes with the debugger shows the program crashes on the call to sem_wait().
if, after the call to sem_open() insert:
if( SEM_FAILED == mutex )
{
perror( "sem_open failed" );
exit( EXIT_FAILURE );
}
then move the statement:
sem_unlink(SEM_NAME);
to before the statement:
mutex = sem_open(SEM_NAME, O_CREAT | O_EXCL, -1, 1);
then it will become obvious that the remaining problem is in this statement:
printf("PID %d Count: %d\n", PID, (*ptr)++);
which causes a bus error signal to be raised. this bus error signal occurs on the very first pass through the while() loop.
there is a simple cause.
the printf() statement, last parameter is trying to both read and write the memory mapped file, but the memory mapping was only for (depending on the command line parameter) either 'PROT_READ' which allows reading or 'PROT_WRITE' which allows writing. the parameter to the call to mmap() needs to include both capabilities AND the call to open() also needs to
have the mode: O_RDWR. (the open() and the mmap() modes need to match
This is the corrected code after Ctx answer. I also found out THIS that is useful to understan why longjmp is not the right solution with signals.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <signal.h>
#include <stdbool.h>
#include <setjmp.h>
#define SEM_NAME "mysem"
#define TEST 1
jmp_buf env;
void segvhandler(int arg) {
siglongjmp(env, 1);
}
bool Test ( int *ptr, sem_t *mutex, pid_t PID)
{
#if (TEST == 1)
signal(SIGSEGV, segvhandler);
#elif (TEST == 2)
sig_t segvhandler_OLD = signal(SIGSEGV, segvhandler);
#endif
int val = sigsetjmp(env, 1);
if (val != 0)
{
printf("Segmentation fault catched.\n");
sem_post(mutex);
#if (TEST == 1)
signal(SIGSEGV, SIG_DFL);
#elif (TEST == 2)
signal(SIGSEGV, segvhandler_OLD);
#endif
return false;
}
sem_wait(mutex);
printf("PID %d Count: %d\n", PID, (*ptr)++);
sem_post(mutex);
#if (TEST == 1)
signal(SIGSEGV, SIG_DFL);
#elif (TEST == 2)
signal(SIGSEGV, segvhandler_OLD);
#endif
return true;
}
int main (int argc, char *argv[])
{
int fd, zero = 0;
int *ptr;
sem_t *mutex;
pid_t PID = getpid();
int mmap_prot = PROT_WRITE;
if (argc < 2)
{
printf(" Usage: Test [OPTION]\n\tW = Write Only\n\tR = Read Only\n");
return 1;
}
if (*argv[1] == 'W')
{
fd = open("Test_SHM", O_RDWR | O_CREAT, -1);
if (fd == -1)
perror("open");
write(fd, &zero, sizeof(int));
}
else
{
fd = open("Test_SHM", O_RDONLY| O_CREAT, -1);
if (fd == -1)
perror("open");
mmap_prot = PROT_READ;
}
ptr = mmap(NULL, sizeof(int), mmap_prot, MAP_SHARED, fd, 0);
close(fd);
if (ptr == MAP_FAILED)
{
perror("mmap");
return 1;
}
// create, initialize, and unlink semaphore
mutex = sem_open(SEM_NAME, O_CREAT | O_EXCL, -1, 1);
sem_unlink(SEM_NAME);
setbuf(stdout, NULL); /* stdout is unbuffered */
printf("Shared Mem ready..\n");
while(1)
{
Test (ptr, mutex, PID);
sleep(1);
}
return 0;
}

Named Semaphore just not working

we had an exam today and we had a task to implement a "train-handler".
There are 7 trains represented by one process each. Each train arrives after a couple of seconds, checks if 1 of our 3 traintracks is available. If not, wait...
If track is free, enter it and lock it.
Stay for at the train station for a few seconds, leave and unlock it.
Me and a few friends are trying to make our program run but we just can't get it done. It seems to be the problem that our shared memory is not synchronized properly (semaphore). Using a mac, so I have to use named semaphores.
compiled with: "gcc -Wall -Werror -std=gnu99 -lpthread process_trains.c -o test"
CODE:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/wait.h>
#include <errno.h>
sem_t *sem;
int *shm_ptr;
int *initShm (int size) {
int shm_fd = 0;
if((shm_fd = shm_open("/shm", O_CREAT | O_RDWR, 0777)) == -1) {
perror("Error creating shared memory segment!");
}
if ((ftruncate(shm_fd, size)) == -1) {
perror("Error sizing shared memory segment!");
}
return (int*) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
}
int trainAboutToArrive(int arrive, int stay, int Y){
int Z=0;
//Zug kommt in "arrive" Sekunden an
sleep(arrive);
while (shm_ptr[Z]!=0) {
Z++;
if(Z==3){
Z=0;
}
}
sem_wait(sem);
shm_ptr[Z]=1;
sem_post(sem);
printf("Zug %d ist auf Gleis %d eingefahren\n", Y, 1+Z);
//Zug hat einen Aufenthalt von "stay" Sekunden
sleep(stay);
sem_wait(sem);
shm_ptr[Z]=0;
sem_post(sem);
sem_close(sem);
printf("Zug %d verlässt Gleis %d\n", Y, 1+Z);
return EXIT_SUCCESS;
}
int main(int argc, char const *argv[]) {
shm_unlink("shm");
int i=0, tracks=3, trains=7, status;
int arrival[]={0,0,3,2,5,4,2};
int stay[]={2,3,7,2,1,4,3};
off_t size = sizeof(int)*tracks;
shm_ptr = initShm(size);
if((sem = sem_open("/semap",O_CREAT,0644,1)) == SEM_FAILED) {
perror("client sem_open");
}
for (i=0; i < tracks; i++) {
shm_ptr[i]= 0;
}
pid_t pids[trains];
for (i = 0; i < trains; i++) {
pids[i] = fork();
if(pids[i] == -1) {
perror("Error creating train-process!!");
} else if (pids[i] == 0) {
trainAboutToArrive(arrival[i], stay[i], 1+i);
exit(0);
}else if (pids[i] > 0) {
}
}
for(i=0; i < trains; i++){
waitpid(pids[i], &status, 0);
}
shm_unlink("shm");
return EXIT_SUCCESS;
}
Link with -pthread!!!! man page of all used semaphore functions tells us >.<
Thanks for everyones help!!
And for everyone who's interested, this is my code now. I improved a lot of things I didn't have time for in the exam. This runs perfectly and in my "beginners-eyes" this is not improvable by using the given functions (semaphores, shared mem...). If it is, I'd be grateful for tips & tricks ;)
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/wait.h>
#include <errno.h>
int *shm_ptr;
int *initShm (off_t size) {
int shm_fd = 0;
if((shm_fd = shm_open("/shm", O_CREAT | O_RDWR, 0777)) == -1) {
perror("Error creating shared memory segment!");
}
if ((ftruncate(shm_fd, size)) == -1) {
perror("Error sizing shared memory segment!");
}
return (int*) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
}
void initSem(sem_t **plats) {
if((plats[0] = sem_open("/one",O_CREAT,0644,1)) == SEM_FAILED) {
perror("client sem_open");
}
if((plats[1] = sem_open("/two",O_CREAT,0644,1)) == SEM_FAILED) {
perror("client sem_open");
}
if((plats[2] = sem_open("/three",O_CREAT,0644,1)) == SEM_FAILED) {
perror("client sem_open");
}
}
int trainAboutToArrive(int arrive, int stay, int train, sem_t **plats){
srand(getpid());
int platform = rand()%3;
sleep(arrive);
while (3) {
sem_wait(plats[platform]);
if(shm_ptr[platform]==0){
shm_ptr[platform]=1;
break;
}
sem_post(plats[platform]);
platform = rand() % 3;
}
printf("Train %d enters platform %d\n", train, 1+platform);
sleep(stay);
shm_ptr[platform]=0;
printf("Train %d leaves platform %d\n", train, 1+platform);
sem_post(plats[platform]);
sem_close(plats[platform]);
return EXIT_SUCCESS;
}
int main(int argc, char const *argv[]) {
shm_unlink("/shm");
sem_unlink("/one");
sem_unlink("/two");
sem_unlink("/three");
int i=0, tracks=3, trains=7, status;
int arrival[]={0,0,3,2,5,4,2};
int stay[]={2,3,7,2,1,4,3};
sem_t *plats[3];
off_t size = sizeof(int)*tracks;
shm_ptr = initShm(size);
initSem(plats);
for (i=0; i < tracks; i++) {
shm_ptr[i]= 0;
}
pid_t pids[trains];
for (i = 0; i < trains; i++) {
pids[i] = fork();
if(pids[i] == -1) {
perror("Error creating train-process!!");
} else if (pids[i] == 0) {
trainAboutToArrive(arrival[i], stay[i], 1+i, plats);
exit(0);
}else if (pids[i] > 0) {
}
}
for(i=0; i < trains; i++){
waitpid(pids[i], &status, 0);
}
shm_unlink("/shm");
sem_unlink("/one");
sem_unlink("/two");
sem_unlink("/three");
return EXIT_SUCCESS;
}

Resources