linux复习资料_第1页
linux复习资料_第2页
linux复习资料_第3页
linux复习资料_第4页
linux复习资料_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

1、1、指令格式:指令名称 选择项 参数列表选择项以-开头通配符* :代表文件名中的任意长度的字符串;?:代表文件名中的任一字符 :代表文件名中的任一属于字符组中的字符2、3 gcc基本选项类型说明-E预处理后即停止,不进行编译、汇编及连接-S编译后即停止,不进行汇编及连接-c编译或汇编源文件,但不进行连接-o file指定输出文件file程序的编译要经历预处理、编译、汇编以及连接4个阶段。4、其他常用工具:putty、makefile、Emacs编辑器5、Linux系统中“一切皆文件”。Linux操作系统是以文件为基础而设计的,除了常规文件以外,目录、设备、管道等都属于文件。6、文件操作方式:#

2、define O_ACCMODE 0003 /主要访问权限位的低两位用来测试权限用#define O_RDONLY 00#define O_WRONLY 01#defein O_RDWR 02#define O_CREAT 0100#define O_EXCL 0200 /如果存在,返回错误#define O_NOCTTY 0400 /终端控制信息#define O_TRUNC 01000#define O_APPEND 020007、 文件打开与创建: if(fd_open=open("/bin/ls",O_RDONLY) = -1)perror("open&q

3、uot;);exit(EXIT_FAILURE); if(fd_open_create=open("./tmp",O_CREAT|O_EXCL,0644) = -1)perror("open");exit(EXIT_FAILURE);if(fd_create=creat("./tmp2",0644) = -1 )perror("open");exit(EXIT_FAILURE);8、 获取文件属性statint stat ( const char *file_name, struct stat *buf );int

4、 fstat ( int filedes, struct stat *buf );9、 opendir、closedir、readdirDIR *opendir ( const char *name );int closedir (DIR * dirp);struct dirent *readdir ( DIR * dir );10、进程及其状态进程就是程序的一次执行过程。进程至少要有三种基本状态。这三种基本状态是:运行态、就绪态和封锁态(或等待态)。进程转换:11、 条件:由于父子进程执行顺序的不确定性,当子进程先于父进程退出时,子进程会留下一些资源来记录运行的信息,以提供给父进程进行访问。

5、如果父进程没有调用wait或waitpid函数的话,则子进程将会一直保留这些信息,成为僵尸进程。如果父进程调用了wait函数,子进程就不会成为僵尸进程。孤儿进程是指因父亲进程先结束而导致一个子进程被init进程收养的进程。12、 可以通过以下方式结束进程。<1>、调用exit或_exit。<2>、在main函数中执行return。<3>隐含的离开main函数。Return与exit区别:return 退出当前函数主体,exit()函数退出当前进程,因此,在main函数里面return(0)和exit(0)完成一样的功能。return仅仅从子函数中返回,而子进

6、程用exit()退出,调用exit()时要调用一段终止处理程序,然后关闭所有I/O流。13、管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信;管道的实质是一个内核缓冲区,进程以先进先出的方式从缓冲区中存取数据:管道一端的进程顺序地将数据写入缓冲区,另一端的进程则顺序地读出数据。两个局限性:1)支持半双工;2)只有具有亲缘关系的进程之间才能使用这种无名管道;读写特殊性:1.当读一个写端已经关闭的管道时,在所有数据被读取之后,read函数返回值为0,以指示到了文件

7、结束处;2.如果写一个读端关闭的管道,则产生SIGPIPE信号。如果忽略该信号或者捕捉该信号并处理程序返回,则write返回-1,errno设置为EPIPE14、 父子进程通过无名管道通信elseclose(pipe_fd0);printf("please input the string:");scanf("%s",buf_w);if(write(pipe_fd1,buf_w,strlen(buf_w)!=-1)printf("parent process has written:%s to the pipe!n",buf_w);c

8、lose(pipe_fd1);waitpid(result,NULL,0);exit(0);#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <string.h>main()pid_t result;int r_num;int pipe_fd2;char buf_r100,buf_w100;memset(buf_r,0,sizeof(buf_r);if(pip

9、e(pipe_fd)<0)perror("pipe");exit(EXIT_FAILURE);result=fork();if(result<0)perror("fork");exit(EXIT_FAILURE);else if(result=0)close(pipe_fd1);if(r_num=read(pipe_fd0,buf_r,100)>0)printf("child process has read %d characters from the pipe,the string is:%sn",r_num,b

10、uf_r);close(pipe_fd0);exit(0);信号是Linux系统中用于进程之间相互通信或操作的一种机制。信号事件的发生有两个来源:1. 硬件来源:用户按某些终端键时将产生信号,如CTRL+C将产生SIGINT(中止信号);硬件异常产生信号,如除数为0或无效的存储访问等。2. 软件来源:终止进程信号,其他进程调用kill函数,将信号发送个另一个进程或进程组;软件异常产生信号。信号处理信号注销信号注册内核进程用户进程信号产生*生命周期(3)设计一个程序,要求打开文件“pass”,如果没有这个文件,新建此文件;读取系统文件“/etc/passwd”,把文件中的内容都写入“pass”文

