期末考核题目汇总_第1页
期末考核题目汇总_第2页
期末考核题目汇总_第3页
期末考核题目汇总_第4页
期末考核题目汇总_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上期末考核内容每人按照老师的要求,顺序或随即从下面shell编程部分和环境编程部分分别选择一题,然后在课堂上进行编程、演示运行结果,并回答老师至少三个问题。如不能回答,则不通过考核。通过考核后,应根据两个题目的题目分析、编程思想、相关知识、流程图、算法设计(如有)、源代码及注释,完成实践课程报告,并打印后提交。老师根据平时考勤、演示结果、回答问题情况、实验报告情况打分。定于第九周周四上午进行考核。请各位同学做好准备。Shell编程部分:1、 设计一个程序cuts,它由标准输入读取数据,获取有第一个参数n和第二个参数m所限定范围的数据,n和m都是整数 ,即从输入的字符串中

2、抽取第n个字符至第m个字符之间的所有字符(包括这两个字符)并显示出来。$ cuts 11 14This is a test of cuts program(回车)test(显示结果)P145 4.18#!/bin/bashread -p "请输入一段内容:" TEXTread -p "请输入两个整数,中间用空格分开:" num1 num2echo $TEXT | cut -b $num1-$num2 2>/dev/null2、 利用for循环将当前目录下的.c文件移到指定的目录下,并按文件大小排序,显示移动后指定目录的内容。P144 4.12#!/

3、bin/bash for file in ls -1 /root/a | grep ".*.c" mv /root/a/$file /root/b ls -lS /root/b3、 编写一个shell脚本,求斐波那契奇数列的前10项及总和。P144 4.14#!/bin/bashi=1j=0for ( k=0; k<10; k+ )do echo -n "$i " let m=i let i+=j let j=mdoneecho ""4、 编写一个shell脚本,程序运行时,要求输入两个数字。然后判断数字的大小,如果两个数字的和

4、大于100,就输出“x+y>100”;如果两个数字的和小于等于100并且大于10,就输出“x+y>10”;如果x>y,则求出x除以y的余数并输出“x mod y = ?(把计算的值代替问号)”,否则求出y除以x的余数并输出“y mod x = ?”.P127 例4.135、 编写一个shell脚本,并利用函数实现数列求和运算。即主程序接受两个数字,分别作为数列头和数列尾,如果第一个数字不小于第二个数字则输出“wrong number”,否则将这两个数字传递给函数;函数把这两个数字中间的数字加起来求和,并输出。如:当输入 3 6时,函数计算 3+4+5+6的值并输出。#!/bi

5、n/shfun() sum=0 for i in seq $1 $2 do sum=expr $i + $sum done echo $sumecho "Please input 2 number:"read a bexpr $a + 0 1>/dev/null 2>&1if $? -ne 0 ;then echo "wrong number" exit 1;fiexpr $b + 0 1>/dev/null 2>&1if $? -ne 0 ;then echo "wrong number" e

6、xit 1;fiif $a -ge $b ;then echo "wrong number" exit 1;fifun $a $b6、 编写特洛伊木马shell脚本。以文本方式登陆界面,接收用户输入的名字和密码。一旦用户在这种伪装界面登陆,就获取用户名和密码,并保存在指定的文件中。接着睡眠几秒,然后显示录入错误的信息,使用户以为输入有误。最后在调用真正的登陆程序,允许用户正常登陆。P107 例4.67、 编写shell脚本,创建一个5个元素的数组,输出各个元素的值8、 编写一个脚本,用于统计所指定文件的行数总数。输出结果为各个文件的名字+行数,以及总行数。如:当输入./脚本

7、名 file1 file2,脚本计算file1和file2两个文件的总行数#!/bin/bash#statistics the raws of the files you inputusage()  echo "usage:please input >= two filenames"totalline=0if $# -lt 2 ; then  usagefiwhile $# -ne 0 doline=cat $1 |wc -lecho "$1:$line"totalline=$ $totalline+$line shiftdon

8、eecho "-"echo "totalline: $totalline"9、 编写一个shell脚本,根据键盘输入的值,给出对应的提示;输入A Z ,输出Uppercase Letter;输入a-z,输出Lowercase Letter;输入0-9,输出Digit;输入其他,输出Punctuation, whitespace, or other#!/bin/bashexport LC_ALL=Cread awhile  $a do   case $a i

