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);
 }
			  
相关推荐
ComputerInBook1 分钟前
C++ 中的 lambda 表达式
开发语言·c++·lambda表达式·匿名函数
刃神太酷啦22 分钟前
《网络基础全链路深度解析:从Socket编程到HTTPS与TCP/UDP内核机制》----《Hello Linux!》(25)
linux·运维·c语言·网络·c++·tcp/ip·https
paeamecium1 小时前
【PAT甲级真题】- Shuffling Machine (20)
c++·算法·pat考试·pat
不想写代码的星星1 小时前
C++协程从入门到放弃?不,是从入门到手搓调度器
开发语言·c++
redaijufeng1 小时前
C++构造函数详解:从基础原理到实际应用
java·jvm·c++
兵哥工控1 小时前
MFC实现 Modbus-TCP 实时采集ZH-T08R 电阻模块阻值实例
c++·tcp/ip·mfc·modbus-tcp
ComputerInBook2 小时前
C++ 14 相比 C++ 11新增之特征
开发语言·c++·c++ 14
Peter·Pan爱编程2 小时前
成员函数与 this 指针:函数属于数据
c++
music score2 小时前
google 的C++自动化测试框架详解(Google Test)(2)
c++·qt·lucene
charlie1145141912 小时前
基于开源项目的现代C++实战——OnceCallback 实战(五):then 链式组合
开发语言·c++·开源