• 正文
  • 相关推荐
申请入驻 产业图谱

某通信公司面试题,你会几道?

06/20 10:15
309
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

笔试部分

1.描述下面代码中两个static各自的含义:

static?void?func(void)
{
? ??static?unsigned?int?i;?
}

参考答案:

行1,static表示静态函数,该函数只有当前文件的其他函数才可以调用它

行3,局部静态变量

生存周期:从程序运行到程序结束

作用域:只有当前函数才可以访问

段位置:全局data段【不是栈区】,并且每次访问都会保存上一次执行的结果

2. 写出执行下面代码后变量a的值:

unsigned?int?a,b=3;
void?move(unsigned?int?*p,unsigned?int?val)
{
?p=&val; ?
? ??
}

void?main(void)
{
?a = b++;
?move(&a,b); ? ? ?
}

参考答案:3

解析:

    行11执行完

先将b的值赋值给a,然后b自加

    行3,调用move后

move函数的形参p指向全局变量a,指针变量p中的值是a的地址

全局变量b的值4赋值给形参val,变量val中的值是4

行5执行完

将val的地址赋值给指针变量p,p不再指向a,转而指向了变量val

本题主要考察传值传址的区别,这是新手最不容易理解的一个知识点。

3. 在32位的单片机系统中,下面的结构体长度是多少?

typedef?struct
{
? ? short a;
? ??char?b;
? ??char?C;
? ??int?d; ? ??
}struct1;
typedef?struct
{
? ??char?a;
? ? short b;
? ??unsigned?char?c;
? ??int?d;
}struct2;

参考答案:8/12

解析:

主要是字节对齐导致的问题,struct1,struct2各成员在内存中分布如下:

实际项目开发中,为了保证结构体字节对齐,往往使用以下宏来保证数据不存在歧义。

#pragma?pack(1)
typedef?struct
{
? ??char?a;
? ? short b;
? ??unsigned?char?c;
? ??int?d;
}struct2;
#pragma

4. 请使用typedef定义一个数据类型func_t为指向void型函数的函数指针,再使用此数据类型定义一个指向void型函数的函数指针,并通过此指针来调用函数test。

参考答案:

#include?<stdio.h>
typedef?void?(*func_t)(int?data)?;

void?testfunc(int?data)
{
printf("yikou linux %dn",data);
}

int?main(int?argc,?char?**argv)
{
func_t?pfunc;

?pfunc = testfunc;

?pfunc(9);
}

函数名也是个地址,我们可以让函数指针指向一个函数。

linux内核中大量使用函数指针,各种不同的外设向子系统注册操作函数集,子系统通过这些操作函数集对外设做不同的操作。

比如下面是字符设备操作函数集,结构体定义:

struct?file_operations?{
?struct?module?*owner;
loff_t?(*llseek) (struct file *,?loff_t,?int);
ssize_t?(*read) (struct file *,?char?__user *,?size_t,?loff_t?*);
ssize_t?(*write) (struct file *,?constchar?__user *,?size_t,?loff_t?*);
ssize_t?(*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t?(*write_iter) (struct kiocb *, struct iov_iter *);
int?(*iterate) (struct file *, struct dir_context *);
int?(*iterate_shared) (struct file *, struct dir_context *);
unsigned?int?(*poll)?(struct file *, struct poll_table_struct *);
long?(*unlocked_ioctl) (struct file *,?unsignedint,?unsignedlong);
long?(*compat_ioctl) (struct file *,?unsignedint,?unsignedlong);
int?(*mmap) (struct file *, struct vm_area_struct *);
int?(*open) (struct inode *, struct file *);
int?(*flush) (struct file *,?fl_owner_t?id);
int?(*release) (struct inode *, struct file *);
int?(*fsync) (struct file *,?loff_t,?loff_t,?int?datasync);
int?(*fasync) (int, struct file *,?int);
int?(*lock) (struct file *,?int, struct file_lock *);
ssize_t?(*sendpage) (struct file *, struct page *,?int,?size_t,?loff_t?*,?int);
unsigned?long?(*get_unmapped_area)(struct file *,?unsigned?long,?unsigned?long,?unsigned?long,?unsigned?long);
int?(*check_flags)(int);
int?(*flock) (struct file *,?int, struct file_lock *);
ssize_t?(*splice_write)(struct pipe_inode_info *, struct file *,?loff_t?*,?size_t,?unsignedint);
ssize_t?(*splice_read)(struct file *,?loff_t?*, struct pipe_inode_info *,?size_t,?unsignedint);
int?(*setlease)(struct file *,?long, struct file_lock **,?void?**);
long?(*fallocate)(struct file *file,?int?mode,?loff_t?offset,
? ? ?loff_t?len);
void?(*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef?CONFIG_MMU
unsigned?(*mmap_capabilities)(struct file *);
#endif
ssize_t?(*copy_file_range)(struct file *,?loff_t, struct file *,
? ?loff_t,?size_t,?unsignedint);
int?(*clone_file_range)(struct file *,?loff_t, struct file *,?loff_t,
? ?u64);
ssize_t?(*dedupe_file_range)(struct file *, u64, u64, struct file *,
? ?u64);
} __randomize_layout;

5.请编写宏定义实现以下功能:

    1)将无符号整数a的第1位置1,同时保证其它位的值不改变;
a |=?0x1<<1;

默认位数从0开始计。

