Linux编程之PING的实现

简介:

PING(Packet InterNet Groper)中文名为因特网包探索器,是用来查看网络上另一个主机系统的网络连接是否正常的一个工具。ping命令的工作原理是:向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把回复报文传回给发送者,这有点象潜水艇声纳系统中使用的发声装置。所以,我们想知道我这台主机能不能和另一台进行通信,我们首先需要确认的是我们两台主机间的网络是不是通的,也就是我说的话能不能传到你那里,这是双方进行通信的前提。在Linux下使用指令ping的方法和现象如下:

在Linux下使用指令ping的方法和现象

PING的实现看起来并不复杂,我想自己写代码实现这个功能,需要些什么知识储备?我简单罗列了一下:

  • ICMP协议的理解
  • RAW套接字
  • 网络封包和解包技能

搭建这么一个ping程序的步骤如下:

  1. ICMP包的封装和解封
  2. 创建一个线程用于ICMP包的发送
  3. 创建一个线程用于ICMP包的接收
  4. 原始套接字编程

PING的流程如下:

一、ICMP包的封装和解封

(1) ICMP协议理解

要进行PING的开发,我们首先需要知道PING的实现是基于ICMP协议来开发的。要进行ICMP包的封装和解封,我们首先需要理解ICMP协议。ICMP位于网络层,允许主机或者路由器报告差错情况和提供有关异常情况的报告。ICMP报文是封装在IP数据报中,作为其中的数据部分。ICMP报文作为IP层数据报的数据,加上数据报头,组成IP数据报发送出去。ICMP报文格式如下:

ICMP报文的种类有两种,即ICMP差错报告报文和ICMP询问报文。PING程序使用的ICMP报文种类为ICMP询问报文。注意一下上面说到的ICMP报文格式中的“类型”字段,我们在组包的时候可以向该字段填写不同的值来标定该ICMP报文的类型。下面列出的是几种常用的ICMP报文类型。

我们的PING程序需要用到的ICMP的类型是回送请求(8)。

因为ICMP报文的具体格式会因为ICMP报文的类型而各不相同,我们ping包的格式是这样的:

(2) ICMP包的组装