11、件。#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>#include <sys/types.h>int main(int agrs,char *args) int fd_src,fd_des,num;char buf10; if(fd_des=open("pass",O_CREAT|O_WRONLY|O_TRUNC,0644)<0) perror("open or create file failed

12、n"); exit(EXIT_FAILURE);if(fd_src=open("/etc/passwd",O_RDONLY,0644)=-1) perror("open passwd file failedn");exit(EXIT_FAILURE); donum =read(fd_src,buf,10);write(fd_des,buf,num);while(num >= 10);close(fd_src); close(fd_des); return 0;(4)设计一个程序,要求新建一个目录,预设权限为d-x-x-x。 #includ

13、e<stdio.h> #include<dirent.h> #include<sys/types.h> #include<sys/stat.h>#include <fcntl.h>#include <stdlib.h>int main(int argc,char *argv)DIR *dir;int fd; if(fd=mkdir("/home/student/dir",0644)=-1) perror("directory created failed!"); exit(EXIT_

14、FAILURE); if(chmod("dir",S_IXUSR|S_IXGRP|S_IXOTH)<0)perror("chomd");exit(EXIT_FAILURE);return 0;. 实现两个进程间使用消息队列进行通信/接受消息#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#include<errno.h>#include<sys/msg.h>#define BUFIZE

15、 1024struct msg_stlong int msg_type;char textBUFIZE;int main()int running=1;int msgid=-1;struct msg_st data;long int msgtype=0;/创建消息队列msgid=msgget(key_t)1234,0666|IPC_CREAT);if(msgid=-1)fprintf(stderr,"msgget failed with error:%dn",errno);exit(EXIT_FAILURE);/从队列中获取消息,直到遇到end消息为止while(runni

16、ng)if(msgrcv(msgid,(void *)&data,BUFIZE,msgtype,0)=-1)fprintf(stderr,"msgget failed with error:%dn",errno);exit(EXIT_FAILURE);printf("You wrote %sn",data.text);/遇到end结束if(strncmp(data.text,"end",3)=0)running=0;/删除消息队列if(msgctl(msgid,IPC_RMID,0)=-1)fprintf(stderr,&qu

17、ot;msgctl(IPC_RMID)failedn");exit(EXIT_FAILURE);exit(EXIT_SUCCESS);return 0;/发送消息#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<sys/msg.h>#include<errno.h>#define MAX_TEXT 512#define BUFSIZE 1024struct msg_stlong int msg_type;char textMAX_TEXT;int ma

18、in()nt running=1;struct msg_st data;char bufferBUFSIZE;int msgid=BUFSIZE;/建立消息队列msgid=msgget(key_t)1234,0666|IPC_CREAT);if(msgid=-1)fprintf(stderr,"msgget failed with error:%dn",errno);exit(EXIT_FAILURE);/向消息队列中写消息,直到写入end结束while(running)/写入数据printf("Enter some text:");fgets(buff

19、er,BUFSIZE,stdin);data.msg_type=1;strcpy(data.text,buffer);/消息队列发送消息if(msgsnd(msgid,(void *)&data,MAX_TEXT,0)=-1)fprintf(stderr,"msgsnd failedn");exit(EXIT_FAILURE);/输入end结束if(strncmp(buffer,"end",3)=0)running=0;sleep(1);exit(EXIT_SUCCESS);return 0; 2. 上机调试课件中killrecever和kill

20、sender程序。/killrecever#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <signal.h>/SIGUSR1信号处理函数void CbSigUsr1(int signo) /输出接收到的信号信息printf("nreceive signal=%d.n",signo);void main()/安装SIGUSR1信号if(signal(SIGUSR1,CbSigUsr1)=SIG_ERR)perror("signal

21、");return;printf("my pid is %dn",getpid(); printf("waiting for SIGUSR1.n");/暂停,等待信号 pause();/killsender#include <stdio.h>#include <signal.h>#include <unistd.h>#include <stdlib.h>void fun_ctrl_c();int main()(void)signal(SIGINT,fun_ctrl_c);printf("

22、Now I'm starting.n");while(1)printf("this is an endless loop unless Ctrl+c are pressd!n");sleep(2);exit(0);void fun_ctrl_c()printf("tCtrl+c were pressed!n");printf("t This is just an example for signal function!n");printf("treset signal SIGINTn");(voi

23、d)signal(SIGINT,SIG_DFL)1. 编程实现由一个父进程产生3个子进程,并且给三个子进程传递不同的参数,收到a的子进程在显示器上输出乘法口诀表;收到b的子进程计算10!;收到c的进程计算1+2+3+19+20的结果。 /计算10!#include<stdio.h> int main()int fac=1,i; for(i=1;i<=10;i+) fac*=i;if(i<10)printf("%d*",i); Else printf("%d=",i); printf("%dn",fac);ret

24、urn 0;/乘法口诀表#include <stdio.h>int main()int i,j,result=0;for(i=1;i<=9;i+)or(j=1;j<=i;j+)result=i*j;printf("%d*%d=%d ",i,j,result);printf("n");return 0;/计算1+2+3+19+20的结果#include<stdio.h> int main()int sum=0,i; for(i=1;i<=20;i+) sum+=i;if(i<20)printf("%

25、d+",i); Else printf("%d=",i); printf("%dn",sum); return 0;/主进程#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>int main(int argc,char *argv ) int flag;pid_t pid;int i=1;int sum=0;int fac=1; while(i<4) pid=fork(); if(pid=-1)

26、 perror("fork");exit(EXIT_FAILURE); switch(*argvi) case 'a': if(pid=0)printf("this is 1 child processn");for(int i=1;i<=9;i+)for(int j=1;j<=i;j+)result=i*j;printf("%d*%d=%d ",i,j,result);printf("n");break;case 'b': if(pid=0) sleep(1);prin

27、tf("this is 2 child processn");for(int i=1;i<=10;i+) fac*=i;(i<10)printf("%d*",i);else printf("%d=",i);printf("%dn",fac); break; case 'c': if(pid=0) sleep(2); printf("this is 3 child processn"); for(int i=1;i<=20;i+) sum+=i; if(i<

28、20)printf("%d+",i);else printf("%d=",i); printf("%dn",sum); break;default: exit(EXIT_FAILURE); / if(i!=3) return 0; i+; return 0;2. execve函数的应用,要在程序执行时设定环境变量,路径为tmp,用户为liu,执行命令env时把这些环境变量传递给系统。在这一函数中,参数e表示可传递新进程环境变量,参数v表示传递的参数(含命令)为构造指针数组,文件查找需给出路径。命令env在“/bin”目录下。把环境变量

29、设定为: char *envp=“PATH=/tmp”,”USER=liu”,NULL;参数的构造指针数组为:char *arg=“env”,NULL;因而此函数的调用形式为:execve(“/bin/env”,”env”,envp);请编写程序进行调试。 #include<stdio.h>#include<stdlib.h>#include<unistd.h>int main()char *env="env",NULL; char *envp="PATH=/tmp","USER=liu",NULL

30、; execve("/bin/env",env,envp);return 0;使用POSIX IO实现计算文件大小#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <fcntl.h>int my_callen ( int fd ) int len; if ( lseek( fd , 0 , SEEK_END ) = -1 ) perror( "lseek“); if ( ( len = lseek ( fd , 0 , SEEK_CUR )

31、 ) = -1 ) perror( "lseek“); return len;int main ( int argc , char * argv ) int len = -1; int fd; if ( argc < 2 ) perror ( "para“) ; if ( ( fd = open ( argv1 , O_RDONLY ) ) =-1 ) perror ( "open“) ; exit(EXIT_FAILURE); len = my_callen ( fd ) ; close(fd); if(len<0) perror ( "c

32、al“) ;elseprintf ( "There are %d bytes in file %s!n“ , len , argv1 ); return 0; 列出目录下的文件信息#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <dirent.h>#include <errno.h>int main ( int argc, char * argv ) if ( argc < 2 ) printf ( "usage:my_g

33、etdir dirnamen“ ) ; exit ( 0 ); if ( my_read ( argv1 ) < 0 ) exit ( EXIT_FAILURE ); return 0;int my_read ( const char * path ) DIR *dir ; struct dirent *ptr; if ( ( dir = opendir ( path ) ) = NULL ) perror (" opendir“);exit(EXIT_FAILURE); while ( ( ptr = readdir ( dir ) ) != NULL ) printf (

34、"file inode :%ldtt“ , ptr->d_ino ); printf ( "file name :%sn“ , ptr->d_name ); closedir ( dir ); return 0;例:生成多个子进程#include <stdio.h>#include <stdlib.h>#include <unistd.h>main() pid_t pid; int i=1;while(i<9) pid=fork();if(pid=0) printf("I'm no. %d proces

