2008年12月22日星期一

内存管理实验源代码

我的内存管理实验的源代码
[gaowei@localhost memory]$ ls
display_menu.c main.c Makefile2 memory.h
上面是代码的目录
编译后的目录如下:
[gaowei@localhost memory]$ ls
display_menu.c display_menu.o main.o Makefile2 memory memory.h
再详细看看我的代码
头文件在memory.h中!
#include <stdio.h>
#include <stdlib.h>/*会引起警告:隐式声明与内建函数 “malloc” "exit“ 不兼容*/
#include <unistd.h>
/*memory.c:331: 警告:隐式声明与内建函数 ‘_exit’ 不兼容*/
#define PROCESS_NAME_LEN 32 /*进程名称的最大长度*/
#define MIN_SLICE 10 /*最小碎片的大小*/
#define DEFAULT_MEM_SIZE 1024 /*默认内存的大小*/
#define DEFAULT_MEM_START 0 /*默认内存的起始位置*/
/* 内存分配算法 */
#define MA_FF 1
#define MA_BF 2
#define MA_WF 3
在看看我的Makefile的文件:
memory: main.o display_menu.o
gcc -o memory main.o display_menu.o
main.o: main.c memory.h
gcc -c main.c
display_memu.o: display_memu.c memory.h
gcc -c display_memu.c
在看看我的菜单的文件,现在由于要考试了,时间紧张,所以只是完成了老师规定的内容,我想用把他做成图形界面的,用#include <curses.h>这个头文件,如果你没有可以下载,是终端界面设计的
所以我的菜单文件是
#include "memory.h"

int display_menu(void){
printf("\n");
printf("1 - Set memory size (default=%d)\n", DEFAULT_MEM_SIZE);
printf("2 - Select memory allocation algorithm\n");
printf("3 - New process \n");
printf("4 - Terminate a process \n");
printf("5 - Display memory usage \n");
printf("0 - Exit\n");

}
我的主函数的代码是
#include "memory.h"

extern void display_menu();

struct allocated_block *find_process(int pid);

/*描述每一个空闲块的数据结构*/
struct free_block_type{
int size;
int start_addr;
struct free_block_type *next;
};

/*指向内存中空闲块链表的首指针*/
struct free_block_type *free_block;

/*每个进程分配到的内存块的描述*/
struct allocated_block{
int pid;
int size;
int start_addr;
char process_name[PROCESS_NAME_LEN];
struct allocated_block *next;
};

/*进程分配内存块链表的首指针*/
struct allocated_block *allocated_block_head = NULL;

int mem_size=DEFAULT_MEM_SIZE; /*内存大小*/
int ma_algorithm = MA_FF; /*当前分配算法*/
static int pid = 0; /*初始pid*/
int flag = 0;/* 设置内存大小标志*/

/*初始化空闲块,默认为一块,可以指定大小及起始地址*/
struct free_block_type* init_free_block(int mem_size){
struct free_block_type *fb;
fb=(struct free_block_type *)malloc(sizeof(struct free_block_type));
if(fb==NULL){
printf("No mem\n");
return NULL;
}
fb->size = mem_size;
fb->start_addr = DEFAULT_MEM_START;
fb->next = NULL;
return fb;
}

/*设置内存的大小*/
set_mem_size() {
int size;
if(flag!=0){ /*防止重复设置*/
printf("Cannot set memory size again\n");
return 0;
}
printf("Total memory size =");
scanf("%d", &size);
if(size>0) {
mem_size = size;
free_block->size = mem_size;
}
flag=1;
return 1;
}

/* 设置当前的分配算法 */
set_algorithm(){
int algorithm;
printf("\t1 - First Fit\n");
printf("\t2 - Best Fit \n");
printf("\t3 - Worst Fit \n");
scanf("%d", &algorithm);
if(algorithm>=1 && algorithm <=3)
ma_algorithm=algorithm; /*按指定算法重新排列空闲区链表*/
rearrange(ma_algorithm); }

/*按指定的算法整理内存空闲块链表*/
rearrange(int algorithm){
switch(algorithm){
case MA_FF: rearrange_FF(); break;
case MA_BF: rearrange_BF(); break;
case MA_WF: rearrange_WF(); break;
}
}

void swap(int *p1,int *p2) {
int temp; temp=*p1; *p1=*p2; *p2=temp;
}

/*按FF算法重新整理内存空闲块链表*/
rearrange_FF(){
struct free_block_type *tmp, *work;
printf("Rearrange free blocks for FF \n");
tmp = free_block; while(tmp!=NULL) {
work = tmp->next;
while(work!=NULL){
if( work->start_addr <>start_addr) { /*地址递增*/
swap(&work->start_addr, &tmp->start_addr);
swap(&work->size, &tmp->size);
}
work=work->next;
}
tmp=tmp->next;
}
}


/*按BF算法重新整理内存空闲块链表*/
rearrange_BF(){
struct free_block_type *tmp, *work;
printf("Rearrange free blocks for BF \n");
tmp = free_block;
while(tmp->next!=NULL) {
work = tmp->next;
while(work!=NULL) {
if(work->size > tmp->size) { /*大小递减*/
swap(&work->start_addr,&tmp->start_addr);
swap(&work->size,&tmp->size);
}
work = work->next;
}
tmp = tmp->next;
}
return 0;
}

/*按WF算法重新整理内存空闲块链表*/
rearrange_WF(){
struct free_block_type *tmp, *work;
printf("Rearrange free blocks for WF \n");
tmp = free_block;
while(tmp->next!=NULL) {
work = tmp->next;
while(work!=NULL) {
if(work->size <>size) { /*大小递增*/
swap(&work->start_addr,&tmp->start_addr);
swap(&work->size,&tmp->size);
}
work = work->next;
}
tmp = tmp->next;
}
return 0;
}

/*创建新的进程,主要是获取内存的申请数量*/
int new_process(void) {
struct allocated_block *ab;
int size;
int ret;
ab = (struct allocated_block *)malloc(sizeof(struct allocated_block));
if(!ab) exit(-5);
ab->next = NULL;
pid++;
sprintf(ab->process_name, "PROCESS-%02d", pid);
ab->pid = pid;

printf("Memory for %s:", ab->process_name);
scanf("%d", &size);
if(size>0) ab->size = size;
ret = allocate_mem(ab); /* 从空闲区分配内存,ret==1表示分配ok*/
/*如果此时allocated_block_head尚未赋值,则赋值*/
if((ret==1) &&(allocated_block_head == NULL)){
allocated_block_head=ab;
return 1;
}
/*分配成功,将该已分配块的描述插入已分配链表*/
else if (ret==1) {
ab->next=allocated_block_head;
allocated_block_head=ab;
return 2;
}
else if(ret==-1){ /*分配不成功*/
printf("Allocation fail\n");
free(ab);
return -1;
}
return 3;
}

/*分配内存模块*/
int allocate_mem(struct allocated_block *ab) {
struct free_block_type *fbt, *pre;
int request_size = ab->size;
fbt = pre = free_block;
printf("%d", fbt->start_addr);
while(fbt!=NULL) {
if(fbt->size>=request_size){/*分配后空闲空间足够大,则分割*/
if((fbt->size-request_size)>MIN_SLICE) {
ab->size = request_size;
ab->start_addr = fbt->start_addr;
fbt->size -= request_size;
fbt->start_addr += request_size;
}
else {/*分割后空闲区成为小碎片,一起分配*/
ab->size = fbt->size;
ab->start_addr = fbt->start_addr;
if (fbt == free_block)
free_block = fbt->next;
else {
pre->next = fbt->next;
free(fbt);
}
}
return 1;
}
pre = fbt;
fbt = fbt->next;
}
return 0;
}

/*删除进程,归还分配的存储空间,并删除描述该进程内存分配的节点*/
kill_process(void){
struct allocated_block *ab;
int pid;
printf("Kill Process, pid=");
scanf("%d", &pid);
ab=find_process(pid);
if(ab!=NULL){
free_mem(ab); /*释放ab所表示的分配区*/
dispose(ab); /*释放ab数据结构节点*/
}
}

