#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
struct lcd_info {
int width; // 屏宽
int height; // 屏高
int depth; // 每点像素数
int line_bytes; // 每行字节数
int data_offset;// 数据区开始偏移值
};
int shmId;
int main(void)
{
int data_size;
void *data;
struct lcd_info lcd;
struct shmid_ds shm;
key_t key = ftok("/sbin/init", 'g');
// 根据init文件的ino节点号,dev设备号,和id组合出一个key序列串,作为唯一key标识[luthr.gliethttp]
// ftok库实现为
// key_t ftok(const char* path, int id)
// {
// struct stat st;
// if ( lstat(path, &st) < 0 )
// return -1;
// return (key_t)( (st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16) | ((id & 255) << 24) );
// }
lcd.width = 800;
lcd.height = 600;
lcd.depth = 16;
lcd.line_bytes = lcd.width * ((lcd.depth+7) >> 3);
lcd.data_offset = 512;
data_size = lcd.line_bytes*lcd.height + lcd.data_offset; // 0-511字节存放控制信息[luther.gliethttp]
// 跳过前512字节的控制信息空间[luther.gliethttp]
shmId = shmget( key, data_size, IPC_CREAT|0666); // 创建一个data_size大小的共享内存,该共享内存
// 对应的唯一索引值为key,调用shmget(key, 0, 0);可以获得该共享内存[luther.gliethttp]
if ( shmId != -1 )
data = (unsigned char *)shmat( shmId, 0, 0 );
else {
shmctl( shmId, IPC_RMID, &shm ); // 删除已经存在的key相同的共享内存[luther.gliethttp].
shmId = shmget( key, data_size, IPC_CREAT|0666);
data = (unsigned char *)shmat( shmId, 0, 0 );
}
if ((int)data == -1) {
printf("Error : shmget\n");
return -1;
}
*((struct lcd_info*)data) = lcd;
for (;;) {
sleep(1);
}
shmctl( shmId, IPC_RMID, &shm ); // 删除共享内存,当然这里是不会执行到这了,应该在signal回调函数中执行,不过再次启动的时候,会自动删除[luther.gliethttp].
return 0;
}
|