35、s, my pid is %d.n", i, getpid();if(i!=1)return 0;i+;由于父子进程执行顺序的不确定性,当子进程先于父进程退出时,子进程会留下一些资源来记录运行的信息,以提供给父进程进行访问。如果父进程没有调用wait或waitpid函数的话,则子进程将会一直保留这些信息,成为僵尸进程。如果父进程调了wait函数,子进程就不会成为僵尸进程。用孤儿进程是指因父亲进程先结束而导致一个子进程被init进程收养的进程。signal的应用#include <stdio.h>#include <signal.h>#include <u

36、nistd.h>#include <stdlib.h>void fun_ctrl_c();int main()(void)signal(SIGINT,fun_ctrl_c);printf("Now I'm starting.n");while(1)printf("this is an endless loop unless Ctrl+c are pressd!n");sleep(2);exit(0);void fun_ctrl_c()printf("tCtrl+c were pressed!n");print

37、f("t This is just an example for signal function!n");printf("treset signal SIGINTn");(void)signal(SIGINT,SIG_DFL);创建消息队列#include <stdio.h>#include <sys/ipc.h>#include <sys/types.h>int main()key_t key;key=ftok(".",1);printf("the key is %xn",ke

38、y);int id;id=msgget(key+1,IPC_CREAT|0666);printf("id=%dn",id);system(“ipcs”);删除消息队列#include <stdio.h>#include <sys/ipc.h>#include <sys/types.h>int main()key_t key;int id;key=ftok(".",1);id=msgget(key+1,IPC_CREAT|0666);printf("id=%dn",id);msgctl(id,IPC_

39、RMID,NULL);sytem(“ipcs”);将数组str中的数据写入文件strtext.txt中,每行一条数据;随后,将文件中的数据读入到数组out中,输出out数组的数据。#include <stdio.h>#include <stdlib.h>main() char str15="January","February","March","April","May", "June","July","August&qu

40、ot;,"september","October","November","December"char out1215;FILE *fp;int i;fp=fopen("strtext.txt","w");if(fp=NULL)printf("cant open file!n");exit(1); fwrite(str,15,12,fp); fclose(fp); printf("write data success!n");fp=fo

41、pen("strtext.txt","r"); if(fp=NULL)printf("cant open file!n"); exit(1);fread(out,15,12,fp); fclose(fp);printf("read data success!n"); printf("the data in array out are:n"); for(i=0;i<12;i+) printf("%sn",outi);fscanf函数实现scanf函数的功能#include

42、 <stdio.h>const int MAX_N = 21;int main()int i;char x;FILE *myfile = fopen("test.txt", "r"); if (myfile) for (i = 1; i <= MAX_N; i+) fscanf(myfile, "%c", &x);printf("%c", x); return 0;将文件描述符转换为流对象#include <stdio.h>#include <stdlib.h>#i

43、nclude <unistd.h>#include <fcntl.h>#include <sys/stat.h>main() int fd;FILE *stream;unlink("test.txt"); fd=open("test.txt",O_CREAT|O_WRONLY,S_IREAD|S_IWRITE)stream=fdopen(fd,"w");if(stream=NULL) printf("fdopen failedn");else fprintf(stream, &q

44、uot;Hello worldn"); fclose(stream);printf("the content of the test.txt is:n"); system("cat test.txt"); return 0; 文件的创建,打开#include <fcntl.h>#include <stdio.h>#include <stdlib.h>main(int argc, char * argv ) int fd_open, fd_open_create, fd_create; if(fd_open=o

