16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Interprocess Communication<br />

shmat<br />

shmctl<br />

- attaches a region to virtual address space of process<br />

- manipulates parameters associated <strong>with</strong> shared memory<br />

shmid = shmget(key, size, flag):<br />

- size is number of bytes in region<br />

- data in shared memory remains intact even when no processes include it as part of <strong>the</strong>ir virtual address space<br />

Shared Region Process Table Per<br />

Memory Table Process Region Table<br />

Table<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

-----<br />

virtaddr = shmat(id, addr, flags):<br />

algorithm shmat /* attach shared memory */<br />

input: (1) shared memory descriptor<br />

(2) virtual address to attach memory<br />

(3) flags<br />

output: virtual address where memory was attached<br />

{<br />

check validity of descriptor, permissions;<br />

if (user specified virtual address)<br />

{<br />

round off virtual address, as specified by flags;<br />

check legality of virtual address, size of region;<br />

}<br />

else /* user wants kernel to find good address */<br />

kernel picks virtual address; error if none available;<br />

attach region to process address space (algorithm attachreg);<br />

if (region being attached for first time)<br />

allocate page tables, memory for region algorithm growreg);<br />

return(virtual address where attached);<br />

}<br />

Where is <strong>the</strong> best place for shared memory?<br />

/* ------------------------------------------ */<br />

/* attaching shared memory twice to a process */<br />

#include <br />

#include <br />

#include <br />

#define SHMKEY 75<br />

#define K 1024<br />

int shmid;<br />

main()<br />

{<br />

int i, *pint;<br />

char *addr1, *addr2;<br />

extern cleanup();<br />

159

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!