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);
 }
			  
相关推荐
黄昏晓x16 分钟前
C++11
android·java·c++
2401_9001515438 分钟前
C++中的桥接模式
开发语言·c++·算法
自动化和Linux39 分钟前
windows11安装GCC+安装Visual Studio Code,Dev-C++
c++·ide·vscode·编辑器
生活很暖很治愈40 分钟前
Linux——HTTP协议
linux·服务器·c++·网络协议·ubuntu·http
知无不研1 小时前
c++垃圾回收机制
开发语言·c++·智能指针·raii·垃圾回收机制
_dindong1 小时前
【单调栈/队列&并查集&字符串哈希&Tire树】习题集锦
数据结构·c++·算法·哈希算法
西装没钱买1 小时前
QT组播的建立和使用(绑定特定的网卡,绑定特定IP)
网络·c++·qt·udp·udp组播
Ralph_Y2 小时前
C++:static
开发语言·c++
松☆2 小时前
C++ 程序设计基础:从 Hello World 到数据类型与 I/O 流的深度解析
c++·算法