9、n   A-Z) echo "Uppercase Letter"   a-z) echo "Lowercase Letter"   0-9) echo "Digit"   *) echo "Punctuation, whitespace, or other" 

10、  esac   read adone10、 编写shell脚本,提示用户输入用户名,并判断此用户名是否存在与系统用户中P123 例4.11Linux环境编程部分:1、 编写一个程序,把一个文件的内容复制到另一个文件上,即实现简单的copy功能,要求:只用open()/read()/write()/close()系统调用,程序的第一个参数为源文件,第二个参数为目的文件。P225 7.3#include <stdio.h>#include <stdlib.h>#include <unistd.h>#i

11、nclude <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define BUF_SIZE 1024*8int main() int fds, fdd; char bufBUF_SIZE; size_t hasread = 0; fds = open("filea", O_RDONLY); fdd = open("fileb", O_WRONLY, O_CREAT); if(fds && fdd) while(hasread = read(fd

12、s, buf, sizeof(buf) > 0) write(fdd, buf, hasread); close(fds); close(fdd); 2、 编写一个程序它利用fork()创建一个子进程;父进程打开一个文件,父子进程都向文件中写入信息(利用write),表明是在那个进程中;每个进程都打印两个进程的ID号。最后父进程执行wait()。P225 7.5#include<stdio.h>#include<unistd.h>#include<malloc.h>#include<fcntl.h>#include<string.h&

13、gt;#include<stdlib.h>int main()pid_t pid;int fd;char *buf1=(char *)malloc(20);char *buf2=(char *)malloc(20);strcpy(buf1,"this is child process:");strcpy(buf2,"this is parent process:");if(fd=open("file",O_CREAT|O_RDWR)<0)printf("open file error");if(p

14、id=fork()=0)if(write(fd,buf1,strlen(buf1)<0)printf("write error!");exit(0);printf("pid:%d",getpid();close(fd);elseif(write(fd,buf2,strlen(buf1)<0)printf("write error!");exit(0);close(fd);wait(NULL);printf("ppid:%d",getppid();return 0;3、 编写一个程序,他创建一个子进程。父

15、进程向子进程发送一个信号,然后等待子进程终止;子进程接受信号,输出自己的状态信息,最后终止自己。P225 7.9#include<stdio.h>#include<unistd.h>#include<malloc.h>#include<fcntl.h>#include<string.h>#include<stdlib.h>int main()pid_t pid;int fd;char *buf1=(char *)malloc(20);char *buf2=(char *)malloc(20);strcpy(buf1,&qu

16、ot;this is child process:");strcpy(buf2,"this is parent process:");if(fd=open("file",O_CREAT|O_RDWR)<0)printf("open file error");if(pid=fork()=0)if(write(fd,buf1,strlen(buf1)<0)printf("write error!");exit(0);printf("pid:%d",getpid();close(

17、fd);elseif(write(fd,buf2,strlen(buf1)<0)printf("write error!");exit(0);close(fd);wait(NULL);printf("ppid:%d",getppid();return 0;4、 写管道的程序打开管道,main函数里的参数由用户输入要写入的内容。读管道创建管道,读出了用户写入用户写入管道的内容。这两个函数用的是非阻塞度写管道5、 打开open函数创建的文件,然后对此文件进行读写操作(将文件打开属性改为可读可写,文件权限要做相应更改)。接着,写入“Hello!I am

18、writing to this file!”,此时文件指针位于文件尾部。接着再使用lseek函数将文件指针移动文件开始处,并读出10个字节并将其打印出来#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h> int main()   &#

19、160;  int fd = -1;     fd = open("zhidao_.dat", O_CREAT | O_TRUNC | O_RDWR, 0666);    if (fd < 0)         perror(&quo

20、t;open");        return -1;            char buff64;    strcpy(buff, "Hello!I am writing to this file!");   

21、60;int count = strlen(buff);    if (write(fd, buff, count) < 0)         perror("write");        return -1;     &

22、#160;      if (lseek(fd, 0, SEEK_SET) < 0)         perror("lseek");        return -1;         

23、60;  if (read(fd, buff, 10) < 0)         perror("read");        return -1;            buff10 =

24、60;0x00;    printf("%sn", buff);    if (fd > 0)         close(fd);        fd = -1;        &#

25、160;   return 0;6、 首先建立一个守护进程,然后让该守护进程每隔10s在/tmp/dameon.log中写入一句话#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>#define MAXFILE 65535int main(void) p

26、id_t pc; int i,fd,len; char *buf="This is a Dameonn" len=strlen(buf); pc=fork(); if(pc<0) printf("error forkn"); exit(1); else if(pc>0) exit(0); setsid(); chdir("/"); umask(0); for(i=0;i<MAXFILE;i+) close(i); while(1) if(fd=open("/tmp/dameon.log",O_CR

27、EAT|O_WRONLY|O_APPEND,0600)<0) perror("open:"); exit(1); write(fd,buf,len+1); close(fd); sleep(10); 7、 在tmp目录下调用open函数,以可读可写的方式创建hello.c文件。open函数带有3个flag参数:O_CREATE、O_TRUNC、O_WRONLY,文件权限设置为0600。8、 首先创建一个子进程,然后让其子进程暂停5s(使用sleep函数)。接下来对原有的父进程使用waitpid函数,并使用参数WNOHANG使该父进程不会阻塞。若有子进程退出,则wait

28、pid返回子进程号;若没有子进程退出,则waitpid返回0,并且父进程每隔1s循环判断一次9、 首先使用fork创建一个子进程,接着为了保证子进程不在父进程调用kill之前退出,在子进程中使用raise函数向子进程发送SIGSTOP信号,使子进程暂停。接下来,再在父进程中调用kill向子进程发送信号,请使用SIGKIL10、 首先创建一个共享内存区,之后将其映射到本进程中,最后再解除这种映射关系。/*创建共享内存*/        int shmid;     &

29、#160;  void *shmadd;        if (shmid = shmget(IPC_PRIVATE,BUFSZ,0666) < 0 )             perror(“fail to shmget ”);         exit(-1);

30、0;           syetem(“ipcs m”);         /*映射共享内存*/        if (shmadd = shmat(shmid,0,0) = NULL )           

31、;  perror(“fail to shmat ”);         exit(-1);            syetem(“ipcs m”);         /*删除共享内存*/       

32、0;if (shmdt(shmadd) < 0 )            perror(“fail to shmdt ”);         exit(-1);            syetem(“ipcs m”);    

33、;     if (shmctl(shmid, IPC_RMID, NULL) < 0 )            perror(“fail to shmctl ”);         exit(-1);           

34、  syetem(“ipcs m”);11、 首先把SIGQUIT、SIGINT两个信号加入信号集,然后将信号集设为阻塞状态,并在该状态下使程序暂停5s.接下来再将信号集设置为非阻塞状态,再对这两个信号分别操作,其中SIGQUIT执行默认操作,而SIGINT执行用户自定义函数的操作。#include <signal.h>#include <unistd.h>#include <sys/types.h>void my_op(int);main()        sigse

35、t_t new_mask;        struct sigaction act;        sigemptyset(&act.sa_mask);        act.sa_flags=0;        act.sa_handler=my_op;    

36、0;   if(sigaction(SIGINT,&act,NULL)   /注册信号SIGINT处理函数为my_op                        printf("install signal SIGINT errorn");     

37、0;          if(sigaction(SIGQUIT,&act,NULL) /注册信号SIGQUIT处理函数为my_op                        printf("install signal SIGQUIT errorn"); 

38、               printf("please send signal: kill -s %d %dn",SIGQUIT, getpid();        sigemptyset(&new_mask);        if(sigprocmask(0, NULL,&new_mas

39、k) < 0) /查询当前进程被挂起的信号存入new_mask中,                                            

40、                               /可用sigpending(&new_mask)代替               

41、0;        printf("get pending mask errorn");                sigaddset(&new_mask,SIGQUIT);               /将信号SIG

42、QUIT,添加到信号集new_mask中,                                              

43、60;                            /也就是在原有的信号掩码中加入信号SIGQUIT。         sigsuspend(&new_mask);                          /将当前的信号掩码替换成new_mask,                          

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论