Linux C/C++ reactor原理与实现

简介: Linux C/C++ reactor原理与实现

一、reactor原理

Reactor 释义“反应堆”,是一种事件驱动机制。

和普通函数调用的不同之处在于:应用程序不是主动的调用某个 API 完成处理,而是恰恰

相反,Reactor 逆置了事件处理流程,应用程序需要提供相应的接口并注册到 Reactor 上,

如果相应的时间发生,Reactor 将主动调用应用程序注册的接口,这些接口又称为“回调函

数”

Reactor 模式是处理并发 I/O 比较常见的一种模式,用于同步 I/O,中心思想是将所有要

处理的 I/O 事件注册到一个中心 I/O 多路复用器上,同时主线程/进程阻塞在多路复用器上;

一旦有 I/O 事件到来或是准备就绪(文件描述符或 socket 可读、写),多路复用器返回并将事

先注册的相应 I/O 事件分发到对应的处理器中。

Reactor架构模式允许事件驱动的应用通过多路分发的机制去处理来自不同客户端的多个请求。

Reactor可以理解为io管理集合,和多线程、多进程没有关系。但后续可以额外在reactor中引入多线程。

除了listenfd用accept处理,其他clientfd用recv和send处理

以前是通过epoll对io进行管理,而现在通过reactor对事件进行管理,可以把reactor理解为一种设计模式

深入理解Reactor 网络编程模型

二、代码部分

