西安邮电大学linux高级编程进程间通信.pptVIP

  • 10
  • 0
  • 约1.77万字
  • 约 72页
  • 2018-09-12 发布于湖北
  • 举报

西安邮电大学linux高级编程进程间通信.ppt

西安邮电大学linux高级编程进程间通信

例一 key=ftok(name,0); shm_id=shmget(key,4096,IPC_CREAT); if(shm_id==-1) { perror(shmget); exit(EXIT_FAILURE); } p_map=(people *)shmat(shm_id,NULL, 0); for(i=0;i10;i++) { printf(name:%s\t,(p_map+i)-name); printf(age :%d\n,(p_map+i)-age); } if(shmdt(p_map)==-1) perror(shmdt);} * * 例一 #include stdio.h #include stdlib.h #include unistd.h #include sys/ipc.h #include sys/shm.h #include sys/types.h typedef struct{ char name[4]; int age;}people; main(){ int shm_id, i; key_t key; people *p_map; char * name=/dev/shm/myshm2; * * 例一 key=ftok(name,0); if(key==-1) { perror(ftok); exit(EXIT_FAILURE); } shm_id=shmget(key,4096,IPC_CREAT|0666); if(shm_id==-1) { perror(shmget); exit(EXIT_FAILURE); } p_map=shmat(shm_id,0,0);//(people *)shmat(shm_id,0,0); temp=a; for(i=0;i10;i++) { temp+=1; memcpy((p_map+i)-name,temp,1); (p_map+i)-age=20+i; } if(shmdt(p_map)==-1) perror(shmdt);} * 人民邮电出版社出版 杨宗德编著 * 例二 #include stdio.h #include stdlib.h #include sys/ipc.h #include sys/shm.h #include sys/types.h #include time.h #include unistd.h typedef struct{ char str1[50]; char str2[50]; int no1,no2;}str; * * 例二 main(int argc, char * agv[]){ int shm_id,i; key_t key; str *p_map; time_t timep; struct tm *p; char * name=/dev/shm/myshm2; key=ftok(name,0); shm_id=shmget(key,4096,IPC_CREAT|0666); if(shm_id==-1) { perror(shmget); exit(EXIT_FAILURE); } p_map=(str *)shmat(shm_id,NULL,0); p_map-no1=0; p_map-no2=0; printf(This is A.\n); fgets(p_map-str2,sizeof(p_map-str2),stdin); p_map-no1=1; * * 例二 while(1) { if(p_map-no2==1) { time(timep); gmtime(timep); p=localtime(timep); printf(A:%s%d:%d:%d\n,p_map-str1,p-tm_hour,p-tm_min,p-tm_sec); p_map-str1[0]=\0; p_map-no2=0; } else continue; if(p_map-no1==0) { fgets(p_map-str2,sizeof(p_map-str2),stdin); p_map-no1=1; } } if(shmdt(p_map)==-1) perror(shmdt);} * * 例二 #include stdio.h #include stdlib.h #include sys/ipc.h #include sys/shm.h #include sys/types.h #include time.h #includ

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档