linux上机实习报告_linux的实习报告
linux上机实习报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“linux的实习报告”。
目 录
Linux 第一次上机(运行环境RedHat)................................................................................1 调试运行CD唱片应用程序.......................................................................................1 运行过程............................................................................................................1 心得体会............................................................................................................3 编写shell脚本,求1到100的和..............................................................................3 运行过程............................................................................................................3 源代码...............................................................................................................4 编写shell脚本,从键盘输入两个数,求这两个数的和.................................................4 运行过程............................................................................................................4 源代码...............................................................................................................5 等待特定用户登录,每30秒确认一次.......................................................................5 运行过程............................................................................................................5 源代码...............................................................................................................6 找出系统中当前消耗磁盘容量最大的前10个用户,并向他们发送警告邮件...............6 运行过程............................................................................................................6 源代码...............................................................................................................8 查找输入文件的路径.................................................................................................8 运行过程............................................................................................................8 源代码...............................................................................................................9 Linux 第二次上机(运行环境RedHat)................................................................................9 定制自己的ls命令。提供至少三种带参数的执行方式................................................9 运行过程............................................................................................................9 源代码..............................................................................................................11 调试编译串行口通信程序p6.5.c...............................................................................16 运行过程..........................................................................................................16 Linux 第三次上机(运行环境RedHat,CentOS)...............................................................16 创建一个系统监听守护进程,一旦接收到其他进程发来的信号,马上给出报告........16 运行过程..........................................................................................................16 源代码.............................................................................................................18 分别利用本地socket套接字和INTENET套接字实现进程间文件传输......................20 本地socket套接字运行过程.............................................................................20 本地socket套接字服务器server1.c源代码........................................................22 本地socket套接字客户端client1.c源代码.........................................................23 INTENET套接字运行过程...............................................................................25 INTENET套接字服务器server2.c源代码..........................................................28 INTENET套接字客户端client2.c源代码...........................................................29 感谢与收获....................................................................................................................30
Linux 第一次上机(运行环境RedHat)调试运行CD唱片应用程序 运行过程
图表 1第一步,使用./CD 命令运行CD脚本
图表 2进入选项界面
图表 3增加CD
图表 4查找CD
图表 5查询数目
图表 6退出
心得体会
通过运行书上的CD唱片应用程序,进一步地了解到shell编程的方法,以及编写程序的思路。
编写shell脚本,求1到100的和 运行过程
图表 7使用./sum1to100运行
源代码
图表 8使用cat sum1to100查看源代码
编写shell脚本,从键盘输入两个数,求这两个数的和
运行过程
图表 9使用./sumAandB 运行
源代码
图表 10使用cat sumAandB查看源代码
等待特定用户登录,每30秒确认一次 运行过程
已知特定用户是zzx,我首先让root用户登录并运行程序,可以发现每隔30s提醒一次
图表 11使用./checkuser运行
当我切换到zzx用户,并再次运行程序,可得如下结果
源代码
图表 12使用cat checkuser查看代码
找出系统中当前消耗磁盘容量最大的前10个用户,并向他们发送警告邮件 运行过程
由于权限等问题,我们使用root用户运行程序
图表 13使用./mymail运行
运行完毕后,我们就可以查看邮件了
图表 14 root用户收到的邮件
图表 15 zzx用户收到的邮件
图表 16 t1用户收到的邮件
源代码
图表 17 使用cat命令查看
查找输入文件的路径 运行过程
由于权限的问题,我们使用root用户运行程序
图表 18 使用./route运行,并按照要求输入文件名
源代码
图表 19 使用cat route查看源代码
Linux 第二次上机(运行环境RedHat)定制自己的ls命令。提供至少三种带参数的执行方式 运行过程
图表 20 使用./ls 运行不带参数ls
图表 21使用./ls-l 运行带参数-l 的ls
图表 22 使用./ls-a运行带参数-a的ls
图表 23使用./ls-al 运行带参数-al的ls 源代码
#include #include #include #include #include #include #include
#include #include #include
int do_ls(char *dir,char *filename,int lflag){
int n;struct stat buf;char out[100];struct pawd *pw;
struct group *gr;struct tm *t;if(lflag == 0){
} {
} return 0;
printf(“%st”,filename);if(lstat(dir,&buf)
case S_IFSOCK:
printf(“s”);} for(n=8;n>=0;n--){
if(buf.st_mode&(1
break;
}
} else { } { case 2:
printf(“r”);break;case 1:
printf(“w”);break;case 0:
printf(“x”);break;default: break;} printf(“-”);printf(“ %d”,buf.st_nlink);
pw = getpwuid(buf.st_uid);printf(“ %s”,pw->pw_name);gr = getgrgid(buf.st_gid);printf(“ %s”,gr->gr_name);printf(“ %ld”,buf.st_size);t = localtime(&buf.st_atime);printf(“ %d-%d-%d %d:%d”
,t->tm_year+1900 ,t->tm_mon+1 ,t->tm_mday ,t->tm_hour,t->tm_min);
printf(“ %s ”,filename);
if(S_ISLNK(buf.st_mode)){
printf(“-> ”);if(readlink(filename,out,100)==-1){ } //printf(“readlink errorn”);
} printf(“%s”,out);} printf(“n”);return 0;int ls_prepare(char *w,int aflag,int lflag){
struct stat buf;char name[100];DIR *dir;
struct dirent *pdr;if(lstat(w,&buf)
dir = opendir(w);{
} closedir(dir);
while((pdr = readdir(dir))!=NULL)if(aflag==0){
if(pdr->d_name[0]=='.')
continue;memset(name,0,100);
strcpy(name,w);
strcat(name,“/”);
strcat(name,pdr->d_name);do_ls(name,pdr->d_name,lflag);}else
{
} memset(name,0,100);strcpy(name,w);strcat(name,“/”);strcat(name,pdr->d_name);do_ls(name,pdr->d_name,lflag);}else { do_ls(w,w,lflag);
} {
} }
return 0;int main(int argc,char **argv)int aflag =0;int lflag =0;char c;int i;while((c = getopt(argc,argv,“al”))!=-1){
} switch(c){ case 'a': aflag =1;break;case 'l': lflag =1;break;default: break;}
if(argc == optind){ ls_prepare(“./”,aflag,lflag);} else {
} for(i=optind;i
调试编译串行口通信程序p6.5.c 运行过程
由于权限问题,我们在root用户下运行程序
图表 24 使用./mytrunk不带参数运行
图表 25使用./mytrunk /dev/ttyS0 0带参数运行
Linux 第三次上机(运行环境RedHat,CentOS)创建一个系统监听守护进程,一旦接收到其他进程发来的信号,马上给出报告 运行过程
图表 26首先运行守护进程之后查看 当前进程
图表 27可以发现守护进程-jincheng在后台运行,且 PPID=1,PID=4085
图表 28可以发现守护进程向test.txt发送了start
图表 29向守护进程发送终止信号,终止守护进程
图表 30可以发现后台运行的守护进程没了
图表 31可以发现守护进程又向test.txt发送了end
图表 32具体查看test.txt
图表 33具体查看test.txt的内容
源代码
#include #include #include #include #include #include #include /* Daemonize myself.*/ int fd1;void sigintHandler(int sig){
if(sig==SIGTERM){ write(fd1,“endn”,5);exit(0);} } int daemon(int nochdir, int noclose){
pid_t pid;
pid = fork();
/* In case of fork is error.*/
if(pid
{
perror(“fork”);
return-1;
}
/* In case of this is parent proce.*/
if(pid!= 0)
exit(0);
/* Become seion leader and get pid.*/
pid = setsid();
if(pid
{
perror(“setsid”);
return-1;
}
/* Change directory to root.*/
if(!nochdir)
chdir(“/”);
/* File descriptor close.*/
if(!noclose)
{
int fd;
fd = open(“/dev/null”, O_RDWR, 0);
if(fd!=-1)
{
dup2(fd, STDIN_FILENO);
}
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if(fd > 2)
close(fd);
}
umask(0027);
return 0;
}
int main(void){ fd1=open(“test.txt”,O_RDWR | O_TRUNC);
} write(fd1,“startn”,7);daemon(0,0);signal(SIGTERM,sigintHandler);sleep(1000);return 0;
分别利用本地socket套接字和INTENET套接字实现进程间文件传输
本地socket套接字运行过程
图表 34使用./server1运行服务器,显示服务器等待状态
图表 35当在另外一个终端运行客户端程序时,服务器显示客户连接,并要求输入传输文件名称
图表 36当服务器输入文件名称server1.c后客户端显示接收信息,不过接收到的文件重定向到
test.txt中
图表 37 server1.c的内容
图表 38 客户端接收到显示在test.txt的内容,和server1.c的内容比较,完全一致
本地socket套接字服务器server1.c源代码
#include #include #include #include #include #include #include #include int main(){
int filefd,n;////////////////
char file[100],buf[1024];////////////////
int server_sockfd,client_sockfd;
int server_len,client_len;
struct sockaddr_un server_addre;
struct sockaddr_un client_addre;
unlink(“server_socket”);
server_sockfd =socket(AF_UNIX,SOCK_STREAM,0);
server_addre.sun_family =AF_UNIX;
strcpy(server_addre.sun_path,“server_socket”);
server_len =sizeof(server_addre);
bind(server_sockfd,(struct sockaddr *)&server_addre,server_len);
listen(server_sockfd,5);
while(1)
{
printf(“server waitingn”);
client_len =sizeof(client_addre);
client_sockfd=accept(server_sockfd,(struct *)&client_addre,&client_len);
if(client_sockfd!=-1)
{
printf(“you have a client ,please put the filename to transport!n”);
scanf(“%s”,file);
if((filefd=open(file,O_RDWR))
{
perror(“can't find the file”);
exit(1);
}
printf(“filefd= %dn”,filefd);
printf(“the file is transported ,please wait...n”);
lseek(filefd,0L,0);//每次接受客户机连接,应将用于读的源文件指针移到文件头
write(client_sockfd,file,sizeof(file));//传送文件名
if((n=read(filefd,buf,sizeof(buf)))>0)
{
}
write(client_sockfd,buf,n);
sockaddr
printf(“you have transport %d bytes and it is end!n”,n);
close(client_sockfd);
}
close(filefd);
} }
本地socket套接字客户端client1.c源代码
#include #include #include #include #include #include
#include #include int main(){
int filefd,n,oldfilefd;/////////////////
char file[100],buf[1024];
int sockfd;
int len;
struct sockaddr_un addre;
int result;
sockfd =socket(AF_UNIX, SOCK_STREAM,0);
addre.sun_family =AF_UNIX;
strcpy(addre.sun_path,“server_socket”);
len=sizeof(addre);
result=connect(sockfd,(struct sockaddr *)&addre,len);
if(result ==-1)
{
perror(“oops:client1”);
exit(1);
}
if(result!=-1)
{
oldfilefd=open(“test.txt”,O_RDWR);
filefd=dup(oldfilefd);
read(sockfd,file,sizeof(file));
printf(“the filename you receive is:%sn”,file);
if((n=read(sockfd,buf,sizeof(buf)))>0)
{
write(filefd,buf,n);
}
printf(“you have received a file which is %d bytes,but the file's context cover the test.txt,so please check into test.txt!n”,n);
close(sockfd);
}
close(filefd);
exit(0);}
INTENET套接字运行过程
图表 39使用./server2运行服务器,显示服务器等待状态
图表 40当在另外一个终端运行客户端程序时,服务器显示客户连接,并要求输入传输文件名称
图表 41当服务器输入文件名称server2.c后客户端显示接收信息,不过接收到的文件重定向到
test.txt中
图表 42 server2.c的内容
图表 43客户端接收到显示在test.txt的内容,和server2.c的内容比较,完全一致
INTENET套接字服务器server2.c源代码
#include #include #include #include #include #include #include #include #include
int main(){ int filefd,n;////////////////
char file[100],buf[4096];/////////////////// int server_sockfd,client_sockfd;
int server_len,client_len;struct sockaddr_in server_addre;struct sockaddr_in client_addre;server_sockfd =socket(AF_INET,SOCK_STREAM,0);server_addre.sin_family=AF_INET;server_addre.sin_addr.s_addr=inet_addr(“127.0.0.1”);server_addre.sin_port =9734;server_len=sizeof(server_addre);bind(server_sockfd,(struct sockaddr *)&server_addre,server_len);listen(server_sockfd,5);while(1)
{
printf(“server waitingn”);
client_len =sizeof(client_addre);
client_sockfd=accept(server_sockfd,(struct *)&client_addre,&client_len);
if(client_sockfd!=-1)
{
printf(“you have a client ,please put the filename to transport!n”);scanf(“%s”,file);
if((filefd=open(file,O_RDWR))
sockaddr
perror(“can't find the file”);
exit(1);
}
printf(“filefd= %dn”,filefd);
printf(“the file is transported ,please wait...n”);
lseek(filefd,0L,0);//每次接受客户机连接,应将用于读的源>文件指针移到文件头
write(client_sockfd,file,sizeof(file));//传送文件名
if((n=read(filefd,buf,sizeof(buf)))>0)
{
write(client_sockfd,buf,n);
}
printf(“you have transport %d bytes and it is end!n”,n);
close(client_sockfd);
}
close(filefd);
} }
INTENET套接字客户端client2.c源代码
#include #include #include #include #include #include #include #include #include
int main(){
int filefd,n,oldfilefd;//////////////////// char file[100],buf[4096];int sockfd;int len;struct sockaddr_in addre;int result;sockfd =socket(AF_INET,SOCK_STREAM,0);addre.sin_family =AF_INET;addre.sin_addr.s_addr =inet_addr(“127.0.0.1”);addre.sin_port =9734;len =sizeof(addre);
result=connect(sockfd,(struct sockaddr *)&addre,len);
if(result ==-1)
{
perror(“oops:client2”);
exit(1);
}
if(result!=-1){
oldfilefd=open(“test.txt”,O_RDWR);
filefd=dup(oldfilefd);
read(sockfd,file,sizeof(file));
printf(“the filename you receive is:%sn”,file);
if((n=read(sockfd,buf,sizeof(buf)))>0)
{
write(filefd,buf,n);
}
printf(“you have received a file which is %d bytes,but the file's context cover the test.txt,so please check into test.txt!n”,n);
close(sockfd);} close(filefd);
exit(0);}
感谢与收获
通过这几次上机实习,使我更加扎实的掌握了有关Linux C编程方面的知识,在设计过程中虽然遇到了一些问题,但经过一次又一次的思考,一遍又一遍的检查终于找出了原因所在,也暴露出了前期我在这方面的知识欠缺和经验不足。实践出真知,通过编程,使我们掌握的知识不再是纸上谈兵。
过而能改,善莫大焉。在课程设计过程中,我们不断发现错误,不断改正,不断领悟,不断获取。最终的检测调试环节,本身就是在践行“过而能改,善莫大焉”的知行观。这次课程设计终于顺利完成了,在设计中遇到了很多问题,最后在老师的指导下,终于游逆而解。在今后社会的发展和学习实践过程中,一定要不懈努力,不能遇到问题就想到要退缩,一定要不厌其烦的发现问题所在,然后一一进行解决,只有这样,才能成功的做成想做的事,才能在今后的道路上披荆斩棘,而不是知难而退,那样永远不可能收获成功,收获喜悦,也永远不可能得到社会及他人对你的认可!
实践课诚然是一门专业课,给我很多专业知识以及专业技能上的提升,同时又是一门讲道课,一门辩思课,给了我许多道,给了我很多思,给了我莫大的空间。同时,设计让我感触很深。使我对抽象的理论有了具体的认识。
我认为,在这学期的课程设计中,不仅培养了独立思考、动手操作的能力,在各种
其它能力上也都有了提高。更重要的是,在课程设计上,我们学会了很多学习的方法。而这是日后最实用的,真的是受益匪浅。要面对社会的挑战,只有不断的学习、实践,再学习、再实践。这对于我们的将来也有很大的帮助。以后,不管有多苦,我想我们都能变苦为乐,找寻有趣的事情,发现其中珍贵的事情。就像中国提倡的艰苦奋斗一样,我们都可以在实验结束之后变的更加成熟,会面对需要面对的事情。
回顾起此课程设计,至今我仍感慨颇多,从理论到实践,在这段日子里,可以说得是苦多于甜,但是可以学到很多很多的东西,同时不仅可以巩固了以前所学过的知识,而且学到了很多在书本上所没有学到过的知识。通过这次课程设计使我懂得了理论与实际相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,从理论中得出结论,才能真正为社会服务,从而提高自己的实际动手能力和独立思考的能力。在设计的过程中遇到问题,可以说得是困难重重,但可喜的是最终都得到了解决。