Instruction
Create and use shared memory
            
          Shared Memory
We learn how to create and use shared memory.
- introduction
- Use the  shmget  
function to create a shared memory segment
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget (key_t key, int size, int shmflag);
- returns the memory ID or -1 on error
 - key - key identifying shared memory
 - size - specifies the memory size in bytes
 - semflag - is a combination of symbolic constants specifying access rights
 
Numerical value Symbolic constant Meaning 0400 SHM_R reading by the owner 0200 SHM_A change by owner 0040 SHM_R>>3 reading by the group 0020 SHM_A>>3 changing by group 0004 SHM_R>>6 reading by others 0002 SHM_A>>6 changing by others 1000 IPC_CREAT 2000 IPC_EXCL  - shmget creates or opens a shared memory segment, but to use it you must attach it
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
char *shmat(int shmid, char *shmaddr, int shmflag);
- the function returns the starting address of the shared memory segment, or -1 on error
 - the address is determined according to the following rules:
- when shmaddr = 0 then the system chooses the address itself
 - when shmaddr != 0 then the forwarded address depends on whether the  SHM_RND  tag is set
- if not set, the shared memory segment will be attached from the address specified by the shmaddr argument
 - if set, it will start with an address rounded down by the value of the constant SHMLBA (Lower Boundary Address)
 
 
 - for most uses it is enough to give 0
 - shmid - shared memory identifier returned by shmget
 - shmflag - may have a tag, e.g. SHM_RDONLY
 
 - disconnection of shared memory is done with 
int shmdt (char * shmaddr);
This function does not delete the shared memory segment - to delete a shared memory segment use the following function: 
int shmctl (int shmid, int cmd, struct shmid_ds * buf);- with the cmd argument as  IPC_RMID 
 
 - with the cmd argument as  IPC_RMID 
 
 - Use the  shmget  
 - Write a server program that:
- creates a shared memory segment of a size that allows writing to a variable of typeint (0,4p)
 - creates two semaphores: 1 lowered, 2 raised (0,4p)
 - in the loop executes: lower semaphore 2, waits a random time from 0 to 1 second, enters the next number (starting from 1) into the shared variable, raises semaphore 1 (1.5p)
 - program should remove shared memory and semaphores after pressing ctrl ^ c (0,5p)
 - make sure that only one server program creates semaphores and memory (exclusive mode) (0,2p)
 
 - creates a shared memory segment of a size that allows writing to a variable of typeint (0,4p)
 - Write a client program that:
- attaches to memory segments and semaphores(0,5)
 - looped (10 times), lower semaphore 1, reads the value from shared memory, raises semaphore 2, displays value, waits a random time from 0 to 1 second (1)
 - check the operation of several clients at once (0,5). 
Launch clients in one terminal, e.g. this way(./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ./client & ) - is there a danger that two clients will get the same number?
 
 - Report
Don't forget the report 
