河南科技大学Linux教学ppt第7章_第1页
河南科技大学Linux教学ppt第7章_第2页
河南科技大学Linux教学ppt第7章_第3页
河南科技大学Linux教学ppt第7章_第4页
河南科技大学Linux教学ppt第7章_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

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

文档简介

第7章Linux环境编程

主要内容系统调用和库函数简介文件操作进程管理和同步进程通信内存管理

7.1系统调用和库函数

7.1.1系统调用系统调用是操作系统提供的、与用户程序之间的接口,也就是操作系统提供给程序员的接口从感觉上系统调用类似于过程调用,都由程序代码构成,使用方式相同两者有实质差别:过程调用只能在用户态下运行,不能进入核心态;而系统调用可以实现从用户态到核心态的转变。系统调用可大致分为五个类别:进程控制、文件管理、设备管理、信息维护和通信7.1.2库函数7.1.2库函数它们本身并不属于操作系统的内核部分库函数可以分为下面六大类:①文件管理②状态信息③文件修改④程序设计语言的支持⑤程序装入和执行⑥通信7.1.3调用方式例如,open系统调用可以打开一个指定文件,其函数原型说明如下:#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>intopen(constchar*path,intoflags);不同的系统调用所需要的头文件(又称前导文件)是不同的。

7.2文件操作

7.2.1有关文件操作的系统调用

常用的有关文件操作的系统调用有:creat,open,close,read,write,lseek,link,unlink,mkdir,rmdir,chdir,chmod等例如:#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>intcreat(constchar*pathname,mode_tmode);#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>intopen(constchar*path,intoflags);intopen(constchar*path,intoflags,mode_tmode);7.2.2应用示例

/*rdwr.c-Thereadandwritesystemcalls*/#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>intmain(void){intfd1,fd2,fd3,nbytes;intflags=O_CREAT|O_TRUNC|O_WRONLY;charbuf[10];if((fd1=open("rdwr.c",O_RDONLY,0644))<0){perror("openrdwr.c");exit(EXIT_FAILURE);}if((fd2=open("/dev/null",O_WRONLY))<0){perror("open/dev/null");exit(EXIT_FAILURE);}

if((fd3=open("/tmp/foo.bar",flags,0644))<0){perror("open/tmp/foo.bar");close(fd1);close(fd2);exit(EXIT_FAILURE);}while((nbytes=read(fd1,buf,10))>0){if(write(fd2,buf,10)<0)perror("write/dev/null");if(write(fd3,buf,nbytes)<0)perror("write/tmp/foo.bar");write(STDOUT_FILENO,buf,10);}close(fd1);close(fd2);close(fd3);exit(EXIT_SUCCESS);}

7.3进程控制

7.3.1有关进程控制的系统调用常用的有关进程控制的系统调用有:fork,exec,wait,exit,getpid,sleep,nice等例如:#include<unistd.h>#include<sys/types.h>pid_tfork(void);#include<unistd.h>#include<sys/types.h>pid_tgetpid(void);pid_tgetppid(void);7.3.2应用示例/*proc1.c演示有关进程操作*/#include<unistd.h>#include<sys/types.h>#include<stdio.h>#include<errno.h>intmain(intargc,char**argv){pid_tpid,old_ppid,new_ppid;pid_tchild,parent;parent=getpid(); /*获得本进程的PID*/if((child=fork())<0){fprintf(stderr,"%s:forkofchildfailed:%s\n",argv[0],strerror(errno));exit(1);}

elseif(child==0){ /*此时是子进程被调度运行*/old_ppid=getppid();sleep(2);new_ppid=getppid();}else{sleep(1);exit(0); /*父进程退出*/}/*下面仅子进程运行*/printf("Originalparent:%d\n",parent);printf("Child:%d\n",getpid());printf("Child'soldppid:%d\n",old_ppid);printf("Child'snewppid:%d\n",new_ppid);exit(0);}

7.4进程通信

Linux下进程间通信的几种主要手段是:管道(pipe)及有名管道(namedpipe)、信号(signal)、消息(message)、共享内存(sharedmemory)、信号量(semaphore)和套接口(socket)。

7.4.1有关进程通信的函数例如:#include<unistd.h>intpipe(intfiledes[2]);#include<sys/types.h>#include<sys/stat.h>intmkfifo(constchar*pathname,mode_tmode);7.4.2应用示例/*pipedemo.c演示使用管道机制进行I/O控制*/#include<unistd.h>#include<stdio.h>#include<errno.h>intmain(intargc,char**argv){staticconstcharmesg[]="HappyNewYearstoyou!";charbuf[BUFSIZ];size_trcount,wcount;intp_fd[2];size_tn;if(pipe(p_fd)<0){/*创建管道*/fprintf(stderr,"%s:pipefailed:%s\n",argv[0],strerror(errno));exit(1);}printf("Readend=fd%d,writeend=fd%d\n",p_fd[0],p_fd[1]);n=strlen(mesg);

if((wcount=write(p_fd[1],mesg,n))!=n){/*写入数据*/fprintf(stderr,"%s:writefailed:%s\n",argv[0],strerror(errno));exit(1);}if((rcount=read(p_fd[0],buf,BUFSIZ))!=wcount){/*读出数据*/fprintf(stderr,"%s:readfailed:%s\n",argv[0],strerror(errno));exit(1);}buf[rcount]='\0';printf("Read<%s>frompipe\n",buf);close(p_fd[0]);close(p_fd[1]);return0;}

7.5内存管理

#include<stdlib.h>void*malloc

温馨提示

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

评论

0/150

提交评论