/*struct free_block_type* find_process(int pid) */
struct allocated_block *find_process(int pid)
{
struct allocated_block *ab=allocated_block_head;
while(ab!=NULL) {
if(pid != ab->pid) {
ab=ab->next;
}
else return ab;
}
}


/*将ab所表示的已分配区归还,并进行可能的合并*/
int free_mem(struct allocated_block *ab){
int algorithm = ma_algorithm;
struct free_block_type *fbt, *pre, *work;

fbt=(struct free_block_type*) malloc(sizeof(struct free_block_type));
if(!fbt) return -1;
fbt->size = ab->size;
fbt->start_addr = ab->start_addr;
/*插入到空闲区链表的头部并将空闲区按地址递增的次序排列*/
fbt->next = free_block;
free_block=fbt;
rearrange(MA_FF);/*默认是FF算法*/
fbt=free_block;
while(fbt!=NULL){
work = fbt->next;
if(work!=NULL){
/*如果当前空闲区与后面的空闲区相连,则合并*/
if(fbt->start_addr+fbt->size == work->start_addr){
fbt->size += work->size;
fbt->next = work->next;
free(work);
continue;
}
}
fbt = fbt->next;
}
rearrange(algorithm); /*重新按当前的算法排列空闲区*/
return 1;
}

/*释放ab数据结构节点*/
int dispose(struct allocated_block *free_ab) {
struct allocated_block *pre, *ab;
if(free_ab == allocated_block_head) { /*如果要释放第一个节点*/
allocated_block_head = allocated_block_head->next;
free(free_ab);
return 1;
}

pre = allocated_block_head;
ab = allocated_block_head->next;

while(ab!=free_ab){ pre = ab; ab = ab->next; }
pre->next = ab->next;
free(ab);
return 2;
}

/* 显示当前内存的使用情况,包括空闲区的情况和已经分配的情况 */
int display_mem_usage(void) {
struct free_block_type *fbt = free_block;
struct allocated_block *ab = allocated_block_head;
if(fbt==NULL) return(-1);
printf("----------------------------------------------------------\n");

/* 显示空闲区 */
printf("Free Memory:\n");
printf("%20s %20s\n", " start_addr", " size");
while(fbt!=NULL) {
printf("%20d %20d\n", fbt->start_addr, fbt->size);
fbt=fbt->next;
}
/* 显示已分配区 */
printf("\nUsed Memory:\n");
printf("%10s %20s %10s %10s\n", "PID", "ProcessName", "start_addr", " size");
while(ab!=NULL){
printf("%10d %20s %10d %10d\n", ab->pid, ab->process_name, ab->start_addr, ab->size);
ab=ab->next;
}
printf("----------------------------------------------------------\n");
return 0;
}



do_exit()
{
/* exit(0); */
_exit(0);
return 0;
}


int main(void)
{
char choice;

pid=0;
free_block = init_free_block(mem_size); /*初始化空闲区*/
for(;;) {
display_menu(); //显示菜单
fflush(stdin);
scanf("%s", &choice);
switch(choice) {
case '1': set_mem_size();break; /*设置内存大小*/
case '2': set_algorithm();flag=1;break;/*设置分配算法*/
case '3': new_process(); flag=1;break; /*创建新进程*/
case '4': kill_process(); flag=1;break;/*删除进程*/
case '5': display_mem_usage();flag=1;break;/*显示内存使用*/
case '0': do_exit(); exit(0); /*释放链表并退出*/
default: break;
}
}
}

2008年12月11日星期四

fc8 重装之后

