dimanche 29 mars 2015

Arithmetic using sysconf() return values produces incorrect value in C



I am trying to get a little brute force program up and running and will be storing the plain text in a buffer and using multiple other threads to manipulate it. Since I am filling a buffer I would like to monitor how much free ram I have left and pause while I there is about 10% free RAM left.


Everything seems to work fine with the exception of the final division.



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "common.h"

void* Brute_force(void *arg)
{
int length = *(int*) arg;
long int percent = 10;
long int available_pages = sysconf(_SC_AVPHYS_PAGES);
long int pages = sysconf(_SC_PAGESIZE);
long int total_pages = sysconf(_SC_PHYS_PAGES);
long int free_mem = available_pages/total_pages;
printf("Available_pages: %ld\n",available_pages);
printf("PageSize: %ld\n",pages);
printf("Physical_pages: %ld\n",total_pages);
printf("%ld free memory left\n",free_mem);
long int multiple = pages * total_pages;
printf("%ld\n",multiple);
long int final = available_pages/multiple;
printf("%ld\n",final);
}


I am having issues with free_mem (along with final as they are identical). Everything else prints out correctly, though as soon as I do available_pages/multiple and print it out, it gives me a zero and I have no idea when since when I do multiple/available_pages it gives me a non-zero number. I am assuming that sysconf() returns a long, as per the man page and so I have no idea why this last step fails. Any insight?




Aucun commentaire:

Enregistrer un commentaire