中南大学LINUX课程实验报告实验名称:专业班级:学号:姓名:实验一LINUX基本操作任务一:Linux 系统安装下图为安装成功,启动时后的登陆界面该系统我装在虚拟机上,为了掌握操作要领,在第一次摸索着安装之后,我又卸载安装了两次。
第一次在是否检测光盘时出现ok和skip选择时,选择了ok,之后就进行不下去了。
重新试了好几次,最终选择skip,然后就能继续下去了。
后来,在一个镜像文件安装完成之后,第二个镜像文件无法插入,就在setting 里面一阵折腾,最终发现在CD/DVD设置中的Device status下的connected 和connect at power on 都要打钩选择,然后接下来镜像文件就能安装了。
之后一直没有出现大问题。
第二次重装的时候,又把这里忘记了,折腾了半天才恍然大悟。
第三次装的时候,一切顺利。
过程中忘记截图了。
项目二:linux 桌面环境介绍一:以用户的身份登录:以管理员的身份登录:想要更改的桌面背景,确定。
更改后,如下图所示:项目三:vi编辑器的使用Vi的三种模式是命令模式,插入模式,可视模式删除5~10行后:将光标移到第五行,键入5dd,删除五行。
如下图将10~20行复制到末尾将光标移到第十行首,键入10yy,复制10~20行,再将光标移到末尾,键入p,粘贴。
如下图将所有的sbin改为bin。
键入g/sbin/s//bin/p,确定即可项目四:用户和组管理下图为操作及修改过程:项目五文件与目录管理任务一:在/home下创建一个test文档,不知道用什么命令做,后来查了,才知道。
所以输入了好几条错命令......查看/etc/passed 的文件类型在/home下建立一个名为newfile的文件用man cat 查看cat命令使用帮助查看/etc/passwd文件的前五行和后五行用cat查看/etc/passed 文件,查看结果:用more 查看/etc/passwd文件,查看结果:用less查看/etc/passwd的结果:用head 和tail 查看/etc/passed的结果:任务三:任务四:项目六权限管理实验1 Linux基本操作1、实验目的(1)复习Linux基本命令。
(2)掌握常用Linux命令2、实验内容(1)练习命令行模式下的常用命令:man:命令帮助ALT-Fx:虚终端切换常用命令:cat、Ls、ps、chmod、kill、ln、cp、mv、rm、cd、pwd、mkdir、chown、who、w、wc、whoami、date、uname等(2) 编辑器vi的使用:使用vi建立并修改一个文本文件root@localhost-desktop:~# vi testVi的三种模式是命令模式,插入模式,可视模式删除5~10行后:将光标移到第五行,键入5dd,删除五行。
如下图将10~20行复制到末尾将光标移到第十行首,键入10yy,复制10~20行,再将光标移到末尾,键入p,粘贴。
如下图键入g/sbin/s//bin/p,确定即可(3) 档案管理rpm、gzip、tar等命令Tar:将整个/etc 目录下的文件全部打包成为/tmp/etc.tar[root@linux ~]# tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩![root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <==打包后,以gzip 压缩[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <==打包后,以bzip2 压缩查阅上述/tmp/etc.tar.gz 文件内有哪些文件[root@linux ~]# tar -ztvf /tmp/etc.tar.gzGzip:将/etc/man.config 複制到/tmp ,并且以gzip 压缩[root@linux ~]# cd /tmp[root@linux tmp]# cp /etc/man.config .[root@linux tmp]# gzip man.config[root@linux tmp]# zcat man.config.gz(4) 网络命令telnet、ftp、talk心得体会:Shell命令是在终端输入命令来进行一些基本操作。
通过这次实验,我加深了对shell命令的了解和运用。
实验过程中,一个参数不对,命令就无法执行,这就要求我们对shell命令很熟悉地掌握,并且还要很认真细心。
通过这次实验,我收获很大,对以后的学习有很大帮助。
实验二:GCC/GNU与Makefile编写一个简单的c语言程序,在Linux下编辑、编译、运行。
用gdb调试器调试上面第2题的程序,查看程序执行每一步变量的值,熟悉gdb 的使用流程。
编写makefile文件编写hello.c文件编写print.c文件编写Makefile文件用gcc编译用gdb调试以及运行结果实验三:Linux文件系统1、在掌握Linux操作系统基本概念的基础上,本实验将通过proc文件系统观察整个系统的一些重要特征,并要求编写一个C程序,利用proc 文件系统获得和修改系统的各种配置参数。
具体要求如下:(1)以超级用户的身份登录Linux系统,并进入/proc目录,查看该目录下内容以及每个文件的读写权限,回答下列问题:CPU的类型和型号所使用的Linux版本从启动到当前时刻经过的时间当前内存状态#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <dirent.h>//A:编写程序在stdout上显示/proc中文件(CPU的类型和型号,所使用的Linux 内核版本,从系统最后一次启动以来已经经历了多长时间)void func_A(){int i,t,d,h,m,s;char c[512];FILE * in1,* in2,* in3;in1=fopen("/proc/cpuinfo","r");//打开acpuinfo文件in2=fopen("/proc/version","r");//打开version文件in3=fopen("/proc/uptime","r");//打开auptime文件//获取需要显示的信息for(i=0;i<5;i++)fgets(c,512,in1);printf("CPU型号: %s",c+13);fgets(c,512,in2);printf("内核版本: %s",c);//转换时间显示形式d=h=m=s=0;fscanf(in3,"%d",&t);d=t/86400;t=t%86400;h=t/3600;t=t%3600;m=t/60;t=t%60;s=t;printf("系统最后一次启动以来的时间: %02d:%02d:%02d:%02d\n",d,h,m,s);fclose(in1);fclose(in2);fclose(in3);}//C:改写A程序,在stdout上显示计算机配置的内存数量、当前可用的内存数量、平均负载列表(至上一分钟的平均数);void func_C(){FILE *in1,*in2; //定义2个文件指针char c[512],d[32]; //存放信息的数组int t,iteration=0;float m;in1=fopen("/proc/meminfo","r");//打开meminfo文件while(fgets(c,512,in1)!=NULL){sscanf(c,"%s %d",&d,&t);if(strcmp(d,"MemTotal:")==0)////定位到计算机配置的内存数量printf("计算机配置的内存数量:%d\n",t);if(strcmp(d,"MemFree:")==0)//定位到当前可用的内存数量printf("当前可用的内存数量:%d\n",t);}printf("平均负载列表至上一分钟的平均数:\n");while(iteration<duration){in2=fopen("/proc/loadavg","r");//打开loadavg文件sleep(internal);fgets(c,512,in2);rewind(in2);sscanf(c,"%f",&m);printf("%f\n",m);fclose(in2);iteration+=internal;}fclose(in1);//关闭文件}//主函数:int internal;int duration;void main(int argc, char *argv[]){char c1,c2;if(argc==1){func_A();return;}if(argc>1){sscanf(argv[1],"%c%c",&c1,&c2);if(c1!='-'){printf("usage: observer [-s] [-l int dur]\n");return;}if(c2=='s'){func_B();return;}if(c2=='l'){internal=atoi(argv[2]);//将string对象转化成int对象duration=atoi(argv[3]);func_C();return;}}}2、编写一个C程序,完成将两个文件合并生成一个新的文件。
#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <dirent.h>char *newfile= "new.txt "; //合并后的新文件//is directoryint testdir(char *path){struct stat buf;if(lstat(path,&buf) <0){return 0;}if(S_ISDIR(buf.st_mode)){return 1; //directory}return 0;}int appendfile(char *to,char *from){unsigned int CMAXLEN = 1024, nbytes;FILE *ffp, *tfp;char buf[CMAXLEN];if((ffp = fopen(from, "rb ")) == NULL) {return -1;}if((tfp = fopen(to, "ab ")) == NULL) {fclose(ffp);return -1;}while(!feof(ffp) && !ferror(ffp)){nbytes = fread(buf, 1, CMAXLEN, ffp); if(fwrite(buf, 1U, nbytes, tfp) != nbytes) {fclose(ffp);fclose(tfp);return -1;}if(nbytes < CMAXLEN)break;}fclose(tfp);fclose(ffp);return 0;}long __countline(char *filename){char buf[1024*1024];FILE *file;long count=0;file=fopen(filename, "r ");if(!file)return -1;while(!feof(file)){fgets(buf,1024*1024,file);count++;}count--;fclose(file);return count;}int directory(char *path){int count=0;DIR *db;char filename[128];struct dirent *p;db=opendir(path);if(db==NULL)return 0;memset(filename,0,128);while ((p=readdir(db))){if((strcmp(p-> d_name, ". ")==0)||(strcmp(p-> d_name, ".. ")==0))continue;else{sprintf(filename, "%s/%s ",path,p-> d_name);//È¡¶þ¼¶Ä¿Â¼Â·¾¶if(testdir(filename))continue;else {appendfile(newfile,filename);printf( "%s %ld行\n ",filename,countline(filename));}}memset(filename,0,64);}closedir(db);return count;}int main(int argc,char **argv){char *path[]={ "test1 ", "path2 ", "path3 ", "path4 ", "path5 "};int i;for( i=0;i <5;i++){if(access(path[i],F_OK)==0&&testdir(path[i])){printf( "is directory\n ");directory(path[i]);}else printf( "%s not exist\n ",path[i]);}}实验四:进程通信1、实验内容1.编写一个多进程通信程序,采用Message Queue或shared Memory或者Pipe File机制进行通信2、实验要求(1)写出源程序,并编译运行(2)详细记录程序调试及运行结果源代码:#include <stdio.h>#include <pthread.h>#define BUFFER_SIZE 16struct prodcons{// 缓冲区相关数据结构int buffer[BUFFER_SIZE];pthread_mutex_t lock;int readpos, writepos;pthread_cond_t notempty;pthread_cond_t notfull;};void init(struct prodcons *b){pthread_mutex_init(&b->lock, NULL);pthread_cond_init(&b->notempty, NULL);pthread_cond_init(&b->notfull, NULL);b->readpos = 0;b->writepos = 0;}void put(struct prodcons *b, int data){pthread_mutex_lock(&b->lock);if ((b->writepos + 1) % BUFFER_SIZE == b->readpos){pthread_cond_wait(&b->notfull, &b->lock); }b->buffer[b->writepos] = data;b->writepos++;if (b->writepos > = BUFFER_SIZE)b->writepos = 0;pthread_cond_signal(&b->notempty); pthread_mutex_unlock(&b->lock);}int get(struct prodcons *b){int data;pthread_mutex_lock(&b->lock);if (b->writepos == b->readpos){pthread_cond_wait(&b->notempty, &b->lock); }data = b->buffer[b->readpos];b->readpos++;if (b->readpos > = BUFFER_SIZE)b->readpos = 0;pthread_cond_signal(&b->notfull);pthread_mutex_unlock(&b->lock);return data;}#define OVER ( - 1)struct prodcons buffer;void *producer(void *data){int n;for (n = 0; n < 10000; n++){printf("%d --->\n", n);put(&buffer, n);} put(&buffer, OVER);return NULL;}void *consumer(void *data){int d;while (1){d = get(&buffer);if (d == OVER)break;printf("--->%d \n", d);}return NULL;}int main(void){pthread_t th_a, th_b;void *retval;init(&buffer);pthread_create(&th_a, NULL, producer, 0);pthread_create(&th_b, NULL, consumer, 0);pthread_join(th_a, &retval);pthread_join(th_b, &retval);return 0;}实验总结:为了能够完成Linux 课程的四次实验,我不断在自己的电脑上的虚拟机里尝试安装各种版本的Linux系统,因为电脑本身的原因,我重新装了好多次,终于顺利完成实验。