0907使用write函數向共享內存中寫入數據,實現不一樣進程間的數據信息傳遞

/*ide

  • 編寫一個程序,使用write函數向共享內存中寫入數據,實現不一樣進程間的數據信息傳遞
    */

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>函數

typedef struct
{
char name[4];
int age;
}people;進程

int main(int argc, char argv[])
{
int shm_id,i;
char temp;
people
p_map;
if(argc!=2)
{
printf("Usage:atshm<identifier>");
exit(1);
}
shm_id=atoi(argv[1]);
p_map=(people )shmat(shm_id,NULL,0);
temp='a';
for(i=0;i<10;i++)
{
temp+=1;
memcpy((
(p_map+i)).name,&temp,1);
(*(p_map+i)).age=20+i;
}
if(shmdt(p_map)==-1)
{
perror("detach error!\n");
}
return 0;
}ip

相關文章
相關標籤/搜索