#include #include #include #include #include #include #include #include "log.h" //reporting and logging functions void cleanup(); //variables for indexing of messages by logging functions int logindex = 0; int *logi = &logindex; //variables for shared memory segments int shmid = -1; //id void* shmaddr; //address where it is attached struct shmid_ds shmbuf; //structure with info /* function main ---------------------------------------------- */ int main() { int i; int *p; if (atexit(cleanup)<0) sys_exit("atexit error"); if ((shmid = shmget(IPC_PRIVATE,100,S_IRUSR|S_IWUSR))<0) sys_exit("shmget error"); else msg("shared memory segment of size 100 with id=%d created\n", shmid); shmaddr = shmat(shmid,(const void *)0,S_IRUSR|S_IWUSR); if (((int)(shmaddr)) == -1) sys_exit("shmat(%d,.....) error",shmid); else msg("shared memory segment id=%d attached at address %u\n", shmid,shmaddr); // store int 235 in it p = shmaddr; *p = 235; //fetch the value from it msg("int value stored in the shared memory segment is %d\n",*p); exit(0); }/* end main */ /* function cleanup --------------------------------------------- */ void cleanup() { if (shmid >= 0) { if (shmctl(shmid,IPC_RMID,&shmbuf)<0) sys("shmctl(%d,IPC_RMID,...) error",shmid); else msg("shared memory segment id=%d removed\n",shmid); } }/* end cleanup */