1.ntyreactor

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/epoll.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>
#include<fcntl.h>
#include<errno.h>
#define BUFFER_LENGTH 4096
#define MAX_EPOLL_EVENTS 1024
#define SERVER_PORT 8888
typedef int NCALLBACK(int,int,void*);
struct ntyevent{
    int fd;
    int events;
    void* arg;
    int (*callback)(int fd,int events,void* arg);
    int status;
    char buffer[BUFFER_LENGTH];
    int length;
    long last_active;
};
struct ntyreactor{
    int epfd;
    ntyevent* events;
};
int recv_cb(int fd, int events, void *arg);
int send_cb(int fd, int events, void *arg);
void nty_event_set(ntyevent* ev,int fd,NCALLBACK callback,void* arg){
    ev->fd=fd;
    ev->callback=callback;
    ev->events=0;
    ev->arg=arg;
    ev->last_active=time(NULL);
}
int nty_event_add(int epfd,int events,ntyevent* ev){
    epoll_event ep_ev;
    ep_ev.data.ptr=ev;
    ep_ev.events = ev->events = events;
    int op;
    if(ev->status==1){
        op=EPOLL_CTL_MOD;
    }else{
        op=EPOLL_CTL_ADD;
        ev->status=1;
    }
    if(epoll_ctl(epfd,op,ev->fd,&ep_ev)<0){
        printf("epoll add faild\n");
        return -1;
    }
    return 0;
}
int nty_event_del(int epfd, struct ntyevent *ev) {
  struct epoll_event ep_ev = {0, {0}};
  if (ev->status != 1) {
    return -1;
  }
  ep_ev.data.ptr = ev;
  ev->status = 0;
  epoll_ctl(epfd, EPOLL_CTL_DEL, ev->fd, &ep_ev);
  return 0;
}
int recv_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  struct ntyevent *ev = reactor->events+fd;
  int len = recv(fd, ev->buffer, BUFFER_LENGTH, 0);
  nty_event_del(reactor->epfd, ev);
  if (len > 0) {
    ev->length = len;
    ev->buffer[len] = '\0';
    printf("C[%d]:%s\n", fd, ev->buffer);
    nty_event_set(ev, fd, send_cb, reactor);
    nty_event_add(reactor->epfd, EPOLLOUT, ev);
  } else if (len == 0) {
    close(ev->fd);
    printf("[fd=%d] pos[%ld], closed\n", fd, ev-reactor->events);
  } else {
    close(ev->fd);
    printf("recv[fd=%d] error[%d]:%s\n", fd, errno, strerror(errno));
  }
  return len;
}
int send_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  struct ntyevent *ev = reactor->events+fd;
  int len = send(fd, ev->buffer, ev->length, 0);
  if (len > 0) {
    printf("send[fd=%d], [%d]%s\n", fd, len, ev->buffer);
    nty_event_del(reactor->epfd, ev);//删掉又恢复,重置时间,(因为超时会断开连接)
    nty_event_set(ev, fd, recv_cb, reactor);
    nty_event_add(reactor->epfd, EPOLLIN, ev);
  } else {
    close(ev->fd);
    nty_event_del(reactor->epfd, ev);
    printf("send[fd=%d] error %s\n", fd, strerror(errno));
  }
  return len;
}
int accept_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  if (reactor == NULL) return -1;
  struct sockaddr_in client_addr;
  socklen_t len = sizeof(client_addr);
  int clientfd;
  if ((clientfd = accept(fd, (struct sockaddr*)&client_addr, &len)) == -1) {
    if (errno != EAGAIN && errno != EINTR) {
    }
    printf("accept: %s\n", strerror(errno));
    return -1;
  }
  int i = 0;
  do {
    for (i = 3;i < MAX_EPOLL_EVENTS;i ++) {
      if (reactor->events[i].status == 0) {
        break;
      }
    }
    if (i == MAX_EPOLL_EVENTS) {
      printf("%s: max connect limit[%d]\n", __func__, MAX_EPOLL_EVENTS);
      break;
    }
    int flag = 0;
    if ((flag = fcntl(clientfd, F_SETFL, O_NONBLOCK)) < 0) {
      printf("%s: fcntl nonblocking failed, %d\n", __func__, MAX_EPOLL_EVENTS);
      break;
    }
    nty_event_set(&reactor->events[clientfd], clientfd, recv_cb, reactor);
    nty_event_add(reactor->epfd, EPOLLIN, &reactor->events[clientfd]);
  } while (0);
  printf("new connect [%s:%d][time:%ld], pos[%d]\n", 
    inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), reactor->events[i].last_active, i);
  return 0;
}
int init_sockfd(uint16_t port){
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    fcntl(sockfd,F_SETFL,O_NONBLOCK);
    sockaddr_in server_addr;
    server_addr.sin_family=AF_INET;
  server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    server_addr.sin_port=htons(port);
    bind(sockfd,(sockaddr*)&server_addr,sizeof(sockaddr_in));
    if(listen(sockfd,20)<0){
        perror("listen");
    }
    return sockfd;
}
int ntyreactor_init(ntyreactor* reactor){
    if(reactor==NULL) return -1;
    memset(reactor,0,sizeof(ntyreactor));
    reactor->epfd=epoll_create(1);
    if(reactor->epfd<=0){
        // perror("create epfd in%s",__func__);
        close(reactor->epfd);
        return -2;
    }
    reactor->events=(ntyevent*)malloc(sizeof(ntyevent)*MAX_EPOLL_EVENTS);
    memset(reactor->events,0,sizeof(ntyevent)*MAX_EPOLL_EVENTS);
    if(reactor->events==NULL){
        // perror("create epfd in%s",__func__);
        close(reactor->epfd);
        return -3;
    }
}
int ntyreactor_destory(ntyreactor *reactor) {
  close(reactor->epfd);
  free(reactor->events);
}
int ntyreactor_addlistener(ntyreactor* reactor,int sockfd,NCALLBACK* acceptor){
    if(reactor==NULL) return -1;
    if(reactor->events==NULL) return -1;
    nty_event_set(&reactor->events[sockfd],sockfd,acceptor,reactor);
    nty_event_add(reactor->epfd,EPOLLIN,&reactor->events[sockfd]);
    return 0;
}
int ntyreactor_run(ntyreactor* reactor){
    if(reactor==NULL) return -1;
    if(reactor->epfd<0) return -1;
    if(reactor->events==NULL) return -1;
    epoll_event events[MAX_EPOLL_EVENTS+1];
    int checkpos=0;
    while(1){
        long now=time(NULL);
        for(int i=0;i<100;i++,checkpos++){
            if(checkpos==MAX_EPOLL_EVENTS){
                checkpos=0;
            }
            if(reactor->events[checkpos].status!=1){
                continue;
            }
            long duration = now - reactor->events[checkpos].last_active;
      if (duration >= 60) {
        close(reactor->events[checkpos].fd);
        printf("[fd=%d] timeout\n", reactor->events[checkpos].fd);
        nty_event_del(reactor->epfd, &reactor->events[checkpos]);
      }
        }
        int nready=epoll_wait(reactor->epfd,events,MAX_EPOLL_EVENTS,1000);
        if(nready<0) continue;
        for(int i=0;i<nready;i++){
            ntyevent* ev=(ntyevent*)events[i].data.ptr;
            if((events[i].events&EPOLLIN)&&(ev->events&EPOLLIN)){
                ev->callback(ev->fd,events[i].events,ev->arg);
            }
            if ((events[i].events & EPOLLOUT) && (ev->events & EPOLLOUT)) {
        ev->callback(ev->fd, events[i].events, ev->arg);
      }
        }
    }
}
int main(int argc,char** argv){
    uint16_t port=SERVER_PORT;
    if(argc==2){
        port=atoi(argv[1]);
    }
    int sockfd=init_sockfd(port);
    ntyreactor* reactor=(ntyreactor*)malloc(sizeof(ntyreactor));
    ntyreactor_init(reactor);
    ntyreactor_addlistener(reactor,sockfd,accept_cb);
    ntyreactor_run(reactor);
    ntyreactor_destory(reactor);
    close(sockfd);
    return 0;
}

