90 likes | 190 Views
System V IPC Identifiers and Keys. IPC structures are identified by a ‘key’ usually defined as long integer keys can be arbitrarily defined, system defined via a ‘get’ call, or defined using the ftok() function arbitrary definition gives rise to conflicts
E N D
System V IPCIdentifiers and Keys • IPC structures are identified by a ‘key’ • usually defined as long integer • keys can be arbitrarily defined, system defined via a ‘get’ call, or defined using the ftok() function • arbitrary definition gives rise to conflicts • programs must use the same key to access the same resource • typically passed from parent to child or shared in a header in client/server suites
Permissions • IPC structures have permissions, similar to files • to change permissions, process must either be the creator of the structure or the superuser • write permissions for semaphores are termed ‘alter’ • there are no permissions corresponding to ‘execute’
Pros and Cons of SV-IPC • Pros • flow controlled • record oriented • not first-in, first-out limited • Cons • have to be deliberately removed • aren’t addressable with simple file I/O commands (open, read, write, etc.) • are overly complex
Picking Keys • keys may be any key_t type value • keys may be chosen arbitrarily • can result in conflicts • a key can be generated by the system using the IPC_PRIVATE keyword • has to somehow be made available to other processes wishing to use the resource • a key can be generated using the ftok() function • doesn’t require shared headers or file reading
Message Queues • linked list of messages stored in the kernel • identified by a ‘queue ID’ • msgget() function creates a new queue or opens an existing one • msgsnd() places a message on a queue • msgrcv() reads a message from a queue • msgctl() controls the properties of a queue
Semaphores • used for resource allocation, not data transfer • essentially, an advisory resource locking mechanism • simple counter, which starts with a ‘number of resources’ available, is decremented when a resource is ‘used’, and incremented when it’s ‘returned’ • semaphores are manipulated in sets of one or more
Semaphore Functions • semget() creates or fetches the ID of a semaphore set • semctl() controls the properties of a semaphore set • uses the semun union • semop() performs operations on semaphores, such as decrementing or incrementing them • uses the sembuf structure
Shared Memory • allows multiple processes to share regions of memory • fastest possible form of IPC • shmget() creates or fetches ID of existing shared memory segment • shmctl() controls the properties of a shared memory segment • shmat() attaches to a shared memory segment • shmdt() detaches from a shared memory segment • this does NOT remove it, call to shmctl() required for that
Shell Tools • two shell tools are available for examining and removing IPC structures • ‘ipcs’ will list structures the user has read access to • always check for structures you may have inadvertently left behind when testing • ‘ipcrm’ will remove structures the user has created or owns, or will remove any for the superuser