操作系统课件:Linux的进程和进程间通信

上传人:汽*** 文档编号:569538857 上传时间:2024-07-30 格式:PPT 页数:23 大小:2.03MB
返回 下载 相关 举报
操作系统课件:Linux的进程和进程间通信_第1页
第1页 / 共23页
操作系统课件:Linux的进程和进程间通信_第2页
第2页 / 共23页
操作系统课件:Linux的进程和进程间通信_第3页
第3页 / 共23页
操作系统课件:Linux的进程和进程间通信_第4页
第4页 / 共23页
操作系统课件:Linux的进程和进程间通信_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《操作系统课件:Linux的进程和进程间通信》由会员分享,可在线阅读,更多相关《操作系统课件:Linux的进程和进程间通信(23页珍藏版)》请在金锄头文库上搜索。

1、LinuxLinux的进程和进程间通信的进程和进程间通信fork 函数:#include pid_t fork();当一个进程调用了fork 以后,系统会创建一个子进程。这个子进程和父进程不同的地方只有他的进程ID 和父进程ID,就象符进程克隆(clone)自己一样。进程创建进程创建区分父进程和子进程:跟踪fork返回值失败:-1否则父进程fork 返回子进程的IDfork 子进程返回0可根据这个返回值来区分父子进程父进程和子进程父进程和子进程父进程阻塞直到子进程完成任务调用wait 或者waitpid 系统调用#include #include pid_t wait(int *stat_lo

2、c);pid_t waitpid(pid_t pid,int *stat_loc,int options);阻塞阻塞wait 系统调用使父进程阻塞直到一个子进程结束或者是父进程接受到了一个信号如果父进程没有子进程或者子进程已经结束了,wait 回立即返回成功时(因一个子进程结束)wait 将返回子进程的ID,否则返回-1Waitpid: 等待指定的子进程直到子进程返回pid 为正值:等待指定的进程(pid)pid为0:等待任何一个组ID 和调用者的组ID 相同的进程pid=-1:等同于wait 调用。Pid-1:等待任何一个组ID 等于pid 绝对值的进程waitwait调用系统程序, 可以使

3、用系统调用exec 族调用。exec 族调用有着5 个函数:#include int execl(const char *path,const char *arg,.);int execlp(const char *file,const char *arg,.);int execle(const char *path,const char *arg,.);int execv(const char *path,char *const argv);int execvp(const char *file,char *const argv):调用系统程序调用系统程序#include #include

4、#include #include #include #include void main(void)pid_t child;int status;printf(This will demostrate how to get child statusn);if(child=fork()=-1) printf(Fork Error :%sn,strerror(errno); exit(1);例子例子1 1 else if(child=0) int i; printf(I am the child:%ldn,getpid(); for(i=0;i1000000;i+) i+; i=5; print

5、f(I exit with %dn,i); exit(i);while(child=wait(&status)=-1)&(errno=EINTR);例子例子1 1(续)(续)#include #include #include #include #include int main()int rtn; /*子进程的返回数值*/if ( fork() = 0 ) /* 子进程执行此命令 */execlp(/bin/ls,ls -al ,(char *)0);/* 如果exec函数返回,表明没有正常执行命令,打印错误信息*/exit( 1 );else /* 父进程, 等待子进程结束,并打印子进程的

6、返回值 */wait ( &rtn );printf( child process return %dn,. rtn );例子例子2 2主要手段:管道(Pipe)信号(Signal)消息(Message)共享内存(Shared memory)信号量(Semaphore)套接口(Socket)进程通信进程通信进程通信的另一个方法是使用共享内存。System V 提供了以下几个函数以实现共享内存:#include #include #include int shmget(key_t key,int size,int shmflg);void *shmat(int shmid,const void

7、*shmaddr,int shmflg);int shmdt(const void *shmaddr);int shmctl(int shmid,int cmd,struct shmid_ds *buf);size 是共享内存的大小shmat 是用来连接共享内存shmdt 是用来断开共享内存的共享内存共享内存#include #include #include #include #include #define PERM S_IRUSR|S_IWUSRint main(int argc,char *argv)int shmid;char *p_addr,*c_addr;if(argc!=2)p

