編輯:關於android開發
這裡提示一下mmap的函數原型:
- mmap( NULL,size_of_map,PROT_READ|PROT_WRITE,MAP_SHARED | MAP_ANON, -1,0 );
匿名映射主要區別是flags 裡多了一個 MAP_ANON標志,和fd傳遞的為-1. 關於更多mmap的資料請自行查詢.
- void* mmap(void* start,size_t length,int prot,int flags,int fd,off_t offset);
2. 讀取操作:
- #include
- #include
- #include
- #include
- #include
- #define CLUSTER_SHARED_FILE "/tmp/cluster_share"
- typedef struct{
- char name[4];
- int age;
- }people;
- struct stu {
- sem_t mutex;
- people s[10];
- };
- struct stu t;
- main(int argc, char** argv) // map a normal file as shared mem:
- {
- int fd,i;
- struct stu *p_map;
- char temp;
- fd=open(CLUSTER_SHARED_FILE,O_CREAT|O_RDWR|O_TRUNC,00777);
- lseek(fd,sizeof(t)-1,SEEK_SET);
- write(fd,"",1);
- p_map = (struct stu *) mmap( NULL,sizeof(t),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0 );
- close(fd);
- temp = 'a';
- sem_init(&p_map->mutex,1,1);
- int j=0;
- while(1)
- {
- j++;
- printf("j 1\n");
- sem_wait(&p_map->mutex);
- printf("j 2\n");
- for(i=1; i<10; i++)
- {
- // temp += 1;
- memcpy(p_map->s[i].name, &temp,2 );
- p_map->s[i].age = p_map->s[i].age +20+i+j;
- }
- printf("j 3\n");
- sem_post(&p_map->mutex);
- printf("j 4\n");
- sleep(1);
- }
- printf(" initialize over \n ");
- sleep(50);
- munmap( p_map, sizeof(t) );
- printf( "umap ok \n" );
- }
至於並非的寫操作,程序稍微修改一下就可以測試. 多並發沒有問題.
- #include
- #include
- #include
- #include
- #include
- #include
- #define CLUSTER_SHARED_FILE "/tmp/cluster_share"
- typedef struct{
- char name[4];
- int age;
- }people;
- struct stu {
- sem_t mutex;
- people s[10];
- };
- struct stu t;
- main(int argc, char** argv) // map a normal file as shared mem:
- {
- int fd,i;
- struct stu *p_map;
- char temp;
- fd=open(CLUSTER_SHARED_FILE,O_CREAT|O_RDWR,00777);
- // lseek(fd,sizeof(people)*5-1,SEEK_SET);
- // write(fd,"",1);
- p_map = (struct stu *) mmap( NULL,sizeof(t),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0 );
- close( fd );
- while(1)
- {
- sleep(1);
- printf("2.....s\n");
- sem_wait(&p_map->mutex);
- printf("2.....\n");
- for(i=0; i<10; i++)
- {
- printf("name:%s,age:%d\n",p_map->s[i].name,p_map->s[i].age);
- }
- printf("2.....0\n");
- sem_post(&p_map->mutex);
- printf("2.......1");
- }
- munmap( p_map, sizeof(t));
- printf( "umap ok \n" );
- }
2. 讀取操作
- #include
- #include
- #include
- #include
- #include
- #include
- //#include "shmdata.h"
- #define TEXT_SZ 2048
- struct shared_use_st
- {
- sem_t mutex;
- int written;//作為一個標志,非0:表示可讀,0表示可寫
- char text[TEXT_SZ];//記錄寫入和讀取的文本
- int cnt;
- };
- int main()
- {
- int running = 1;
- void *shm = NULL;
- struct shared_use_st *shared = NULL;
- char buffer[4096]="hello world";
- int shmid;
- int i=0;
- //創建共享內存
- #if 0
- //刪除共享內存
- if(shmctl(shmid, IPC_RMID, 0) == -1)
- {
- fprintf(stderr, "shmctl(IPC_RMID) failed\n");
- exit(EXIT_FAILURE);
- }
- #endif
- shmid = shmget((key_t)12345, sizeof(struct shared_use_st), 0666|IPC_CREAT);
- if(shmid == -1)
- {
- fprintf(stderr, "shmget failed\n");
- exit(EXIT_FAILURE);
- }
- //將共享內存連接到當前進程的地址空間
- shm = shmat(shmid, (void*)0, 0);
- if(shm == (void*)-1)
- {
- fprintf(stderr, "shmat failed\n");
- exit(EXIT_FAILURE);
- }
- printf("Memory attached at %X\n", (int)shm);
- //設置共享內存
- shared = (struct shared_use_st*)shm;
- sem_init(&shared->mutex,1,1);
- while(running)//向共享內存中寫數據
- {
- strncpy(shared->text, buffer, TEXT_SZ);
- sem_wait(&shared->mutex);
- //shared->cnt = 0;
- shared->cnt = shared->cnt +i;
- sem_post(&shared->mutex);
- i++;
- shared->written = 1;
- sleep(1);
- }
- //把共享內存從當前進程中分離
- if(shmdt(shm) == -1)
- {
- fprintf(stderr, "shmdt failed\n");
- exit(EXIT_FAILURE);
- }
- sleep(2);
- exit(EXIT_SUCCESS);
- }
- #include
- #include
- #include
- #include
- //#include "shmdata.h"
- #include
- #define TEXT_SZ 2048
- struct shared_use_st
- {
- sem_t mutex;
- int written;//作為一個標志,非0:表示可讀,0表示可寫
- char text[TEXT_SZ];//記錄寫入和讀取的文本
- int cnt;
- };
- int main()
- {
- int running = 1;//程序是否繼續運行的標志
- void *shm = NULL;//分配的共享內存的原始首地址
- struct shared_use_st *shared;//指向shm
- int shmid;//共享內存標識符
- //創建共享內存
- shmid = shmget((key_t)12345, sizeof(struct shared_use_st), 0666);
- if(shmid == -1)
- {
- fprintf(stderr, "shmget failed\n");
- exit(EXIT_FAILURE);
- }
- //將共享內存連接到當前進程的地址空間
- shm = shmat(shmid, 0, 0);
- if(shm == (void*)-1)
- {
- fprintf(stderr, "shmat failed\n");
- exit(EXIT_FAILURE);
- }
- printf("\nMemory attached at %X\n", (int)shm);
- //設置共享內存
- shared = (struct shared_use_st*)shm;
- // shared->written = 0;
- while(running)//讀取共享內存中的數據
- {
- sem_wait(&shared->mutex);
- printf("text:%s,cnt:%d\n",shared->text,shared->cnt);
- sem_post(&shared->mutex);
- sleep(1);
- }
- //把共享內存從當前進程中分離
- if(shmdt(shm) == -1)
- {
- fprintf(stderr, "shmdt failed\n");
- exit(EXIT_FAILURE);
- }
- #if 0
- //刪除共享內存
- if(shmctl(shmid, IPC_RMID, 0) == -1)
- {
- fprintf(stderr, "shmctl(IPC_RMID) failed\n");
- exit(EXIT_FAILURE);
- }
- #endif
- exit(EXIT_SUCCESS);
- }
Java的進化? Kotlin初探與集成Android項目 介紹: Statically typed programming language for the JVM
ViewPager+GridView實現橫向滑動 仿大眾點評,viewpagergridview先看演示效果: 1 ViewPager類提供了
安卓虛擬機adb shell sqlite3數據庫,安卓sqlite3 adb shell 連接: //http://www.c
創建第一個Android 5.0應用程序,android5.01、新建一個Andriod項目,並配置相關參數 2、接著下一步 3、接著下一步,配置相關參數 &nb