linux上机实习报告.docx

上传人:b****8 文档编号:12057581 上传时间:2023-06-04 格式:DOCX 页数:35 大小:736.31KB
下载 相关 举报
linux上机实习报告.docx_第1页
第1页 / 共35页
linux上机实习报告.docx_第2页
第2页 / 共35页
linux上机实习报告.docx_第3页
第3页 / 共35页
linux上机实习报告.docx_第4页
第4页 / 共35页
linux上机实习报告.docx_第5页
第5页 / 共35页
linux上机实习报告.docx_第6页
第6页 / 共35页
linux上机实习报告.docx_第7页
第7页 / 共35页
linux上机实习报告.docx_第8页
第8页 / 共35页
linux上机实习报告.docx_第9页
第9页 / 共35页
linux上机实习报告.docx_第10页
第10页 / 共35页
linux上机实习报告.docx_第11页
第11页 / 共35页
linux上机实习报告.docx_第12页
第12页 / 共35页
linux上机实习报告.docx_第13页
第13页 / 共35页
linux上机实习报告.docx_第14页
第14页 / 共35页
linux上机实习报告.docx_第15页
第15页 / 共35页
linux上机实习报告.docx_第16页
第16页 / 共35页
linux上机实习报告.docx_第17页
第17页 / 共35页
linux上机实习报告.docx_第18页
第18页 / 共35页
linux上机实习报告.docx_第19页
第19页 / 共35页
linux上机实习报告.docx_第20页
第20页 / 共35页
亲,该文档总共35页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

linux上机实习报告.docx

《linux上机实习报告.docx》由会员分享,可在线阅读,更多相关《linux上机实习报告.docx(35页珍藏版)》请在冰点文库上搜索。

linux上机实习报告.docx

linux上机实习报告

Linux第一次上机(运行环境RedHat)

调试运行CD唱片应用程序

运行过程

图表1第一步,使用./CD命令运行CD脚本

图表2进入选项界面

图表3增加CD

图表4查找CD

图表5查询数目

图表6退出

心得体会

通过运行书上的CD唱片应用程序,进一步地了解到shell编程的方法,以及编写程序的思路。

编写shell脚本,求1到100的和

运行过程

图表7使用./sum1to100运行

源代码

图表8使用catsum1to100查看源代码

编写shell脚本,从键盘输入两个数,求这两个数的和

运行过程

图表9使用./sumAandB运行

源代码

图表10使用catsumAandB查看源代码

等待特定用户登录,每30秒确认一次

运行过程

已知特定用户是zzx,我首先让root用户登录并运行程序,可以发现每隔30s提醒一次

图表11使用./checkuser运行

当我切换到zzx用户,并再次运行程序,可得如下结果

源代码

图表12使用catcheckuser查看代码

找出系统中当前消耗磁盘容量最大的前10个用户,并向他们发送警告邮件

运行过程

由于权限等问题,我们使用root用户运行程序

图表13使用./mymail运行

运行完毕后,我们就可以查看邮件了

图表14root用户收到的邮件

图表15zzx用户收到的邮件

图表16t1用户收到的邮件

源代码

图表17使用cat命令查看

查找输入文件的路径

运行过程

由于权限的问题,我们使用root用户运行程序

图表18使用./route运行,并按照要求输入文件名

源代码

图表19使用catroute查看源代码

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

intdo_ls(char*dir,char*filename,intlflag)

{

intn;

structstatbuf;

charout[100];

structpasswd*pw;

structgroup*gr;

structtm*t;

if(lflag==0)

{

printf("%s\t",filename);

return0;

}

if(lstat(dir,&buf)<0)

{

fprintf(stderr,"staterror:

%s\n",strerror(errno));

return-1;

}

switch(buf.st_mode&S_IFMT){

caseS_IFREG:

printf("-");

break;

caseS_IFDIR:

printf("d");

break;

caseS_IFCHR:

printf("c");

break;

caseS_IFBLK:

printf("b");

break;

caseS_IFIFO:

printf("p");

break;

caseS_IFLNK:

printf("l");

break;

caseS_IFSOCK:

printf("s");

break;

}

for(n=8;n>=0;n--)

{

if(buf.st_mode&(1<

{

switch(n%3)

{

case2:

printf("r");

break;

case1:

printf("w");

break;

case0:

printf("x");

break;

default:

break;

}

}

else

{

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("readlinkerror\n");

}

printf("%s",out);

}

printf("\n");

return0;

}

intls_prepare(char*w,intaflag,intlflag){

structstatbuf;

charname[100];

DIR*dir;

structdirent*pdr;

if(lstat(w,&buf)<0)

{

fprintf(stderr,"staterror:

%s\n",strerror(errno));

return-1;

}

if(S_ISDIR(buf.st_mode))

{

dir=opendir(w);

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);

}

}

closedir(dir);

}else

{

do_ls(w,w,lflag);

}

return0;

}

intmain(intargc,char**argv)

{

intaflag=0;

intlflag=0;

charc;

inti;

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

ls_prepare(argv[i],aflag,lflag);

}

printf("\n");

return0;

}

调试编译串行口通信程序p6.5.c

运行过程

由于权限问题,我们在root用户下运行程序

图表24使用./mytrunk不带参数运行

图表25使用./mytrunk/dev/ttyS00带参数运行

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

/*Daemonizemyself.*/

intfd1;

voidsigintHandler(intsig)

