Hi I am a student who attended the FOSS.IN LTP workout (Bangalore, India). I have attached one of the testcases we were working on. Thought I would get it reviewed before making a patch. More testcases to follow.
Comments appreciated. Naufal
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
int flag_exit;
int flag_allocated;
unsigned int memsize;
/*
process_options: process user specified options
*/
void process_options(int argc, char **argv) {
int c;
opterr = 0;
while ((c = getopt(argc, argv, "m:")) != -1) {
switch(c) {
case 'm':
memsize = atoi(optarg) * 1024 * 1024;
break;
default:
errx(1, "Invalid option specifed");
}
}
if(!memsize)
errx(1, "Memory size not specified");
}
/*
touch_memory: force physical memory allocation
*/
void touch_memory(char *p, int size) {
int i;
int pagesize = getpagesize();
for (i = 0; i < size; i += pagesize) {
p[i] = 0xef;
}
}
void mem_map() {
static char *p;
if (!flag_allocated) {
if ((p = mmap(NULL, memsize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0)) == MAP_FAILED)
errx(1, "mmap failed");
touch_memory(p, memsize);
}
else {;
if (munmap(p, memsize) == -1)
errx(1, "munmap failed");
}
}
/*
sigint: set the exit flag on receiving SIGINT
*/
void sigint(int num) {
flag_exit = 1;
}
/*
sigusr1: allocate/free memory on receving SIGUSR1
*/
void sigusr1(int num) {
mem_map();
flag_allocated = !flag_allocated;
}
int main(int argc, char **argv) {
signal(SIGINT, sigint);
signal(SIGUSR1, sigusr1);
process_options(argc, argv);
while(!flag_exit)
sleep(1);
return 0;
}
test_mem_fun.sh
Description: Bourne shell script
------------------------------------------------------------------------------ This SF.Net email is sponsored by the Verizon Developer Community Take advantage of Verizon's best-in-class app development support A streamlined, 14 day to market process makes app distribution fast and easy Join now and get one step closer to millions of Verizon customers http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
