Slides Pratica Named Pipes e Select

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Operating Systems

Department of Informatcs Engneerng


FCTUC
Unversty of Combra

Named Pipes &


/O Multiplexing
2
Named Pipes (also known as FFOs)

Smar to ppes but aow communcaton between


unreated processes.

Each ppe has a name ().

The ppe s wrtten persstenty n the fe system.

For creatng a named ppe, use the "mkffo" command or


ca mkffo(const char* fename, mode_t mode);

Typcay, ke ppes, they are haf-dupex

Means that they must be open read-ony or wrte-ony

They are opened ke fes, but they are not files

You cannot fseek() a named ppe; wrte() aways appends


to the ppe, read() aways returns data from the begnnng
of the ppe.

After data s read from the named ppe, ts no onger


there. Its not a fe, ts an ob|ect n the unx kerne!


3
Unrelated client/server program (np_server.c)
#de11he PTPE_hAhE "hp_c1eh1_seJveJ"
{...)
1h1 ma1h{)
{
// CJea1es 1he hamed p1pe 11 11 doesh`1 ex1s1 ye1
11 {{mk11o{PIPE_NANE, _CEAT|_EXCL|6666)<0) && {eJJho!= EEXTST)) {
peJJoJ{"Cahho1 cJea1e p1pe. "),
ex11{0),
}
// 0pehs 1he p1pe 1oJ Jead1hg
1h1 1d,
11 {{1d = open{PIPE_NANE, _NLY)) < 0) {
peJJoJ{"Cahho1 opeh p1pe 1oJ Jead1hg. "),
ex11{0),
}
// 0o some woJk
humbeJs h,
wh1e {1) {
read{1d, &n, szeo1{numbers))
pJ1h11{"|SERvER] Rece1ved {d,d), add1hg 11. d\h",
h.a, h.b, h.a+h.b),
}
Je1uJh 0,
}
4
Unrelated client/server program (np_client.c)
#de11he PTPE_hAhE "hp_c1eh1_seJveJ"
{...)
1h1 ma1h{)
{
// 0pehs 1he p1pe 1oJ wJ111hg
1h1 1d,
11 {{1d = open{PIPE_NANE, _WNLY)) < 0) {
peJJoJ{"Cahho1 opeh p1pe 1oJ wJ111hg. "),
ex11{0),
}
// 0o some woJk
wh1e {1) {
humbeJs h,
h.a = Jahd{) 100,
h.b = Jahd{) 100,
pJ1h11{"|CLTEhT] Sehd1hg {d,d) 1oJ add1hg\h", h.a, h.b),
Wr1e{1d, &n, szeo1{numbers))
seep{2),
}
Je1uJh 0,
}


5
Some interesting issues...

If you get a SIGPIPE sgna, ths means that you are tryng to
read/wrte from a cosed ppe

A named ppe s a connecton between two processes. A


process bocks unt the other party open the ppe...

Beng t for readng or wrtng.

Its possbe to bypass ths behavor (open t non-bockng


- O_NONBLOCK), but be very, very carefu: f not propery
programmed, t can ead to busy watng. If a named ppe
s open non-bockng, EOF s ndcated when read() returns
0.

When desgnng a cent/server mutpe cent appcaton,


ths means that ether the ppe s re-opened after each
cent dsconnects, or the ppe s open read-wrte.

If opened "read-wrte", the server w not bock unt the


other party connects (snce, he tsef s aso another
party!)




9
select()

Carefu: n s the number of the hghest fe


descrptor added of one.

Its not the number of fe descrptors


fd_set
A bt set representng
fe descrptors

FD_ZERO(fd_set* set)
Ceans up the fe descrptor set

FD_SET(nt fd, fd_set* set)


Sets a bt n the fe descrptor set

FD_CLEAR(nt fd, fd_set* set)


Cears a bt n the fe descrptor set

FD_ISSET(nt fd, fd_set* set)


Tests f a fe descrptor s set
10
Example (printerd.c)
{...)
#de11he BbF_STZE 4096
#de11he hbh_PRThTERS 3
cohs1 chaJ* PRThTER_hAhE|] = {
"pJ1h1eJ1", "pJ1h1eJ2", "pJ1h1eJ3"
},
// The pJ1h1eJ 11e descJ1p1oJs
1h1 pJ1h1eJ|hbh_PRThTERS],
vo1d cJea1e_pJ1h1eJs{) {
1oJ {1h1 1=0, 1<hbh_PRThTERS, 1++) {
uh1hk{PRThTER_hAhE|1]),
mk111o{PRThTER_hAhE|1], 0_CREAT]0_EXCL]0666),
pJ1h1eJ|1] = opeh{PRThTER_hAhE|1], 0_R00hLY]0_h0hBL0CK),
asseJ1{pJ1h1eJ|1] >= 0),
}
}
1h1 ma1h{1h1 aJgc, chaJ* aJgv|]) {
cJea1e_pJ1h1eJs{),
accep1_Jeques1s{),
}


11
Example (printerd.c) (2)
vo1d accep1_Jeques1s{) {
wh1e {1) {
1d_se1 Jead_se1,
F0_ZER0{&Jead_se1),
1oJ {1h1 1=0, 1<hbh_PRThTERS, 1++)
F0_SET{pJ1h1eJ|1], &Jead_se1),
11 { seec1{pJ1h1eJ|hbh_PRThTERS-1]+1, &Jead_se1, hbLL, hbLL, hbLL) > 0 ) {
1oJ {1h1 1=0, 1<hbh_PRThTERS, 1++) {
11 {F0_TSSET{pJ1h1eJ|1], &Jead_se1)) {
pJ1h11{"|<s> PRThTThC]. ", PRThTER_hAhE|1]),
chaJ bu1|BbF_STZE],
1h1 h = 0,
do {
h = Jead{pJ1h1eJ|1], bu1, BbF_STZE),
11 {h > 0) {
bu1|h] = `\0`,
pJ1h11{"s", bu1),
}
} wh1e {h > 0),

cose{pJ1h1eJ|1]),
pJ1h1eJ|1] = opeh{PRThTER_hAhE|1], 0_R00hLY]0_h0hBL0CK),
}
} } }
}

You might also like