2

I wanted to implement memory management functions using C. The situation is like.. Total size of physical memory is 256mb.

How can I allocate 128mb to one process 64mb to other process.

I want to implement best fit algorithm using freelist & needs to do compaction.
Can anybody help me in this regard, or suggest any book for studying the same?

lucasg
  • 10,734
  • 4
  • 35
  • 57
  • This might help, but you should do some conceptual study about approach to manage memory i.e. a scheme / data structure to keep track of what is free, what is allocated and how to find new chunks http://stackoverflow.com/questions/13159564/implementing-malloc-in-c – fkl Oct 03 '13 at 07:31
  • What type of RAM banks do you have ? Depending on the size, you can easily dispatch the process thanks to the linker script. – lucasg Oct 03 '13 at 07:37
  • What kind of context do you want to do it in? – Jan Hudec Oct 03 '13 at 08:41
  • Note: If you want to do it for two _processes_, it implies you are doing it in _kernel_, but thanks to virtual memory (or do you have an embedded system without MMU?) you don't need continuous memory there. – Jan Hudec Oct 03 '13 at 08:43

1 Answers1

2

You can set the maximum amount of memory a process can use (Resident Set) with

ulimit -m 131072

for example to limit all forked processes from your shell to 128mb of maximum resident set.

Or in C via

#include <sys/time.h>
#include <sys/resource.h>
int setrlimit(int resource, const struct rlimit *rlim);

e.g.

struct rlimit rlim;
getrlimit(RLIMIT_RSS, &rlim);
rlim.rlim_cur = (128 << 20) / sysconf(_SC_PAGESIZE) // 128 MiB
setrlimit(RLIMIT_RSS, &rlim);
Sergey L.
  • 21,822
  • 5
  • 49
  • 75