    2)将无符号整数b的第5位清0,同时保证其它位的值不改变;
b &=(~(0x1<<5));
    3)计算出任意结构体类型的常数组(如struct tt tab[])的元素个数
#define?ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

main()
{
?printf("array num:%dn",ARRAY_SIZE(tab));
}

宏定义在内核中也频繁的使用,来看下等待队列宏定义:

#define?wait_event(wq_head, condition) ? ? ?
do { ? ? ? ? ?
?might_sleep(); ? ? ? ?
?if?(condition) ? ? ? ?
? break; ? ? ? ?
?__wait_event(wq_head, condition); ? ? 
} while (0)
#define?__wait_event(wq_head, condition) ? ? 
?(void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, 
? ? ? ?schedule())
#define?___wait_event(wq_head, condition, state, exclusive, ret, cmd) ?
({ ? ? ? ? ?
?__label__ __out; ? ? ? 
?struct wait_queue_entry __wq_entry; ? ? 
?long __ret = ret;?/* explicit shadow */? ? 
? ? ? ? ? 
?init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); 
?for (;;) { ? ? ? ?
? long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);
? ? ? ? ? 
if?(condition) ? ? ? 
? ?break; ? ? ? 
? ? ? ? ? 
if?(___wait_is_interruptible(state) && __int) { ? 
? ?__ret = __int; ? ? ?
? ?goto __out; ? ? ?
? } ? ? ? ?
? ? ? ? ? 
? cmd; ? ? ? ?
?} ? ? ? ? 
?finish_wait(&wq_head, &__wq_entry); ? ? 
__out: __ret; ? ? ? ? 
})

要完全看懂这段代码,还是需要一定功底的,不光要看懂语法,还要了解内核相关的其他子系统原理,1个月5个月1年2年????

6.请按照说明实现下面的函数:

/*功能:把十六进制数转换为字符,如0xA8转换为字母A和数字8
*参数:hex是待转换的十六进制数;char1和char2是转换后的字符的存储指针
*返回值:返回0表示转换成功,返回-1表示参数错误或转换失败*/
unsigned?char?hex2char(unsigned?char?ch)
{
printf("ch:%dn",ch);
if?((ch >=?0) && (ch <=?9)) {
return?ch +?'0';
?}
if?((ch >=?0xa) && (ch <=?0xf)) {
return?ch -?10?+?'A';
?}
return?(unsignedchar)0xff;
}

int?hex_to_chars(unsigned?char?hex,?char?*charl,?char?*char2)
{
? ??unsignedchar?high,low;

?low = hex&0xf;
?high = (hex>>4)&0xf;

?charl[0] = hex2char(low);
?char2[0] = hex2char(high);
}

int?main(int?argc,?char?**argv)
{
unsignedchar?data =?0x18;
char?char1[10]={0};
char?char2[10]={0};

?hex_to_chars(data,char1,char2);

printf("0x%s%sn",char2,char1);
}

本题主要考察数据在内存的形式相关知识点,在实际应用中可以说非常广,很不错的一道题目。

读者可以尝试下面一个问题

如何将16进制的字符串,转换成对应的16进制整数?

字符串数组
char?buf[]="a8";

将字母a和8拼成0xa8,赋值给hex
unsigned?char?hex;

7. i2c编程题

请根据PCAxxxxx这款芯片的数据手册,编写芯片的驱动代码,要求涵盖芯片90%以上的功能:可以忽略INT(中断)引脚的功能:可以使用标准C语言或伪代码进行编写;I2C总线驱动部分,可以只设计驱动接口,不进行具体实现。

参考答案,下面是基于linux的i2c驱动架构:

#define?YIKOU_MAJOR 500
#define?YIKOU_MINOR 0

struct?yikou_device?{
struct?cdev?cdev;
struct?i2c_client?*client;
};
struct?yikou_device?*yikou;

static?int?yikou_read_byte(struct i2c_client *client,?unsigned?char?reg)
{
int?ret;

char?txbuf[1] = { reg };
char?rxbuf[1];

struct?i2c_msg?msg[2] = {
? {client->addr,?0,?1, txbuf},
? {client->addr, I2C_M_RD,?1, rxbuf}
?};

?ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
if?(ret <?0) {
? printk("ret = %dn", ret);
return?ret;
?}

return?rxbuf[0];
}

static?int?yikou_write_byte(struct i2c_client *client,?unsigned?char?reg,?unsigned?char?val)
{
char?txbuf[2] = {reg, val};

struct?i2c_msg?msg[2] = {
? {client->addr,?0,?2, txbuf},
?};

?i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));

return0;
}

static?long?yikou_ioctl(struct file *file,?unsigned?int?cmd,?unsigned?long?arg)
{
union?yikou_data data;
struct?i2c_client?*client?=?yikou->client;

switch(cmd) {
? ? ? ??case?CMD1:
? ? ? ? ? ? data.data1 = mpu6050_read_byte(client, REG1);
? ? ? ? ? ??break;

? ? ? ??default:
? ? ? ? ? ? printk("invalid argumentn");
? ? ? ? ? ??return?-EINVAL;
?}

if?(copy_to_user((void?*)arg, &data,?sizeof(data)))
return?-EFAULT;

returnsizeof(data);
}

struct?file_operations?yikou_fops?= {
?.unlocked_ioctl = yikou_ioctl,
? ? ......
};

static?int?yikou_probe(struct i2c_client *client,?const?struct i2c_device_id *id)
{
int?ret;
dev_t?devno = MKDEV(YIKOU_MAJOR, YIKOU_MINOR);
?printk("match OK!n");

?yikou = kzalloc(sizeof(*yikou), GFP_KERNEL);
if?(yikou ==?NULL) {
return?-ENOMEM;
?}
?yikou->client = client;
?ret = register_chrdev_region(devno,?1,?"yikou");
?cdev_init(&yikou->cdev, &yikou_fops);
?ret = cdev_add(&yikou->cdev, devno,?1);
? ......
return0;

}

static?int?yikou_remove(struct i2c_client *client)
{
?......
return0;
}

staticstruct?of_device_id?yikou_dt_match[] = {
?{.compatible =?"invensense,yikou"?},
?......
};

struct?i2c_driver?yikou_driver?= {
?.driver = {
? ......
? .of_match_table = of_match_ptr(yikou_dt_match),
?},
?.probe ? = yikou_probe,
?......
};

module_i2c_driver(yikou_driver);

其中struct i2c_msg封装需要参考datasheet读写数据时序

编写i2c_msg信息原则如下:

    有几个S信号,msg数组就要有几个元素;addr为从设备地址,通过i2c总线调用注册的probe函数的参数i2c_client传递下来;len的长度不包括S、AD、ACK、P;buf为要发送或者要读取的DATA的内存地址。

比如下面是某芯片写和读的时序:

在这里插入图片描述

    Single-Byte Write Sequence时序只需要1个i2c_msg,len值为2,buf内容为是RA、DATA;Single-Byte Read Sequence时序需要2个i2c_msg,len值分别都为1,第1个msg的buf是RA,第2个msg的buf缓冲区用于存取从设备发送的DATA。

在这里插入图片描述

点评:i2c是非常重要的一个知识点,基本上做嵌入式,或早或晚都会接触他。

面试部分:

1.面试官问对公司有什么了解吗?

2.自我介绍,讲一下做的项目;

3.拦截网站怎么实现;

4.wan/lan自适应具体怎么实现;

5.路由器主要承担一个什么样的角色,传输数据的过程会用到哪些协议;

6.手机连接路由器的lan口之后怎么获取数据包;

7.应用和驱动哪个熟练;

8.内核里创建线程(pthread_create)后怎么结束?

 

相关推荐

登录即可解锁
  • 海量技术文章
  • 设计资源下载
  • 产业链客户资源
  • 写文章/发需求
立即登录

公众号『一口Linux』号主彭老师,拥有15年嵌入式开发经验和培训经验。曾任职ZTE,某研究所,华清远见教学总监。拥有多篇网络协议相关专利和软件著作。精通计算机网络、Linux系统编程、ARM、Linux驱动、龙芯、物联网。原创内容基本从实际项目出发,保持原理+实践风格,适合Linux驱动新手入门和技术进阶。