/*A module to present hw client-server may be benefit by applying shared memory access*/
//suppose we have a client....a server.....& process.....we have to take operator & operand by client...data is registers into server 1st....then applied to the process....process will do operation on those nos...&then write result into shared memory which will be further read by server & client...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>//inter process communication is also used
#include<sys/shm.h>//header file is used
#include<sys/wait.h>
struct anu
{
int type;
int no1;//a structure is made into which all nos....that r to be written ..on which some operation is to be performed....& result is taken back....are defined here
int no2;
char op;//operator
int result;//result comes into it
};
int main()
{
int ret,kid;void* ptr;
struct anu *ptr1;//declared a pointer of structure when we declare pointer of structure ...all elements of structure will access memory
kid=shmget(441,sizeof(struct anu),666|IPC_CREAT);//memory of type struct means diffrent data types of sizeof struct is to be get by the system
printf("\nkid=%d",kid);//shmid
ptr=shmat(kid,(void*)0,0);//ptr is base address of accessed memory
printf("\nadd of ptr=%p",ptr);
ptr1=(struct anu *)ptr;//typecast that base address of memory with struct anu* type ...made is for diffrent data types
ptr1->type=1;//made type=1...bcz ptr1 is pointer so to access element through pointer -> sign is used....if ptr would be variable ....then ptr.type... '.' variable would be used
if(ptr1->type==1)//check if type=1...then write oprends & operator into memory...one by one
{
ptr1->no1=5;
ptr1->no2=3;
ptr1->op='+';
}
//sleep(3);
//pause(ptr1->type==1);
while(ptr1->type==1)//untill type not became zero ...clients should stop here
printf("");
if(ptr1->type==0)//In process after addition type is made to be zero ...so check if type is made zero
{
printf("\nresult=%d",ptr1->result);//then print the result........means stop untill result is not written
ptr1->type=1;//made type 1 again to write another oprends or operator to get more operations to be done
int no1;//structure of diffrent data types ...in which no1 & no2 will have operands..op will have operator
int no2;
char op;
int res;//res will have result written by process into shared memory
};
int main()
{
int key;
void* ptr;struct anu *next;//pointer of structure
key=shmget(441,sizeof(struct anu),666|IPC_CREAT);//shared memory is created ..if there is already shared memory...then...by using same +ve integer value given at the time of creation to shared memory..is provided
printf("\nkey=%d",key);
ptr=shmat(key,(void*)0,SHM_RND);// void* memory is accessed ..shm typeat will return base addess of this memory
next=(struct anu *)ptr;//typecasted that memory
//if(next->type==1)
{
printf("\nnext->type=%d next.no1=%d next.no2=%d next.op=%c ",next->type,next->no1,next->no2,next->op);//putting base address of shared memory into base address of variable of structure....& elements of structure are accessed by that variable...which will have no1...no2...op...variables respectively...if they would be put individually ...again they (nos.) would be definetly in this shared memory
}
//next->type=2;
//sleep(4);
while(next->type==1)//untill process is not done ...& type is not made to be zero server shold stop here in this loop
printf("");
if(next->type==0)//if result is written is written by process..& then.type is made 0 by process...then server will read result untill then it stops.....