Linux下进程间通讯实验

 (1)无名管道父子进程间通讯pipe()

 

 
 
  1. #include <unistd.h> 
  2. #include <sys/types.h> 
  3. #include <errno.h> 
  4. #include <stdio.h> 
  5. #include <stdlib.h> 
  6.  
  7. int main() 
  8.     int pipe_fd[2]; 
  9.     pid_t pid; 
  10.     char buf_r[100]; 
  11.     char* p_wbuf; 
  12.     int r_num; 
  13.      
  14.     memset(buf_r,0,sizeof(buf_r)); 
  15.      
  16.     /*创建管道*/ 
  17.     if(pipe(pipe_fd)<0) 
  18.     { 
  19.         printf("pipe create error\n"); 
  20.         return -1; 
  21.     } 
  22.      
  23.     /*创建子进程*/ 
  24.     if((pid=fork())==0)  //子进程 OR 父进程? 
  25.     { 
  26.         printf("\n"); 
  27.         close(pipe_fd[1]); 
  28.         sleep(2); /*为什么要睡眠*/ 
  29.         if((r_num=read(pipe_fd[0],buf_r,100))>0) 
  30.         { 
  31.             printf(   "%d numbers read from the pipe is %s\n",r_num,buf_r); 
  32.         }    
  33.         close(pipe_fd[0]); 
  34.         exit(0); 
  35.     } 
  36.     else if(pid>0) 
  37.     { 
  38.         close(pipe_fd[0]); 
  39.         if(write(pipe_fd[1],"Hello",5)!=-1) 
  40.             printf("parent write1 Hello!\n"); 
  41.         if(write(pipe_fd[1]," Pipe",5)!=-1) 
  42.             printf("parent write2 Pipe!\n"); 
  43.         close(pipe_fd[1]); 
  44.         sleep(3); 
  45.         waitpid(pid,NULL,0); /*等待子进程结束*/ 
  46.         exit(0); 
  47.     } 
  48.     return 0; 

 

(2)有名管道任意进程间通讯mkfifo()

 

 
 
  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <errno.h> 
  4. #include <fcntl.h> 
  5. #include <stdio.h> 
  6. #include <stdlib.h> 
  7. #include <string.h> 
  8. #define FIFO "/tmp/myfifo" 
  9.  
  10. main(int argc,char** argv) 
  11.     char buf_r[100]; 
  12.     int  fd; 
  13.     int  nread; 
  14.      
  15.     /* 创建管道 */ 
  16.     if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST)) 
  17.         printf("cannot create fifoserver\n"); 
  18.      
  19.     printf("Preparing for reading bytes...\n"); 
  20.      
  21.     memset(buf_r,0,sizeof(buf_r)); 
  22.      
  23.     /* 打开管道 */ 
  24.     fd=open(FIFO,O_RDONLY|O_NONBLOCK,0); 
  25.     if(fd==-1) 
  26.     { 
  27.         perror("open"); 
  28.         exit(1);     
  29.     } 
  30.     while(1) 
  31.     { 
  32.         memset(buf_r,0,sizeof(buf_r)); 
  33.          
  34.         if((nread=read(fd,buf_r,100))==-1) 
  35.         { 
  36.             if(errno==EAGAIN) 
  37.                 printf("no data yet\n"); 
  38.         } 
  39.         printf("read %s from FIFO\n",buf_r); 
  40.         sleep(1); 
  41.     }    
  42.     pause(); /*暂停,等待信号*/ 
  43.     unlink(FIFO); //删除文件 

 

 

 
 
  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <errno.h> 
  4. #include <fcntl.h> 
  5. #include <stdio.h> 
  6. #include <stdlib.h> 
  7. #include <string.h> 
  8. #define FIFO_SERVER "/tmp/myfifo" 
  9.  
  10. main(int argc,char** argv) 
  11.     int fd; 
  12.     char w_buf[100]; 
  13.     int nwrite; 
  14.          
  15.     /*打开管道*/ 
  16.     fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0); 
  17.      
  18.     if(argc==1) 
  19.     { 
  20.         printf("Please send something\n"); 
  21.         exit(-1); 
  22.     } 
  23.      
  24.     strcpy(w_buf,argv[1]); 
  25.      
  26.     /* 向管道写入数据 */ 
  27.     if((nwrite=write(fd,w_buf,100))==-1) 
  28.     { 
  29.         if(errno==EAGAIN) 
  30.             printf("The FIFO has not been read yet.Please try later\n"); 
  31.     } 
  32.     else  
  33.         printf("write %s to the FIFO\n",w_buf); 

 

