alarm流量控制1

cpp 复制代码
#include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
 #include <unistd.h>
 
 #define CPS 10
 #define BUFSIZE CPS
 #define BURST 100
 
 static volatile int token = 0;
 
 static void alarm_handler(int s)
 {
     alarm(1);
     token++;
     if(token > BURST)
         token = BURST;
 }
 
 
 int main(int argc, char *argv[])
 {
     int sfd, dfd = 1;
     char buf[BUFSIZE];
     int len, ret, pos;
 
     if(argc < 2)
	 {
         fprintf(stderr, "Usage....\n");
         exit(1);
     }
     signal(SIGALRM, alarm_handler);
     alarm(1);
 
     do
     {
         sfd = open(argv[1], O_RDONLY);
         if(sfd < 0)
         {
             if(errno != EINTR)
             {
                 perror("open()");
			 }
         }
 
     }while(sfd < 0);
 
     while(1)
     {
         while(token <= 0)
         {
             pause();
         }
         token--;
         while((len = read(sfd, buf, BUFSIZE)) < 0)
         {
             if(errno == EINTR)
			 continue;
             perror("read()");
             break;
         }
         if (len == 0)
         {
             break;
         }
         pos = 0;
         while(len > 0)
         {
            ret =  write(dfd, buf + pos, len);
            if(ret < 0)
            {
              if(errno == EINTR)
				continue;
              perror("read()");
              break;
            }
            len -= ret;
            pos += ret;
         }
 
     }
     exit(0);
 }
			  
相关推荐
fpcc8 小时前
c++23中的print和println
c++·c++23
玉树临风江流儿9 小时前
C++左值、右值、move移动函数
开发语言·c++
许长安10 小时前
c/c++ static关键字详解
c语言·c++·经验分享·笔记
Murphy_lx10 小时前
C++ thread类
开发语言·c++
月夜的风吹雨10 小时前
【C++ STL 深度剖析】:vector 底层模拟实现与核心陷阱解析
c++·vector·类和对象·visual studio
彩妙不是菜喵10 小时前
C++ 中 nullptr 的使用与实践:从陷阱到最佳实践
开发语言·jvm·c++
_dindong12 小时前
笔试强训:Week-4
数据结构·c++·笔记·学习·算法·哈希算法·散列表
liu****12 小时前
12.线程(二)
linux·开发语言·c++·1024程序员节
小冯的编程学习之路13 小时前
【C++】:C++基于微服务的即时通讯系统(2)
开发语言·c++·微服务
许长安13 小时前
C/C++中的extern关键字详解
c语言·开发语言·c++·经验分享·笔记