对照上面的ping包格式,我们封装ping包的代码可以这么写:


  
  
  1. void icmp_pack(struct icmp* icmphdr, int seq, int length) 
  2. {    int i = 0; 
  3.  
  4.     icmphdr->icmp_type = ICMP_ECHO;  //类型填回送请求 
  5.     icmphdr->icmp_code = 0;    
  6.     icmphdr->icmp_cksum = 0; //注意,这里先填写0,很重要! 
  7.     icmphdr->icmp_seq = seq;  //这里的序列号我们填1,2,3,4.... 
  8.     icmphdr->icmp_id = pid & 0xffff;  //我们使用pid作为icmp_id,icmp_id只是2字节,而pid有4字节 
  9.     for(i=0;i<length;i++) 
  10.     { 
  11.         icmphdr->icmp_data[i] = i;  //填充数据段,使ICMP报文大于64B    } 
  12.  
  13.     icmphdr->icmp_cksum = cal_chksum((unsigned short*)icmphdr, length); //校验和计算}  

这里再三提醒一下,icmp_cksum 必须先填写为0再执行校验和算法计算,否则ping时对方主机会因为校验和计算错误而丢弃请求包,导致ping的失败。我一个同事曾经就因为这么一个错误而排查许久,血的教训请铭记。

这里简单介绍一下checksum(校验和)。

计算机网络通信时,为了检验在数据传输过程中数据是否发生了错误,通常在传输数据的时候连同校验和一块传输,当接收端接受数据时候会从新计算校验和,如果与原校验和不同就视为出错,丢弃该数据包,并返回icmp报文。

算法基本思路:

IP/ICMP/IGMP/TCP/UDP等协议的校验和算法都是相同的,采用的都是将数据流视为16位整数流进行重复叠加计算。为了计算检验和,首先把检验和字段置为0。然后,对有效数据范围内中每个16位进行二进制反码求和,结果存在检验和字段中,如果数据长度为奇数则补一字节0。当收到数据后,同样对有效数据范围中每个16位数进行二进制反码的求和。由于接收方在计算过程中包含了发送方存在首部中的检验和,因此,如果首部在传输过程中没有发生任何差错,那么接收方计算的结果应该为全0或全1(具体看实现了,本质一样) 。如果结果不是全0或全1,那么表示数据错误。


  
  
  1. /*校验和算法*/ 
  2.  
  3. unsigned short cal_chksum(unsigned short *addr,int len) 
  4. {       int nleft=len;        int sum=0; 
  5.         unsigned short *w=addr; 
  6.         unsigned short answer=0;        /*把ICMP报头二进制数据以2字节为单位累加起来*/ 
  7.         while(nleft>1) 
  8.         {        
  9.             sum+=*w++; 
  10.             nleft-=2; 
  11.         }        /*若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个2字节数据的高字节,这个2字节数据的低字节为0,继续累加*/ 
  12.         if( nleft==1) 
  13.         {        
  14.             *(unsigned char *)(&answer)=*(unsigned char *)w; 
  15.             sum+=answer; 
  16.         } 
  17.         sum=(sum>>16)+(sum&0xffff); 
  18.         sum+=(sum>>16); 
  19.         answer=~sum;        return answer; 
  20.  

(3) ICMP包的解包

知道怎么封装包,那解包就也不难了,注意的是,收到一个ICMP包,我们不要就认为这个包就是我们发出去的ICMP回送回答包,我们需要加一层代码来判断该ICMP报文的id和seq字段是否符合我们发送的ICMP报文的设置,来验证ICMP回复包的正确性。


  
  
  1. int icmp_unpack(char* buf, int len) 
  2. {    int iphdr_len;    struct timeval begin_time, recv_time, offset_time;    int rtt;  //round trip time 
  3.  
  4.     struct ip* ip_hdr = (struct ip *)buf; 
  5.     iphdr_len = ip_hdr->ip_hl*4;    struct icmp* icmp = (struct icmp*)(buf+iphdr_len); //使指针跳过IP头指向ICMP头 
  6.     len-=iphdr_len;  //icmp包长度 
  7.     if(len < 8)   //判断长度是否为ICMP包长度    { 
  8.         fprintf(stderr, "Invalid icmp packet.Its length is less than 8\n");        return -1; 
  9.     }    //判断该包是ICMP回送回答包且该包是我们发出去的 
  10.     if((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == (pid & 0xffff)))  
  11.     {        if((icmp->icmp_seq < 0) || (icmp->icmp_seq > PACKET_SEND_MAX_NUM)) 
  12.         { 
  13.             fprintf(stderr, "icmp packet seq is out of range!\n");            return -1; 
  14.         } 
  15.  
  16.         ping_packet[icmp->icmp_seq].flag = 0; 
  17.         begin_time = ping_packet[icmp->icmp_seq].begin_time;  //去除该包的发出时间 
  18.         gettimeofday(&recv_time, NULL); 
  19.  
  20.         offset_time = cal_time_offset(begin_time, recv_time); 
  21.         rtt = offset_time.tv_sec*1000 + offset_time.tv_usec/1000; //毫秒为单位 
  22.         printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%d ms\n"
  23.             len, inet_ntoa(ip_hdr->ip_src), icmp->icmp_seq, ip_hdr->ip_ttl, rtt);         
  24.  
  25.     }    else 
  26.     { 
  27.         fprintf(stderr, "Invalid ICMP packet! Its id is not matched!\n");        return -1; 
  28.     }    return 0; 
  29.  

二、发包线程的搭建

根据PING程序的框架,我们需要建立一个线程用于ping包的发送,我的想法是这样的:使用sendto进行发包,发包速率我们维持在1秒1发,我们需要用一个全局变量记录第一个ping包发出的时间,除此之外,我们还需要一个全局变量来记录我们发出的ping包到底有几个,这两个变量用于后来收到ping包回复后的数据计算。


  
  
  1. void ping_send() 
  2. {    char send_buf[128]; 
  3.     memset(send_buf, 0, sizeof(send_buf)); 
  4.     gettimeofday(&start_time, NULL); //记录第一个ping包发出的时间 
  5.     while(alive) 
  6.     {        int size = 0; 
  7.         gettimeofday(&(ping_packet[send_count].begin_time), NULL); 
  8.         ping_packet[send_count].flag = 1; //将该标记为设置为该包已发送 
  9.         icmp_pack((struct icmp*)send_buf, send_count, 64); //封装icmp包 
  10.         size = sendto(rawsock, send_buf, 64, 0, (struct sockaddr*)&dest, sizeof(dest)); 
  11.         send_count++; //记录发出ping包的数量 
  12.         if(size < 0) 
  13.         { 
  14.             fprintf(stderr, "send icmp packet fail!\n");            continue
  15.         } 
  16.  
  17.         sleep(1); 
  18.     } 
  19.  

三、收包线程的搭建

我们同样建立一个接收包的线程,这里我们采用select函数进行收包,并为select函数设置超时时间为200us,若发生超时,则进行下一个循环。同样地,我们也需要一个全局变量来记录成功接收到的ping回复包的数量。


  
  
  1. void ping_recv() 
  2. {    struct timeval tv; 
  3.     tv.tv_usec = 200;  //设置select函数的超时时间为200us 
  4.     tv.tv_sec = 0; 
  5.     fd_set read_fd;    char recv_buf[512]; 
  6.     memset(recv_buf, 0 ,sizeof(recv_buf));    while(alive) 
  7.     {        int ret = 0; 
  8.         FD_ZERO(&read_fd); 
  9.         FD_SET(rawsock, &read_fd); 
  10.         ret = select(rawsock+1, &read_fd, NULLNULL, &tv);        switch(ret) 
  11.         {            case -1: 
  12.                 fprintf(stderr,"fail to select!\n");                break;            case 0:                break;            default
  13.                 {                    int size = recv(rawsock, recv_buf, sizeof(recv_buf), 0);                    if(size < 0) 
  14.                     { 
  15.                         fprintf(stderr,"recv data fail!\n");                        continue
  16.                     } 
  17.  
  18.                     ret = icmp_unpack(recv_buf, size); //对接收的包进行解封 
  19.                     if(ret == -1)  //不是属于自己的icmp包,丢弃不处理                    {                        continue
  20.                     } 
  21.                     recv_count++; //接收包计数                }                break; 
  22.         } 
  23.  
  24.     } 
  25.  

四、中断处理

我们规定了一次ping发送的包的最大值为64个,若超出该数值就停止发送。作为PING的使用者,我们一般只会发送若干个包,若有这几个包顺利返回,我们就crtl+c中断ping。这里的代码主要是为中断信号写一个中断处理函数,将alive这个全局变量设置为0,进而使发送ping包的循环停止而结束程序。


  
  
  1. oid icmp_sigint(int signo) 
  2.     alive = 0; 
  3.     gettimeofday(&end_time, NULL); 
  4.     time_interval = cal_time_offset(start_time, end_time); 
  5.  
  6. signal(SIGINT, icmp_sigint);  

五、总体实现

各模块介绍完了,现在贴出完整代码。


  
  
  1. #include <stdio.h>   
  2. #include <netinet/in.h>   
  3. #include <netinet/ip.h>   
  4. #include <netinet/ip_icmp.h>   
  5. #include <unistd.h>   
  6. #include <signal.h>   
  7. #include <arpa/inet.h>   
  8. #include <errno.h>   
  9. #include <sys/time.h>  
  10. #include <string.h>  
  11. #include <netdb.h>  
  12. #include <pthread.h>  
  13.    
  14.    
  15. #define PACKET_SEND_MAX_NUM 64  
  16.     
  17.  typedef struct ping_packet_status  
  18.  {  
  19.       struct timeval begin_time;  
  20.       struct timeval end_time;  
  21.       int flag;   //发送标志,1为已发送  
  22.       int seq;     //包的序列号  
  23.   }ping_packet_status;  
  24.     
  25.     
  26.     
  27.   ping_packet_status ping_packet[PACKET_SEND_MAX_NUM];  
  28.     
  29.   int alive;  
  30.   int rawsock;  
  31.   int send_count;  
  32.   int recv_count;  
  33.   pid_t pid;  
  34.   struct sockaddr_in dest;  
  35.   struct timeval start_time;  
  36.   struct timeval end_time;  
  37.   struct timeval time_interval;  
  38.     
  39.   /*校验和算法*/  
  40.   unsigned short cal_chksum(unsigned short *addr,int len) {       int nleft=len;  
  41.           int sum=0;  
  42.           unsigned short *w=addr;  
  43.           unsigned short answer=0;  
  44.     
  45.           /*把ICMP报头二进制数据以2字节为单位累加起来*/  
  46.           while(nleft>1)  
  47.           {        
  48.               sum+=*w++;  
  49.               nleft-=2;  
  50.           }  
  51.           /*若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个2字节数据的高字节,这个2字节数据的低字节为0,继续累加*/  
  52.           if( nleft==1)  
  53.           {        
  54.               *(unsigned char *)(&answer)=*(unsigned char *)w;  
  55.               sum+=answer;  
  56.           }  
  57.           sum=(sum>>16)+(sum&0xffff);  
  58.           sum+=(sum>>16);  
  59.           answer=~sum;  
  60.           return answer;  
  61.   }  
  62.     
  63.   struct timeval cal_time_offset(struct timeval begin, struct timeval end)  
  64.   {  
  65.       struct timeval ans;  
  66.       ans.tv_sec = end.tv_sec - begin.tv_sec;  
  67.       ans.tv_usec = end.tv_usec - begin.tv_usec;  
  68.       if(ans.tv_usec < 0) //如果接收时间的usec小于发送时间的usec,则向sec域借位  
  69.       {  
  70.           ans.tv_sec--;  
  71.           ans.tv_usec+=1000000;  
  72.       }  
  73.       return ans;  
  74.   }  
  75.     
  76.   void icmp_pack(struct icmp* icmphdr, int seq, int length)  
  77.   {  
  78.       int i = 0;  
  79.     
  80.       icmphdr->icmp_type = ICMP_ECHO;  
  81.       icmphdr->icmp_code = 0;  
  82.       icmphdr->icmp_cksum = 0;  
  83.       icmphdr->icmp_seq = seq;  
  84.       icmphdr->icmp_id = pid & 0xffff;  
  85.       for(i=0;i<length;i++)  
  86.       {  
  87.           icmphdr->icmp_data[i] = i;  
  88.       }  
  89.     
  90.       icmphdr->icmp_cksum = cal_chksum((unsigned short*)icmphdr, length);  
  91.   }  
  92.     
  93.   int icmp_unpack(char* buf, int len)  
  94.   {  
  95.       int iphdr_len;  
  96.       struct timeval begin_time, recv_time, offset_time;  
  97.       int rtt;  //round trip time  
  98.    
  99.      struct ip* ip_hdr = (struct ip *)buf; 
  100.      iphdr_len = ip_hdr->ip_hl*4; 
  101.  
  102.      struct icmp* icmp = (struct icmp*)(buf+iphdr_len); 
  103.      len-=iphdr_len;  //icmp包长度 
  104.      if(len < 8)   //判断长度是否为ICMP包长度 
  105.      { 
  106.          fprintf(stderr, "Invalid icmp packet.Its length is less than 8\n"); 
  107.          return -1; 
  108.      }   
  109.   
  110.      //判断该包是ICMP回送回答包且该包是我们发出去的 
  111.      if((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == (pid & 0xffff)))  
  112.      { 
  113.          if((icmp->icmp_seq < 0) || (icmp->icmp_seq > PACKET_SEND_MAX_NUM)) 
  114.   
  115.          { 
  116.          
  117.              fprintf(stderr, "icmp packet seq is out of range!\n"); 
  118.              return -1; 
  119.          } 
  120.   
  121.          ping_packet[icmp->icmp_seq].flag = 0; 
  122.          begin_time = ping_packet[icmp->icmp_seq].begin_time; 
  123.          gettimeofday(&recv_time, NULL); 
  124.   
  125.          offset_time = cal_time_offset(begin_time, recv_time); 
  126.          rtt = offset_time.tv_sec*1000 + offset_time.tv_usec/1000; //毫秒为单位 
  127.   
  128.          printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%d ms\n"
  129.              len, inet_ntoa(ip_hdr->ip_src), icmp->icmp_seq, ip_hdr->ip_ttl, rtt);         
  130.   
  131.      } 
  132.      else 
  133.      { 
  134.          fprintf(stderr, "Invalid ICMP packet! Its id is not matched!\n"); 
  135.          return -1; 
  136.      } 
  137.      return 0; 
  138.  } 
  139.   
  140.  void ping_send() 
  141.  { 
  142.      char send_buf[128]; 
  143.      memset(send_buf, 0, sizeof(send_buf)); 
  144.      gettimeofday(&start_time, NULL); //记录第一个ping包发出的时间 
  145.      while(alive) 
  146.      { 
  147.          int size = 0; 
  148.          gettimeofday(&(ping_packet[send_count].begin_time), NULL); 
  149.          ping_packet[send_count].flag = 1; //将该标记为设置为该包已发送 
  150.   
  151.          icmp_pack((struct icmp*)send_buf, send_count, 64); //封装icmp包 
  152.          size = sendto(rawsock, send_buf, 64, 0, (struct sockaddr*)&dest, sizeof(dest)); 
  153.          send_count++; //记录发出ping包的数量 
  154.          if(size < 0) 
  155.          { 
  156.              fprintf(stderr, "send icmp packet fail!\n"); 
  157.              continue
  158.          } 
  159.   
  160.          sleep(1); 
  161.      } 
  162.  } 
  163.   
  164.  void ping_recv() 
  165.  {     struct timeval tv; 
  166.      tv.tv_usec = 200;  //设置select函数的超时时间为200us 
  167.      tv.tv_sec = 0; 
  168.      fd_set read_fd; 
  169.      char recv_buf[512]; 
  170.      memset(recv_buf, 0 ,sizeof(recv_buf)); 
  171.      while(alive) 
  172.      { 
  173.          int ret = 0; 
  174.          FD_ZERO(&read_fd); 
  175.          FD_SET(rawsock, &read_fd); 
  176.          ret = select(rawsock+1, &read_fd, NULLNULL, &tv); 
  177.          switch(ret) 
  178.          { 
  179.              case -1: 
  180.                  fprintf(stderr,"fail to select!\n"); 
  181.                 break; 
  182.              case 0: 
  183.                  break; 
  184.              default
  185.                  { 
  186.                      int size = recv(rawsock, recv_buf, sizeof(recv_buf), 0); 
  187.                      if(size < 0) 
  188.                      { 
  189.                          fprintf(stderr,"recv data fail!\n"); 
  190.                          continue
  191.                      } 
  192.   
  193.                     ret = icmp_unpack(recv_buf, size); //对接收的包进行解封 
  194.                      if(ret == -1)  //不是属于自己的icmp包,丢弃不处理 
  195.                      { 
  196.                          continue
  197.                      } 
  198.                      recv_count++; //接收包计数 
  199.                  } 
  200.                  break; 
  201.          } 
  202.   
  203.      }    
  204.  } 
  205.   
  206.  void icmp_sigint(int signo) 
  207.  { 
  208.      alive = 0; 
  209.      gettimeofday(&end_time, NULL); 
  210.      time_interval = cal_time_offset(start_time, end_time); 
  211.  } 
  212.   
  213.  void ping_stats_show()  
  214.  { 
  215.      long time = time_interval.tv_sec*1000+time_interval.tv_usec/1000; 
  216.      /*注意除数不能为零,这里send_count有可能为零,所以运行时提示错误*/ 
  217.      printf("%d packets transmitted, %d recieved, %d%c packet loss, time %ldms\n"
  218.          send_count, recv_count, (send_count-recv_count)*100/send_count, '%'time); 
  219.  
  220.  
  221. int main(int argc, char* argv[]) 
  222. int size = 128*1024;//128k 
  223. struct protoent* protocol = NULL
  224. char dest_addr_str[80]; 
  225. memset(dest_addr_str, 0, 80); 
  226. unsigned int inaddr = 1; 
  227. struct hostent* host = NULL
  228.  
  229. pthread_t send_id,recv_id;   
  230.   
  231. if(argc < 2) 
  232. {   
  233. printf("Invalid IP ADDRESS!\n"); 
  234.          return -1; 
  235.   }    
  236.   
  237.   protocol = getprotobyname("icmp"); //获取协议类型ICMP 
  238. if(protocol == NULL
  239.   {   
  240.   printf("Fail to getprotobyname!\n"); 
  241.       return -1; 
  242.   }     
  243.   
  244.   memcpy(dest_addr_str, argv[1], strlen(argv[1])+1); 
  245.  
  246.   rawsock = socket(AF_INET,SOCK_RAW,protocol->p_proto); 
  247.   if(rawsock < 0) 
  248.   {    
  249.      printf("Fail to create socket!\n"); 
  250.    return -1; 
  251.   }   
  252.  
  253.   pid = getpid(); 
  254.  
  255.   setsockopt(rawsock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); //增大接收缓冲区至128K 
  256.   
  257.   bzero(&dest,sizeof(dest)); 
  258.  
  259.  dest.sin_family = AF_INET; 
  260.  
  261.  inaddr = inet_addr(argv[1]); 
  262.  if(inaddr == INADDR_NONE)   //判断用户输入的是否为IP地址还是域名 
  263.  {   
  264.          //输入的是域名地址 
  265.          host = gethostbyname(argv[1]); 
  266.          if(host == NULL
  267.          {     
  268.              printf("Fail to gethostbyname!\n"); 
  269.              return -1; 
  270.          }        
  271.   
  272.          memcpy((char*)&dest.sin_addr, host->h_addr, host->h_length); 
  273.      }  
  274.      else 
  275.      { 
  276.          memcpy((char*)&dest.sin_addr, &inaddr, sizeof(inaddr));//输入的是IP地址 
  277.      } 
  278.      inaddr = dest.sin_addr.s_addr; 
  279.      printf("PING %s, (%d.%d.%d.%d) 56(84) bytes of data.\n",dest_addr_str, 
  280.          (inaddr&0x000000ff), (inaddr&0x0000ff00)>>8,  
  281.          (inaddr&0x00ff0000)>>16, (inaddr&0xff000000)>>24); 
  282.   
  283.      alive = 1;  //控制ping的发送和接收 
  284.   
  285.      signal(SIGINT, icmp_sigint); 
  286.  
  287.      if(pthread_create(&send_id, NULL, (void*)ping_send, NULL)) 
  288.      {   
  289.          printf("Fail to create ping send thread!\n"); 
  290.          return -1; 
  291.      }  
  292.   
  293.      if(pthread_create(&recv_id, NULL, (void*)ping_recv, NULL)) 
  294.      { 
  295.          printf("Fail to create ping recv thread!\n"); 
  296.          return -1; 
  297.      }   
  298.   
  299.      pthread_join(send_id, NULL);//等待send ping线程结束后进程再结束 
  300.      pthread_join(recv_id, NULL);//等待recv ping线程结束后进程再结束 
  301.   
  302.      ping_stats_show();  
  303.   
  304.      close(rawsock);    
  305.      return 0; 
  306.   
  307.  }  

编译以及实验现象如下:

我的实验环境是两台服务器,发起ping的主机是172.0.5.183,被ping的主机是172.0.5.182,以下是我的两次实验现象(ping IP和ping 域名)。

特别注意:

只有root用户才能利用socket()函数生成原始套接字,要让Linux的一般用户能执行以上程序,需进行如下的特别操作:用root登陆,编译以上程序gcc -lpthread -o ping ping.c

实验现象可以看出,PING是成功的,表明两主机间的网络是通的,发出的所有ping包都收到了回复。

下面是Linux系统自带的PING程序,我们可以对比一下我们设计的PING程序跟系统自带的PING程序有何不同。







本文作者:佚名
来源:51CTO

目录
相关文章
|
4月前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
1月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
164 20
|
1月前
|
存储 监控 Linux
嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间
在嵌入式Linux系统编程中,`times`和 `clock`函数是获取进程时间的两个重要工具。`times`函数提供了更详细的进程和子进程时间信息,而 `clock`函数则提供了更简单的处理器时间获取方法。根据具体需求选择合适的函数,可以更有效地进行性能分析和资源管理。通过本文的介绍,希望能帮助您更好地理解和使用这两个函数,提高嵌入式系统编程的效率和效果。
109 13
|
2月前
|
运维 监控 Linux
别再只会使用简单的 ping 命令了,Linux 中这些高级 ping 命令可以提高工作效率!
在 Linux 系统中,ping 命令不仅用于检测网络连通性和延迟,还拥有多种高级选项和技巧,如定制数据包大小、获取详细统计信息、持续 ping、指定源地址和多目标 ping。本文详细介绍这些高级命令及其在性能测试、故障排查和网络监控中的实际应用,帮助你提升网络管理效率。
247 3
|
2月前
|
安全 网络协议 Linux
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。通过掌握 ping 命令,读者可以轻松测试网络连通性、诊断网络问题并提升网络管理能力。
214 3
|
2月前
|
运维 监控 Shell
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
|
4月前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
4月前
|
Linux Shell
Linux系统编程:掌握popen函数的使用
记得在使用完 `popen`打开的流后,总是使用 `pclose`来正确关闭它,并回收资源。这种做法符合良好的编程习惯,有助于保持程序的健壮性和稳定性。
210 6
|
4月前
|
Linux Shell
Linux系统编程:掌握popen函数的使用
记得在使用完 `popen`打开的流后,总是使用 `pclose`来正确关闭它,并回收资源。这种做法符合良好的编程习惯,有助于保持程序的健壮性和稳定性。
212 3
|
4月前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
144 0

热门文章

最新文章