图表 1第一步,使用./CD 命令运行CD脚本
图表 2进入选项界面
图表 3增加CD
图表 4查找CD
图表 5查询数目
图表 6退出
通过运行书上的CD唱片应用程序,进一步地了解到shell编程的方法,以及编写程序的思路。
图表 7使用./sum1to100运行
图表 8使用cat sum1to100查看源代码
图表 9使用./sumAandB 运行
图表 10使用cat sumAandB查看源代码
已知特定用户是zzx,我首先让root用户登录并运行程序,可以发现每隔30s提醒一次
图表 11使用./checkuser运行
当我切换到zzx用户,并再次运行程序,可得如下结果
图表 12使用cat checkuser查看代码
由于权限等问题,我们使用root用户运行程序
图表 13使用./mymail运行
运行完毕后,我们就可以查看邮件了
图表 14 root用户收到的邮件
图表 15 zzx用户收到的邮件
图表 16 t1用户收到的邮件
图表 17 使用cat命令查看
由于权限的问题,我们使用root用户运行程序
图表 18 使用./route运行,并按照要求输入文件名
图表 19 使用cat route查看源代码
图表 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 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<
{
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
ls_prepare(argv[i],aflag,lflag);
}
printf("\n");
return 0;
}
由于权限问题,我们在root用户下运行程序
图表 24 使用./mytrunk不带参数运行
图表 25使用./mytrunk /dev/ttyS0 0带参数运行
图表 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,"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;
}
图表 34使用./server1运行服务器,显示服务器等待状态
图表 35当在另外一个终端运行客户端程序时,服务器显示客户连接,并要求输入传输文件名称
图表 36当服务器输入文件名称server1.c后客户端显示接收信息,不过接收到的文件重定向到test.txt中
图表 37 server1.c的内容
图表 38 客户端接收到显示在test.txt的内容,和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_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);
}
}
#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 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);
}
图表 39使用./server2运行服务器,显示服务器等待状态
图表 40当在另外一个终端运行客户端程序时,服务器显示客户连接,并要求输入传输文件名称
图表 41当服务器输入文件名称server2.c后客户端显示接收信息,不过接收到的文件重定向到test.txt中
图表 42 server2.c的内容
图表 43客户端接收到显示在test.txt的内容,和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_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);
}
}
#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 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编程方面的知识,在设计过程中虽然遇到了一些问题,但经过一次又一次的思考,一遍又一遍的检查终于找出了原因所在,也暴露出了前期我在这方面的知识欠缺和经验不足。实践出真知,通过编程,使我们掌握的知识不再是纸上谈兵。
过而能改,善莫大焉。在课程设计过程中,我们不断发现错误,不断改正,不断领悟,不断获取。最终的检测调试环节,本身就是在践行“过而能改,善莫大焉”的知行观。这次课程设计终于顺利完成了,在设计中遇到了很多问题,最后在老师的指导下,终于游逆而解。在今后社会的发展和学习实践过程中,一定要不懈努力,不能遇到问题就想到要退缩,一定要不厌其烦的发现问题所在,然后一一进行解决,只有这样,才能成功的做成想做的事,才能在今后的道路上披荆斩棘,而不是知难而退,那样永远不可能收获成功,收获喜悦,也永远不可能得到社会及他人对你的认可!
实践课诚然是一门专业课,给我很多专业知识以及专业技能上的提升,同时又是一门讲道课,一门辩思课,给了我许多道,给了我很多思,给了我莫大的空间。同时,设计让我感触很深。使我对抽象的理论有了具体的认识。
我认为,在这学期的课程设计中,不仅培养了独立思考、动手操作的能力,在各种其它能力上也都有了提高。更重要的是,在课程设计上,我们学会了很多学习的方法。而这是日后最实用的,真的是受益匪浅。要面对社会的挑战,只有不断的学习、实践,再学习、再实践。这对于我们的将来也有很大的帮助。以后,不管有多苦,我想我们都能变苦为乐,找寻有趣的事情,发现其中珍贵的事情。就像中国提倡的艰苦奋斗一样,我们都可以在实验结束之后变的更加成熟,会面对需要面对的事情。
回顾起此课程设计,至今我仍感慨颇多,从理论到实践,在这段日子里,可以说得是苦多于甜,但是可以学到很多很多的东西,同时不仅可以巩固了以前所学过的知识,而且学到了很多在书本上所没有学到过的知识。通过这次课程设计使我懂得了理论与实际相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,从理论中得出结论,才能真正为社会服务,从而提高自己的实际动手能力和独立思考的能力。在设计的过程中遇到问题,可以说得是困难重重,但可喜的是最终都得到了解决。
本文来源:https://www.2haoxitong.net/k/doc/0436ecb7ad51f01dc281f1ba.html
文档为doc格式