2.ntyreactor(链表+数组 存储event的方式 )

服务端使用100个端口,Linux C/C++ 开发(学习笔记十三):百万并发的服务器实现

实现百万并发

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#define BUFFER_LENGTH   4096
#define MAX_EPOLL_EVENTS  1024
#define SERVER_PORT     8888
#define PORT_COUNT      100
typedef int NCALLBACK(int ,int, void*);
struct ntyevent {
  int fd;
  int events;
  void *arg;
  int (*callback)(int fd, int events, void *arg);
  int status;
  char buffer[BUFFER_LENGTH];
  int length;
  long last_active;
};
struct eventblock {
  struct eventblock *next;
  struct ntyevent *events;
};
struct ntyreactor {
  int epfd;
  int blkcnt;
  struct eventblock *evblk; //fd --> 100w
};
int recv_cb(int fd, int events, void *arg);
int send_cb(int fd, int events, void *arg);
struct ntyevent *ntyreactor_idx(struct ntyreactor *reactor, int sockfd);
void nty_event_set(struct ntyevent *ev, int fd, NCALLBACK callback, void *arg) {
  ev->fd = fd;
  ev->callback = callback;
  ev->events = 0;
  ev->arg = arg;
  ev->last_active = time(NULL);
  return ;
}
int nty_event_add(int epfd, int events, struct ntyevent *ev) {
  struct epoll_event ep_ev = {0, {0}};
  ep_ev.data.ptr = ev;
  ep_ev.events = ev->events = events;
  int op;
  if (ev->status == 1) {
    op = EPOLL_CTL_MOD;
  } else {
    op = EPOLL_CTL_ADD;
    ev->status = 1;
  }
  if (epoll_ctl(epfd, op, ev->fd, &ep_ev) < 0) {
    printf("event add failed [fd=%d], events[%d]\n", ev->fd, events);
    return -1;
  }
  return 0;
}
int nty_event_del(int epfd, struct ntyevent *ev) {
  struct epoll_event ep_ev = {0, {0}};
  if (ev->status != 1) {
    return -1;
  }
  ep_ev.data.ptr = ev;
  ev->status = 0;
  epoll_ctl(epfd, EPOLL_CTL_DEL, ev->fd, &ep_ev);
  return 0;
}
int recv_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  struct ntyevent *ev = ntyreactor_idx(reactor, fd);
  int len = recv(fd, ev->buffer, BUFFER_LENGTH , 0); // 
  nty_event_del(reactor->epfd, ev);
  if (len > 0) {
    ev->length = len;
    ev->buffer[len] = '\0';
    printf("C[%d]:%s\n", fd, ev->buffer);
    nty_event_set(ev, fd, send_cb, reactor);
    nty_event_add(reactor->epfd, EPOLLOUT, ev);
  } else if (len == 0) {
    close(ev->fd);
    //printf("[fd=%d] pos[%ld], closed\n", fd, ev-reactor->events);
  } else {
    close(ev->fd);
    printf("recv[fd=%d] error[%d]:%s\n", fd, errno, strerror(errno));
  }
  return len;
}
int send_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  struct ntyevent *ev = ntyreactor_idx(reactor, fd);
  int len = send(fd, ev->buffer, ev->length, 0);
  if (len > 0) {
    printf("send[fd=%d], [%d]%s\n", fd, len, ev->buffer);
    nty_event_del(reactor->epfd, ev);
    nty_event_set(ev, fd, recv_cb, reactor);
    nty_event_add(reactor->epfd, EPOLLIN, ev);
  } else {
    close(ev->fd);
    nty_event_del(reactor->epfd, ev);
    printf("send[fd=%d] error %s\n", fd, strerror(errno));
  }
  return len;
}
int accept_cb(int fd, int events, void *arg) {
  struct ntyreactor *reactor = (struct ntyreactor*)arg;
  if (reactor == NULL) return -1;
  struct sockaddr_in client_addr;
  socklen_t len = sizeof(client_addr);
  int clientfd;
  if ((clientfd = accept(fd, (struct sockaddr*)&client_addr, &len)) == -1) {
    if (errno != EAGAIN && errno != EINTR) {
    }
    printf("accept: %s\n", strerror(errno));
    return -1;
  }
  int flag = 0;
  if ((flag = fcntl(clientfd, F_SETFL, O_NONBLOCK)) < 0) {
    printf("%s: fcntl nonblocking failed, %d\n", __func__, MAX_EPOLL_EVENTS);
    return -1;
  }
  struct ntyevent *event = ntyreactor_idx(reactor, clientfd);
  nty_event_set(event, clientfd, recv_cb, reactor);
  nty_event_add(reactor->epfd, EPOLLIN, event);
  printf("new connect [%s:%d], pos[%d]\n", 
    inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), clientfd);
  return 0;
}
int init_sock(short port) {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  fcntl(fd, F_SETFL, O_NONBLOCK);
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(port);
  bind(fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
  if (listen(fd, 20) < 0) {
    printf("listen failed : %s\n", strerror(errno));
  }
  return fd;
}
int ntyreactor_alloc(struct ntyreactor *reactor) {
  if (reactor == NULL) return -1;
  if (reactor->evblk == NULL) return -1;
  struct eventblock *blk = reactor->evblk;
  while (blk->next != NULL) {
    blk = blk->next;
  }
  struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  if (evs == NULL) {
    printf("ntyreactor_alloc ntyevents failed\n");
    return -2;
  }
  memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
  if (block == NULL) {
    printf("ntyreactor_alloc eventblock failed\n");
    return -2;
  }
  memset(block, 0, sizeof(struct eventblock));
  block->events = evs;
  block->next = NULL;
  blk->next = block;
  reactor->blkcnt ++; //
  return 0;
}
struct ntyevent *ntyreactor_idx(struct ntyreactor *reactor, int sockfd) {
  int blkidx = sockfd / MAX_EPOLL_EVENTS;
  while (blkidx >= reactor->blkcnt) {
    ntyreactor_alloc(reactor);
  }
  int i = 0;
  struct eventblock *blk = reactor->evblk;
  while(i ++ < blkidx && blk != NULL) {
    blk = blk->next;
  }
  return &blk->events[sockfd % MAX_EPOLL_EVENTS];
}
int ntyreactor_init(struct ntyreactor *reactor) {
  if (reactor == NULL) return -1;
  memset(reactor, 0, sizeof(struct ntyreactor));
  reactor->epfd = epoll_create(1);
  if (reactor->epfd <= 0) {
    printf("create epfd in %s err %s\n", __func__, strerror(errno));
    return -2;
  }
  struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  if (evs == NULL) {
    printf("ntyreactor_alloc ntyevents failed\n");
    return -2;
  }
  memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
  struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
  if (block == NULL) {
    printf("ntyreactor_alloc eventblock failed\n");
    return -2;
  }
  memset(block, 0, sizeof(struct eventblock));
  block->events = evs;
  block->next = NULL;
  reactor->evblk = block;
  reactor->blkcnt = 1;
  return 0;
}
int ntyreactor_destory(struct ntyreactor *reactor) {
  close(reactor->epfd);
  //free(reactor->events);
  struct eventblock *blk = reactor->evblk;
  struct eventblock *blk_next = NULL;
  while (blk != NULL) {
    blk_next = blk->next;
    free(blk->events);
    free(blk);
    blk = blk_next;
  }
  return 0;
}
int ntyreactor_addlistener(struct ntyreactor *reactor, int sockfd, NCALLBACK *acceptor) {
  if (reactor == NULL) return -1;
  if (reactor->evblk == NULL) return -1;
  //reactor->evblk->events[sockfd];
  struct ntyevent *event = ntyreactor_idx(reactor, sockfd);
  nty_event_set(event, sockfd, acceptor, reactor);
  nty_event_add(reactor->epfd, EPOLLIN, event);
  return 0;
}
int ntyreactor_run(struct ntyreactor *reactor) {
  if (reactor == NULL) return -1;
  if (reactor->epfd < 0) return -1;
  if (reactor->evblk == NULL) return -1;
  struct epoll_event events[MAX_EPOLL_EVENTS+1];
  int checkpos = 0, i;
  while (1) {
/*
    long now = time(NULL);
    for (i = 0;i < 100;i ++, checkpos ++) {
      if (checkpos == MAX_EPOLL_EVENTS) {
        checkpos = 0;
      }
      if (reactor->events[checkpos].status != 1) {
        continue;
      }
      long duration = now - reactor->events[checkpos].last_active;
      if (duration >= 60) {
        close(reactor->events[checkpos].fd);
        printf("[fd=%d] timeout\n", reactor->events[checkpos].fd);
        nty_event_del(reactor->epfd, &reactor->events[checkpos]);
      }
    }
*/
    int nready = epoll_wait(reactor->epfd, events, MAX_EPOLL_EVENTS, 1000);
    if (nready < 0) {
      printf("epoll_wait error, exit\n");
      continue;
    }
    for (i = 0;i < nready;i ++) {
      struct ntyevent *ev = (struct ntyevent*)events[i].data.ptr;
      if ((events[i].events & EPOLLIN) && (ev->events & EPOLLIN)) {
        ev->callback(ev->fd, events[i].events, ev->arg);
      }
      if ((events[i].events & EPOLLOUT) && (ev->events & EPOLLOUT)) {
        ev->callback(ev->fd, events[i].events, ev->arg);
      }
    }
  }
}
// 3, 6w, 1, 100 == 
// <remoteip, remoteport, localip, localport>
int main(int argc, char *argv[]) {
  unsigned short port = SERVER_PORT; // listen 8888
  if (argc == 2) {
    port = atoi(argv[1]);
  }
  struct ntyreactor *reactor = (struct ntyreactor*)malloc(sizeof(struct ntyreactor));
  ntyreactor_init(reactor);
  int i = 0;
  int sockfds[PORT_COUNT] = {0};
  for (i = 0;i < PORT_COUNT;i ++) {
    sockfds[i] = init_sock(port+i);
    ntyreactor_addlistener(reactor, sockfds[i], accept_cb);
  }
  ntyreactor_run(reactor);
  ntyreactor_destory(reactor);
  for (i = 0;i < PORT_COUNT;i ++) {
    close(sockfds[i]);
  }
  free(reactor);
  return 0;
}


相关文章
|
3月前
|
网络协议 安全 Linux
Linux C/C++之IO多路复用(select)
这篇文章主要介绍了TCP的三次握手和四次挥手过程,TCP与UDP的区别,以及如何使用select函数实现IO多路复用,包括服务器监听多个客户端连接和简单聊天室场景的应用示例。
105 0
|
3月前
|
存储 Linux C语言
Linux C/C++之IO多路复用(aio)
这篇文章介绍了Linux中IO多路复用技术epoll和异步IO技术aio的区别、执行过程、编程模型以及具体的编程实现方式。
145 1
Linux C/C++之IO多路复用(aio)
|
2月前
|
算法 Unix Linux
深入理解Linux内核调度器:原理与优化
本文探讨了Linux操作系统的心脏——内核调度器(Scheduler)的工作原理,以及如何通过参数调整和代码优化来提高系统性能。不同于常规摘要仅概述内容,本摘要旨在激发读者对Linux内核调度机制深层次运作的兴趣,并简要介绍文章将覆盖的关键话题,如调度算法、实时性增强及节能策略等。
|
3月前
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
857 3
|
3月前
|
资源调度 Linux 调度
Linux C/C++之线程基础
这篇文章详细介绍了Linux下C/C++线程的基本概念、创建和管理线程的方法,以及线程同步的各种机制,并通过实例代码展示了线程同步技术的应用。
48 0
Linux C/C++之线程基础
|
3月前
|
Linux C++
Linux C/C++之IO多路复用(poll,epoll)
这篇文章详细介绍了Linux下C/C++编程中IO多路复用的两种机制:poll和epoll,包括它们的比较、编程模型、函数原型以及如何使用这些机制实现服务器端和客户端之间的多个连接。
68 0
Linux C/C++之IO多路复用(poll,epoll)
|
3月前
|
网络协议 Linux 网络性能优化
Linux C/C++之TCP / UDP通信
这篇文章详细介绍了Linux下C/C++语言实现TCP和UDP通信的方法,包括网络基础、通信模型、编程示例以及TCP和UDP的优缺点比较。
80 0
Linux C/C++之TCP / UDP通信
|
2月前
|
Linux 网络安全 数据安全/隐私保护
Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
在 Linux 系统中,xxd 命令是一个强大的十六进制 dump 工具,可以将文件或数据以十六进制和 ASCII 字符形式显示,帮助用户深入了解和分析数据。本文详细介绍了 xxd 命令的基本用法、高级功能及实际应用案例,包括查看文件内容、指定输出格式、写入文件、数据比较、数据提取、数据转换和数据加密解密等。通过掌握这些技巧,用户可以更高效地处理各种数据问题。
222 8
|
2月前
|
监控 Linux
如何检查 Linux 内存使用量是否耗尽?这 5 个命令堪称绝了!
本文介绍了在Linux系统中检查内存使用情况的5个常用命令:`free`、`top`、`vmstat`、`pidstat` 和 `/proc/meminfo` 文件,帮助用户准确监控内存状态,确保系统稳定运行。
889 6
|
2月前
|
Linux
在 Linux 系统中,“cd”命令用于切换当前工作目录
在 Linux 系统中,“cd”命令用于切换当前工作目录。本文详细介绍了“cd”命令的基本用法和常见技巧,包括使用“.”、“..”、“~”、绝对路径和相对路径,以及快速切换到上一次工作目录等。此外,还探讨了高级技巧,如使用通配符、结合其他命令、在脚本中使用,以及实际应用案例,帮助读者提高工作效率。
141 3