28.01.2013 Views

1. Procese Windows; comunicaţii prin pipe şi IPC 2

1. Procese Windows; comunicaţii prin pipe şi IPC 2

1. Procese Windows; comunicaţii prin pipe şi IPC 2

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.

<strong>Procese</strong> <strong>Windows</strong>; <strong>comunicaţii</strong> <strong>prin</strong> <strong>pipe</strong> <strong>şi</strong> <strong>IPC</strong> - 18 -<br />

Pentru implementarea schimbului de mesaje sunt folosite functiile Read, Write,<br />

ReadMes, WriteMes.<br />

Primele doua aplica repetat apeluri ReadFile, WriteFile pana la schimbarea a exact n<br />

octeti, n parametru de intrare.<br />

ReadMes, WriteMes schimba mai intai lungimea continutului, dupa care cere schimbul<br />

complet al acestuia.<br />

Cele patru functii sunt prezentate in sursa ReadWrite.cpp:<br />

void Read(HANDLE f, char *t, int n) {<br />

char *p;<br />

int c;<br />

DWORD i;<br />

for (p=t, c=n; ; ) {<br />

ReadFile(f, p, c, &i, NULL);<br />

if (i == c) return;<br />

c -= i;<br />

p += i;<br />

}<br />

}<br />

void Write(HANDLE f, char *t, int n) {<br />

char *p;<br />

int c;<br />

DWORD i;<br />

for (p=t, c=n; c; ) {<br />

WriteFile(f, p, c, &i, NULL);<br />

if (i == c) return;<br />

c -= i;<br />

p += i;<br />

}<br />

}<br />

Mesaj *ReadMes(HANDLE canal) {<br />

static Mesaj mesaj;<br />

DWORD no;<br />

ReadFile(canal, (char*)&mesaj.lung, sizeof(int), &no, NULL);<br />

Read(canal, (char*)&mesaj+sizeof(int), mesaj.lung);<br />

return &mesaj;<br />

}<br />

void WriteMes(HANDLE canal, Mesaj *pm) {<br />

DWORD no;<br />

WriteFile(canal, (char*)pm, sizeof(int), &no, NULL);<br />

Write(canal, (char*)pm+sizeof(int), pm->lung);<br />

}<br />

Sursa parinte.cpp este:<br />

void parinte(HANDLE in, HANDLE out) {<br />

Mesaj *pm;<br />

for ( ; ; ) {<br />

pm = ReadMes(in);<br />

pm = dir(pm->i, pm->s);<br />

WriteMes(out, pm);<br />

}<br />

}<br />

Actiunea <strong>prin</strong>cipala a clientului este dirijata de functia descrisa in sursa fiu.cpp:<br />

void fiu(HANDLE in, HANDLE out) {

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

Saved successfully!

Ooh no, something went wrong!