Trying to put a file into virtual memory many times
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well I took a look at the manual, I didn't see a cp command, does this have the function of the map file command without actually having to declare an array that the file is being mapped to?
Ok, so I will create 100k memory maps of my file length, and then write each file into the memory map. It says to type in the length of the new mapping, so for a file that is 99.4 mb, do I just type in 99,400,00 for the length of the file?
It makes the memory mapping in baca decimal, so for 99.4 mb it would be 0x5ECB940, then I write a code to do this and I don't think that it will be able to create 100k memory mapped files and copy 100k files into them instantly. That is the same problem I had in Java.
what the heck is baca decimal?
You may use the command ls to check the size of a file, in bytes. Or try wc (see man pages for detailed usage).
What is the problem with cp?
Quote:
I don't think that it will be able to create 100k memory mapped files and copy 100k files into them instantly.
I meant to say hexa decimal but I am going to try a new way. Since all of Linux is already run using virtual memory, my plan is to just copy the same file over and over again 100k times into the same folder. So I put the file into the main directory that the terminal uses. When I type in the code:
For I in {1..100000} ; do cp FILE ~/ ; done
I get the msg that they are both the same file... Is there a way to use the cp command to rename them all different names in the same code somehow? I've tried looking online but it says to use -a and then when I do it still doesn't work. -a is the archive command.
I just wanted to update this again: I still have not found any working solution to filling up the virtual memory with one file, I have been trying things posted and checking the virtual memory in use using: free -t Nothing shows up. So I will post my code here, I believe that it isn't working because it is just using the same virtual memory address each time it does a new memory mapping? I looped it 200k times using the for loop but after I check the virtual memory usage using free -t I dont see any swap space being used.
/* For the size of the file. */
#include <sys/stat.h>
/* This contains the mmap calls. */
#include <sys/mman.h>
/* These are for error printing. */
#include <errno.h>
#include <string.h>
#include <stdarg.h>
/* This is for open. */
#include <fcntl.h>
#include <stdio.h>
/* For exit. */
#include <stdlib.h>
/* For the final part of the example. */
#include <ctype.h>
/* "check" checks "test" and prints an error and exits if it is
true. */
int main ()
{
/* The file descriptor. */
int fd;
/* Information about the file. */
struct stat s;
int status;
size_t size;
/* The file name to open. */
const char * file_name = "MentalClarity.png";
/* The memory-mapped thing itself. */
const char * mapped[200000];
int i;
/* Open the file for reading. */
fd=open("MentalClarity.png", O_RDWR);
check (fd < 0, "open %s failed: %s", file_name, strerror (errno));
/* Get the size of the file. */
status = fstat (fd, & s);
check (status < 0, "stat %s failed: %s", file_name, strerror (errno));
size = s.st_size;
Why should it do anything but what you told it to do? You opened the file, you mapped it.
Nothing else. Each time you call mmap all you do is set the amount - and just set it to the same thing. It was put in your virtual memory map, all 200,000 times...
All you did was use up system time on one system call.
One last thing - once you exited the virtual memory got released.
Oh I see, so I ran the program and then exited the program, and then the virtual memory disappeared because the program was the only thing that was using it... I'll change the script to allow the program to run continuously after the memory mappings and then test the virtual memory with free -t
Oh I see, so I ran the program and then exited the program, and then the virtual memory disappeared because the program was the only thing that was using it... I'll change the script to allow the program to run continuously after the memory mappings and then test the virtual memory with free -t
It won't change unless something directs the system to access the pages. After that, pages will be dropped to be reused for other pages...
As long as nothing access the pages you won't see any changes.
Oh ok so I need to make the program read each memory mapping continuously and then have the file run until I want it to exit? That way the memory mappings will all stay running right?
Last edited by Thirdeyematt; 10-08-2016 at 07:54 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.