LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2005, 06:54 AM   #1
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Rep: Reputation: 15
Does Linux have virtual memory management APIs?


On Windows platform m$ provides a set of APIs that manages virtual memory. I consulted Linux man, but could not find such topics.

I can use malloc to allocate a large block of memory that is larger than system's physical memory. I want to know if there are a *special* set of APIs to manage virtual memory in Linux?

I am using FC4 test 3.
 
Old 06-02-2005, 08:49 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
ls /proc/sys/vm
 
Old 06-03-2005, 05:12 PM   #3
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
I see a list of 0-byte files. What should I do?
 
Old 06-04-2005, 04:14 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Look inside them anyway.

All in /proc are not real files on disk. But a way to read or set kernel configuration.

E.g. To make your system swap less and increase tendency to keep more processes in RAM, lower the contents of /proc/sys/vm/swappiness:
Code:
# Seems to have 0 bytes:
ls -l /proc/sys/vm/swappiness 
-rw-r--r--  1 root root 0 Jun  4 11:12 

# Yet not empty:
$ cat /proc/sys/vm/swappiness 
60

# Make system swap less:
echo 30 > /proc/sys/vm/swappiness

# Check to see the change:
$ cat /proc/sys/vm/swappiness 
30
 
Old 06-04-2005, 03:34 PM   #5
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
That's interesting.

But how do I explicitly allocate memory from swapfiles using C/C++ calls?
 
Old 06-05-2005, 01:19 AM   #6
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Get an overview of the memory management techniques that are available to Linux™ programmers, focusing on the C language but applicable to other languages as well. This article gives you the details of how memory management works, and then goes on to show how to manage memory manually, how to manage memory semi-manually using referencing counting or pooling, and how to manage memory automatically using garbage collection.
Complete Article


Last edited by win32sux; 06-05-2005 at 01:44 AM.
 
Old 06-05-2005, 02:24 AM   #7
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
Wow, thanks!
 
Old 06-05-2005, 10:44 AM   #8
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
Can someone look at my test code and tell me if I am doing things right?

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>

void test_mmap()
{
	int		fd;
	int		page_size;
	char*	p;
	char*	file;
	
	file = "mmapfile";
	fd = open(file, O_RDWR | O_NONBLOCK);
	if(fd < 0)
	{
		printf("Unabled to open target file:\n%s", file);
		return;
	}

	page_size = getpagesize();
	printf("page size is %d\n", (int)page_size);

	p = (char*)mmap(0, page_size,
		PROT_READ | PROT_WRITE, MAP_SHARED,
		fd, 0);
	if(p)
	{
		puts("Dumping first 1000 bytes:");
		for(int i = 0; i < 1000; i++)
		{
			printf("0x%04x:0x%02x\n", i, (int)p[i] & 0xff);
		}

		puts("\nPadding with garbage data...");
		for(int i = 0; i < page_size; i++)
		{
			p[i] = i;
		}
		munmap(p, page_size);
	}
	else
	{
		puts("mmap failed");
	}
	close(fd);

	puts("Done!");
}

int main()
{
	test_mmap();

	return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux VRM (Virtual Memory) itz2000 Linux - General 2 11-06-2005 09:13 AM
Regarding Memory management in Linux alfredhitchcock Programming 4 09-07-2004 03:20 AM
Linux Memory Management beatlelane Red Hat 5 10-04-2003 12:39 AM
linux memory management kb8doa Linux - Newbie 1 06-18-2003 10:36 AM
RH 8.0 Mapping Virtual Memory to get access to VMIC Reflective Memory PCI card. Merlin53 Linux - Hardware 0 05-05-2003 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration