linux下共享内存(shm)使用示例
#include <sys/ipc.h>
#include <stdio.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define PERM IPC_CREAT //S_IRUSR|S_IWUSR
#include <errno.h>
int main(int argc,char **argv)
{
int shmid[2048];
char *p_addr,*c_addr;
if(argc!=2)
{
fprintf(stderr,"Usage:%s InputString\n\a",argv[0]);
return 1;
}
int i =0;
for ( i = 0; i<1024; i++ )
{
char *p = new char[1024*1024];
if ( NULL == p )
{
printf( "error new\n" );
return -1;
}
}
for ( i =0; i<1026; i++ )
{
if((shmid[i]=shmget(IPC_PRIVATE,/*1610612736*/ 1024*1024,PERM))==-1)
{
fprintf(stderr,"Create Share Memory Error:%s\n\a",strerror(errno));
return 1;
}
{
p_addr=(char*)shmat(shmid[i],0,0);
p_addr[0] = i/1000 + '0';
p_addr[1] = i%1000/100 + '0';
p_addr[2] = i%100/10 + '0';
p_addr[3] = i%10 + '0';
p_addr[4] = '\0';
printf( "%d, %s\n", i, p_addr );
}
}
}
相关文档:
例一:发送Signaling Packet:
Signaling Command是2个Bluetooth实体之间的L2CAP层命令传输。所以得Signaling Command使用CID 0x0001.
多个Command可以在一个C-frame(control frame)中发送。
如果要直接发送Signaling Command.需要建立SOCK_RAW类型的L2CAP连接Socket。这样才有机会自己填充Command Code,Identi ......
Service Discovery Protocol(SDP)提供一种能力,让应用程序有方法发现哪种服务可用以及这种服务的特性。
服务发现协议(SDP或Bluetooth SDP)在蓝牙协议栈中对蓝牙环境中的应用程序有特殊的含意,发现哪个服务是可用的和确定这些可用服务的特征。SDP定义了bluetooth client发现可用bluetooth server服务和它们的特征的方法。 ......
linux shell pwd 显示当前路径
假若有test.cpp
g++ test.cpp -o test
./test
想在test中找到当前执行程序所在的路径
可以再test.cpp中使用readlink函数
具体见如下实例:
#include<iostream>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
#include<string>
using ......
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <s ......