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);
 }
			  
相关推荐
hanbr8 小时前
C++ 初涉
开发语言·c++
Дерек的学习记录8 小时前
C++:入门基础(下)
开发语言·数据结构·c++·学习·算法·visualstudio
云深麋鹿9 小时前
标准库中的String类
开发语言·c++·容器
愚者游世11 小时前
力扣解决二进制 | 题型常用知识点梳理
c++·程序人生·算法·leetcode·职场和发展
蜡笔小马11 小时前
15.Boost.Geometry 坐标系统详解
c++·boost
AD钙奶-lalala11 小时前
Android编译C++代码步骤详解
android·开发语言·c++
rhett. li12 小时前
FreeBSD系统中使用clang/clang++编译Skia源码的方法
c++·ui·用户界面
_风华ts12 小时前
C++函数指针
c++·函数指针
威桑12 小时前
解决 Qt6 程序 在Linux 环境下无法输入中文的问题
linux·c++·qt
浅念-13 小时前
C++ :类和对象(4)
c语言·开发语言·c++·经验分享·笔记·学习·算法