[<prev] [next>] [day] [month] [year] [list]
Message-ID: <CAFf+5zi6ZwVdY_ZQ98vH_m00xPzb9-VLf8MUE_jD1BZ6j+vJZA@mail.gmail.com>
Date: Thu, 18 Sep 2025 09:14:19 +0530
From: Amit <amitchoudhary0523@...il.com>
To: linux-kernel@...r.kernel.org
Subject: mmap example (Three files: mmap_example.c, compile_mmap_example.sh,
and ReadMe.txt).
mmap example (Three files: mmap_example.c, compile_mmap_example.sh, and
ReadMe.txt).
---------------
mmap_example.c
---------------
/*
* License:
*
* This file has been released under "unlicense" license
* (https://unlicense.org).
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute
* this software, either in source code form or as a compiled binary, for any
* purpose, commercial or non-commercial, and by any means.
*
* For more information about this license, please visit - https://unlicense.org
*/
/*
* Author: Amit Choudhary
* Email: amitchoudhary0523 AT gmail DOT com
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
// Function prototype(s) for gcc flag -Werror-implicit-function-declaration.
long get_file_size(char *file_path);
void mmap_file_and_print_contents(char *file_path);
long get_file_size(char *file_path)
{
struct stat st = {0};
int retval = -1;
if (!file_path) {
fprintf(stderr, "\nError: %s(): Argument 'file_path' is NULL.\n\n",
__FUNCTION__);
return -1;
}
retval = stat(file_path, &st);
if (retval < 0) {
fprintf(stderr, "\nError: %s(): stat() call failed for file \"%s\": %s."
"\n\n", __FUNCTION__, file_path, strerror(errno));
return -1;
}
return st.st_size;
} // end of function get_file_size()
void mmap_file_and_print_contents(char *file_path)
{
int fd = -1;
long file_size = -1;
long page_size = -1;
char *addr = NULL;
long num_bytes_read = -1;
long offset = 0;
long i = 0;
char *retval_ptr = NULL;
if (!file_path) {
printf("\nError: %s(): Argument 'file_path' is NULL. Exiting..\n\n",
__FUNCTION__);
exit(EXIT_FAILURE);
}
fd = open(file_path, O_RDONLY);
if (fd < 0) {
printf("\nError: %s(): Could not open file \"%s\" for reading: %s."
" Exiting..\n\n", __FUNCTION__, file_path, strerror(errno));
exit(EXIT_FAILURE);
}
file_size = get_file_size(file_path);
if (file_size < 0) {
close(fd);
printf("\nError: %s(): Could not get the size of the file \"%s\"."
" Exiting..\n\n", __FUNCTION__, file_path);
exit(EXIT_FAILURE);
}
if (file_size == 0) {
close(fd);
printf("\nThe size of file \"%s\" is 0. There is nothing to print.\n\n",
file_path);
return;
}
errno = 0;
page_size = sysconf(_SC_PAGE_SIZE);
if (page_size < 0) {
close(fd);
printf("\nError: %s(): sysconf() returned error: Could not get page"
" size%s%s Exiting..\n\n", __FUNCTION__,
(errno ? ": " : ""), (errno ? strerror(errno) : ""));
exit(EXIT_FAILURE);
}
if (page_size == 0) {
close(fd);
printf("\nError: %s(): Page size returned by sysconf() is 0. Because of"
" this, this program will not work. Exiting..\n\n",
__FUNCTION__);
exit(EXIT_FAILURE);
}
while (1) {
retval_ptr = mmap(addr, (size_t)(page_size), PROT_READ,
(addr ? (MAP_PRIVATE | MAP_FIXED) : MAP_PRIVATE),
fd, offset);
if (retval_ptr == (char *)(-1)) {
close(fd);
if (addr) {
munmap(addr, (size_t)(page_size));
}
printf("\nError: %s(): mmap() call failed: %s. Exiting..\n\n",
__FUNCTION__, strerror(errno));
exit(EXIT_FAILURE);
}
addr = retval_ptr;
num_bytes_read = (file_size < page_size) ? file_size : page_size;
for (i = 0; i < num_bytes_read; i++) {
printf("%c", addr[i]);
}
file_size = file_size - page_size;
if (file_size <= 0) {
close(fd);
munmap(addr, (size_t)(page_size));
return;
}
offset = offset + page_size;
} // end of while (1) loop
return;
} // end of function mmap_file_and_print_contents()
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("\nError: This program expects only one argument which is the"
" path of the file whose contents have to be printed on the"
" output screen by mapping the file into memory.\n");
printf("\nUsage: %s file_name\n\n", argv[0]);
exit(EXIT_FAILURE);
}
mmap_file_and_print_contents(argv[1]);
exit(EXIT_SUCCESS);
} // end of function main
------------------------
compile_mmap_example.sh
------------------------
#!/bin/bash
set -x
rm -f mmap_example.out
gcc -Wall -Werror -Wextra -Wundef -Wunreachable-code -Winit-self
-Wparentheses -Wconversion -Wsign-conversion -Wsign-compare
-Werror-implicit-function-declaration -Wmissing-prototypes
-Wmissing-declarations -Wformat-security mmap_example.c -o
mmap_example.out
-----------
ReadMe.txt
-----------
File "mmap_example.c" calls mmap() system call to map a file into memory and
then it prints the contents of the file on the output screen.
This program doesn't map the whole file in a single mmap() call, it maps one
page at a time (page size was 4K bytes on the system on which this program was
tested). So, this program maps only 4K bytes at a time. So, if a file's size is
more than 4K bytes then this program will make multiple mmap() calls to read the
whole file.
---- End of ReadMe.txt ----
Powered by blists - more mailing lists