版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实战演练参考答案第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 10. #netstat -all第2章 C语言编程基础1.#include <stdio.h>int main(void) printf("*n&
2、quot;); 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 <stdio.h>int main(void) int x,y; printf("please input
3、x: n"); scanf("%d",&x); if(x<1) y=x; else if(x>=1&&x<10) y=2*x-1; else y=3*x-8; printf("y = %d",y); return 0;4.#include<stdio.h> int main(void) double f,c; int a; printf("please input the type number of Temp:n"); printf("0 : Fahrenhe
4、it -> 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<stdio.h> int ma
5、in(void) int i,j,m,n; double a5;double b5; double s=0; for(i=0; i<5; i+) scanf("%lf",&ai); for(j=0; j<5; j+) bj=1.0/aj; for(m=0; m<5; m+) printf("%f ",bm); for(n=0; n<5; n+) s = s + bn; printf("n %f n",s); return 0;6.#include <stdio.h>int main(void
6、) 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;j<=3;j+) if(aij>max) 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(n<0) printf("n<0, dataerr
7、or!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 <stdio.h> int main() double a1000; double s=0; int i; int z; for(i=0;i+)
8、 if(!scanf("%lf",&ai) break; for(z=0;z<i;z+) s = s + az; s=s/i; printf("%f",s);9.#include <stdio.h> #include <string.h>struct address char name30; int phone;int main(void) int m,n; char a30; struct address stu3; for(m=0; m<3; m+) scanf("%s",&st
9、); scanf("%d",&stum.phone); scanf("%s",&a); for(n=0; n<3; n+) if(!strcmp(a,) printf("%s,%dn",,stun.phone); return 0;10.#include<stdio.h>node *insert_node(node *head, node *item) /head != NULL node *p = head; node *q = NULL; /始终指
10、向p之前的节点 while(p->data < item->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之
11、后 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);
12、/把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 < head2->data ) head = head1 ; head->next = MergeRecursive(head1->next,head2); else head
13、= 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表示从光标所
14、在行的行首开始插入新的字符a表示从光标所在位置的下一个字符开始插入新的输入字符A表示从光标所在行的行尾开始插入新的字符o表示新增加一行,并将光标移动到下一行的开头开始插入字符O表示在当前行的上面新增加一行,并将光标移动到上一行的开头开始插入字符4. #vi /usr/src/linux-8/kernel/fork.c5. #vi /usr/src/linux-8/kernel/fork.c 键入“/”进入底行模式,然后输入字符串“fork”,键入回车后便会自动查找当前文档中的“fork” 字符串。6. 略(见光盘中的视频讲解)7. 略(见光盘中的视频讲解)8. 使用“C-x 2”快捷键可将当前
15、窗口均分为两个工作窗口9. #Emacs /root/install.log 键入“C-s”进入Emacs的搜索模式,然后再输入想要查找的字符“i”10. 键入“M-!”,Emacs进入Shell模式,此时便可在Emacs的命令行输入ls、pwd、who、uname等命令并运行之。第4章 gcc编译器与gdb调试器1.#include <stdio.h>main() int i,j,k; for(i=1; i<=4; i+) for(j=1; j<=4; j+) if(i>=j) k=i+j; printf("%d+%d=%d ",i,j,k)
16、; printf("n"); 2. 比如头文件myhead.h存放在/home/zhangfan/include目录下,则应使用下面的编译命令:# gcc main.c -I /home/zhangfan/include -o main3. 略(见光盘中的视频讲解)4. #gcc v #gdb5. 改正后的程序如下:#include <stdio.h>main() int num1,num2; printf("Input two numbers: "); scanf("%d %d",&num1,&num2)
17、; max(num1, num2);int max(int i, int j) if(i>j) 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这个目标为伪目标:phonya
18、ll : 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)第
19、6章 文件I/O操作1. #ls -l > filelist2. #pwd >> filelist3. #xxx 2>> filelist4.#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <string.h>#define FLAGS O_RDONLY /*定义flags:只读方式*/#define PATHNAME "/usr/src/linux-8/init/ma
20、in.c"int 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 <sys/types.h>#include <sys/stat.h>#inc
21、lude <fcntl.h>#include <unistd.h>#include <stdio.h>#include <string.h>#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; /*
22、文件描述符*/ 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
23、); if(write(fd, write_buf, strlen(write_buf)=-1) printf("error,write file failed!n"); exit(1); /*写出错,退出*/ return 0;6.#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#define FLAGS O_RDWR /*定义flags:读写方式*/#define FILENAME "filelist&q
24、uot;int 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 <sys/types.
25、h>#include <sys/stat.h>#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
26、 <sys/types.h>#include <unistd.h>#include <stdio.h>#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&
27、quot;); return 0;9.#include <stdio.h>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 <unistd.h>int main(
28、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 <stdio.h>int main(void) printf("stdin is "); if(stdin->_flag
29、s & _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
30、_buf_base); /*打印缓冲区的大小*/ printf("file discriptor is %dnn", fileno(stdin); /*标准输入流的文件描述符*/ return 0;2.#include <stdio.h>#include <fcntl.h>int main(void) FILE *fp; if(fp = fopen("/root/install.log", "r") = NULL) printf("fail to open!n"); exit(1); /*出
31、错退出*/ printf("open successfuly!n"); fclose(fp); return 0;3.#include <stdio.h>#include <stdlib.h>#define Src_File "/home/zhangfan/hello"#define Des_File "/home/zhangfan/tmp"int main(void) FILE *fp1, *fp2; /*源文件和目标文件的文件指针*/ int c; /*要进行输入和输出的字符*/ if(fp1 = fope
32、n(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 (&quo
33、t;fail to writen"); exit(1); /*出错退出*/ fclose(fp1); /*操作完毕,关闭源文件和目标文件*/ fclose(fp2); return 0;第4题参见程序7.65.#include <stdio.h>#define File_path "/home/zhangfan/tmp"int main(void) FILE *fp; /*指向操作文件的文件指针*/ char buf ="Linux C program." fp=fopen(File_path,"w"); /*以
34、只写方式打开文件*/ fprintf(fp, "%s", buf); /*向该文件流输出字符串数据buf*/ fclose(fp); /*关闭文件流*/ return 0;第6题参见程序7.67.#include <stdio.h>#define File_path " "int main(void)FILE *fp;char buf 1024;fp=fopen(File_path, "r");fscanf(fp,"%s",&buf);fclose(fp);printf("%sn&qu
35、ot;,buf);return 0;第8章 进程控制1. #ps # ps -aux2. kill3.#include <sys/types.h>#include <unistd.h>main()printf("The current process ID is %dn",getpid();4.#include <sys/types.h>#include <unistd.h>#include <stdio.h>int main (void)pid_t pid; /*此时仅有一个进程*/ if(pid=fork()
36、< 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 <sys/types.h>#include <unistd.h>#include <stdio.h>int main (void)i
37、nt 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
38、 the count is: %d, and the child is: %dn", getpid(), count, child); return 0;6.#include <stdlib.h>int main(void)printf("Hello, world!n"); exit(0); printf("Hello, world again!n");7.#include <stdlib.h> int main(void) printf("nparent process, output beginn"
39、;); printf("parent process, content in buffer"); /*没有换行符n*/ _exit(0); 8.#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdio.h>int main(void) pid_t pc,pr; if (pc = fork() < 0) printf("error in fork!"); exit(1); /* fork出错退出*/ els
40、e if(pc=0) /*子进程*/ printf("This is child process with pid of %dn",getpid(); sleep(10); /*睡眠10秒钟*/ else /*父进程*/ pr=wait(NULL); /*在这里等待*/ exit(0);9.#include <stdlib.h>#include <stdio.h>int main(void) int status; if (status = system("date") < 0) printf("system er
41、ror!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 <sys/types.h>#include <unistd.h>main()printf("The current pr
42、ocess ID is %dn",getpgrp();第9章 信号1. #kill -l2.#include <signal.h>#include <stdio.h>void SignHandler(int iSignNum) printf("Capture signal number:%dn",iSignNum); int main(void) signal(SIGQUIT,SignHandler); while(1) sleep(1); return 0;3.#include <signal.h>#include <s
43、tdio.h>int main(void) signal(SIGQUIT,SIG_IGN); while(1) sleep(1); return 0;4.#include <stdio.h>#include <string.h>#include <unistd.h>#include <signal.h>int g_iSeq=0;void SignHandlerNew(int iSignNo,siginfo_t *pInfo,void *pReserved) int iSeq=g_iSeq+; printf("%d Enter Si
44、gnHandlerNew,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
45、(SIGQUIT,&act,NULL); return 0;5.#include<unistd.h>#include<signal.h>#include<sys/types.h>#include<sys/wait.h>#include<stdio.h>int 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
46、, 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 <sys/types.h>#include <signal.h>int main(void) printf("Hello, I like Linux C programs!n"); if(rais
47、e(SIGABRT) = -1) printf("raise failed!n"); exit(1); printf("Hello, I like Linux C programs,again!n"); return 0;7.#include<stdio.h>#include<signal.h>#include<unistd.h>void SigHandler(int signo,siginfo_t *info,void *context) char *pMsg=(char*)info->si_value.siv
48、al_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);
49、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<unistd.h>#include<signal.h>void handler()printf("Hello, I like Linux C programs!n
50、");int main(void)int i;signal(SIGALRM,handler);alarm(3);for(i=1;i<5;i+)printf("sleeping %d .n",i);sleep(1); return 0;9.#include <stdio.h>#include <string.h>#include <unistd.h>#include <signal.h>int g_iSeq=0;void SignHandlerNew(int iSignNo,siginfo_t *pInfo,vo
51、id *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; sigempty
52、set(&sigSet); sigaddset(&sigSet,SIGINT); sigprocmask(SIG_BLOCK,&sigSet,NULL); sigemptyset(&act.sa_mask); sigaction(SIGINT,&act,NULL); /*注册SIGINT信号*/ sigaction(SIGQUIT,&act,NULL); /*注册SIGQUIT信号*/ return 0;10.#include <signal.h> #include <stdio.h>#include <unistd.h>void SignHandler(int iSignNo) printf("signal:%dn",iSignNo)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 氢气专项施工方案(3篇)
- 沟渠围挡施工方案(3篇)
- 厂房总施工方案(3篇)
- 中秋售卖活动策划方案(3篇)
- 制作柜子施工方案(3篇)
- 零售行业库存管理指南(标准版)
- 培训机构如何续班
- 2025年中职数控管理(管理技术)试题及答案
- 2025年大学单板滑雪平行大回转运动与管理(平行大回转技术)试题及答案
- 2025年高职会计(高级财务会计)试题及答案
- (人教2024版)数学四年级上册第8单元《数学广角-优化》大单元教学课件
- 临床生物化学检验练习题库(含答案)
- G -B- 15607-2023 涂装作业安全规程 粉末静电喷涂工艺安全(正式版)
- (正式版)SHT 3229-2024 石油化工钢制空冷式热交换器技术规范
- 2018年4月自考00265西方法律思想史试题及答案含解析
- 小红书创业计划书
- 青岛版六年级上册分数乘除混合运算练习400题及答案
- 英语1-10-数字课件
- 保险合同纠纷起诉状
- 重庆市大渡口区2023年九年级第一次适应性检测数学试题【含答案】
- GB/T 42506-2023国有企业采购信用信息公示规范
评论
0/150
提交评论