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.

Processes II<br />

For every semop operation that specifies SEM_UNDO:<br />

- if <strong>the</strong> semaphore value goes up, <strong>the</strong> adjustment values goes down by <strong>the</strong> same amount;<br />

- if <strong>the</strong> semaphore values goes down, <strong>the</strong> adjustment values goes up by <strong>the</strong> same amount;<br />

- kernel applies adjustment on exist.<br />

Simpler Semaphore Operations<br />

System V semaphore facility is not simple to understand or use.<br />

There are problems:<br />

- creation of a semaphore <strong>with</strong> semget is independent of its initialization using semctl. This can lead to race<br />

conditions if not careful.<br />

- unless a semaphore is explicitly removed, it exists <strong>with</strong>in <strong>the</strong> system, using system resources, until <strong>the</strong> system<br />

is rebooted.<br />

Shared Memory<br />

Normal steps in client-server file copying:<br />

- The server reads from <strong>the</strong> input file. Data is read by kernel into its internal block buffers and copied to <strong>the</strong><br />

server's buffer.<br />

- The server writes this data in a message (via a pipe, FIFO, or message queue). Data is copied from user's buffer<br />

into <strong>the</strong> kernel.<br />

- The client reads <strong>the</strong> data from <strong>the</strong> IPC channel. Data is copied from kernel's IPC buffer to client's buffer.<br />

- Finally <strong>the</strong> data is copied from <strong>the</strong> client's buffer to <strong>the</strong> output buffer. This might involve just copying <strong>the</strong> data<br />

into a kernel buffer and returning, <strong>with</strong> <strong>the</strong> kernel doing <strong>the</strong> actual write operation to <strong>the</strong> device at some later<br />

time.<br />

Most <strong>Unix</strong> implementations try to speed up <strong>the</strong>se copies as much as possible expensive in time.<br />

Movement of data between client and server<br />

client<br />

server<br />

output<br />

file<br />

FIFO, pipe<br />

or message<br />

kernel<br />

input<br />

file<br />

The problem <strong>with</strong> <strong>the</strong>se forms of IPC - pipes, FIFOs and message queues - is that for processes to exchange data, it<br />

has to go through <strong>the</strong> kernel.<br />

Shared memory provides a way around this by letting two or more processes share a memory segment.<br />

The steps for <strong>the</strong> client-server examples:<br />

- The server gets access to a shared memory segment using a semaphore.<br />

- The server reads from <strong>the</strong> input file into <strong>the</strong> shared memory segment. address to read into points into shared<br />

memory.<br />

- When <strong>the</strong> read is complete <strong>the</strong> server notifies <strong>the</strong> client, again using a semaphore.<br />

- The client writes <strong>the</strong> data from <strong>the</strong> shared memory segment to <strong>the</strong> output file<br />

Movement of data between client and server<br />

client<br />

shared memory<br />

server<br />

output<br />

file<br />

kernel<br />

input<br />

file<br />

Data is only copied twice. Both of <strong>the</strong>se copies involve <strong>the</strong> kernel's block buffers. For every shared memory<br />

140

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

Saved successfully!

Ooh no, something went wrong!