read from /dev/urandom 返回值异常

 #include<stdio.h>
#include<iostream>
#include <fcntl.h>
#include <sys/mman.h>
using namespace std;
int main(int argc,char* argv[])
{
ssize_t bytesToWrite = **;
const char *pathZero = "/dev/zero";
int fdZero = open(pathZero, O_RDONLY);
void *pageBuffPtr = mmap(NULL, bytesToWrite, PROT_READ | PROT_WRITE, MAP_PRIVATE, fdZero, );
int randomData = open("/dev/urandom", O_RDONLY);
if (randomData < )
{
// something went wrong
}
else
{
char* myRandomData;
ssize_t result = read(randomData, pageBuffPtr, bytesToWrite);
if (result < )
{
// something went wrong
}
cout <<"len:"<<bytesToWrite << endl;
cout <<"result:"<<result << endl;
}
return ;
}

以上代码运行在suse12上,len和result总是差1

后来查看urandom的man手册中有这么一句:Since Linux 3.16, a read(2) from /dev/urandom will return at most 32 MB

即最大只能读 32MB -1

上一篇:快速入门系列--MVC--03控制器和IOC应用


下一篇:为代码减负之<三>视图(SQL)