Linux C单例执行程序

保证系统种某个进程只有一个实例在运行,举例下面2种方法:

  1. 使用popen执行ps -ef 后分析结果
  2. 使用文件锁 代码如下:
第一种:
int exist(void){
    FILE* fp;
    char buf[10] = {0};
    char command[100] = {0};
    sprintf(command, "ps -ef | grep %s | grep -v grep | wc -l", argv[0]);
    printf("command : %s\n", command);
    fp = popen(command, "r");
    if(fp == NULL)
    {
        perror("popen error!");
        return -1;
    }
    if((fgets(buf, 10, fp))!= NULL)
    {
        if(atoi(buf) != 0)
        {
            printf("%s is running!", argv[0]);
        }
        else
        {
            printf("%s is not running!", argv[0]);
        }
    }
    pclose(fp);
    return 0;
}
第二种
#define LOCKFILE  "/var/run/lte8718wdt.pid"
#define LOCKMODE  (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

int lockfile(int fd)
{
    struct flock fl;
    int ret;

    fl.l_type = F_WRLCK;
    fl.l_start = 0;
    fl.l_whence = SEEK_SET;
    fl.l_len = 0; // lock the whole file

    ret = fcntl(fd, F_SETLK, &fl);
    if(-1 == ret)
        perror("set file lock error!");
    return ret;
}
int is_running(const char* filename)
{
    int fd;
    char pid[15] = {0};

    fd = open(filename, O_RDWR | O_CREAT, LOCKMODE);
    if(fd < 0)
    {
        perror("open file error!");
        return -1;
    }
    if(lockfile(fd) == -1)
    {
        if(errno == EACCES || errno == EAGAIN)
        {
            printf("%s already locked\n", filename);
            close(fd);
            return -1;
        }
    }
    ftruncate(fd, 0);
    sprintf(pid, "%ld", (long)getpid());
    write(fd, pid, sizeof(pid) - 1);
    return 0;
}

Linux Kernel Driver自动创建设备

static int watchdog_init(void)
{
	wdt_major = register_chrdev(0, "lte8718wdt", <e8718wdt_fopts);
	wdt_class = class_create(THIS_MODULE, wdt_name);
	device_create(wdt_class, NULL, MKDEV(wdt_major, 0), NULL, wdt_name);
}

static int watchdog_exit(void)
{
	device_destroy(wdt_class, MKDEV(wdt_major, 0));
	class_destroy(wdt_class);
	unregister_chrdev(lte8718wdt_major, "lte8718wdt");
}

Linux清理系统文件缓存

OS : opensuse11.4

echo 1>/proc/sys/vm/drop_caches
echo 2>/proc/sys/vm/drop_caches
echo 3>/proc/sys/vm/drop_caches

wordpress自定义页面输出所有文章列表

  1. Copy 一份所用主题的 page.php,改名为 page-allpost.php

  2. 打开 page-allpost.php,找到类似下面这个文章内容输出函数

<?php the_content(); ?>

3.添加如下代码:

<div id="page-allpost">
     <table>
         <strong>All Post</strong>
         <tr>
             <td><strong>S.No</strong></td>
             <td><strong>Published Date</strong></td>
             <td><strong>Post Header</strong></td>
         </tr>
     <?php $count_posts = wp_count_posts(); $published_posts = $count_posts->publish;
     query_posts( 'posts_per_page=-1' );
     while ( have_posts() ) : the_post();
         echo '<tr>';
         echo '<td> </td>';
         echo '<td> </td>';
         echo '<td> </td>';
         echo '</tr>';
         echo '<tr>';
         echo '<td width="100">'.$published_posts.'</td>';
         echo '<td width="200">';
         the_time(get_option( 'date_format' ));
         echo '</td ><td><a href="';
         the_permalink();
         echo '" title="'.esc_attr( get_the_title() ).'">';
         the_title();
         echo '</a></td></tr>';
         $published_posts--;
     endwhile;
     wp_reset_query(); ?>
     </table>
 </div>

4.新建一个页面,别名为 allpost

Linux Driver Makefile Demo

ubuntu下测试通过

ifneq ($(KERNELRELEASE),)
     obj-m := hello.o
else
     KERNELDIR ?= /lib/modules/$(shell uname -r)/build
     PWD := $(shell pwd)
default:
     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif