分类 编程语言 下的文章

c++中,调用c语言函数时要对函数添加声明extern "C",这是因为c和c++的符号表机制不一样导致的。

假设存在函数fun,经过gcc编译后,该函数在符号表中的名字是fun,但是经过g++编译后,该函数在符号表中的结果是xxfunxx,c++编译器会在函数两侧加上一些随机字符。因为两者的编译结果不统一,所以混合使用的时候程序找不到符号,无法运行(甚至都无法编译)。一个典型的错误就是(参考实际开发中出现的一个案例:一次undefined reference to *的编译错记录):

/tmp/cccZeFer.o: In function `main':
main.cpp:(.text+0xf): undefined reference to `add'
collect2: error: ld returned 1 exit status

extern "C"语句的作用就是告诉c++编译器:我这个是c语言函数,不要按照你的格式来修改这个函数的符号,按照我的标准来就行了。于是c++编译器编译的时候就按照c的规范来,把两者符号统一。

示例

#include<stdio.h>

int add(int i, int j) {
    return i + j;
} 

int main() {
    printf("%d\n", add(1, 2));
    return 0;
}

代码中有一个函数add,看看它经过gcc和g++编译后分别变成了什么。

使用gcc编译:

image.png

使用g++编译:

imagedb520d0468aeb499.png

两者对比,很明显能看到,使用g++编译后的add函数,符号表前后加上了一些特殊字符。而gcc编译出来的符号就是add本身。

给add函数加上extern "C"声明后:

#ifdef __cplusplus
extern "C" {
#endif

int add(int i, int j) {
    return i + j;
} 

#ifdef __cplusplus
}
#endif

再次使用g++编译,add的符号就和gcc编译出来的符号一样了:

image416e247cd34d6a8f.png

使用logging模块打印日志的时候遇到错误:

Traceback (most recent call last):
  File "saas_utils.py", line 106, in <module>
    sangfor_login("192.168.10.1", login_user, login_pwd)
  File "saas_utils.py", line 72, in sangfor_login
    logging.INFO("%s: %s" % (type(cookie_str), cookie_str))
TypeError: 'int' object is not callable

网上查了一下,出现这个问题的原因是函数或者变量命名错误了,但是检查了一下没有发现有重复的函数和变量名。

后面仔细推敲了一下才发现函数名字写错了,logging.info被我写成了loggin.INFOloggin.INFO是日志的调试级别,当成函数使用当然报错了。

一、问题

使用percpu变量时编译报错:

### make  in drv
make -C /usr/src/linux-wm M=/Packet/ac/module/saas_ctl/drv modules
  CC [M]  /Packet/ac/module/saas_ctl/drv/saas_action.o
  CC [M]  /Packet/ac/module/saas_ctl/drv/saas_main.o
/Packet/ac/module/saas_ctl/drv/saas_main.c: In function 'match_saas_rules':
/Packet/ac/module/saas_ctl/drv/saas_main.c:395: error: 'per_cpu__g_ssl_cpu_data' undeclared (first use in this function)
/Packet/ac/module/saas_ctl/drv/saas_main.c:395: error: (Each undeclared identifier is reported only once
/Packet/ac/module/saas_ctl/drv/saas_main.c:395: error: for each function it appears in.)
cc1: warnings being treated as errors
/Packet/ac/module/saas_ctl/drv/saas_main.c:395: error: type defaults to 'int' in declaration of 'type name'
/Packet/ac/module/saas_ctl/drv/saas_main.c:395: error: invalid type argument of 'unary *' (have 'int')
make[3]: *** [/Packet/ac/module/saas_ctl/drv/saas_main.o] Error 1
make[2]: *** [_module_/Packet/ac/module/saas_ctl/drv] Error 2
make[1]: *** [all] Error 2
make: *** [drv] Error 2

原因:

percpu变量在其他模块定义的,当前模块使用前要声明:

DECLARE_PER_CPU(type, name);

一、class和struct的区别

C++中class和struct的区别:

  1. 继承权限,struct的默认继承权限为public,class的默认继承权限为private。
  2. 访问权限,struct的默认访问权限为public,class的默认访问权限为private。

网上还流传着其他一些的区别,但总体来说最大的区别就是这两点,其他的区别或许并不常用到。

二、C和C++中struct的区别

第一、C++中的struct可以定义成员函数,但是C语言不行,C语言中的结构体可以定义函数指针。

例如以下代码:

struct stu_st {
    void print();
};

int main() {
    return 0;
}

使用GCC编译会报错:

第二、C语言声明结构体必须要加struct,C++不用。

C语言中如果不加struct声明变量,编译器会报错:

如若不想加struct修饰,则需要使用typedef来重新定义类型。

第三、C语言中空结构体大小为0,C++中结构体大小为1。

相同的代码:

#include "stdio.h"

struct stu_st {
};

int main() {
    printf("%u\n", (unsigned int)sizeof(struct stu_st));
    return 0;
}

使用gccg++编译结果也不一样:

一、linux内核模块

Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合。之所以提供模块机制,是因为Linux本身是一个单内核。单内核由于所有内容都集成在一起,效率很高,但可扩展性和可维护性相对较差,模块机制可弥补这一缺陷。

Linux模块可以通过静态或动态的方法加载到内核空间,静态加载是指在内核启动过程中加载;动态加载是指在内核运行的过程中随时加载。

一个模块被加载到内核中时,就成为内核代码的一部分。模块加载入系统时,系统修改内核中的符号表,将新加载的模块提供的资源和符号添加到内核符号表中,以便模块间的通信。

内核提供了接口函数来注册我们自己的模块:

// linux/init.h
/**
 * module_init() - driver initialization entry point
 * @x: function to be run at kernel boot time or module insertion
 * 
 * module_init() will either be called during do_initcalls() (if
 * builtin) or at module insertion time (if a module).  There can only
 * be one per module.
 */
#define module_init(x)    __initcall(x);


/**
 * module_exit() - driver exit entry point
 * @x: function to be run when driver is removed
 * 
 * module_exit() will wrap the driver clean-up code
 * with cleanup_module() when used with rmmod when
 * the driver is a module.  If the driver is statically
 * compiled into the kernel, module_exit() has no effect.
 * There can only be one per module.
 */
#define module_exit(x)    __exitcall(x);

module_initmodule_exit分别用于加载和卸载模块,其中的x的函数声明为:

int f(void);

二、编译一个自己的模块

2.1 编写一个模块

首先下载内核源码:yum groupinstall "Development Tools",、安装好后源码在/usr/src/kernels/2.6.32-754.9.1.el6.x86_64/注意最后面的一串内核版本,可能和uname -r不一致

然后准备测试代码test.c

#include <linux/module.h>
#include <linux/init.h>

static int test_init(void) {
    printk("===Test Module Start!===\n");
    return 0;
}

static int test_exit(void) {
    printk("===Test Module Has Been Closed!===");
    return 0;
}

module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("MaQian");

其中linux/module.hlinux/init.h是必须包含的两个问头文件,MODULE_LICENSE是模块许可证声明,一般设置为GPL,可选。MODULE_AUTHOR是模块作者,可选。

2.2 Makefile编写

# test是模块的名字
obj-m := test.o
# 内核代码的位置
KERNEL := /usr/src/kernels/2.6.32-754.9.1.el6.x86_64/
# 当前模块的目录
PWD := $(shell pwd)

modules:
    $(MAKE) -C $(KERNEL) M=$(PWD) modules

.PHONY: clean
clean:
    rm -rf *.o *.ko

如果不出意外,执行make即可生成我们要的模块文件test.ko,但是一般情况下,第一次编译是会失败的,会报错,错误的解决方案在最下面。

2.3 挂载模块到系统

挂载驱动使用insmod,卸载使用rmmod,先开启另外一个窗口开始打印调试信息:

> while :; do dmesg -c; sleep 1; done

插入模块到系统:

> insmod test.ko

输出:

===Test Module Start!===

卸载:

rmmod test.ko

输出:

===Test Module Has Been Closed!===

