SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
TLPI - Chapter 44
Pipe and Fifos
Shu-Yu Fu (shuyufu at gmail.com)
Pipes
● Pipes can be used to pass data between
related processes.
$ ls | wc -l
Important Characteristics of Pipes
● A pipe is a byte stream
● Reading from a pipe
● Pipes are unidirectional
● Writes of up to PIPE_BUF bytes are
guaranteed to be atomic
○ using fpathconf(fd, _PC_PIPE_BUF) to return the
actual upper limit
● Pipes have a limited capacity
○ using fcntl(fd, F_SETPIPE_SZ, size) to change the
capacity of the pipe referred to by fd
○ using fcntl(fd, F_GETPIPE_SZ) to get the capacity of
the pipe referred to by fd
Creating and Using Pipes
#include <unistd.h>
int pipe(int filedes[2]);
Creating and Using Pipes (cont.)
● We can also use the stdio functions (printf(),
scanf(), and so on) with pipes by first using
fdopen() to obtain a file stream corresponding
to one of the descriptors in fieldes.
○ Using ioctl(fd, FIONREAD, &cnt) to get the number of
unread bytes in the pipe referred to by fd
A pipe has few uses within a single
process
A pipe has few uses within a single
process (cont.)
● If we require bidirectional communication,
there is a simpler way: just create two pipes,
one for sending data in each direction
between the two processes.
○ deadlocks may occur if both processes block while
trying to read from empty pipes or while trying to
write to pipes that are already full
Closing unused pipe file descriptors
● Ensuring a process doesn't exhaust its limited set of file
descriptors.
● It is only after all file descriptors in all processes that refer to
a pipe are closed that the pipe is destroyed.
Pipes as a Method of Process
Synchronization
● Listing 44-3: Using a pipe to synchronize
multiple processes
● It can be used to coordinate the actions of
one process with multiple other (related)
processes.
● The fact that multiple (standard) signals can't
be queued makes signals unsuitable in this
case. But signals can be broadcasted by one
process to all of the members of a process
group.
Using Pipes to Connect Filters
● Listing 44-4: Using a pipe to connect ls and
wc
● dup2() allows us to explicitly specify the
descriptor to be bound
int pfd[2];
pipe(pfd);
if (pfd[1] != STDOUT_FILENO) {
dup2(pfd[1], STDOUT_FILENO);
close(pfd[1];
}
Talking to a Shell Command via a
Pipe: popen()
#include <stdio.h>
FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);
Pipes and stdio Buffering
● Since the file stream pointer returned by a
call to popen() doesn't refer to a terminal, the
stdio library applies block buffering to the file
stream.
Pipes and stdio Buffering (cont.)
● When we call popen() with a mode of w, then
output is sent to the child process only when
the stdio buffer is filled or we close the pipe
with pclose().
○ using fflush() or setbuf(fp, NULL) to ensure the child
process receives data immediately.
Pipes and stdio Buffering (cont.)
● If the process calling popen() is reading from
the pipe, things may not be so
straightforward.
○ Modifying the source code to include calls to setbuf()
or fflush().
○ Using pseudoterminal (Chapter 64)
FIFOs
● FIFOs are a variation on the pipe concept. The
important difference is that FIFOs can be used
for communication between any processes.
● A FIFO has a name within the file system
and is opened in the same way as a regular
file.
$ mkfifo [ -m mode ] pathname
or
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
FIFOs (cont.)
● Opening a FIFO for reading blocks until
another process opens the IFO for writing,
and vice versa for writing.
○ Using O_NONBLOCK flag to prevent blocking.
● Using FIFOs and tee(1) to create a dual
pipeline
$ mkfifo myfifo
$ wc -l < myfifo &
$ ls -l | tee myfifo | sort -k5n
Nonblocking I/O (O_NONBLOCK flag)
● open()
○ It allows a single process to open both ends of a
FIFO.
○ It prevents deadlocks between processes opening
two FIFOs.
Nonblocking I/O (O_NONBLOCK flag)
● A deadlock
● O_NONBLOCK affects the semantics of
subsequent read() and write() calls.
○ Using fcntl(fd, F_SETFL, flags) to enable/disable
O_NONBLOCK bit.
Semantics of read() and write() on
Pipes and FIFOs
● read()
Semantics of read() and write() on
Pipes and FIFOs (cont.)
● write()

Mais conteúdo relacionado

Mais procurados

Inter process communication
Inter process communicationInter process communication
Inter process communication
Pradeep Kumar TS
 
Compression Commands in Linux
Compression Commands in LinuxCompression Commands in Linux
Compression Commands in Linux
Pegah Taheri
 
Network file system (nfs)
Network file system (nfs)Network file system (nfs)
Network file system (nfs)
Raghu nath
 

Mais procurados (20)