(3)信号通讯signal()

 

 
 
  1. #include <signal.h> 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4.  
  5. void my_func(int sign_no) 
  6.     if(sign_no==SIGINT) 
  7.         printf("I have get SIGINT\n"); 
  8.     else if(sign_no==SIGQUIT) 
  9.         printf("I have get SIGQUIT\n"); 
  10. int main() 
  11.     printf("Waiting for signal SIGINT or SIGQUIT \n "); 
  12.      
  13.     /*注册信号处理函数*/ 
  14.     signal(SIGINT, my_func); 
  15.     signal(SIGQUIT, my_func); 
  16.      
  17.     pause();/*等待信号出现*/ 
  18.     exit(0); 

 

(4)共享内存实现通讯shmget(), shmat()

 

 
 
  1. #include <stdlib.h> 
  2. #include <stdio.h> 
  3. #include <string.h> 
  4. #include <errno.h> 
  5. #include <unistd.h> 
  6. #include <sys/stat.h> 
  7. #include <sys/types.h> 
  8. #include <sys/ipc.h> 
  9. #include <sys/shm.h> 
  10.  
  11. #define PERM S_IRUSR|S_IWUSR 
  12. /* 共享内存 */ 
  13.  
  14. int main(int argc,char **argv)  
  15. {  
  16.     int shmid;  
  17.     char *p_addr,*c_addr;  
  18.      
  19.     if(argc!=2)  
  20.     {  
  21.         fprintf(stderr,"Usage:%s\n\a",argv[0]);  
  22.         exit(1);  
  23.     } 
  24.  
  25.     /* 创建共享内存 */     
  26.     if((shmid=shmget(IPC_PRIVATE,1024,PERM))==-1)  
  27.     {  
  28.         fprintf(stderr,"Create Share Memory Error:%s\n\a",strerror(errno));  
  29.         exit(1);  
  30.     }  
  31.  
  32.     /* 创建子进程 */ 
  33.     if(fork()) // 父进程写 
  34.     {  
  35.         p_addr=shmat(shmid,0,0); /*中间的0表示系统自动分配地址,不为0则为指定的地址*/ 
  36.         memset(p_addr,'\0',1024); //p_addr为映射的地址 
  37.         strncpy(p_addr,argv[1],1024); 
  38.         wait(NULL); // 释放资源,不关心终止状态 
  39.         exit(0);  
  40.     }  
  41.     else       // 子进程读 
  42.     {  
  43.         sleep(1); // 暂停1秒        
  44.         c_addr=shmat(shmid,0,0);  
  45.         printf("Client get %p\n",c_addr);  
  46.         exit(0);  
  47.     }  
  48. }  

 

(5)消息队列 ftok(),msgget(),msgsnd(),msgrcv()

 

 
 
  1. #include <sys/types.h> 
  2. #include <sys/msg.h> 
  3. #include <unistd.h> 
  4.  
  5. struct msg_buf 
  6.     { 
  7.         int mtype; 
  8.         char data[255]; 
  9.     }; 
  10.   
  11. int main() 
  12.         key_t key; 
  13.         int msgid; 
  14.         int ret; 
  15.         struct msg_buf msgbuf; 
  16.   
  17.         key=ftok("/tmp/2",'a'); 
  18.         printf("key =[%x]\n",key); 
  19.         msgid=msgget(key,IPC_CREAT|0666); /*通过文件对应*/ 
  20.  
  21.         if(msgid==-1) 
  22.         { 
  23.                 printf("create error\n"); 
  24.                 return -1; 
  25.         } 
  26.   
  27.         msgbuf.mtype = getpid();//指定类型,为int,可随意,只要收和发的类型相同即可 
  28.         strcpy(msgbuf.data,"test haha"); 
  29.         ret=msgsnd(msgid,&msgbuf,sizeof(msgbuf.data),IPC_NOWAIT); 
  30.         if(ret==-1) 
  31.         { 
  32.                 printf("send message err\n"); 
  33.                 return -1; 
  34.         } 
  35.   
  36.         memset(&msgbuf,0,sizeof(msgbuf)); 
  37.         ret=msgrcv(msgid,&msgbuf,sizeof(msgbuf.data),getpid(),IPC_NOWAIT); 
  38.         if(ret==-1) 
  39.         { 
  40.                 printf("recv message err\n"); 
  41.                 return -1; 
  42.         } 
  43.         printf("recv msg =[%s]\n",msgbuf.data); 
  44.   

 

(6)信号量semget(),semop()

 

本文出自 “Mr~钟” 博客,请务必保留此出处http://6386296.blog.51cto.com/6376296/1121584

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值