当前位置:文档之家› 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 <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdio.h>#include <string.h>#include <errno.h>#include <pwd.h>#include <grp.h>#include <time.h>#include <dirent.h>int do_ls(char *dir,char *filename,int lflag){int n;struct stat buf;char out[100];struct passwd *pw;struct group *gr;struct tm *t;if(lflag == 0){printf("%s\t",filename);return 0;}if(lstat(dir,&buf)<0){fprintf(stderr,"stat error:%s\n",strerror(errno));return -1;}switch(buf.st_mode & S_IFMT) {case S_IFREG:printf("-");break;case S_IFDIR:printf("d");break;case S_IFCHR:printf("c");break;case S_IFBLK:printf("b");break;case S_IFIFO:printf("p");break;case S_IFLNK:printf("l");break;case S_IFSOCK:printf("s");break;}for(n=8;n>=0;n--){if(buf.st_mode&(1<<n)){switch(n%3){case 2:printf("r");break;case 1:printf("w");break;case 0: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("readlink error\n");}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)<0){fprintf(stderr,"stat error:%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);}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<argc;i++)ls_prepare(argv[i],aflag,lflag);}printf("\n");return 0;}调试编译串行口通信程序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 <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <signal.h>#include <fcntl.h>#include <syslog.h>/* Daemonize myself. */int fd1;void sigintHandler(int sig){if(sig==SIGTERM){write(fd1,"end\n",5);exit(0);}}int daemon (int nochdir, int noclose){pid_t pid;pid = fork ();/* In case of fork is error. */if (pid < 0){perror ("fork");return -1;}/* In case of this is parent process. */if (pid != 0)exit (0);/* Become session leader and get pid. */ pid = setsid();if (pid < -1){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,"start\n",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<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<sys/un.h>#include<unistd.h>#include<stdlib.h>#include<sys/stat.h>#include<fcntl.h>int main(){int filefd,n; ////////////////char file[100],buf[1024]; ////////////////int server_sockfd,client_sockfd;int server_len,client_len;struct sockaddr_un server_address;struct sockaddr_un client_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,(struct sockaddr *)&server_address,server_len);listen(server_sockfd,5);while(1){printf("server waiting\n");client_len =sizeof(client_address);client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,&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))<0){perror("can't find the file");exit(1);}printf("filefd= %d\n",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);}}本地socket套接字客户端client1.c源代码#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<sys/un.h>#include<unistd.h>#include<stdlib.h>#include<sys/stat.h>#include<fcntl.h>int main(){int filefd,n,oldfilefd; /////////////////char file[100],buf[1024];int sockfd;int len;struct sockaddr_un address;int result;sockfd =socket(AF_UNIX, SOCK_STREAM,0);address.sun_family =AF_UNIX;strcpy(address.sun_path,"server_socket");len=sizeof(address);result=connect(sockfd,(struct sockaddr *)&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("the filename you receive is:%s\n",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<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdlib.h>#include<sys/stat.h>#include<fcntl.h>int main(){int filefd,n;////////////////char file[100],buf[4096];///////////////////int server_sockfd,client_sockfd;int server_len,client_len;struct sockaddr_in server_address;struct sockaddr_in client_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,(struct sockaddr *)&server_address,server_len);listen(server_sockfd,5);while(1){printf("server waiting\n");client_len =sizeof(client_address);client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,&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))<0){perror("can't find the file");exit(1);}printf("filefd= %d\n",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<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdlib.h>#include<sys/stat.h>#include<fcntl.h>int main(){int filefd,n,oldfilefd;////////////////////char file[100],buf[4096];int sockfd;int len;struct sockaddr_in address;int result;sockfd =socket(AF_INET,SOCK_STREAM,0);address.sin_family =AF_INET;address.sin_addr.s_addr =inet_addr("127.0.0.1");address.sin_port =9734;len =sizeof(address);result=connect(sockfd,(struct sockaddr *)&address,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:%s\n",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编程方面的知识,在设计过程中虽然遇到了一些问题,但经过一次又一次的思考,一遍又一遍的检查终于找出了原因所在,也暴露出了前期我在这方面的知识欠缺和经验不足。

相关主题