自学内容网 自学内容网

C++ 功能使用技巧

1. 通过C执行shell指令,比如,在代码中查找进程名称,并进行终止

2.单实例线程操作

1. 要点

snprintf 函数被用于构建一个包含多个命令的字符串

在C语言中,system函数用于执行shell命令

在C或C++编程中,::mkdir 函数用于创建一个新的目录

该例子中,编译生成的进程名称为motion_realmachine

void system_kill_last() {
    // GDB 调试时,不用杀掉,否则会退出GDB
    char* pathVar = getenv("_");
    if (pathVar != NULL) {
        if (0 == strcmp(pathVar, "/usr/bin/gdb")) {
            printf("# GDB _=[%s]\n", pathVar);
            return;
        }
    }
    // 清理残余线程
    char kill_command[256] = {0x00};
    snprintf(kill_command, sizeof(kill_command),
            "sudo true ; ps -aux | grep motion_realmachine | grep -v grep | grep -v %d | "
            "awk '{print $2}' | xargs -r sudo kill -9",
            gettid()
    );
    // std::cout << "kill_command: " << kill_command << std::endl;
    int ret = system(kill_command);
    if (0 == ret) {
        sleep(1);
    }
}




int main(int argc, char *argv[]) {
    system_kill_last();
    return 0;
}
ret = ::mkdir("/tmp/er", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  • ret 是一个变量,用于存储mkdir函数的返回值。如果目录成功创建,mkdir通常返回0;如果失败,则返回-1,并设置errno以指示错误类型。
  • "/tmp/er" 是你想要创建的目录的路径。
  • S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH 是权限设置,这些宏在<sys/stat.h>中定义。具体来说:
    • S_IRWXU 代表用户(文件所有者)具有读、写和执行权限。
    • S_IRWXG 代表组具有读、写和执行权限。
    • S_IROTH 代表其他用户具有读权限。
    • S_IXOTH 代表其他用户具有执行权限。
    • 通过将这些宏使用位或运算符(|)组合,你为新目录设置了非常宽松的权限:所有者、组成员和其他用户都可以读、写和执行该目录(尽管S_IROTHS_IXOTH在这里可能有些多余,因为通常不会给其他用户写权限)。

2. 链接如下:

c++线程测试例程_ubuntu c++ sleep-CSDN博客

3.C++ 实现对文件状态的监控 Linux 内核中 inotify 子系统

 inotify 是用来监控目录的,而不是单个文件。当您对一个目录调 inotify_add_watch 时,您可以监控该目录内部发生的文件事件(如创建、删除、修改等)。注意路径末尾的斜杠(/),它表示您正在监控一个目录,不可以省略。

在事件处理循环中,您需要检查 event->name 是否与您关心的文件名匹配。由于您正在监控整个目录,因此您可能会收到与该目录中其他文件相关的事件。可以使用判断进行过滤,只监控testinotify文件。

"if (strcmp(event->name, "testinotify") == 0) {  
    // 处理与 "testinotify" 文件相关的事件  
}"

赋予testinotify文件读写权限

sudo chmod 777 ./tmp/testinotify
#include <sys/inotify.h>  
#include <unistd.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <limits.h>  
#include<iostream>
#include <fstream>
#define EVENT_BUF_LEN (1024 * (sizeof(struct inotify_event) + 16))  

bool read_string_from_file(std::string filename, std::string &value) {

    std::ifstream file(filename); 
    if (!file.is_open()) {  
        std::cerr << "Failed to open file: " << filename << std::endl;  
        return false;  
    }  

    // char line[256];
    std::string line;  
    if (std::getline(file, line)) {  // 使用 std::getline 读取整行  
        value = line;  
        std::cout << "Value: " << value << std::endl;  
        return true;  
    } else {  
        std::cerr << "Failed to read line from file: " << filename << std::endl;  
        return false;  
    } 

    std::string error_msg = "read_string_from_file " + filename + " error";
    perror(error_msg.c_str());
    return false;
}

std::string filename = "/home/gdp/test/testcmake/tmp/testinotify";
std::string filepath = "/home/gdp/test/testcmake/tmp/";
void print_tmp_command() {
    // std::cout << "testinotify" << std::endl;
    std::string values;
    
    bool ret = read_string_from_file(filename, values);
    if (!ret) {
        std::cout << "update_tmp_command() read_string_from_file error " << filename << std::endl;
        return;
    }
    // data_mtx_.lock();
    // motion_can_control_ = false;
    // data_mtx_.unlock();
}



int main() {  
    int fd = inotify_init();  
    if (fd < 0) {  
        perror("inotify_init");  
        exit(EXIT_FAILURE);  
    }  
  
    int wd = inotify_add_watch(fd, "/home/gdp/test/testcmake/tmp/", IN_DELETE | IN_CLOSE_WRITE | IN_MODIFY);  
    if (wd == -1) {  
        perror("inotify_add_watch");  
        close(fd);  
        exit(EXIT_FAILURE);  
    }  
  
    char buffer[EVENT_BUF_LEN];  
    ssize_t length;  


    while ((length = read(fd, buffer, EVENT_BUF_LEN)) > 0) {  
        const struct inotify_event *event;  
        char *ptr = buffer;  
  
        while (ptr < buffer + length) {  
            event = (const struct inotify_event *) ptr;  
            ptr += sizeof(struct inotify_event) + event->len;  
if (strcmp(event->name, "testinotify") == 0) {   
            if (event->len) {  
                if (event->mask & IN_DELETE) {  
                    printf("The file %s was deleted.\n", event->name);  
                } else if((event->mask & IN_CLOSE_WRITE) || (event->mask & IN_MODIFY))
                {
                    printf("event->wd: %d\n", event->wd);
                    // printf("eventmask: %d\n", event->mask);
                    printf("The file %s was writed and closed.\n", event->name);
                    print_tmp_command();
                }
                // 处理其他事件类型...  
            }  
}
        }  
    }  
  
    if (length < 0) {  
        perror("read");  
    }  
  
    inotify_rm_watch(fd, wd);  
    close(fd);  
  
    return 0;  
}

如果inotify_add_watch是个目录,则可以监控新建文件等操作,是个文件,则可以通过wd获取文件的内容,并执行打印出来的操作。

可以再运行一个shell脚本监控文件内容的变化,并显示出来

#!/bin/bash

# 定义刷新间隔(秒)
REFRESH_INTERVAL=0.05

file_name=$1

# 保存上一次的输出
last_output=""

# 无限循环
while true; do
    # 执行命令并获取输出,这里以显示时间为例
    output=$(cat $file_name)

    # 如果输出与上次不同,则打印
    if [[ "$output" != "$last_output" ]]; then
        clear
        echo "$output"
        last_output="$output"
    fi

    # 按照毫秒级等待
    sleep ${REFRESH_INTERVAL}s
done

新开一个终端1,运行如下指令,实时打印文件内容的变化

./watch_cat_file.bash ./tmp/testinotify

新开一个终端2,更改testinotify文件

文件中写入值 echo  "1" > testinotify  文件中追加  echo  "2" >> testinotify   

可以再终端1中,实时看到文件中数值的变化


原文地址:https://blog.csdn.net/weixin_50084948/article/details/142832408

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!