Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7723417
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2010-10-30 08:23:43

/***************

管道创建与读写

***************/

 

#include

#include

#include

#include

#include

#define size 256

 

int main()

{

      pid_t pid;

      int fd[2];

      int status;

      char buf[size]="Hello Word\n";

      if(pipe(fd)<0)

      {

           printf("pipe error\n");

           exit(0);

      }

      pid=fork();

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

      {

           close(fd[0]);

           write(fd[1],buf,sizeof(buf));

      }

      else

      {

           close(fd[1]);

           read(fd[0],buf,sizeof(buf));

           printf("Received message from child process:\n%s",buf);

           if(pid!=wait(&status))

           {

                 printf("waite error\n");

                 exit(1);

           }   

           printf("Done!\n");

      }

}

 

 

/***************

复制文本文件

***************/

 

#include

#include

#include

#include

#include

#define size 256

 

int main(int argc, char **argv)

{

      pid_t pid;

      FILE *fp;

      int fd[2];

      int status;

      char buf[size];

      if(argc!=3)

      {

           printf("too  few arguments\n");

           exit(0);

      }

      if(pipe(fd)<0)

      {

           printf("pipe error\n");

           exit(0);

      }

      pid=fork();

      if(pid<0)

      {

           printf("fork error\n");

           exit(1);

      }

      if(pid==0)

      {

           fp=fopen(argv[1],"r");

           if(fp==NULL)

           {

                 printf("fopen read error\n");

                 exit(1);

           }

          

           while(fgets(buf,sizeof(buf),fp)!=NULL)

           {

                 close(fd[0]);

                 write(fd[1],buf,sizeof(buf));

           }

          

           fclose(fp);

           strcpy(buf,'~');

           close(fd[0]);

           write(fd[1],buf,sizeof(buf));        

      }

      else

      {

           fp=fopen(argv[2],"w");

           if(fp==NULL)

           {

                 printf("fopen wite error\n");

                 exit(1);

           }

           close(fd[1]);

           read(fd[0],buf,sizeof(buf));

           while('~'!=buf[0])       //?

           {

                 fputs(buf,fp);

                 close(fd[1]);

                 read(fd[0],buf,sizeof(buf));

           }

          

           fclose(fp);

           if(pid!=wait(&status))

            {

                 printf("waite error\n");

                 exit(1);

           }   

           printf("Done!\n");

      }

}

 

 

/***************

创建命名管道

***************/

 

#include

#include

#include

#include

#include

 

int main(int argc, char **argv)

{

      mode_t mode=0750; 

      int status;  

      if(argc!=2)

      {

           printf("too  few arguments\n");

           exit(0);

      }

      status=mkfifo(argv[1],mode);

      if(status<0)

      {

           printf("mkfifo erro \n");

           exit(0);

      }   

      else

      {

           printf("fifo create success\n");

      }   

      printf("Done!\n");

      return 0;

}

 

 

 

/***************

命名管道之间通信1

***************/

 

#include

#include

#include

#include

#include

#include

#define size 256

int main(int argc, char **argv)

{

      int fd; 

      int status;  

      char buf[size];

      if(argc!=2)

      {

           printf("too  few arguments\n");

           exit(0);

      }

      status=mkfifo(argv[1],0777);

      if(status<0)

      {

           printf("mkfifo erro \n");

           exit(0);

      }   

     

      fd=open(argv[1],O_RDWR);

     

      if(fd<0)

      {

           printf("open error");

           exit(0);

      }

      printf("Server\n");

      printf("input the massage:");

      fgets(buf,sizeof(buf),stdin);

      write(fd,buf,sizeof(buf));

      printf("send\n");

      unlink(argv[1]);    

      printf("Done!\n");

      return 0;

}

 

 

 

/***************

命名管道之间通信2

***************/

 

#include

#include

#include

#include

#define size 256

 

int main(int argc, char **argv)

{

      int fd;       

      char buf[size];

      if(argc!=2)

      {

           printf("too  few arguments\n");

           exit(0);

      }   

      fd=open(argv[1],O_RDONLY);  

      if(fd<0)

      {

           printf("open error");

           exit(0);

      }

      printf("Clinet:\n");

      read(fd,buf,sizeof(buf));  

      printf("Received message:%s\n",buf);          

      printf("Done!\n");

      return 0;

}

 

 

/**********************

非阻塞方式命名管道之间通信

***********************/

 

#include

#include

#include

#include

#include

#define size 256

 

int main(int argc, char **argv)

{

      int fd,num;     

      char buf[size];

      if(argc!=2)

      {

           printf("too  few arguments\n");

           exit(0);

      }   

      fd=open(argv[1],O_RDWR|O_NONBLOCK);

      if(fd<0)

      {

           printf("open error");

           exit(0);

      }

      while(1)

      {

           printf("Clinet:\n");

           num=read(fd,buf,sizeof(buf));

           if(num==-1)

           {

                 if(errno=EAGAIN)

                 {

                      printf("No data avlaible\n");

                 }

           }

           else

           {

                 printf("read byte:%d\n",num);     

                 printf("Received message:%s\n",buf);          

                 break;

           }

           sleep(5);

      }

      printf("Done!\n");

      return 0;

}

 

 

 

阅读(2518) | 评论(0) | 转发(2) |
0

上一篇:C 进程操作编程

下一篇:C 信号编程

给主人留下些什么吧!~~