| Sun's profile孙涛的空间 : 大悲无泪,大悟无言,大笑无声。Blog | Help |
|
December 01 华为公司软件研发招聘您好:
华为公司正在大力招聘软件人才,要求具有两年以上C/C++,或者Java或者数据库方面的工作经验。
待遇水平高,发展空间大。如果您或者您的同事,朋友有兴趣,请和我联系。
联系方式:
Email:csusuntao@hotmail.com
Tel: 0755-28977653
QQ: 244914362
联系人:孙先生
绝非广告,非诚勿扰,谢谢! September 16 Using File Mapping(二)To read from a file view, a process dereferences the pointer returned by the MapViewOfFile function:
DWORD dwLength; dwLength = *((LPDWORD) lpMapAddress); The process also uses the pointer returned by MapViewOfFile to write to the file view:
*((LPDWORD) lpMapAddress) = dwLength;The FlushViewOfFile function copies the specified number of bytes of the file view to the physical file, without waiting for the cached write operation to occur:
if (!FlushViewOfFile(lpMapAddress, dwBytesToFlush)) { ErrorHandler("Could not flush memory to disk."); } Cleaning Up Each process uses the UnmapViewOfFile function to invalidate the pointer to the mapped memory. This destroys the file view in the address space of the process. If any pages of the file view have changed since the file view was mapped, UnmapViewOfFile also copies the changed pages to the file on disk.
if (!UnmapViewOfFile(lpMapAddress)) { ErrorHandler("Could not unmap view of file."); } When each process finishes using the file-mapping object and has umapped all views, it must close the file mapping object's handle by calling CloseHandle.
CloseHandle(hMapFile); Using File Mapping(一) The first process calls the CreateFileMapping function to create a file-mapping object and give it the name
MyFileMappingObject. By using the PAGE_READWRITE flag, the processes will have read/write permission to the memory through any file views that are created. HANDLE hMapFile; hMapFile = CreateFileMapping(hFile, // Current file handle. NULL, // Default security. PAGE_READWRITE, // Read/write permission. 0, // Max. object size. 0, // Size of hFile. "MyFileMappingObject"); // Name of mapping object. if (hMapFile == NULL) { ErrorHandler("Could not create file-mapping object."); } The process then uses the file-mapping object handle returned by CreateFileMapping in the call to MapViewOfFile to create a view of the file in the process's address space. The MapViewOfFile function returns a pointer to the file view.
LPVOID lpMapAddress; lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object. FILE_MAP_ALL_ACCESS, // Read/write permission 0, // Max. object size. 0, // Size of hFile. 0); // Map entire file. if (lpMapAddress == NULL) { ErrorHandler("Could not map view of file."); } The second process calls the OpenFileMapping function with the name
MyFileMappingObject to use the same file-mapping object as the first process. Like the first process, the second process uses the MapViewOfFile function to obtain a pointer to the file view. HANDLE hMapFile; LPVOID lpMapAddress; hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission. FALSE, // Do not inherit the name "MyFileMappingObject"); // of the mapping object. if (hMapFile == NULL) { ErrorHandle("open file-mapping object."); } lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object. FILE_MAP_ALL_ACCESS, // Read/write permission. 0, // Max. object size. 0, // Size of hFile. 0); // Map entire file. if (lpMapAddress == NULL) { ErrorHandler("Could not map view of file."); } June 03 随机数类的实现#include <iostream>
#include <time.h> using namespace std;
// generate random numbers
class randomNumber { public: // initialize the random number generator randomNumber(long s = 0); // return a 32-bit random integer m, 1 <= m <= 2^31-2
long random(); // return a 32-bit random integer m, 0 <= m <= n-1,
// where n <= 2^31-1 long random(long n); // return a real number x, 0 <= x < 1
double frandom(); private:
static const long A; static const long M; static const long Q; static const long R; long seed;
}; const long randomNumber::A = 48271;
const long randomNumber::M = 2147483647; const long randomNumber::Q = M / A; const long randomNumber::R = M % A; randomNumber::randomNumber(long s)
{ if (s < 0) s = 0; if (s == 0)
{ // get time of day in seconds since 12:00 AM, // January 1, 1970 long t_time = time(NULL); // mix-up bits by squaring
t_time *= t_time; // result can overflow. handle cases // > 0, < 0, = 0 if (t_time > 0) s = t_time ^ 0x5EECE66DL; else if (t_time < 0) s = (t_time & 0x7fffffff) ^ 0x5EECE66DL; else s = 0x5EECE66DL; } seed = s;
} long randomNumber::random()
{ long tmpSeed = A * ( seed % Q ) - R * ( seed / Q ); if( tmpSeed >= 0 )
seed = tmpSeed; else seed = tmpSeed + M; return seed;
} long randomNumber::random(long n)
{ double fraction = double(random())/double(M); return int(fraction * n);
} double randomNumber::frandom()
{ return double(random())/double(M); } 构造函数:randomNumber(long s=0)
设定随机数发生的种子.使用默认值0时,系统时间为初始化种子;否则,用户提供发生器的种子.
double frandom(): 返回实数x, 0.0<=x<1.0
long random():返回long型随机整数m, 0<=m<exp(2,31)-1;
long random(long n):返回随机整数m, 0<=m<n;
应用程序:掷骰子游戏 先掷两个骰子,根据总点数确定赢家?
通过声明一个随机数对象die进行掷骰子游戏的模拟,这个随机数使用自动种子,表达式:
(1+die.random(6)) + (1+die.random(6)). 注意,这里最好使用一个随机数来获取两次随机值.若用两个随机数对象来分别获取,则获取的两个随机数可能一样.因为默认初始化都使用系统时间,由于速度原因,可能将同样的时间设定为随机种子.
|
|
|