8、rintf(Usage:%sna,argv0);exit(1);if(shmid=shmget(IPC_PRIVATE,1024,PERM)=-1)printf(Create Share Memory Errorna);exit(1);例子例子if(fork()p_addr=shmat(shmid,0,0);memset(p_addr,0,1024);strncpy(p_addr,argv1,1024);exit(0);elsec_addr=shmat(shmid,0,0);printf(Client get %s,c_addr);exit(0);例子(续)例子(续)为了便于进程之间通信, 可

9、以使用管道通信System V 提供的函数来实现进程的通信,这就是消息队列。#include ;#include ;#include ;int msgget(key_t key,int msgflg);int msgsnd(int msgid,struct msgbuf *msgp,int msgsz,int msgflg);int msgrcv(int msgid,struct msgbuf *msgp,int msgsz, long msgtype,int msgflg);int msgctl(Int msgid,int cmd,struct msqid_ds *buf);struct m

10、sgbuf long msgtype; /* 消息类型*/. /* 其他数据类型*/消息队列消息队列msgget 函数返回一个消息队列的标志msgctl 函数对消息进行控制msgsnd 和msgrcv 函数进行消息通讯msgid 是接受或者发送的消息队列标志msgp 是接受或者发送的内容msgsz 是消息的大小 结构msgbuf 包含的内容是至少有一个为msgtype,其他的成分是用户定义的。对于发送函数msgflg 指出缓冲区用完时候的操作,接收函数指出无消息时候的处理.一般为0. 接收函数msgtype 指出接收消息时候的操作:如果msgtype=0,接收消息队列的第一个消息大于0 接收队

11、列中消息类型等于这个值的第一个消息小于0 接收消息队列中小于或者等于msgtype 绝对值的所有消息中的最小一个消息消息队列(续)消息队列(续)#include #include #include #include #include #include #include #include #include #define MSG_FILE server.c#define BUFFER 255#define PERM S_IRUSR|S_IWUSRstruct msgtype long mtype;char bufferBUFFER+1;例子例子server.cserver.cint main()

12、struct msgtype msg;key_t key;int msgid;/*key_t ftok(char * pathname,char projid)据pathname和proj来创建一个关键字,*/*此关键字在创建信号量,创建消息队列的时候都需要使用。其中pathname必须是一*/*个存在的可访问的路径或文件,proj必须不得为0。*/if(key=ftok(MSG_FILE,a)=-1) printf(Creat Key Errorn); exit(1);if(msgid=msgget(key,PERM|IPC_CREAT|IPC_EXCL)=-1) printf(Creat

13、Message Errorn); exit(1);例子例子server.c(server.c(续)续)while(1) msgrcv(msgid,&msg,sizeof(struct msgtype),1,0); printf(Server Receive:%sn,msg.buffer); msg.mtype=2; msgsnd(msgid,&msg,sizeof(struct msgtype),0);exit(0);例子例子server.c(server.c(续)续)#include #include #include #include #include #include #include

14、#include #define MSG_FILE server.c#define BUFFER 255#define PERM S_IRUSR|S_IWUSRstruct msgtype long mtype;char bufferBUFFER+1;例子例子client.cclient.cint main(int argc,char *argv)struct msgtype msg;key_t key;int msgid;if(argc!=2) printf(Usage:%s stringna,argv0); exit(1);if(key=ftok(MSG_FILE,a)=-1) print

15、f(Creat Key Error:n); exit(1);例子例子client.c(client.c(续续) )if(msgid=msgget(key,PERM)=-1) printf(Creat Message Error:an); exit(1);msg.mtype=1;strncpy(msg.buffer,argv1,BUFFER);msgsnd(msgid,&msg,sizeof(struct msgtype),0);memset(&msg,0,sizeof(struct msgtype);msgrcv(msgid,&msg,sizeof(struct msgtype),2,0);printf(Client receive:%sn,msg.buffer);exit(0);例子例子client.c(client.c(续续) )

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号