




已阅读5页,还剩26页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
31 实战演练参考答案第1章 Linux系统概述1. 略(见光盘中的视频讲解)2. #who#uname -r3. #echo $SHELL# /bin/csh4. #mkdir /home/temp5. #cp /root/hello /home/temp/hello6. #chmod 755 /home/temp/hello7. 略(见光盘中的视频讲解)8. $HOME Hello World9. #ifconfig eth0 00 netmask gw 10. #netstat -all第2章 C语言编程基础1.#include int main(void) printf(*n); printf( Hello, Linux world! n); printf(*n); return 0;2.main() char ch; scanf(%c,&ch); ch=(ch=A&ch=Z)?(ch+32):ch; printf(%c,ch);3.#include int main(void) int x,y; printf(please input x: n); scanf(%d,&x); if(x=1&x10) y=2*x-1; else y=3*x-8; printf(y = %d,y); return 0;4.#include int main(void) double f,c; int a; printf(please input the type number of Temp:n); printf(0 : Fahrenheit - Celsiusn); printf(1 : Celsius - Fahrenheitn); scanf(%d,&a); if(a=0) scanf(%lf,&f); printf(%lfn,(f-32)*(5/9.0); else scanf(%lf,&c); printf(%lfn,(c*1.8)+32); return 0;5.#include int main(void) int i,j,m,n; double a5;double b5; double s=0; for(i=0; i5; i+) scanf(%lf,&ai); for(j=0; j5; j+) bj=1.0/aj; for(m=0; m5; m+) printf(%f ,bm); for(n=0; n5; n+) s = s + bn; printf(n %f n,s); return 0;6.#include int main(void) int i,j,row=0,colum=0,max; int a34 = 1,2,3,4,9,8,7,6,-10,10,-5,2; max=a00; for(i=0;i=2;i+) for(j=0;jmax) max=aij; row=i; colum=j; printf(max = %d,row = %d, colum = %dn,max,row,colum); return 0;7.float func(int n) float f; if(n0) printf(n0, dataerror!n); else if(n=0|n=1) f=1; else f=func(n-1)*n; return f;main() int n; float y; printf(pleale input an integer:); scanf(%d,&n); y=func(n); printf(%d! = %10.0f,n,y);8.#include int main() double a1000; double s=0; int i; int z; for(i=0;i+) if(!scanf(%lf,&ai) break; for(z=0;zi;z+) s = s + az; s=s/i; printf(%f,s);9.#include #include struct address char name30; int phone;int main(void) int m,n; char a30; struct address stu3; for(m=0; m3; m+) scanf(%s,&); scanf(%d,&stum.phone); scanf(%s,&a); for(n=0; n3; n+) if(!strcmp(a,) printf(%s,%dn,,stun.phone); return 0;10.#includenode *insert_node(node *head, node *item) /head != NULL node *p = head; node *q = NULL; /始终指向p之前的节点 while(p-data data & p != NULL) q = p; p = p-next; if (p = head) /插入到原头节点之前 item-next = p; return item; q-next = item; item-next = p; return head; /* 两个有序链表进行合并 */ node *merge(node* head1, node* head2) node* head; /合并后的头指针 node *p; node *nextP; /指向p之后 if ( head1 = NULL ) /有一个链表为空的情况,直接返回另一个链表 return head2; else if ( head2 = NULL ) return head1; / 两个链表都不为空 if(length(head1) = length(head2) /选取较短的链表 /这样进行的插入次数要少些 head = head1; p = head2; else head = head2; p = head1; while(p != NULL) nextP = p-next; /保存p的下一个节点 head = insert_node(head, p); /把p插入到目标链表中 p = nextP; /指向将要插入的下一个节点 return head; node *MergeRecursive(node *head1, node *head2) node *head = NULL; if (head1 = NULL) return head2; if (head2 = NUL) return head1; if ( head1-data data ) head = head1 ; head-next = MergeRecursive(head1-next,head2); else head = head2 ; head-next = MergeRecursive(head1,head2-next); return head ; int main(void) node *head1 = create(); /创建单链表1 node *head2 = create(); /创建单链表2 node *head = MergeRecursive(head1, head2); print(head); return 0; 第3章 vi与Emacs编辑器1. 略(见光盘中的视频讲解)2. 如图3.2所示3. 如表3.5所示:i表示从光标所在的位置开始插入新的字符I表示从光标所在行的行首开始插入新的字符a表示从光标所在位置的下一个字符开始插入新的输入字符A表示从光标所在行的行尾开始插入新的字符o表示新增加一行,并将光标移动到下一行的开头开始插入字符O表示在当前行的上面新增加一行,并将光标移动到上一行的开头开始插入字符4. #vi /usr/src/linux-2.4.20-8/kernel/fork.c5. #vi /usr/src/linux-2.4.20-8/kernel/fork.c 键入“/”进入底行模式,然后输入字符串“fork”,键入回车后便会自动查找当前文档中的“fork” 字符串。6. 略(见光盘中的视频讲解)7. 略(见光盘中的视频讲解)8. 使用“C-x 2”快捷键可将当前窗口均分为两个工作窗口9. #Emacs /root/install.log 键入“C-s”进入Emacs的搜索模式,然后再输入想要查找的字符“i”10. 键入“M-!”,Emacs进入Shell模式,此时便可在Emacs的命令行输入ls、pwd、who、uname等命令并运行之。第4章 gcc编译器与gdb调试器1.#include main() int i,j,k; for(i=1; i=4; i+) for(j=1; j=j) k=i+j; printf(%d+%d=%d ,i,j,k); printf(n); 2. 比如头文件myhead.h存放在/home/zhangfan/include目录下,则应使用下面的编译命令:# gcc main.c -I /home/zhangfan/include -o main3. 略(见光盘中的视频讲解)4. #gcc v #gdb5. 改正后的程序如下:#include main() int num1,num2; printf(Input two numbers: ); scanf(%d %d,&num1,&num2); max(num1, num2);int max(int i, int j) if(ij) return(i); else return(j);6. (gdb)print k7. (gdb)display k8. (gdb)info line main.c:max第5章 make的使用和Makefile的编写1.all : main.c fun1.c fun2.c fun3.cgcc main.c fun1.c fun2.c fun3.c -o all2. clean:rm -f *.o3. 用“.PHONY : phonyall”声明了phonyall这个目标为伪目标:phonyall : prog1 prog2 prog3.PHONY : phonyall4. 使用“$”符号表示引用变量值:var= Hello, Linux C programecho $var5.$(subst e, E, sweat will become to sweet)6.$(wordlist 2, 8, I like Linux C programs)7.$(notdir /root/install.log /home/zhangfan/hello.txt hello.c)8.$(suffix hello.c foo.s fun.o usr/src/linux-2.4/Makefile)第6章 文件I/O操作1. #ls -l filelist2. #pwd filelist3. #xxx 2 filelist4.#include #include #include #include #include #define FLAGS O_RDONLY /*定义flags:只读方式*/#define PATHNAME /usr/src/linux-2.4.20-8/init/main.cint main(void) int fd; /*文件描述符*/ if(fd=open(PATHNAME, FLAGS)=-1) /*调用open函数*/ printf(error,open file failed!n); exit(1); /*出错退出*/ printf(OK,open file successful!n); printf(fd=%dn,fd); return 0;5.#include #include #include #include #include #include #define FILENAME /home/zhangfan/test /*要进行写操作的文件*/#define SIZE 80 /*定义缓冲区大小*/#define FLAGS O_RDWR | O_APPEND#define MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH int main(void) int fd; /*文件描述符*/ char write_bufSIZE; /*写缓冲区*/ const char *pathname = FILENAME; /*指向需要打开文件的路径名*/ if(fd=open(pathname,FLAGS,MODE)=-1) /*调用open函数打开文件*/ printf(error,open file failed!n); exit(1); /*出错退出*/ printf(OK,open file successful!n); printf(Begin write:n); gets(write_buf); if(write(fd, write_buf, strlen(write_buf)=-1) printf(error,write file failed!n); exit(1); /*写出错,退出*/ return 0;6.#include #include #include #include #define FLAGS O_RDWR /*定义flags:读写方式*/#define FILENAME filelistint main(void) int fd; /*文件描述符*/ if(fd=open(FILENAME, FLAGS)=-1) /*调用open函数*/ printf(error,open file failed!n); exit(1); /*出错退出*/ if(lseek(fd, 10, SEEK_SET)=-1) printf(lseek error!n); exit(1); /*出错退出*/ else printf(lseek OK!n); exit(0);7.#include #include #define PATHNAME /home/zhangfan/temp#define MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH int main(void) if(mkdir(PATHNAME, MODE)=-1) printf(make dir error!n); exit(1); else printf(make dir successfuly!n); return 0;8.#include #include #include #define MODE 0755/*定义参数MODE :文件所有者读、写、执行;组读、执行;其他读、执行方式*/int main(void) if(chmod(filelist,MODE) = -1) /*调用chmod函数改变文件权限*/ printf(error,change failed!n); exit(1); /*出错退出*/ printf(OK,change successful!n); return 0;9.#include int main(void) if(rename(/home/zhangfan/hello.txt, /home/zhangfan/temp.txt)=-1) printf(error, rename failed!n); exit(1); /*出错退出*/ printf(OK, rename successful!n); return 0;10.#include int main(void) if(link(/home/zhangfan/hello.txt, /root/hello.txt)=-1) printf(error, link failed!n); exit(1); /*出错退出*/ printf(OK, link successful!n); return 0;第7章 基于流的I/O操作1.#include int main(void) printf(stdin is ); if(stdin-_flags & _IO_UNBUFFERED) /*判断标准输入流对象的缓冲区类型*/ printf(unbufferedn); /*无缓存*/ else if(stdin-_flags & _IO_LINE_BUF) printf(line-bufferedn); /*行缓存*/ else printf(fully-bufferedn); /*全缓存*/ printf(buffer size is %dn, stdin-_IO_buf_end - stdin-_IO_buf_base); /*打印缓冲区的大小*/ printf(file discriptor is %dnn, fileno(stdin); /*标准输入流的文件描述符*/ return 0;2.#include #include int main(void) FILE *fp; if(fp = fopen(/root/install.log, r) = NULL) printf(fail to open!n); exit(1); /*出错退出*/ printf(open successfuly!n); fclose(fp); return 0;3.#include #include #define Src_File /home/zhangfan/hello#define Des_File /home/zhangfan/tmpint main(void) FILE *fp1, *fp2; /*源文件和目标文件的文件指针*/ int c; /*要进行输入和输出的字符*/ if(fp1 = fopen(Src_File, rb) = NULL) /*以只读方式打开源文件*/ printf(fail to open source filen); exit(1); /*出错退出*/ if(fp2 = fopen(Des_File, wb) = NULL) /*以只写方式打开目标文件*/ printf (fail to open des filen); exit(1); while(c = fgetc(fp1) != EOF) if(fputc(c, fp2) = EOF) printf (fail to writen); exit(1); /*出错退出*/ fclose(fp1); /*操作完毕,关闭源文件和目标文件*/ fclose(fp2); return 0;第4题参见程序7.65.#include #define File_path /home/zhangfan/tmpint main(void) FILE *fp; /*指向操作文件的文件指针*/ char buf =Linux C program.; fp=fopen(File_path,w); /*以只写方式打开文件*/ fprintf(fp, %s, buf); /*向该文件流输出字符串数据buf*/ fclose(fp); /*关闭文件流*/ return 0;第6题参见程序7.67.#include #define File_path /root/install.log.syslog int main(void)FILE *fp;char buf 1024;fp=fopen(File_path, r);fscanf(fp,%s,&buf);fclose(fp);printf(%sn,buf);return 0;第8章 进程控制1. #ps # ps -aux2. kill3.#include #include main()printf(The current process ID is %dn,getpid();4.#include #include #include int main (void)pid_t pid; /*此时仅有一个进程*/ if(pid=fork() 0) printf(error in fork!); exit(1); /* fork出错退出*/ else if(pid=0) printf(Child process ID is %dn, getpid(); else printf(Parent process ID is %dn, getpid(); return 0;5.#include #include #include int main (void)int count = 1; int child; if(child= vfork() 0) printf(error in vfork!); exit(1); /* fork出错退出*/ if(child=0) /*子进程*/ printf(This is son, his pid is: %d and the count is: %dn, getpid(), +count); exit(1); else /*父进程*/ printf(After son, This is father, his pid is: %d and the count is: %d, and the child is: %dn, getpid(), count, child); return 0;6.#include int main(void)printf(Hello, world!n); exit(0); printf(Hello, world again!n);7.#include int main(void) printf(nparent process, output beginn); printf(parent process, content in buffer); /*没有换行符n*/ _exit(0); 8.#include #include #include #include int main(void) pid_t pc,pr; if (pc = fork() 0) printf(error in fork!); exit(1); /* fork出错退出*/ else if(pc=0) /*子进程*/ printf(This is child process with pid of %dn,getpid(); sleep(10); /*睡眠10秒钟*/ else /*父进程*/ pr=wait(NULL); /*在这里等待*/ exit(0);9.#include #include int main(void) int status; if (status = system(date) 0) printf(system error!n); exit(0); if (status = system(ls) 0) printf(system error!n); exit(0); if (status = system(pwd) 0) printf(system error!n); exit(0); return 0;10.#include #include main()printf(The current process ID is %dn,getpgrp();第9章 信号1. #kill -l2.#include #include void SignHandler(int iSignNum) printf(Capture signal number:%dn,iSignNum); int main(void) signal(SIGQUIT,SignHandler); while(1) sleep(1); return 0;3.#include #include int main(void) signal(SIGQUIT,SIG_IGN); while(1) sleep(1); return 0;4.#include #include #include #include int g_iSeq=0;void SignHandlerNew(int iSignNo,siginfo_t *pInfo,void *pReserved) int iSeq=g_iSeq+; printf(%d Enter SignHandlerNew,signo:%d.n,iSeq,iSignNo); sleep(3); /*睡眠3秒钟*/ printf(%d Leave SignHandlerNew,signo:%dn,iSeq,iSignNo);int main(void) struct sigaction act; act.sa_sigaction=SignHandlerNew; act.sa_flags=SA_SIGINFO; sigemptyset(&act.sa_mask); sigaction(SIGINT,&act,NULL); sigaction(SIGQUIT,&act,NULL); return 0;5.#include#include#include#include#includeint main(void)pid_t pid;int status;if(!(pid= fork()printf(Hi I am child process!n);sleep(10); printf(Hi I am child process, again!n);return;elseprintf(send signal to child process (%d) n,pid);sleep(1);if(kill(pid ,SIGABRT) = -1) printf(kill failed!n); return 0;6.#include #include int main(void) printf(Hello, I like Linux C programs!n); if(raise(SIGABRT) = -1) printf(raise failed!n); exit(1); printf(Hello, I like Linux C programs,again!n); return 0;7.#include#include#includevoid SigHandler(int signo,siginfo_t *info,void *context) char *pMsg=(char*)info-si_value.sival_ptr; printf(Receive signal number:%dn, signo); printf(Receive Message:%sn, pMsg);int main(void) struct sigaction sigAct; sigAct.sa_flags=SA_SIGINFO; sigAct.sa_sigaction=SigHandler; if(sigaction(SIGUSR1,&sigAct,NULL)=-1) printf(sigaction failed!n); exit(1); sigval_t val; char pMsg =I like Linux C programs!; val.sival_ptr = pMsg; if(sigqueue(getpid(),SIGUSR1,val) = -1) printf(sigqueue failed!n); exit(1); sleep(3); return 0;8.#include#includevoid handler()printf(Hello, I like Linux C programs!n);int main(void)int i;signal(SIGALRM,handler);alarm(3);for(i=1;i5;i+)printf(sleeping %d .n,i);sleep(1); return 0;9.#include #include #include #include int g_iSeq=0;void SignHandlerNew(int iSignNo,siginfo_t *pInfo,void *pReserved) int iSeq=g_iSeq+; printf(%d Enter SignHandlerNew,signo:%d.n,iSeq,iSignNo); sleep(3); printf(%d Leave SignHandlerNew,signo:%dn,iSeq,iSignNo); int main(void) struct sigaction act; act.sa_sigaction=SignHandlerNew; act.sa_flags=SA_SIGINFO; sigset_t sigSet; sigemptyset(&sigSet); sigaddset(&sigSet,SIGINT); sigprocmask(SIG_BLOCK,&sigSet,NULL); sigemptyset(&act.sa_mask); sigaction(SIGINT,&act,NULL); /*注册SIGINT信号*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 甘肃省会宁县第五中学2026届化学高二上期末检测试题含答案
- 2025秋季学期勤工俭学合同
- 河北省唐山市唐县第一中学2026届高二化学第一学期期末教学质量检测试题含答案
- 在线平台合伙协议
- 出版物订阅协议
- 2025购车合同协议书范本
- 2025年高级月嫂试题及答案
- 线上品牌推广雇佣协议
- 商铺品牌维护机制协议
- 2025年公办中小学编制教师招聘面试(科学)模拟题及答案
- 2024年度软件即服务(SaaS)平台租赁合同3篇
- 网络攻防原理与技术 第3版 教案 -第12讲 网络防火墙
- 2024年新课标培训2022年小学英语新课标学习培训课件
- 2024小学语文教学及说课课件:二年级上册《田家四季歌》
- 2024至2030年中国聚脲涂料行业市场发展调研及投资前景分析报告
- 1.1 鸦片战争 课件 2024-2025学年统编版八年级历史上册
- 2024至2030年中国演播室行业市场调查研究及发展战略规划报告
- DB11∕T 420-2019 电梯安装、改造、重大修理和维护保养自检规则
- 国旗台施工合同
- 总代理授权书
- 医疗器械售后服务能力证明资料模板
评论
0/150
提交评论