首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

IPC机制和实现(2)

IPC机制和实现(2)

读进程:
#include
#include
#include
#include
#include
#include
#include
#define FIFO "/tmp/myfifo"
main(int argc,char** argv)
{
char buf_r[100];
int  fd;
int  nread;

if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
  printf("cannot create fifoservern");
printf("Preparing for reading bytes...n");

memset(buf_r,0,sizeof(buf_r));
fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
if(fd==-1)
{
  perror("open");
  exit(1);
}
while(1)
{
  memset(buf_r,0,sizeof(buf_r));

  if((nread=read(fd,buf_r,100))==-1){
   if(errno==EAGAIN)
    printf("no data yetn");
  }
  printf("read %s from FIFOn",buf_r);
  sleep(1);
}
pause();
unlink(FIFO);
}
写进程:
#include
#include
#include
#include
#include
#include
#include
#define FIFO_SERVER "/tmp/myfifo"
main(int argc,char** argv)
{
int fd;
char w_buf[100];
int nwrite;

if(fd==-1)
  if(errno==ENXIO)
   printf("open error; no reading processn");
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
if(argc==1)
  printf("Please send somethingn");
strcpy(w_buf,argv[1]);
if((nwrite=write(fd,w_buf,100))==-1)
{
  if(errno==EAGAIN)
   printf("The FIFO has not been read yet.Please try latern");
}
else
  printf("write %s to the FIFOn",w_buf);
}
二、信号
(1)信号的发出:kill(),raise()给自身进程发送信号。
#include
#include
#include
#include
#include
int main()
{
pid_t pid;
int ret;
if((pid=fork())<0){
  perror("fork");
  exit(1);
}
if(pid == 0){
  raise(SIGSTOP);
  exit(0);
}
else{
  printf("pid=%dn",pid);
  if((waitpid(pid,NULL,WNOHANG))==0){
   if((ret=kill(pid,SIGKILL))==0)
    printf("kill %dn",pid);
   else{
    perror("kill");
   }
  }
}
}
(2)alarm(),pause()
#include
#include
#include
int main()
{
        int ret;
        ret=alarm(5);
        pause();
        printf("I have been waken up.n",ret);
}
(3)signal(),发送信号
ctrl+c:SIGINT,ctrl+/:SIGQUIT
#include
#include
#include
void my_func(int sign_no)
{
if(sign_no==SIGINT)
  printf("I have get SIGINTn");
else if(sign_no==SIGQUIT)
  printf("I have get SIGQUITn");
}
int main()
{
printf("Waiting for signal SIGINT or SIGQUIT n ");
signal(SIGINT, my_func);
signal(SIGQUIT, my_func);
pause();
exit(0);
}
(4)信号集,接受多个信号,同时多个信号处理函数
#include
#include
#include
#include
#include
void my_func(int signum)
{
printf("If you want to quit,please try SIGQUITn");
}
int main()
{
sigset_t set,pendset;
struct sigaction action1,action2;
if(sigemptyset(&set)<0)
  perror("sigemptyset");
if(sigaddset(&set,SIGQUIT)<0)
  perror("sigaddset");
if(sigaddset(&set,SIGINT)<0)
  perror("sigaddset");
if(sigprocmask(SIG_BLOCK,&set,NULL)<0)
  perror("sigprocmask");
else
{
  printf("blockedn");
  sleep(5);
}
if(sigprocmask(SIG_UNBLOCK,&set,NULL)<0)
  perror("sigprocmask");
else
  printf("unblockn");
while(1){
  if(sigismember(&set,SIGINT)){
   sigemptyset(&action1.sa_mask);
   action1.sa_handler=my_func;
   sigaction(SIGINT,&action1,NULL);
  }else if(sigismember(&set,SIGQUIT)){
   sigemptyset(&action2.sa_mask);
   action2.sa_handler = SIG_DFL;
   sigaction(SIGTERM,&action2,NULL);
  }
}
}
继承事业,薪火相传
返回列表