#include #include #include #include #include #include "info.h" main(void) { int i, id; struct info *ctrl; int v1, v2; int choice; // create the shared memory segment // .. id is the file identifier of the shared memory segment // .. if the key is shared, any process can attach to this shared memory segment // .. 0666 is the file permission for the shared memory segment if ( (id = shmget( KEY,MSIZ,IPC_CREAT | 0666) ) < 0 ) { //error…; printf("error 1\n"); exit(1) ; } // attach a local pointer to the shared memory segment created // .. exit if fails if ( (ctrl = (struct info *) shmat( id, 0, 0)) <= (struct info *) (0) ) { //error … ; printf("error 2\n"); exit(1) ; } ctrl->flag=0; ctrl->value1=0; ctrl->value1=0; ctrl->sum=0; while(ctrl->flag!=-1) { //ctrl->flag=0; // ask for numbers and set flag to 1 printf("Value 1: "); scanf("%d",&v1); printf("Value 2: "); scanf("%d",&v2); ctrl->value1 = v1; ctrl->value2 = v2; ctrl->flag = 1; printf("Would you like to continue? Yes(1) or No(-1) : "); scanf("%d",&choice); if (choice == -1) { ctrl->flag = -1; } else ctrl->flag=0; } //now detach the pointer from the shared memory shmdt(ctrl); //let us delete the shared memory shmctl(id,IPC_RMID,NULL); //job done }