Linux Commands - Cheat Sheet
Linux Commands - Cheat Sheet Linux Commands - Cheat Sheet
Linux Commands - Cheat Sheet
 
2
22
2
 
introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Compression Commands in Linux
Compression Commands in LinuxCompression Commands in Linux
Compression Commands in Linux
 
Tcpdump
TcpdumpTcpdump
Tcpdump
 
Top 10 Random Linux/Ubuntu Commands
Top 10 Random Linux/Ubuntu CommandsTop 10 Random Linux/Ubuntu Commands
Top 10 Random Linux/Ubuntu Commands
 
Linux System Administration - NFS Server
Linux System Administration - NFS ServerLinux System Administration - NFS Server
Linux System Administration - NFS Server
 
Basic commands
Basic commandsBasic commands
Basic commands
 
Unix system programming
Unix system programmingUnix system programming
Unix system programming
 
MPI
MPIMPI
MPI
 
Linux06 nfs
Linux06 nfsLinux06 nfs
Linux06 nfs
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in Linux
 
BPF: Next Generation of Programmable Datapath
BPF: Next Generation of Programmable DatapathBPF: Next Generation of Programmable Datapath
BPF: Next Generation of Programmable Datapath
 
Fun with FUSE
Fun with FUSEFun with FUSE
Fun with FUSE
 
Network file system (nfs)
Network file system (nfs)Network file system (nfs)
Network file system (nfs)
 
Linux commands part -2
Linux commands part -2Linux commands part -2
Linux commands part -2
 
Rpm Introduction
Rpm IntroductionRpm Introduction
Rpm Introduction
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)
 

Destaque

程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1
Shu-Yu Fu
 
程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體
Shu-Yu Fu
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4
Shu-Yu Fu
 
程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5
Shu-Yu Fu
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8
Shu-Yu Fu
 
Tlpi chapter 38 writing secure privileged programs
Tlpi   chapter 38 writing secure privileged programsTlpi   chapter 38 writing secure privileged programs
Tlpi chapter 38 writing secure privileged programs
Shu-Yu Fu
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 Process
Shu-Yu Fu
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File Systems
Shu-Yu Fu
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
Lin Yo-An
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory Allocation
Shu-Yu Fu
 

Destaque (11)

程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1
 
程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4
 
程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8
 
Tlpi chapter 38 writing secure privileged programs
Tlpi   chapter 38 writing secure privileged programsTlpi   chapter 38 writing secure privileged programs
Tlpi chapter 38 writing secure privileged programs
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 Process
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File Systems
 
BT5攻防手法
BT5攻防手法BT5攻防手法
BT5攻防手法
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory Allocation
 

Semelhante a TLPI - Chapter 44 Pipe and Fifos

18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx
MdshadabArshad
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
Rajeshkumar Reddy
 