三、错误处理

3.1 Kernel configuration is invalid

make -C /usr/src/kernels/2.6.32-754.6.3.el6.x86_64/ M=/home/maqian/code/hello_netfilter modules
make[1]: Entering directory `/usr/src/linux-2.6.32'

  ERROR: Kernel configuration is invalid.
         include/linux/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.


  WARNING: Symbol version dump /usr/src/linux-2.6.32/Module.symvers
           is missing; modules will have no dependencies and modversions.

  Building modules, stage 2.
/usr/src/linux-2.6.32/scripts/Makefile.modpost:42: include/config/auto.conf: No such file or directory
make[2]: *** No rule to make target `include/config/auto.conf'.  Stop.
make[1]: *** [modules] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.32'
make: *** [modules] Error 2

进入到内核代码目录,执行sudo make oldconfig && sudo make prepare,一路回车确认。

3.2 Symbol version dump xxx is missing

make -C /usr/src/kernels/2.6.32-754.6.3.el6.x86_64/ M=/home/maqian/code/hello_netfilter modules
make[1]: Entering directory `/usr/src/linux-2.6.32'

  WARNING: Symbol version dump /usr/src/linux-2.6.32/Module.symvers
           is missing; modules will have no dependencies and modversions.

  Building modules, stage 2.
  MODPOST 0 modules
/bin/sh: scripts/mod/modpost: No such file or directory
make[2]: *** [__modpost] Error 127
make[1]: *** [modules] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.32'
make: *** [modules] Error 2

还是在刚刚的目录执行:sudo make scripts

当一个模块编译完成之后,使用insmod加载,使用rmmod加载,lsmod可以查看已经挂载的模块,modinfo查看一个模块的详细信息。

3.3 insmod: error inserting 'test.ko': -1 Invalid module format

插入驱动报错:

insmod: error inserting 'test.ko': -1 Invalid module format

dmesg错误:

test: no symbol version for module_layout

检查驱动内核版本是否一致,最开始出现这个问题是因为内核代码是自己手动下载的,后面改成yum下载就好了。

区别:

  1. 指针是一个变量类型,引用只是一个变量别名。
  2. 指针可以不用初始化,引用必须初始化。
  3. 指针可以指向空地址,引用不能指向空。
  4. 指针初始化后可以修改,引用不能修改。

其他:

  • 引用本质上也是一个指针,内部实现是一个常量指针。
  • C++中一般建议使用引用,不要使用指针。函数传值建议使用const引用。

 一、条件变量

条件变量是多线程的一种同步方式,它允许多个线程以无竞争的方式等待特定事件发生。无竞争的意思是,当条件满足时,条件满足这个讯号会发送给所有的监听者线程,但多个线程中只有一个能获取到特定事件。条件变量需要配合互斥量一起使用。

相关数据结构和函数:

#include <pthread.h>

// 销毁条件变量
int pthread_cond_destroy(pthread_cond_t *cond);
// 初始化条件变量
int pthread_cond_init(pthread_cond_t *restrict cond,
       const pthread_condattr_t *restrict attr);
// 初始化条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

条件变量的数据结构为pthread_cond_t,和其他同步的数据结构一样,也提供了两种方式来初始化。一种是通过赋值的方式,一种是通过函数的方式。在使用pthread_cond_init对初始化条件变量的时候,attr参数一般为NULL。

对条件变量加锁的方式:

#include <pthread.h>

// 等待条件满足
int pthread_cond_wait(pthread_cond_t *restrict cond,
       pthread_mutex_t *restrict mutex);
// 带有超时条件的等待
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
       pthread_mutex_t *restrict mutex,
       const struct timespec *restrict abstime);

在对条件变量加锁的时候,需要传入一把已经处于加锁状态的互斥量,此时函数会把当前线程放到条件等待的列表上并对互斥量解锁。此时线程进入条件等待状态,当有信号到达时便会触发。

通知条件满足的函数:

#include <pthread.h>
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);

pthread_cond_signal函数会唤醒条件等待队列上的至少一个线程,pthread_cond_broadcast会唤醒条件队列上的所有线程。

条件队列一般用于消息队列,用于统一、协调多个生产者和消费者之间的竞争关系。

二、使用示例

以下使用了三个线程作为消费者,分别从全局队列上获取消息消费:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include "log.h"

#define THREAD_COUNT 3

// 消息结构
struct msg_st {
    unsigned int msg_id;
    struct msg_st *next;
};

static struct msg_st *msg_queue = NULL;
static pthread_cond_t g_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

// 消费线程函数
void *handle_msg(void *arg) {
    struct msg_st *msg;
    while (1) {
        pthread_mutex_lock(&g_mutex);
        while (msg_queue == NULL) {
            pthread_cond_wait(&g_cond, &g_mutex);
        }

        // 提取消息
        msg = msg_queue;
        msg_queue = msg_queue->next;
        pthread_mutex_unlock(&g_mutex);

        // 退出
        if (msg->msg_id == (unsigned int) -1) {
            info("Thread 0x%x exit!", (unsigned int) pthread_self());
            break;
        }

        info("Thread 0x%x: msg_id = %u", (unsigned int) pthread_self(), msg->msg_id);
    }
    return NULL;
}

// 生产消息函数
void create_msg(struct msg_st *msg) {
    pthread_mutex_lock(&g_mutex);
    msg->next = msg_queue;
    msg_queue = msg;
    pthread_mutex_unlock(&g_mutex);
    pthread_cond_signal(&g_cond);
}

int main() {
    int i;
    struct msg_st msg[10];
    pthread_t pid[THREAD_COUNT];

    debug("start create threads");
    // 创建3个线程作为消费者
    for (i = 0; i < 3; i++) {
        pthread_create(&pid[i], NULL, handle_msg, NULL);
    }

    debug("start create msgs");
    // 生产10个消息
    for (i = 0; i < 10; i++) {
        msg[i].msg_id = i + 1;
        msg[i].next = NULL;
        create_msg(&msg[i]);
    }

    // 休眠1秒,确保所有消息都被消费者消费完成
    sleep(1);

    // 退出所有线程
    debug("start create exit msgs");
    for (i = 0; i < THREAD_COUNT; i++) {
        msg[i].msg_id = (unsigned int) -1;
        msg[i].next = NULL;
        create_msg(&msg[i]);
    }

    // 回收所有线程
    debug("start join threads");
    for (i = 0; i < THREAD_COUNT; i++) {
        pthread_join(pid[i], NULL);
    }

    debug("program end");

    return 0;
}

运行结果,10个消息分别被3个线程打印出来了:

三、其他

3.1 “惊群”效应

条件变量是否存在惊群效应呢?

不会,线程执行wait操作的时候会被放到一个条件等待队列里面去。当条件满足的时候,系统会自动选择队列前面的线程来消费队列。

3.2 为什么wait前要加锁,这样不会死锁吗?

不会,执行wait操作时需要的是一把已经加锁的互斥量,这个锁在wait函数中会解开。

这样做的目的:如果在wait前没有加锁,生产者线程产生了一个消息并发送信号,再执行wait后信号就丢失了。

3.3 生产者的信号为什么放在unlock之后?

wait收到信号之后要对互斥量加锁,此时锁还被生产者持有,消费者依旧还要等待锁的释放,才能持有锁。对消费者而言,最理想的情况就是生产者产生消息后,我立马就能读取消息,此时的互斥量是用来和其他消费者线程同步的,而不是和生产者线程同步。

linux c获取文件路径和文件名

linux提供了两个函数分别用来获取文件所在的目录和文件名:

char *dirname(char *path);
char *basename(char *path);

它们被包含在头文件libgen.h中,dirname取得的路径名不包含最后的/basename取自于最后一个下划线后的内容。

以下是几个示例:

路径dirnamebasename
/usr/lib/usrlib
/usr//usr
usr.usr
...
.....

通过这两个函数我们就可以写出属于我们自己的dirname和basename命令了:

#include <libgen.h>
#include <stdio.h>

int main(int argc, char **argv) {
    if (argc < 2) {
        printf("Usage: %s path ...\n", argv[0]);
        return 0;
    }

    for (int i = 1; i < argc; i++) {
        printf("%s\n", dirname(argv[i])); // basename直接替换这里的函数即可
    }

    return 0;
}

编译运行:

> gcc dirname.c -o dirname
> ./dirname /usr/local /data/
/usr
/data

实际上linux也内置了dirnamebasename两个命令:

> basename /usr/lib/libDeployPkg.so.0 
libDeployPkg.so.0
> dirname /usr/local/ /data
/usr
/

一、原理

linux支持多进程间共享打开文件,即同一时刻允许多个进程同时打开同个文件,每个进程之间的读写操作互不影响。为了实现这一个机制,linux内核使用了三种数据结构来表示打开的文件,它们之间的关系决定了在文件共享方面一个进程对另一个进程可能产生的影响。

1.1 内核数据结构

每个进程的进程表中有一个记录项,包含了当前进程所有打开的文件描述符,它包含了一个指向文件表项的指针和文件描述符标志。内核中,为所有打开的文件维持一张表,它包含了以下内容:

  • 当前文件打开的状态:以何种方式打开的该文件,只读、只写或是可读可写等。
  • 当前文件的偏移量:当前文件指针所处的位置。
  • 指向该文件节点表的指针:节点包含了当前文件的属性信息。

每个文件的信息被封装在一个v节点表项中,包含了当前文件的文件名、所有者以及inode等信息。

三者之间的状态关系为:

1.2 多进程共享同一个文件

对于多个进程打开的同一个文件,其状态关系为:

正因为每个文件描述符都有一个属于自己的文件表项,所以每个进程间的文件指针偏移相互独立,互相读写不干扰。但是打开同一个文件的时候v节点指针都指向同一个节点:

  • 每次完成write后,文件表项的当前文件指针偏移量也会立马加上写入的字节数。
  • 如果打开文件的时候加了O_APPEND参数,每次写入数据前会先把偏移量设置到文件末尾。
  • 通过lseek函数只修改当前文件偏移量,不进行任何I/O操作。

有一个要注意的是,每次fork进程后,子进程会复制父进程的文件描述符,两者相互独立。

二、dup和dup2

dup和dup2都可以用来复制一个现有的文件描述符,其用法如下:

#include <unistd.h>

int dup(int fd);
int dup2(int fd1, int fd2);

dup函数直接把复制后的文件描述符返回,返回的一定是当前文件描述符表中的最小数值。

对于dup2,可以通过fd2表示新描述符的值,如果fd2已经打开,系统会先关闭。如果fd1等于fd2,则直接返回不关闭。

复制过后的文件描述符共享一个文件表项,共享后的状态如下:

我们可以通过一个程序来验证这一个结论:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() {
    char buff[6] = { 0 };
    int fd_1, fd_2;

    fd_1 = open("data.txt", O_RDONLY);
    if (fd_1 == -1) {
        perror("open file error");
        return -1;
    }

    fd_2 = dup(fd_1);
    if (fd_2 == -1) {
        perror("dup error");
        return -1;
    }

    if (read(fd_1, buff, 5) == -1) {
        perror("read error at fd_1");
        return -1;
    }
    printf("fd_1 read: %s\n", buff);

    if (read(fd_2, buff, 5) == -1) {
        perror("read error at fd_2");
    }
    printf("fd_2 read: %s\n", buff);

    close(fd_1);
    close(fd_2);

    return 0;
}

上面的代码中通过fd_1打开文件data.txtfd_2复制fd_1,两个文件描述符文件从文件中读取5个字节数据并打印出来。

我们编译代码执行:

# 先写十个字节数据到文件
> echo "HelloWorld" > data.txt
> mkdir debug
# 编译
> gcc dup.c -o debug/dup
# 执行
>  ./debug/dup 
fd_1 read: Hello
fd_2 read: World

可以看到,fd_2读取的数据是从第5个字节开始,即从fd_1读完偏移处开始,两者确实共享了同一个文件表项。