Starting from:

$30

CPE434-Lab 4 IPC Using Shared Memory Solved

Write the code for two distinct C processes that are not part of a parent-child relation. They both attach to shared memory area containing a new type structure with four data types (first number, second-number, Sum and Flag). The first process must set the values for first and second numbers, then it updates the value of flag to 1 (which means there is new data). The second process is to find the sum of these two numbers and display the sum of the two values and changes the flag to zero. Two processes continue running until the first process sets the value of flag to -1.  

Hint  
Define the shared structure like:  

 

struct ​info ​{  float ​value1​, ​value2​;  float ​sum​;  int ​flag​;  

};  

# define KEY ((key_t)(1234))  

# define SEGSIZE sizeof (struct info) 
 

 

Topics for theory:  shmget()​: ​int shmget(key_t key, size_t size, int shmflg);  

It returns the identifier of the shared memory segment associated with the value of ​key​. Based on a value of ​shmflg​, either a new segment is created or identifier of already created segment  shmat()​: ​void *shmat(int shmid, const void *shmaddr, int shmflg);  

It attaches the shared memory segment identified by ​shmid ​and returns the pointer in the calling process address space.  shmctl()​: ​int shmctl(int shmid, int cmd, struct shmid_ds *buf);  

It performs the control operation in the shared memory segment identified by ​shmid​. The operation is as defined by value ​shmid  

shmdt()​: ​int shmdt(const void *shmaddr);  

This function detaches the shared memory segment identified by ​shmaddr 

More products