• mmap,malloc分配随机内存


    随机数1G

    #cat malloc_rand_1g.c
    #include <stdio.h>  /* printf, scanf, NULL */
    #include <stdlib.h>  /* malloc, free, rand, system */
    #include <unistd.h>
    #include <memory.h>
    
    int main ()
    {
        int size;
    	int	n;
        char * buffer;
    	size=1024*1024*1024; //1G
        printf ("输入字符串的长度:%d bytes
    ",size);
    	//scanf ("%d", &i);
    
        buffer = (char*)malloc(sizeof(char) * size);  // 字符串最后包含 
        if(buffer==NULL) exit(1);  // 判断是否分配成功
    
    	memset(buffer,'a',sizeof(char) * size);
    	printf("total bytes:%d bytes 
    ",sizeof(char) * size);
        // 随机生成字符串
        for(n=0; n<size; n++){
            buffer[n] = rand()%26+'a';
    //		printf("%c",buffer[n]);
    	}
    	printf("
    ");
    
        buffer[size+1]='';
      //  printf ("随机生成的字符串为:%s
    ",buffer);
    
    	printf("wait about 1800s....
    ");
    	sleep(1800);
        free(buffer);  // 释放内存空间
    	printf("clean up....
    ");
        return 0;
    }
    
    

    memset(a)

    #cat  malloc_a_1g.c
    #include <stdio.h>  /* printf, scanf, NULL */
    #include <stdlib.h>  /* malloc, free, rand, system */
    #include <unistd.h>
    #include <memory.h>
    
    int main ()
    {
        int size;
    	int	n;
        char * buffer;
    	size=1024*1024*1024; //1G
        printf ("输入字符串的长度:%d bytes
    ",size);
    	//scanf ("%d", &i);
    
        buffer = (char*)malloc(sizeof(char) * size);  // 字符串最后包含 
        if(buffer==NULL) exit(1);  // 判断是否分配成功
    
    	memset(buffer,'a',sizeof(char) * size);
    	printf("total bytes:%d bytes 
    ",sizeof(char) * size);
        // 随机生成字符串
        for(n=0; n<size; n++){
            buffer[n] = 'a';
    //		printf("%c",buffer[n]);
    	}
    	printf("
    ");
    
        buffer[size+1]='';
      //  printf ("随机生成的字符串为:%s
    ",buffer);
    
    	printf("wait about 1800s....
    ");
    	sleep(1800);
        free(buffer);  // 释放内存空间
    	printf("clean up....
    ");
        return 0;
    }
    
    

    mmap

    #cat mmap_1g.c
    #include<stdio.h>
    #include<sys/mman.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #define SIZE  1024*1024*1024
    #define GB 1
    int main(int argc,char* argv[]) {
        char *p;
    	int i=1;
    	for (i=1;i<=GB;i++) {
    		if ((p = (char *)mmap(NULL,SIZE, PROT_READ |
    					PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == (void *)-1) {
    			perror("mmap");
    		 }
    		memset(p,'c',SIZE);
    		printf("Get %d GB...
    ",i);
    	}
    	sleep(3000);
    	return 0;
    }
    
    
  • 相关阅读:
    System.setProperty()
    ExtJs自学教程(1):一切从API開始
    【福利!】英雄联盟国服更新补丁速度极慢的解决方法
    ORACLE触发器具体解释
    我的Hook学习笔记
    Runood-JSP:JSP 客户端请求
    Runoob-JSP:JSP 隐式对象
    Runoob-JSP:JSP 动做元素
    Runoob-JSP:JSP 指令
    Runoob-JSP:JSP 生命周期
  • 原文地址:https://www.cnblogs.com/muahao/p/7489254.html
Copyright © 2020-2023  润新知