命令
挂载ntfs格式的命令
mount -t ntfs-3g /dev/sda7 /media/gao/ -o force
看一些权限
cat /boot/config-2.6.23.1-42.fc8 | grep -i ext3
解压rar包
/media/SOFTWARE/linux/rar/rar x
alacarte 是Main Menu的命令
用cat /proc/下面的看到很多的信息
如:cat /proc/version 看linux的版本
cat /proc/meminfo 看内存信息
cat /proc/cpuinfo 看cpu的信息
在终端打开emacs -nw
mysoftware
1。安装chm阅读器sudo yum install gnochm
2。firefox
标签/home/gaowei/.mozilla/firefox/8umrv5sm.default
bookmarksb.bak
bookmarksb.html
3.webmin-1.400-1.noarch.rpm
4.realplayer 11 gold
http://linux.softpedia.com/progDownload/RealPlayer-Download-2742.html
Preparing... ########################################### [100%]
package RealPlayer-11.0.1.1056-20081001 is already installed
6。gtk
当./configure完了之后就
出现下面的内容:
checking pkg-config is at least version 0.9.0... yes
checking for BASE_DEPENDENCIES... configure: error: Package requirements (glib-2.0 >= 2.17.6 atk >= 1.13.0 pango >= 1.20 cairo >= 1.6) were not met:
Requested 'glib-2.0 >= 2.17.6' but version of GLib is 2.14.2
Requested 'pango >= 1.20' but version of Pango is 1.18.3
Requested 'cairo >= 1.6' but version of cairo is 1.4.10
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables BASE_DEPENDENCIES_CFLAGS
and BASE_DEPENDENCIES_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
7 VirtualBox
sudo rpm -ivh qt4-4.4.1-2.fc8.i386.rpm
sudo rpm -ivh qt4-4.4.3-1.fc8.i386.rpm
sudo rpm -ivh qt4-x11-4.4.3-1.fc8.i386.rpm
sudo rpm -ivh VirtualBox-2.0.6_39765_fedora8-1.i386.rpm
rpm
口令:
Preparing... ########################################### [100%]
1:VirtualBox ########################################### [100%]
chcon: can't apply partial context to unlabeled file /usr/lib/virtualbox/VirtualBox
chcon: can't apply partial context to unlabeled file /usr/lib/virtualbox/VBoxSDL
chcon: can't apply partial context to unlabeled file /usr/lib/virtualbox/VBoxHeadless
chcon: can't apply partial context to unlabeled file /usr/lib/virtualbox/vboxwebsrv
Creating group 'vboxusers'. VM users must be member of that group!
No precompiled module for this kernel found -- trying to build one. Messages
emitted during module compilation will be logged to /var/log/vbox-install.log.
Success!
8。关于一些绘图软件
gaphor
安装要超级用户,而且要有网络才可以,好像要下载一些东西的
You can start Gaphor by running the run-gaphor.sh
dia
很好安装,和一般的源代码安装方法是一样的!
关于一些图片的位置
/usr/share/pixmaps
/usr/share/icons/hicolor/scalable/apps
9
[gaowei@localhost qq]$ sudo rpm -ivh skype-2.0.0.72-fc5.i586.rpm
warning: skype-2.0.0.72-fc5.i586.rpm: Header V3 DSA signature: NOKEY, key ID d66b746e
Preparing... ########################################### [100%]
1:skype ########################################### [100%]
[gaowei@localhost qq]$ skype
10.
[gaowei@localhost player]$ ./Livestation-2.5.0.run
----------------------------------------\n
Do you accept the license [y/n]: y
\n----------------------------------------\n
-> Non-root user detected, will try to sudo when copying.
\n----------------------------------------\n
Installation directory [/usr/local/Livestation]:
Installation directory [/usr/local/Livestation]:
\n----------------------------------------\n
Create a symlink in /usr/bin [Y/n]: y
\n----------------------------------------\n
-> Making directory /usr/local/Livestation.
口令:
-> Copying files.
-> Creating symlinks.
\n----------------------------------------\n
Done. Run 'livestation' to start the player.\n
[gaowei@localhost player
http://tvants.en.softonic.com/
11.3d软件
[gaowei@localhost ArtOfIllusion]$ ls
aoi-linux-install.jar aoisetup.sh
[gaowei@localhost ArtOfIllusion]$ ./aoisetup.sh
/usr/bin/java -jar aoi-linux-install.jar
[gaowei@localhost ArtOfIllusion]$
12
[root@localhost install_flash_player_10_linux]# ./flashplayer-installer
Copyright(C) 2002-2006 Adobe Macromedia Software LLC. All rights reserved.
Adobe Flash Player 10 for Linux
Adobe Flash Player 10 will be installed on this machine.
You are running the Adobe Flash Player installer as the "root" user.
Adobe Flash Player 10 will be installed system-wide.
Support is available at http://www.adobe.com/support/flashplayer/
To install Adobe Flash Player 10 now, press ENTER.
To cancel the installation at any time, press Control-C.
NOTE: Please exit any browsers you may have running.
Press ENTER to continue...
Please enter the installation path of the Mozilla, Netscape,
or Opera browser (i.e., /usr/lib/mozilla):
10。links浏览器
vmware
序列号N1HPX-6U248-D206Y-4Y8Z6
[root@localhost ~]# /usr/bin/vmware-config.pl
1.
In which directory do you want to install the theme icons?
[/usr/share/icons]
What directory contains your desktop menu entry files? These files have a
.desktop file extension. [/usr/share/applications]
In which directory do you want to install the application's icon?
[/usr/share/pixmaps]
None of the pre-built vmmon modules for VMware Workstation is suitable for your
running kernel. Do you want this program to try to build the vmmon module for
your system (you need to have a C compiler installed on your system)? [yes]
Using compiler "/usr/bin/gcc". Use environment variable CC to override.
What is the location of the directory of C header files that match your running
kernel? [/lib/modules/2.6.23.1-42.fc8/build/include]
None of the pre-built vmblock modules for VMware Workstation is suitable for
your running kernel. Do you want this program to try to build the vmblock
module for your system (you need to have a C compiler installed on your system)? [yes]
Do you want networking for your virtual machines? (yes/no/help) [yes]
You must read and accept the VMware VIX API End User License Agreement to
continue.
Press enter to display it.
Do you accept? (yes/no) yes
Thank you.
In which directory do you want to install the VMware VIX API binary files?
[/usr/bin]
In which directory do you want to install the VMware VIX API library files?
[/usr/lib/vmware-vix/lib]
The path "/usr/lib/vmware-vix/lib" does not exist currently. This program is
going to create it, including needed parent directories. Is this what you want?
[yes]
In which directory do you want to install the VMware VIX API document pages?
[/usr/share/doc/vmware-vix]
In which directory do you want to install the VMware VIX API document pages?
[/usr/share/doc/vmware-vix]
In which directory do you want to install the VMware VIX API document pages?
[/usr/share/doc/vmware-vix]
hardware
[gaowei@localhost AMUSEMENT ]$ mount
/dev/sda9 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
none on /proc/fs/vmblock/mountPoint type vmblock (rw)
/dev/sda8 on /media/AMUSEMENT type fuseblk (rw,nosuid,nodev,noatime,allow_other,blksize=4096)
/dev/sda1 on /media/OS type vfat (rw,nosuid,nodev,uhelper=hal,shortname=lower,uid=500)
/dev/sda6 on /media/SOFTWARE type vfat (rw,nosuid,nodev,uhelper=hal,shortname=lower,uid=500)
/dev/sda7 on /media/LESSON type fuseblk (rw,nosuid,nodev,noatime,allow_other,blksize=4096)
/dev/sda5 on /media/VMWARE type ext2 (rw,nosuid,nodev,uhelper=hal)
[gaowei@localhost AMUSEMENT ]$ df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda9 19304228 8826728 9481072 49% /
tmpfs 772932 12 772920 1% /dev/shm
/dev/sda8 12330328 6150732 6179596 50% /media/AMUSEMENT
/dev/sda1 15716960 712152 15004808 5% /media/OS
/dev/sda6 15716960 2769888 12947072 18% /media/SOFTWARE
/dev/sda7 15732328 4274160 11458168 28% /media/LESSON
/dev/sda5 34911367 5847569 27228610 18% /media/VMWARE
一种IDE应为hdb是用来命名主IDE上的从接口
另一种SCSI接口设备是用sd命名的
http://219.238.239.156:8000/osw/nimda
用户 u1006 口令 pass
 李开复:一是“内功”要学好,不要只是去学各种皮毛的语言、工具,和一些三流公司招聘列出来的要求,要把数据结构、算法、数据库、操作系统原理、计算机结构、离散数学等课程学好;二是多编程,最好大学四年有十万行编程的经验;还要讲究“实干”,Google是动手者的天堂,另外要有一些数学能力,还要练习团队精神,与人合作。
在 Linux 内核源文件 include/asm-i386/unistd.h 中,可以找到所有系统调用的定义。
http://kernelnewbies.org/KernelHacking
解压
.tar
解包: tar xvf FileName.tar
打包:tar cvf FileName.tar DirName
(注:tar是打包,不是压缩!)
---------------------------------------------
.gz
解压1:gunzip FileName.gz
解压2:gzip -d FileName.gz
压缩:gzip FileName
.tar.gz
解压:tar zxvf FileName.tar.gz
压缩:tar zcvf FileName.tar.gz DirName
---------------------------------------------
.bz2
解压1:bzip2 -d FileName.bz2
解压2:bunzip2 FileName.bz2
压缩: bzip2 -z FileName
.tar.bz2
解压:tar jxvf FileName.tar.bz2
压缩:tar jcvf FileName.tar.bz2 DirName
---------------------------------------------
.bz
解压1:bzip2 -d FileName.bz
解压2:bunzip2 FileName.bz
压缩:未知
.tar.bz
解压:tar jxvf FileName.tar.bz
压缩:未知
---------------------------------------------
.Z
解压:uncompress FileName.Z
压缩:compress FileName
.tar.Z
解压:tar Zxvf FileName.tar.Z
压缩:tar Zcvf FileName.tar.Z DirName
---------------------------------------------
.tgz
解压:tar zxvf FileName.tgz
压缩:未知
.tar.tgz
解压:tar zxvf FileName.tar.tgz
压缩:tar zcvf FileName.tar.tgz FileName
---------------------------------------------
.zip
解压:unzip FileName.zip
压缩:zip FileName.zip DirName
---------------------------------------------
.rar
解压:rar a FileName.rar
压缩:r ar e FileName.rar


rar请到:http://www.rarsoft.com/download.htm 下载!
解压后请将rar_static拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp rar_static /usr/bin/rar
---------------------------------------------
.lha
解压:lha -e FileName.lha
压缩:lha -a FileName.lha FileName

lha请到:http://www.infor.kanazawa-it.ac.jp/.../lhaunix/下载!
>解压后请将lha拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp lha /usr/bin/
---------------------------------------------
.rpm
解包:rpm2cpio FileName.rpm | cpio -div
---------------------------------------------
.tar .tgz .tar.gz .tar.Z .tar.bz .tar.bz2 .zip .cpio .rpm .deb .slp .arj .rar .ace .lha .lzh
.lzx .lzs .arc .sda .sfx .lnx .zoo .cab .kar .cpt .pit .sit .sea
解压:sEx x FileName.*
压缩:sEx a FileName.* FileName

sEx只是调用相关程序,本身并无压缩、解压功能,请注意!
sEx请到: http://sourceforge.net/projects/sex下载!
解压后请将sEx拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp sEx /usr/bin/
(其实看帮助是最好的方法,一般各个命令都可以用“--help”参数得到常用使用方法!
卸载wine安装的软件
Wine Software Uninstaller万一不行就用下面的!
~/.wine/drive_c/删除你不想要的!
cd~/.local/share/applications/wine/
这四个目录中保存着安装的程序信息,删除就可以了:
$HOME/.config/menus/applications-merged/wine* (好像是xml文件,有乱码也没有关系,不会影响显示!)
$HOME/.local/share/applications/wine (直接影响开始程序菜单中的显示!)
$HOME/.local/share/desktop-directories/wine*
$HOME/.local/share/icons/????_*.xpm (程序图标)

time