文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>内存控制篇、日期时间篇(Linux c)

内存控制篇、日期时间篇(Linux c)

时间:2006-04-28  来源:Leaf_KA

calloc(配置内存空间)
相关函数 malloc,free,realloc,brk
表头文件 #include <stdlib.h>
定义函数 void *calloc(size_t nmemb,size_t size);
函数说明 calloc()用来配置nmemb个相邻的内存单位,每一单位的大小为
size,并返回指向第一个元素的指针。这和使用下列的方式效果相
同:malloc(nmemb*size);不过,在利用calloc()配置内存时会将内
存内容初始化为0。
返回值 若配置成功则返回一指针,失败则返回NULL。
范例 /* 动态配置10个struct test 空间*/
#include<stdlib.h>
struct test
{
int a[10];
char b[20];
}
main()
{
struct test *ptr=calloc(sizeof(struct test),10);
}
free(释放原先配置的内存)
相关函数 malloc,calloc,realloc,brk
表头文件 #include<stdlib.h>
定义函数 void free(void *ptr);
函数说明 参数ptr为指向先前由malloc()、calloc()或realloc()所返回的内
存指针。调用free()后ptr所指的内存空间便会被收回。假若参数
ptr所指的内存空间已被收回或是未知的内存地址,则调用free()可
能会有无法预期的情况发生。若参数ptr为NULL,则free()不会有任
何作用。
getpagesize(取得内存分页大小)
相关函数 sbrk
表头文件 #include<unistd.h>
Untitled Document 页码,1/4
file://D:\linux_c\function\03.html 2004-1-9
定义函数 size_t getpagesize(void);
函数说明 返回一分页的大小,单位为字节(byte)。此为系统的分页大小,
不一定会和硬件分页大小相同。
返回值 内存分页大小。附加说明在Intel x86 上其返回值应为4096bytes。
范例 #include <unistd.h>
main()
{
printf(“page size = %d\n”,getpagesize( ) );
}
malloc(配置内存空间)
相关函数 calloc,free,realloc,brk
表头文件 #include<stdlib.h>
定义函数 void * malloc(size_t size);
函数说明 malloc()用来配置内存空间,其大小由指定的size决定。
返回值 若配置成功则返回一指针,失败则返回NULL。
范例 void p = malloc(1024); /*配置1k的内存*/
mmap(建立内存映射)
相关函数 munmap,open
表头文件 #include <unistd.h>
#include <sys/mman.h>
定义函数 void *mmap(void *start,size_t length,int prot,int flags,int
fd,off_t offsize);
函数说明 mmap()用来将某个文件内容映射到内存中,对该内存区域的存取即
是直接对该文件内容的读写。参数start指向欲对应的内存起始地
址,通常设为NULL,代表让系统自动选定地址,对应成功后该地址
会返回。参数length代表将文件中多大的部分对应到内存。
参数 prot代表映射区域的保护方式有下列组合
PROT_EXEC 映射区域可被执行
PROT_READ 映射区域可被读取
PROT_WRITE 映射区域可被写入
PROT_NONE 映射区域不能存取
参数 flags会影响映射区域的各种特性
MAP_FIXED 如果参数start所指的地址无法成功建立映射时,则放弃
映射,不对地址做修正。通常不鼓励用此旗标。
MAP_SHARED对映射区域的写入数据会复制回文件内,而且允许其他
Untitled Document 页码,2/4
file://D:\linux_c\function\03.html 2004-1-9
映射该文件的进程共享。
MAP_PRIVATE 对映射区域的写入操作会产生一个映射文件的复制,
即私人的“写入时复制”(copy on write)对此区域作的任何修改
都不会写回原来的文件内容。
MAP_ANONYMOUS建立匿名映射。此时会忽略参数fd,不涉及文件,而
且映射区域无法和其他进程共享。
MAP_DENYWRITE只允许对映射区域的写入操作,其他对文件直接写入
的操作将会被拒绝。
MAP_LOCKED 将映射区域锁定住,这表示该区域不会被置换
(swap)。
在调用mmap()时必须要指定MAP_SHARED 或MAP_PRIVATE。参数fd为
open()返回的文件描述词,代表欲映射到内存的文件。参数offset
为文件映射的偏移量,通常设置为0,代表从文件最前方开始对应,
offset必须是分页大小的整数倍。
返回值 若映射成功则返回映射区的内存起始地址,否则返回MAP_FAILED(-
1),错误原因存于errno 中。
错误代码 EBADF 参数fd 不是有效的文件描述词
EACCES 存取权限有误。如果是MAP_PRIVATE 情况下文件必须可读,
使用MAP_SHARED则要有PROT_WRITE以及该文件要能写入。
EINVAL 参数start、length 或offset有一个不合法。
EAGAIN 文件被锁住,或是有太多内存被锁住。
ENOMEM 内存不足。
范例 /* 利用mmap()来读取/etc/passwd 文件内容*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/mman.h>
main()
{
int fd;
void *start;
struct stat sb;
fd=open(“/etc/passwd”,O_RDONLY); /*打开/etc/passwd*/
fstat(fd,&sb); /*取得文件大小*/
start=mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,fd,0);
if(start= = MAP_FAILED) /*判断是否映射成功*/
return;
printf(“%s”,start);
munma(start,sb.st_size); /*解除映射*/
closed(fd);
}
执行 root : x : 0 : root : /root : /bin/bash
bin : x : 1 : 1 : bin : /bin :
Untitled Document 页码,3/4
file://D:\linux_c\function\03.html 2004-1-9
daemon : x : 2 : 2 :daemon : /sbin
adm : x : 3 : 4 : adm : /var/adm :
lp : x :4 :7 : lp : /var/spool/lpd :
sync : x : 5 : 0 : sync : /sbin : bin/sync :
shutdown : x : 6 : 0 : shutdown : /sbin : /sbin/shutdown
halt : x : 7 : 0 : halt : /sbin : /sbin/halt
mail : x : 8 : 12 : mail : /var/spool/mail :
news : x :9 :13 : news : /var/spool/news :
uucp : x :10 :14 : uucp : /var/spool/uucp :
operator : x : 11 : 0 :operator : /root:
games : x : 12 :100 : games :/usr/games:
gopher : x : 13 : 30 : gopher : /usr/lib/gopher-data:
ftp : x : 14 : 50 : FTP User : /home/ftp:
nobody : x :99: 99: Nobody : /:
xfs :x :100 :101 : X Font Server : /etc/xll/fs : /bin/false
gdm : x : 42 :42 : : /home/gdm: /bin/bash
kids : x : 500 :500 :/home/kids : /bin/bash
munmap(解除内存映射)
相关函数 mmap
表头文件 #include<unistd.h>
#include<sys/mman.h>
定义函数 int munmap(void *start,size_t length);
函数说明 munmap()用来取消参数start所指的映射内存起始地址,参数length
则是欲取消的内存大小。当进程结束或利用exec相关函数来执行其
他程序时,映射内存会自动解除,但关闭对应的文件描述词时不会
解除映射。
返回值 如果解除映射成功则返回0,否则返回-1,错误原因存于errno中错
误代码EINVAL
参数 start或length 不合法。
范例 参考mmap()
页Untitled Document 码,4/4
file://D:\linux_c\function\03.html 2004-1-9
 
 
asctime(将时间和日期以字符串格式表示)
相关函数 time,ctime,gmtime,localtime
表头文件 #include<time.h>
定义函数 char * asctime(const struct tm * timeptr);
函数说明 asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所
使用的时间日期表示方法,然后将结果以字符串形态返回。此函数
已经由时区转换成当地时间,字符串格式为:“Wed Jun 30
21:49:08 1993\n”
返回值 若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与
ctime不同处在于传入的参数是不同的结构。
附加说明 返回一字符串表示目前当地的时间日期。
范例 #include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
执行 Sat Oct 28 02:10:06 2000
ctime(将时间和日期以字符串格式表示)
相关函数 time,asctime,gmtime,localtime
表头文件 #include<time.h>
定义函数 char *ctime(const time_t *timep);
函数说明 ctime()将参数timep所指的time_t结构中的信息转换成真实世界所
使用的时间日期表示方法,然后将结果以字符串形态返回。此函数
已经由时区转换成当地时间,字符串格式为“Wed Jun 30
21 :49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可
能会被破坏。
返回值 返回一字符串表示目前当地的时间日期。
范例 #include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
Untitled Document 页码,1/6
file://D:\linux_c\function\04.html 2004-1-9
}
执行 Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的时间)
相关函数 time,ctime,ftime,settimeofday
表头文件 #include <sys/time.h>
#include <unistd.h>
定义函数 int gettimeofday ( struct timeval * tv , struct timezone *
tz )
函数说明 gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的
信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 结构定义为:
struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所
代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
返回值 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT
指针tv和tz所指的内存空间超出存取权限。
范例 #include<sys/time.h>
#include<unistd.h>
main(){
struct timeval tv;
struct timezone tz;
Untitled Document 页码,2/6
file://D:\linux_c\function\04.html 2004-1-9
gettimeofday (&tv , &tz);
printf(“tv_sec; %d\n”, tv,.tv_sec) ;
printf(“tv_usec; %d\n”,tv.tv_usec);
printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
}
执行 tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
gmtime(取得目前时间和日期)
相关函数 time,asctime,ctime,localtime
表头文件 #include<time.h>
定义函数 struct tm*gmtime(const time_t*timep);
函数说明 gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界
所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值 返回结构tm代表目前UTC 时间
Untitled Document 页码,3/6
file://D:\linux_c\function\04.html 2004-1-9
范例 #include <time.h>
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p-
>tm_mday);
printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p-
>tm_min, p->tm_sec);
}
执行 2000/10/28 Sat 8:15:38
localtime(取得当地目前时间和日期)
相关函数 time, asctime, ctime, gmtime
表头文件 #include<time.h>
定义函数 struct tm *localtime(const time_t * timep);
函数说明 localtime()将参数timep所指的time_t结构中的信息转换成真实世
界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm
的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时
区。
返回值 返回结构tm代表目前的当地时间。
范例 #include<time.h>
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”
Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得当地时间*/
printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p-
>tm_mday);
printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p-
>tm_min, p->tm_sec);
}
执行 2000/10/28 Sat 11:12:22
页Untitled Document 码,4/6
file://D:\linux_c\function\04.html 2004-1-9
mktime(将时间结构数据转换成经过的秒数)
相关函数 time,asctime,gmtime,localtime
表头文件 #include<time.h>
定义函数 time_t mktime(strcut tm * timeptr);
函数说明 mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年
1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值 返回经过的秒数。
范例 /* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数
*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d \n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
执行 time():974943297
time()->localtime()->mktime():974943297
settimeofday(设置目前时间)
相关函数 time,ctime,ftime,gettimeofday
表头文件 #include<sys/time.h>
#include<unistd.h>
定义函数 int settimeofday ( const struct timeval *tv,const struct
timezone *tz);
函数说明 settimeofday()会把目前时间设成由tv所指的结构信息,当地时区
信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注
意,只有root权限才能使用此函数修改时间。
返回值 成功则返回0,失败返回-1,错误代码存于errno。
错误代码 EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
页Untitled Document 码,5/6
file://D:\linux_c\function\04.html 2004-1-9
time(取得目前的时间)
相关函数 ctime,ftime,gettimeofday
表头文件 #include<time.h>
定义函数 time_t time(time_t *t);
函数说明 此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现
在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存
到t指针所指的内存。
返回值 成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno
中。
范例 #include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
执行 9.73E+08
页Untitled Document 码,6/6
file://D:\linux_c\function\04.html 2004-1-9
相关阅读 更多 +
排行榜 更多 +
浴血混战官方下载

浴血混战官方下载

飞行射击 下载
检票员模拟器免广告下载

检票员模拟器免广告下载

模拟经营 下载
最终前哨最终版手机版下载

最终前哨最终版手机版下载

休闲益智 下载