ubuntu buildroot 禁止进程多开

该博客介绍了在Ubuntu系统中,通过读写临时文件`/tmp/spid.dat`来检查并确保进程只有一个实例运行的方法。代码示例中,首先尝试读取`spid.dat`文件,如果文件内容对应的有效进程存在,则返回,否则创建新进程并写入当前PID到文件,以防止进程多开。

环境:ubuntu, buildroot

功能:禁止进程多开

    char pidBuf[10];
    const char *wiwtool_spid = "/tmp/spid.dat";
    if ((fp = fopen(wiwtool_spid, "r")) != NULL) {
        fgets(pidBuf, 10, fp);
        char pidPath[20];
        sprintf(pidPath, "/proc/%s", pidBuf);
        if (access(pidPath, F_OK) == 0) {
            //wdbg_log("the process is running\n");
            fclose(fp);
            return 0;
        }
        fclose(fp);
    }
    pid_t pid = getpid();
    if ((fp = fopen(wiwtool_spid, "wb+")) == NULL) {
        //wdbg_log("cannot run...\n");
        return 0;
    }
    fprintf(fp, "%d", pid);
    fclose(fp);

 

参考:https://blog.youkuaiyun.com/fishrain/article/details/8241624

Ubuntu上搭建Buildroot环境可以按照以下步骤进行: 1. **安装必要的依赖项**: 首先,确保你的系统是最新的,并安装一些必要的软件包。 ```bash sudo apt-get update sudo apt-get upgrade sudo apt-get install -y git build-essential libncurses5-dev libncursesw5-dev libssl-dev flex bison gawk gettext libfile-which-perl libpython3-dev python3-distutils libxml-parser-perl sed cvs subversion coreutils unzip texi2html docbook-utils gawk python-patch util-linux 2. **下载Buildroot源码**: 使用git克隆Buildroot的官方仓库。 ```bash git clone https://github.com/buildroot/buildroot.git cd buildroot ``` 3. **配置Buildroot**: 你可以选择默认配置,也可以根据需要进行自定义配置。 ```bash make menuconfig ``` 在这个菜单界面中,你可以选择目标架构、工具链、文件系统等。 4. **编译Buildroot**: 配置完成后,始编译。编译过程可能需要一些时间,取决于你的系统性能和配置选项。 ```bash make ``` 5. **安装和测试**: 编译完成后,你可以在`output/images`目录下找到生成的文件系统镜像。你可以使用这些镜像在目标设备上进行测试。 ```bash ls output/images ``` 你可以使用QEMU进行测试: ```bash qemu-system-<arch> -M <machine> -kernel output/images/<kernel_image> -drive file=output/images/<rootfs_image>,if=<interface>,format=<format> -append "root=/dev/<root_device>" ``` 替换`<arch>`、`<machine>`、`<kernel_image>`、`<rootfs_image>`、`<interface>`和`<root_device>`为实际值。 通过以上步骤,你就可以在Ubuntu上搭建并使用Buildroot环境了。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值