45、pen("/bin/ls",O_RDONLY) = -1)perror("open");exit(EXIT_FAILURE); printf ( "the file's descriptor is: %dn " , fd_open ); if(fd_open_create=open("./tmp",O_CREAT|O_EXCL,0644) = -1)perror("open");exit(EXIT_FAILURE); printf ( " the tmp1 file descr

46、iptor is: %dn" ,fd_open_create);if(fd_create=creat("./tmp2",0644) = -1 )perror("open");exit(EXIT_FAILURE); printf("the tmp2 file descriptor is: %dn",fd_create);close(fd_open); close(fd_open_create); close(fd_create); return 0;文件读写#include <stdio.h>#include &l

47、t;stdlib.h>#include <unistd.h>int main() char buf80; int n; n=read(STDIN_FILENO,buf,80);if(n<0) perror("read STDIN_FILENO"); exit(EXIT_FAILURE); write(STDOUT_FILENO,buf,n); printf("n");return 0;使用POSIX IO实现文件拷贝 #include <stdio.h>#include <stdlib.h>#include

48、 <fcntl.h>#include <string.h>#include <sys/types.h>main(int argc, char * argv) int fd_src,fd_des; char buf128;int num;if(argc!=3) printf("tje format must be: cp file_src file_des");exit(EXIT_FAILURE); if(fd_src=open(argv1,O_RDONLY)=-1) perror("open1");exit(EXIT_

49、FAILURE); if(fd_des=open(argv2,O_CREAT|O_EXCL|O_WRONLY,0644)=-1) perror("open2");exit(EXIT_FAILURE); do num=read(fd_src,buf,128);write(fd_des,buf,num);while(num=128);close(fd_src); close(fd_des);父子进程的交替执行int main() pid_t pid; printf(“Lets study fork function!n”); pid=fork(); switch(pid) ca

50、se -1: perror(“fork failure”); break; case 0: printf(“hellow, Im child process, my pid is %d, my parent is %d!n”, getpid(), getppid(); break;default:printf(“hi, Im parent process, my pid is %d!n”,getpid(); break; exit(0);父子进程读写文件#include <stdlib.h>#include <stdio.h>#include <unistd.h&

51、gt;#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>int main() int fd; pid_t pid; char buf45;if ( ( fd = open ( “code”, O_CREAT|O_RDWR|O_TRUNC, S_IRWXU ) ) < 0 ) perror ( "open error“ ); exit ( 0 ); pid = fork ( );switch ( pid ) case -1: perror ( "fork erro

52、r“ ); break; case 0: printf ( "child processn“ ); if (write(fd,"I'm child process. Daddy, do you see me?“,41 )<0) perror ( "child error“ ); exit ( 0 ); break; default : sleep ( 6 ); printf ( "nparent processn“ ); lseek ( fd, 0, SEEK_SET); if ( read ( fd, buf, 41) < 0 )

53、perror ( "parent error“ ); exit ( 0 ); printf("n%sn",buf); close(fd); break; 生成多个子进程#include <stdio.h>#include <stdlib.h>#include <unistd.h>main() pid_t pid; int i=1; while(i<9) pid=fork();if(pid=0) printf("I'm no. %d process, my pid is %d.n", i, get

54、pid(); if(i!=1)return 0;i+;产生一个孤儿进程#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>int main ( ) pid_t pid; int k=3; pid = fork ( ); switch ( pid ) case -1: perror ( "fork errorn“ ); break;case 0 : while ( k > 0 ) printf ( "I'm ch

55、ild process, my PID is %d, my parent is %d.n", getpid ( ), getppid ( ) );sleep(1); k-;break;default : printf ( "I'm parent process, my PID is %d.n", getpid ( ) ); break; exit与return的区别C语言关键字与函数exit()在main函数退出时有相似之处,但两者有本质的区别:return 退出当前函数主体,exit()函数退出当前进程,因此,在main函数里面return(0)和exi

56、t(0)完成一样的功能。return仅仅从子函数中返回,而子进程用exit()退出,调用exit()时要调用一段终止处理程序,然后关闭所有I/O流。守护进程的实现 #include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <signal.h>#include <fcntl.h>#include <sys/syslog.h>#include<sys/param.h>#include<sys/types.h>#include <

57、;sys/stat.h>int init_daemon(const char *pname,int facility) int pid;int i;signal(SIGTTOU,SIG_IGN);signal(SIGTTIN,SIG_IGN);signal(SIGTSTP,SIG_IGN);signal(SIGHUP,SIG_IGN);pid=fork();if(pid>0)exit(EXIT_SUCCESS);else if(pid<0)perror("fork");exit(EXIT_FAILURE);setsid();pid=fork();if(pi

58、d>0)exit(EXIT_SUCCESS);else if(pid<0)perror("fork");exit(EXIT_FAILURE);for(i=0;i<NOFILE;i+)close(i);open("/dev/null",O_RDONLY);open("/dev/null",O_RDWR);open("/dev/null",O_RDWR);chdir("/");umask(0);signal(SIGCHLD,SIG_IGN);openlog(pname,LOG_PI

59、D,facility);return;main(int argc,char * argv) FILE *fp;time_t ticks;init_daemon(argv0,LOG_KERN);while(1)sleep(1);ticks=time(NULL);syslog(LOG_INFO,"%s",asctime(localtime(&ticks);父子进程通过无名管道通信#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include

温馨提示

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

评论

0/150

提交评论