{

if(sig==SIGTERM)

{

write(fd1,"end\n",5);

exit(0);

}

}

intdaemon(intnochdir,intnoclose)

{

pid_tpid;

pid=fork();

/*Incaseofforkiserror.*/

if(pid<0)

{

perror("fork");

return-1;

}

/*Incaseofthisisparentprocess.*/

if(pid!

=0)

exit(0);

/*Becomesessionleaderandgetpid.*/

pid=setsid();

if(pid<-1)

{

perror("setsid");

return-1;

}

/*Changedirectorytoroot.*/

if(!

nochdir)

chdir("/");

/*Filedescriptorclose.*/

if(!

noclose)

{

intfd;

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);

return0;

}

intmain(void)

{

fd1=open("test.txt",O_RDWR|O_TRUNC);

write(fd1,"start\n",7);

daemon(0,0);

signal(SIGTERM,sigintHandler);

sleep(1000);

return0;

}

分别利用本地socket套接字和INTENET套接字实现进程间文件传输

本地socket套接字运行过程

图表34使用./server1运行服务器,显示服务器等待状态

图表35当在另外一个终端运行客户端程序时,服务器显示客户连接,并要求输入传输文件名称

图表36当服务器输入文件名称server1.c后客户端显示接收信息,不过接收到的文件重定向到test.txt中

图表37server1.c的内容

图表38客户端接收到显示在test.txt的内容,和server1.c的内容比较,完全一致

本地socket套接字服务器server1.c源代码

#include

#include

#include

#include

#include

#include

#include

#include

intmain()

{

intfilefd,n;////////////////

charfile[100],buf[1024];////////////////

intserver_sockfd,client_sockfd;

intserver_len,client_len;

structsockaddr_unserver_address;

structsockaddr_unclient_address;

unlink("server_socket");

server_sockfd=socket(AF_UNIX,SOCK_STREAM,0);

server_address.sun_family=AF_UNIX;

strcpy(server_address.sun_path,"server_socket");

server_len=sizeof(server_address);

bind(server_sockfd,(structsockaddr*)&server_address,server_len);

listen(server_sockfd,5);

while

(1)

{

printf("serverwaiting\n");

client_len=sizeof(client_address);

client_sockfd=accept(server_sockfd,(structsockaddr*)&client_address,&client_len);

if(client_sockfd!

=-1)

{

printf("youhaveaclient,pleaseputthefilenametotransport!

!

\n");

scanf("%s",file);

if((filefd=open(file,O_RDWR))<0)

{

perror("can'tfindthefile");

exit

(1);

}

printf("filefd=%d\n",filefd);

printf("thefileistransported,pleasewait...\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("youhavetransport%dbytesanditisend!

!

\n",n);

close(client_sockfd);

}

close(filefd);

}

}

本地socket套接字客户端client1.c源代码

#include

#include

#include

#include

#include

#include

#include

#include

intmain()

{

intfilefd,n,oldfilefd;/////////////////

charfile[100],buf[1024];

intsockfd;

intlen;

structsockaddr_unaddress;

intresult;

sockfd=socket(AF_UNIX,SOCK_STREAM,0);

address.sun_family=AF_UNIX;

strcpy(address.sun_path,"server_socket");

len=sizeof(address);

result=connect(sockfd,(structsockaddr*)&address,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("thefilenameyoureceiveis:

%s\n",file);

if((n=read(sockfd,buf,sizeof(buf)))>0)

{

write(filefd,buf,n);

}

printf("youhavereceivedafilewhichis%dbytes,butthefile'scontextcoverthetest.txt,sopleasecheckintotest.txt!

!

\n",n);

close(sockfd);

}

close(filefd);

exit(0);

}

INTENET套接字运行过程

图表39使用./server2运行服务器,显示服务器等待状态

图表40当在另外一个终端运行客户端程序时,服务器显示客户连接,并要求输入传输文件名称

图表41当服务器输入文件名称server2.c后客户端显示接收信息,不过接收到的文件重定向到test.txt中

图表42server2.c的内容

图表43客户端接收到显示在test.txt的内容,和server2.c的内容比较,完全一致

INTENET套接字服务器server2.c源代码

#include

#include

#include

#include

#include

#include

#include

#include

#include

intmain()

{

intfilefd,n;////////////////

charfile[100],buf[4096];///////////////////

intserver_sockfd,client_sockfd;

intserver_len,client_len;

structsockaddr_inserver_address;

structsockaddr_inclient_address;

server_sockfd=socket(AF_INET,SOCK_STREAM,0);

server_address.sin_family=AF_INET;

server_address.sin_addr.s_addr=inet_addr("127.0.0.1");

server_address.sin_port=9734;

server_len=sizeof(server_address);

bind(server_sockfd,(structsockaddr*)&server_address,server_len);

listen(server_sockfd,5);

while

(1)

{

printf("serverwaiting\n");

client_len=sizeof(client_address);

client_sockfd=accept(server_sockfd,(structsockaddr*)&client_address,&client_len);

if(client_sockfd!

=-1)

{

printf("youhaveaclient,pleaseputthefilenametotransport!

!

\n");

scanf("%s",file);

if((filefd=open(file,O_RDWR))<0)

{

perror("can'tfindthefile");

exit

(1);

}

printf("filefd=%d\n",filefd);

printf("thefileistransported,pleasewait...\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("youhavetransport%dbytesanditisend

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2