16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

141<br />

Processes II<br />

segment <strong>the</strong> kernel maintains.<br />

#include <br />

#include /* defines ipc-perm structure */<br />

struct shrnid_ds {<br />

struct ipc_perm shrn_perm; /* operation perm struct */<br />

int shrn_seqsz; /* segment size */<br />

struct XXX shrn_YYY; /* hardware & implementation dependent information */<br />

ushort shrn_lpid; /* pid of last operation */<br />

ushort shrn_cpid; /* creator pid */<br />

ushort shrn_nattch; /* current # attached */<br />

ushort shrn_cnattch; /* in-core # attached */<br />

time_t shrn_atime; /* last attach time */<br />

time_t shrn_dtime; /* last detach time */<br />

time_t shrn_ctime; /* last change time */<br />

};<br />

int shrnget(key_t key, int size, int shrnflag);<br />

/* used to create or open access to an existing shared memory segment */<br />

shrnflag<br />

0400 SHM_R read by owner<br />

0200 SHM_W write by owner<br />

0040 SHM_R >> 3 read by group<br />

0020 SHM_W >> 3 write by group<br />

0004 SHM_R >> 6 read by world<br />

0002 SHM_W >> 6 write by world<br />

IPC_CREAT<br />

IPC_EXCL<br />

char *shrnat(int shrnid, char *shraddr, int shrnflag);<br />

/* returns <strong>the</strong> starting address of shared memory segment */<br />

if shrnaddr == 0<br />

system selects address for <strong>the</strong> caller<br />

else<br />

if value for shrnflag specifies SHM_RND,<br />

shared memory is attached at <strong>the</strong> address specified by <strong>the</strong> shrnaddr<br />

argument rounded down by SHMLBA (lower boundary address)<br />

else<br />

shared memory is attached at <strong>the</strong> address specified by <strong>the</strong> shrnaddr argument<br />

int shrndt(char *shrnaddr);<br />

/* detaches <strong>the</strong> segment but does not delete <strong>the</strong> shared memory segment */<br />

int shrnctl(int shrnid, int cmd, struct shrnid_ds *buf);<br />

/* a cmd of IPC_RMID removes a shared memory segment from <strong>the</strong> system */<br />

See shared_memory.c<br />

The two process wait for access to <strong>the</strong> shared memory by waiting for a semaphore's value to become greater than<br />

zero.<br />

This is <strong>the</strong> most efficient way to wait for <strong>the</strong> resource, since it is <strong>the</strong> kernel that does all semaphore operations and<br />

<strong>the</strong> kernel puts a process to sleep when it has to wait for a semaphore.<br />

! Busy-waiting - instead of sleeping<br />

- keep trying to obtain resource<br />

/* server loop */<br />

mesgptr->mesg_flag = 0; /* signal client */<br />

while (mesgptr->mesg_flag == 0)<br />

; /* wait for client to process */

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

Saved successfully!

Ooh no, something went wrong!