I wrote an utility which maps a file to a memory and process one. Several copies of my utility can be run at the same time, but usually this value is less than the number of processors. In this case performance of my utility is low, but the CPU usage is also low! I feel that most of times processors are locked. What is wrong in my source?
Here is the plan of my utility.
Here is the plan of my utility.
Code:
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
int mm_file;
void* mm_memory = MAP_FAILED;
size_t mm_size;
void* map_file(const std::string& filepath)
{
mm_file = open(filepath.c_str(), O_RDONLY);
if (mm_file < 0)
{
printf("open file error()\n");
return NULL;
}
struct stat stat_buf;
if (fstat(mm_file, &stat_buf) < 0)
{
printf("fstat error() %d\n", errno);
return NULL;
}
mm_size = stat_buf.st_size;
mm_memory = mmap(0, mm_size, PROT_READ, MAP_PRIVATE, mm_file, 0);
if (mm_memory == MAP_FAILED)
{
printf("mmap error\n");
return NULL;
}
return mm_memory;
}
void unmap_file()
{
if (mm_memory != MAP_FAILED)
munmap(mm_memory, mm_size);
if (mm_file)
close(mm_file);
}
int main(int argc, char* argv[])
{
void data* map_file("/usr/home/...");
process_data(data);
unmap_file(data);
}