Named pipe is for a bidirection communication- A named pipe (also kn.pdf
Named pipe is for a bidirection communication-   A named pipe (also kn.pdfNamed pipe is for a bidirection communication-   A named pipe (also kn.pdf
Named pipe is for a bidirection communication- A named pipe (also kn.pdf
actexerode
 

Semelhante a TLPI - Chapter 44 Pipe and Fifos (20)

Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx
 
Unix systems programming interprocess communication/tutorialoutlet
Unix systems programming interprocess communication/tutorialoutletUnix systems programming interprocess communication/tutorialoutlet
Unix systems programming interprocess communication/tutorialoutlet
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Handout#01
Handout#01Handout#01
Handout#01
 
File management
File managementFile management
File management
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
File handling(some slides only)
File handling(some slides only)File handling(some slides only)
File handling(some slides only)
 
File management
File managementFile management
File management
 
File management
File managementFile management
File management
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx
 
EST 102 Programming in C-MODULE 5
 EST 102 Programming in C-MODULE 5 EST 102 Programming in C-MODULE 5
EST 102 Programming in C-MODULE 5
 
Lavigne bsdmag may13
Lavigne bsdmag may13Lavigne bsdmag may13
Lavigne bsdmag may13
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
Named pipe is for a bidirection communication- A named pipe (also kn.pdf
Named pipe is for a bidirection communication-   A named pipe (also kn.pdfNamed pipe is for a bidirection communication-   A named pipe (also kn.pdf
Named pipe is for a bidirection communication- A named pipe (also kn.pdf
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 

Último

Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448
ont65320
 
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Apsara Of India
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
russian goa call girl and escorts service
 
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl GoaRussian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goa
sexy call girls service in goa
 

Último (20)

↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Sonagachi ⟟ 8250192130 ⟟ High Class Call Girl...
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
 
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment BookingCall Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
 
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
 
Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448
 
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
 
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
 
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
 
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceCollege Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
 
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
 
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
 
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
 
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
 
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl GoaRussian Escorts Agency In Goa  💚 9316020077 💚 Russian Call Girl Goa
Russian Escorts Agency In Goa 💚 9316020077 💚 Russian Call Girl Goa
 
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
 

TLPI - Chapter 44 Pipe and Fifos

  • 1. TLPI - Chapter 44 Pipe and Fifos Shu-Yu Fu (shuyufu at gmail.com)
  • 2. Pipes ● Pipes can be used to pass data between related processes. $ ls | wc -l
  • 3. Important Characteristics of Pipes ● A pipe is a byte stream ● Reading from a pipe ● Pipes are unidirectional ● Writes of up to PIPE_BUF bytes are guaranteed to be atomic ○ using fpathconf(fd, _PC_PIPE_BUF) to return the actual upper limit ● Pipes have a limited capacity ○ using fcntl(fd, F_SETPIPE_SZ, size) to change the capacity of the pipe referred to by fd ○ using fcntl(fd, F_GETPIPE_SZ) to get the capacity of the pipe referred to by fd
  • 4. Creating and Using Pipes #include <unistd.h> int pipe(int filedes[2]);
  • 5. Creating and Using Pipes (cont.) ● We can also use the stdio functions (printf(), scanf(), and so on) with pipes by first using fdopen() to obtain a file stream corresponding to one of the descriptors in fieldes. ○ Using ioctl(fd, FIONREAD, &cnt) to get the number of unread bytes in the pipe referred to by fd
  • 6. A pipe has few uses within a single process
  • 7. A pipe has few uses within a single process (cont.) ● If we require bidirectional communication, there is a simpler way: just create two pipes, one for sending data in each direction between the two processes. ○ deadlocks may occur if both processes block while trying to read from empty pipes or while trying to write to pipes that are already full
  • 8. Closing unused pipe file descriptors ● Ensuring a process doesn't exhaust its limited set of file descriptors. ● It is only after all file descriptors in all processes that refer to a pipe are closed that the pipe is destroyed.
  • 9. Pipes as a Method of Process Synchronization ● Listing 44-3: Using a pipe to synchronize multiple processes ● It can be used to coordinate the actions of one process with multiple other (related) processes. ● The fact that multiple (standard) signals can't be queued makes signals unsuitable in this case. But signals can be broadcasted by one process to all of the members of a process group.
  • 10. Using Pipes to Connect Filters ● Listing 44-4: Using a pipe to connect ls and wc ● dup2() allows us to explicitly specify the descriptor to be bound int pfd[2]; pipe(pfd); if (pfd[1] != STDOUT_FILENO) { dup2(pfd[1], STDOUT_FILENO); close(pfd[1]; }
  • 11. Talking to a Shell Command via a Pipe: popen() #include <stdio.h> FILE *popen(const char *command, const char *mode); int pclose(FILE *stream);
  • 12. Pipes and stdio Buffering ● Since the file stream pointer returned by a call to popen() doesn't refer to a terminal, the stdio library applies block buffering to the file stream.
  • 13. Pipes and stdio Buffering (cont.) ● When we call popen() with a mode of w, then output is sent to the child process only when the stdio buffer is filled or we close the pipe with pclose(). ○ using fflush() or setbuf(fp, NULL) to ensure the child process receives data immediately.
  • 14. Pipes and stdio Buffering (cont.) ● If the process calling popen() is reading from the pipe, things may not be so straightforward. ○ Modifying the source code to include calls to setbuf() or fflush(). ○ Using pseudoterminal (Chapter 64)
  • 15. FIFOs ● FIFOs are a variation on the pipe concept. The important difference is that FIFOs can be used for communication between any processes. ● A FIFO has a name within the file system and is opened in the same way as a regular file. $ mkfifo [ -m mode ] pathname or #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);
  • 16. FIFOs (cont.) ● Opening a FIFO for reading blocks until another process opens the IFO for writing, and vice versa for writing. ○ Using O_NONBLOCK flag to prevent blocking. ● Using FIFOs and tee(1) to create a dual pipeline $ mkfifo myfifo $ wc -l < myfifo & $ ls -l | tee myfifo | sort -k5n
  • 17. Nonblocking I/O (O_NONBLOCK flag) ● open() ○ It allows a single process to open both ends of a FIFO. ○ It prevents deadlocks between processes opening two FIFOs.
  • 18. Nonblocking I/O (O_NONBLOCK flag) ● A deadlock ● O_NONBLOCK affects the semantics of subsequent read() and write() calls. ○ Using fcntl(fd, F_SETFL, flags) to enable/disable O_NONBLOCK bit.
  • 19. Semantics of read() and write() on Pipes and FIFOs ● read()
  • 20. Semantics of read() and write() on Pipes and FIFOs (cont.) ● write()