Advance Innovation Centre
  • AIC Knowledge @ EEC for All
  • 😎Logical Thinking
    • Karel Robot
    • Code to Flowchart
    • Play with Docker
    • CNX Software
  • MCU & Interfacing with Infineon PSOCâ„Ē
    • Basic MCU Interfacing
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
      • PSoCâ„Ē 6S2 Peripherals Interfacing (GPIO)
        • Hello World and LED Blinking
        • GPIO Principles
        • PSoCâ„Ē 6S2 GPIO-HAL LED Blink Lab
        • PSoCâ„Ē 6S2 GPIO-PDL LED Blink Lab
        • Button "Bounce" Principles
          • Push/Pull Button to Turn ON/OFF LED via HAL
          • Push/Pull Button to Turn ON/OFF LED via PDL
          • GPIO Button Interrupt via HAL
          • GPIO Button Interrupt via PDL
        • GPIO variables & functions
      • PSoCâ„Ē 6S2 Peripherals Interfacing (ADC, PWM)
        • PSoCâ„Ē 6S2 SAR ADC
          • ADC Principles
          • PSoCâ„Ē 6S2 with ADC Labs
            • Reading potentiometer sensor value via an ADC HAL
            • Reading potentiometer sensor value via an ADC PDL
        • PSoCâ„Ē 6S2 PWM & TCPWM
          • PWM Principles
          • PSoCâ„Ē 6S2 for PMW Function Labs
            • LED Brightness using PWM via HAL
            • LED Brightness using PWM via PDL
    • Sensor Interfacing and HMI
      • OLED Display
        • OLED Display Principles
        • Calling BDH’s OLED functions
        • Display ADC via Potentiometer on OLED
      • BDH Shell
        • Shell Principles
        • LED Blinking and CAPSENSE via BDH Shell
        • Adding "History" command
        • Adding "Reboot" command
        • CAPSENSE Button and Slider
          • CAPSENSE Button and Slider with Capsense Tuner
          • CAPSENSE Button and Slider using FreeRTOS
    • Serial Communication & Visualization
      • UART, I2C, SPI Communication via Infineon PSoCâ„Ē6
      • BMX160 Sensor Communication via Infineon PSoCâ„Ē6
        • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
        • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
        • Motion Sensors GUI Integration via Serial Studio
    • IoT Connectivity & Data Analytics via Node-Red
      • Node-Red Installation
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • Edge AI on PSoCâ„Ē
      • Machine Learning on PSoCâ„Ē6 via Edge-Impulse
    • Infineon PSoCâ„Ē Troubleshooting
  • IoT Development with Infineon PSOCâ„Ē & BDH Platform
    • PSoCâ„Ē IoT Development Kit
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
        • Hello World and LED Blinking
    • IoT Connectivity
      • Node-Red Installation
      • Controlling PSoCâ„Ē LED using MQTT
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • BDH IoT Connectivity
    • WireLinXâ„Ē IoT PLC
    • BDH X-Brain Data Analytics
      • PSoC6 Data Collection to CSV log file
    • Data Visualization
      • āļŠāļĢāđ‰āļēāļ‡ Dashboard āļ”āđ‰āļ§āļĒ Looker Studio
  • ðŸ–ĨïļOperation Systems
    • Prerequisites
      • Guideline from Ubuntu
        • Ubuntu and VSCode on WSL2
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ WSL 2
      • Run Ubuntu on VirtualBox7
    • Zero to Linux Hero
      • Computer OS Architecture
      • Anatomy of Linux System
        • UNIX/Linux History
        • UNIX/Linux Evolution
        • GNU Project
        • Linux OS Architecture
        • Command Line Interface (CLI)
          • Basic Commands
          • 😎Level up your Linux Shell
          • File & Dir. Commands
          • Searching Commands
          • 😎ChatGPT-based Terminal
          • SysAdmin Commands
          • Network Commands
          • Hacker Commands
        • Busybox
        • Shell Script
          • Awk Script
          • Bash Shell Script
            • Bash Snippets
            • Bash Useful Examples
      • Anatomy of Linux Kernel
        • Linux Kernel Principles
        • Linux Environment for Developer
      • Anatomy of Embedded Linux
        • Embedded Linux
        • Host & Target
        • Cross Toolchains
        • Bootloader
        • Building Embedded Linux
    • Linux OS Dev. Engineer
      • Process Management
        • Process Basic
        • Process State
        • Basic Process Mgmt. Commands
        • Advance Process Mgmt. Commands
        • Process API Programming
      • IPC
        • IPC Anatomy
        • Signal Programming
        • Pipe Programming
        • FIFO Programming
        • Msg. Queue Programming
          • System V
        • Share Memory Programming
          • System V
        • Socket Programming
      • POSIX Threads
        • Multi-tasking Basic
        • POSIX Thread Anatomy
        • Threading Programming
      • Applied IPC
        • Remote Commander
        • Multi-Remote Commanders
      • Process Synchronization
        • Mutex Programming
        • Semaphore Programming
      • Applied IPC with Semaphore
  • ⌚Embedded Systems Development
    • Introduction to ESD
      • Why's ESD?
      • What it use for?
      • How it works?
    • Enbedded System Development via PSoC6
      • Basic MCU Interfacing
        • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
        • Development Environment Preparation
        • PSoCâ„Ē 6S2 Peripherals Interfacing (GPIO)
          • Hello World and LED Blinking
          • GPIO Principles
          • PSoCâ„Ē 6S2 GPIO-HAL LED Blink Lab
          • PSoCâ„Ē 6S2 GPIO-PDL LED Blink Lab
          • Button "Bounce" Principles
            • Push/Pull Button to Turn ON/OFF LED via HAL
            • Push/Pull Button to Turn ON/OFF LED via PDL
            • GPIO Button Interrupt via HAL
            • GPIO Button Interrupt via PDL
          • GPIO variables & functions
        • PSoCâ„Ē 6S2 Peripherals Interfacing (ADC, PWM)
          • PSoCâ„Ē 6S2 SAR ADC
            • ADC Principles
            • PSoCâ„Ē 6S2 with ADC Labs
              • Reading potentiometer sensor value via an ADC HAL
              • Reading potentiometer sensor value via an ADC PDL
          • PSoCâ„Ē 6S2 PWM & TCPWM
            • PWM Principles
            • PSoCâ„Ē 6S2 for PMW Function Labs
              • LED Brightness using PWM via HAL
              • LED Brightness using PWM via PDL
      • Sensor Interfacing and HMI
        • OLED Display
          • OLED Display Principles
          • Calling BDH’s OLED functions
          • Display ADC via Potentiometer on OLED
        • BDH Shell
          • Shell Principles
          • LED Blinking and CAPSENSE via BDH Shell
          • Adding "History" command
          • Adding "Reboot" command
          • CAPSENSE Button and Slider
            • CAPSENSE Button and Slider with Capsense Tuner
            • CAPSENSE Button and Slider using FreeRTOS
      • Serial Communication & Visualization
        • UART, I2C, SPI Communication via Infineon PSoCâ„Ē6
        • BMX160 Sensor Communication via Infineon PSoCâ„Ē6
          • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
          • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
          • Motion Sensors GUI Integration via Serial Studio
    • Edge Computing and IoT Connectivity
    • Cloud-Based Data Analytics and Digital Twin
    • Edge Vision AI
    • Resources
      • Basic Hardware and Firmware
        • Environment Preparation
          • āļāļēāļĢāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄ Arduino IDE
            • āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™āđƒāļŠāđ‰āļ‡āļēāļ™ Arduino IDE
          • āļāļēāļĢāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄāļŠāļģāļŦāļĢāļąāļšāđƒāļŠāđ‰āļ‡āļēāļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ§āļąāļ” NI MyDAQ
            • āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļ•āļąāđ‰āļ‡āļ„āđˆāļēāđƒāļŠāđ‰ Digital Multimeter -NI ELVISmx
            • āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļ•āļąāđ‰āļ‡āļ„āđˆāļēāđƒāļŠāđ‰ Oscilloscope-NI ELVISmx
          • āļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄ KingstVIS
        • Basic measurement
          • Basic Digital and Analog I/O
            • LAB: Basic Digital Input/Output
            • LAB: Basic Analog Input/Output
          • Waveform
            • LAB: Oscilloscope
            • LAB: Oscilloscope and Function Generator
            • LAB: Pulse Width Modulation (PWM)
              • Homework
        • Interfacing and Communication
          • LAB: UART, RS485, RS232 Protocol
          • LAB: I2C Protocol
            • HOMEWORK
          • LAB: SPI Protocol
      • IoT Connectivity
        • Example: IoT with MQTT on Node-red
        • Data logger
        • LAB: Data Visualization
  • 🛠ïļC/C++ for Embedded Programming
    • Development Environment Preparation
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ WSL 2
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ Ubuntu environment
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄ Visual Studio Code
      • āļāļēāļĢāđ€āļŠāļ·āđˆāļ­āļĄāļ•āđˆāļ­ Virtual studio code āđ€āļ‚āđ‰āļēāļāļąāļš WSL
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ docker on WSL
    • Principle C/C++ Programming
      • Get started with C++
      • Makefile
        • Makefile Examples
      • Compiling and running
        • How to create a program that you can enter inputs.
          • Lab 1 Exercise
      • Arguments
        • Command line arguments in C and C++
      • signed and unsigned data types
      • Variable and Operator
      • If and If else
      • Loop, Infinite loop, and flag
        • Loop and Flag exercise
      • Array
        • Get to know with arrays
        • Implement example
      • Vector
    • Object Oriented Programming (OOP) in C++
      • Class and Object
      • Encapsulation and Abstraction
      • Polymorphism and Inheritance
    • C/C++ Preprocessing
      • Macro
        • Quiz Macro
      • File Inclusion
      • Conditional Compilation
      • Pragma directive
        • Quiz Pragma
    • String in C++
      • Concatenation
      • Split
    • Type conversions for C/C++
      • Conversion using Cast operator
    • Error handling
    • Data logger
      • āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āđ„āļŸāļĨāđŒāđāļĨāļ°āđ€āļ‚āļĩāļĒāļ™āđ„āļŸāļĨāđŒ
      • āļāļēāļĢāļ­āđˆāļēāļ™āđ„āļŸāļĨāđŒ
      • āļāļēāļĢāđ€āļāđ‡āļšāļ‚āđ‰āļ­āļĄāļđāļĨāļāļąāļšTime stamp
    • High performance programing
      • Multi-task and Multi-thread
        • Multi-threading example
      • Mutex
      • Queue
      • OpenCV
    • C/C++ Techniques
      • Makefile in action
      • Object Oriented Programming (OOP) in C++
        • Class and Object
        • Encapsulation and Abstraction
        • Polymorphism and Inheritance
      • C/C++ Preprocessing
        • Macro
          • Quiz Macro
        • File Inclusion
        • Conditional Compilation
        • Pragma directive
          • Quiz Pragma
      • Binary, Octal and Hexadecimal Numbers
      • Array and properties of an array
        • Get to know with arrays
        • Implement example
      • What's next?
  • ðŸĪ–Artificial Intelligence (AI)
    • VAMStack Design House, BUU
    • Data Analytics
      • Data cleansing
      • Data analytics
      • Data analytic exercise
    • Machine Learning
      • Neural Network Layers
      • Machine learning type
      • Dataset
      • Using Edge Impulse for AI Model
    • Basic Image Processing
      • Computer Vision using Python Language
        • Installation
        • Computer Vision Basics
          • Pixel and Color
          • Draw image
          • Basic Image processing
          • Morphology Transformations
          • Gaussian blur
          • Simple Thresholding
          • Contour
          • Canny edge detection
        • Case Study
          • Coin counting
          • Color detection & tracking
        • VAM_CV SDK
  • ⚙ïļFPGA Design and Development
    • Verilog HDL via Vivado IDE
      • LAB1: Setting Environment and Create Project
        • Create Vivado Project
      • LAB2: Hardware Description Language Work Flow
        • Simulation code
      • LAB3: Design HDL Project
        • Top Level Design
        • Top-level Simulation
      • LAB4: Asynchronous VS Synchronous Circuit
        • Simulation Synchronous counter
    • C/C++ Programming on Ultra96v2 FPGA Board
      • Application C/C++ on Ultra96v2 Part 1
        • Design Overview
        • Step 1 - Burn the image to SD card
        • Step 2 - Bring up Ultra96v2
        • Step 3 - Installing the Vitis-AI runtime packages
      • Application C/C++ on Ultra96v2 Part 2
        • STEP 1 : Setting auto boot Wifi
        • STEP 2 : How to working on Embedded
        • STEP 3 : How to run the test code
  • ðŸĪ–Robotics
    • Dobot Magician
      • Instruction of Dobot
      • Software Download
      • Basically of Program
        • Teaching and Playback
        • Write and Draw
        • LaserEngraving
        • 3D Printer
    • Robotino
      • Software Download
        • Robotino View
        • Robotino SIM
      • Charging
      • Connecting
      • Follow Line example
        • Basic block in Follow Line
    • RaspBlock
      • Get Started with Raspblock
  • ðŸšĐSpecial Topics
    • Node-Red
      • Set up Raspberry Pi
      • Install node red in Raspberry Pi
      • Get started with Node Red
        • Open node-red
        • Turn off node red
        • Install Dashboard on Node-red
        • Use node red to show message
        • Using Ultrasonic sensor with node-red
    • IoT Cloud
      • Overview
        • How do they work?
          • Basic Knowlege
      • Installations
        • Install Docker
        • Install Mosquitto Broker
        • Install InfluxDB
        • Install Telegraf
        • Install Grafana
      • Get Sensor Value and Send to MQTT
        • Connect ESP3266 to sensor
        • Connect ESP3266 to MQTT
      • Integration
    • Senses IoT
      • SENSES IoT Platform
      • LAB8: MCU send data to IoT platform
    • CrowPi Dev Kit
      • Raspberry Pi with CrowPi
      • Remote to Raspberry Pi
      • Cross-Compile
        • Lab 1: Programming and cross complier
      • Hardware and Interfaces Usage CLI
        • LAB: Usage GPIO via CLI
        • LAB: Scan I2C bus via CLI
      • Python library for Crow Pi
      • wiringPi library (C) for CrowPi
        • Lab2: Crowpi and sensors
    • LVGL Development
      • LVGL - Light and Versatile Embedded Graphics Library
        • Setting program for LVGL Simulator
        • Get started with LVGL simulator
        • Example Library of LVGL
        • Create your own screen
          • Exercise
        • Style
          • Exercise
        • Event
    • Docker OS
      • Docker OS Part 1
        • Part 1 : Installation
        • Part 2 : Basic Docker OS and Linux CLI
      • Docker OS Part 2
        • Part 1 : Docker communication
        • Part 2 : Docker compose
      • Application Gstreamer on devcontainer
        • STEP 1 : Setting gstreamer environment
        • STEP 2 : Create the Gstreamer element on template
        • STEP 3 : Testing and application on your gst element
  • ðŸĪŸRecommended by AIC
    • Skill Roadmap
      • Embedded Engineer
      • Developer
    • Hardware Programming
    • Embedded Programming
    • General-propose Programming
    • Algorithmica
    • Thai Expert Knowledge
    • RT-Thread University Program
      • Infineon PSoC6
      • Kernel
        • Kernel Basics
        • Thread Management
        • Clock Management
        • Inter-thread synchronization
        • Inter-thread communication
        • Memory Management
        • Interrupt Management
        • Kernel porting
        • Atomic Operations
        • RT-Thread SMP
        • Kernel API Changelog
      • Tools
      • Devices & Drivers
        • SENSOR Devices
        • Touch Equipment
        • CRYPTO Devices
        • AUDIO Devices
        • Pulse Encoder Devices
      • Components
        • C Library (libc)
        • ISO/ANSI C Standard
        • POSIX Standard
          • FILE (File IO)
          • Pthread
          • Timer
          • IPC Semaphore
          • IPC Message Queues
          • Dynamic Modules
        • Network Components
          • FinSH Console
          • FAL: Flash Abstraction Layer
          • Virtual File System
          • tmpfs: temporary file system
          • ulog log
          • utest testing framework
          • Power Management
          • RT-Link
        • Software Packages
          • Internet of Things
            • MQTT-umqtt
            • Telnet
          • Tools
            • SystemView
            • SEGGER_RTT
          • LVGL Manual
            • Touch Screen Driver
      • Demo
        • Infineon Gateway
        • Handwriting Recognition (MNIST)
        • Object Detection (Darknet)
        • ROS using RT-Thread
        • Control the car using RT-Thread
        • LiDAR via RT-Thread
        • Detection via RT-Thread and ROS
        • Sensor Driver Development Guide
Powered by GitBook

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University

On this page

Was this helpful?

  1. Operation Systems
  2. Linux OS Dev. Engineer
  3. IPC

Pipe Programming

PreviousSignal ProgrammingNextFIFO Programming

Last updated 1 year ago

Was this helpful?

āđ„āļ›āļ›āđŒ (Pipe)

āļŦāļĨāļēāļĒāđ†āļ„āļĢāļąāđ‰āļ‡āļ—āļĩāđˆāļœāļđāđ‰āđƒāļŠāđ‰āļ‡āļēāļ™āļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāļĨāļĩāļ™āļļāļāļ‹āđŒāļˆāļ°āļ•āđ‰āļ­āļ‡āļĄāļĩāļāļēāļĢāđƒāļŠāđ‰āļ„āļģāļŠāļąāđˆāļ‡āļĄāļēāļāļāļ§āđˆāļēāļŦāļ™āļķāđˆāļ‡āļ„āļģāļŠāļąāđˆāļ‡āđ€āļžāļ·āđˆāļ­āļ•āđ‰āļ­āļ‡āļāļēāļĢāļ—āļĩāđˆāļˆāļ°āļāļĢāļ­āļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļ—āļĩāđˆāļ•āđ‰āļ­āļ‡āļāļēāļĢ āđ‚āļ”āļĒāđ€āļŠāļ·āđˆāļ­āļĄāļŠāļļāļ”āļ„āļģāļŠāļąāđˆāļ‡āđ€āļŦāļĨāđˆāļēāļ™āļąāđ‰āļ™āļ”āđ‰āļ§āļĒāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļŦāļĄāļēāļĒ â€œ|” āļŦāļĢāļ·āļ­āđ€āļĢāļĩāļĒāļāļ§āđˆāļēāđ„āļ›āļ›āđŒ (Pipe) āļ”āļąāļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡ āļ‹āļķāđˆāļ‡āļœāļĨāļĨāļąāļžāļ˜āđŒāļ‚āļ­āļ‡āļ„āļģāļŠāļąāđˆāļ‡āļ”āđ‰āļēāļ™āļ‹āđ‰āļēāļĒāļˆāļ°āļŠāđˆāļ‡āđ€āļ‚āđ‰āļēāđ€āļ›āđ‡āļ™āļ­āļīāļ™āļžāļļāļ—āļ•āđˆāļ­āđ„āļ›āļ‚āļ­āļ‡āļ„āļģāļŠāļąāđˆāļ‡āļ”āđ‰āļēāļ™āļ‚āļ§āļēāļ­āļĒāđˆāļēāļ‡āļ™āļĩāđ‰āđ„āļ›āđ€āļĢāļ·āđˆāļ­āļĒāđ†āļˆāļ™āļāļĢāļ°āļ—āļąāđˆāļ‡āļ–āļķāļ‡āļ„āļģāļŠāļąāđˆāļ‡āļŠāļļāļ”āļ—āđ‰āļēāļĒāļœāļĨāļĨāļąāļžāļ˜āđŒāļāđ‡āļˆāļ°āđāļŠāļ”āļ‡āļ­āļ­āļāļŦāļ™āđ‰āļēāļˆāļ­āđƒāļ™āļ—āļĩāđˆāļŠāļļāļ”

$  echo "Hello World" | awk '{print $1}'
Hello

āļ—āđˆāļ­ (Pipe) āđ€āļ›āđ‡āļ™āļāļĢāļ°āļšāļ§āļ™āļāļēāļĢāļ—āļĩāđˆāđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļ•āļīāļ”āļ•āđˆāļ­āļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļ—āļĩāđˆāļ‡āđˆāļēāļĒāļ—āļĩāđˆāļŠāļļāļ”āļ„āļ·āļ­āļāļēāļĢāļ™āļģāļœāļĨāļĨāļąāļžāļ˜āđŒāļ—āļĩāđˆāđ„āļ”āđ‰āļˆāļēāļāđ‚āļ›āļĢāđāļāļĢāļĄāļŦāļ™āļķāđˆāļ‡āđ„āļ›āđ€āļ›āđ‡āļ™āļ­āļīāļ™āļžāļļāļ—āļ‚āļ­āļ‡āļ­āļĩāļāđ‚āļ›āļĢāđāļāļĢāļĄāļŦāļ™āļķāđˆāļ‡ āđ‚āļ”āļĒāđ„āļ›āļ›āđŒāļˆāļ°āđ€āļ›āđ‡āļ™āļ•āļąāļ§āļāļĨāļēāļ‡āđƒāļ™āļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļ‹āļķāđˆāļ‡āļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļˆāļ°āđ€āļ›āđ‡āļ™āđāļšāļšāļ—āļīāļĻāļ—āļēāļ‡āđ€āļ”āļĩāļĒāļ§ (unidirectional) āļ”āļąāļ‡āļĢāļđāļ›

āđ‚āļ”āļĒāđ€āļĄāļ·āđˆāļ­āđ‚āļ›āļĢāđ€āļ‹āļŠāļŦāļ™āļķāđˆāļ‡āļ•āđ‰āļ­āļ‡āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āđ„āļ›āļ›āđŒāļ‚āļķāđ‰āļ™āļĄāļē āļ‚āļšāļ§āļ™āļāļēāļĢāļ‚āļ­āļ‡āđ€āļ„āļ­āļĢāđŒāđ€āļ™āļĨāļāđ‡āļˆāļ°āļ—āļģāļāļēāļĢāļŠāļĢāđ‰āļēāļ‡ file descriptor (fd) āļ‚āļķāđ‰āļ™āļĄāļēāļŠāļ­āļ‡āđ„āļŸāļĨāđŒāļŠāļģāļŦāļĢāļąāļšāđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡ pipe āļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĨāļ°āđ€āļ„āļ­āļĢāđŒāđ€āļ™āļĨ āđ‚āļ”āļĒāđ„āļŸāļĨāđŒ fd āļ•āļąāļ§āđāļĢāļāļˆāļ°āļ–āļđāļāđƒāļŠāđ‰āđ€āļ›āđ‡āļ™āđ€āļŠāđ‰āļ™āļ—āļēāļ‡āļāļēāļĢāļ›āđ‰āļ­āļ™āļ‚āđ‰āļ­āļĄāļđāļĨāļŦāļĢāļ·āļ­āđ€āļ‚āļĩāļĒāļ™āļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļ‚āđ‰āļēāđ„āļ›āđƒāļ™ pipe (write) āļŠāđˆāļ§āļ™āđ„āļŸāļĨāđŒ fd āļ•āļąāļ§āļ—āļĩāđˆāļŠāļ­āļ‡āļˆāļ°āđƒāļŠāđ‰āđ€āļ›āđ‡āļ™āļ•āļąāļ§āļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāļ—āļĩāđˆāđ„āļ”āđ‰āļĄāļēāļˆāļēāļ pipe (read) āļ—āļģāđƒāļŦāđ‰āđ‚āļ›āļĢāđ€āļ‹āļŠāļ™āļąāđ‰āļ™āļŠāļēāļĄāļēāļĢāļ–āđƒāļŠāđ‰ pipe āđƒāļ™āļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāđ„āļ›āļĄāļēāđƒāļŦāđ‰āļāļąāļ™āđ€āļ­āļ‡āļ āļēāļĒāđƒāļ™āđ‚āļ›āļĢāđ€āļ‹āļŠāđ„āļ”āđ‰ āļ”āļąāļ‡āđāļŠāļ”āļ‡āđƒāļ™āļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡

āļˆāļēāļāļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļ•āđ‰āļ™āđāļŠāļ”āļ‡āđƒāļŦāđ‰āđ€āļŦāđ‡āļ™āļ§āđˆāļēāđ€āļĄāļ·āđˆāļ­āđ„āļŸāļĨāđŒ fd āļ—āļąāđ‰āļ‡āļŠāļ­āļ‡ (pfd0 āđāļĨāļ° pfd1) āļ–āļđāļāđ€āļŠāļ·āđˆāļ­āļĄāļ–āļķāļ‡āļāļąāļ™āđāļĨāđ‰āļ§ āđ€āļĄāļ·āđˆāļ­āđ‚āļ›āļĢāđ€āļ‹āļŠāļ•āđ‰āļ­āļ‡āļāļēāļĢāļ—āļĩāđˆāļˆāļ°āļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļœāđˆāļēāļ™ pipe (pfd1) āļ‚āđ‰āļ­āļĄāļđāļĨāļāđ‡āļˆāļ°āļ–āļđāļāļŠāđˆāļ‡āļœāđˆāļēāļ™āđ€āļ„āļ­āļĢāđŒāđ€āļ™āļĨāļ—āļĩāđˆāļŠāļĢāđ‰āļēāļ‡āđ€āļ›āđ‡āļ™ pipe āļ‚āļķāđ‰āļ™āļĄāļēāđāļĨāđ‰āļ§āļœāđˆāļēāļ™āļ‚āđ‰āļ­āļĄāļđāļĨāļ™āļąāđ‰āļ™āļ•āđˆāļ­āđ„āļ›āļĒāļąāļ‡āļ­āļĩāļāļ”āđ‰āļēāļ™āļ‚āļ­āļ‡ pipe āđ€āļžāļ·āđˆāļ­āļāļĨāļēāļĒāđ€āļ›āđ‡āļ™āļ­āļīāļ™āļžāļļāļ—āļ‚āļ­āļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļĄāļēāļĒāļąāļ‡āđ„āļŸāļĨāđŒ pfd0 āļ”āļąāļ‡āđāļŠāļ”āļ‡āđƒāļ™āļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡

āđƒāļ™āļāļēāļĢāđƒāļŠāđ‰āļ‡āļēāļ™āļˆāļĢāļīāļ‡āļ™āļąāđ‰āļ™ pipe āļˆāļ°āļ™āļģāļĄāļēāđƒāļŠāđ‰āļĄāļēāļāđƒāļ™āļāļēāļĢāļŠāļ·āđˆāļ­āļŠāļēāļĢāļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĄāđˆāđāļĨāļ°āđ‚āļ›āļĢāđ€āļ‹āļŠāļĨāļđāļ āđ€āļ™āļ·āđˆāļ­āļ‡āļˆāļēāļāđ‚āļ›āļĢāđ€āļ‹āļŠāļˆāļ°āđ„āļ”āđ‰āļĢāļąāļšāļŠāļ·āļšāļ—āļ­āļ” (inherit) āđƒāļ™āļ—āļĢāļąāļžāļĒāļēāļāļĢāļ•āđˆāļēāļ‡āđ†āļˆāļēāļāđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĄāđˆ āđ€āļŠāđˆāļ™ file descriptors āļ—āļąāđ‰āļ‡āļŦāļĨāļēāļĒāļ—āļĩāđˆāđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĄāđˆāđ„āļ”āđ‰āļŠāļĢāđ‰āļēāļ‡āđ€āļ­āļēāđ„āļ§āđ‰āļ—āļąāļ™āļ—āļĩ āļ”āļąāļ‡āđāļŠāļ”āļ‡āđƒāļ™āļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡

āļˆāļēāļāļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļ•āđ‰āļ™āļˆāļ°āļŠāļąāļ‡āđ€āļāļ•āđ€āļŦāđ‡āļ™āļ§āđˆāļēāļ—āļąāđ‰āļ‡āļŠāļ­āļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļĄāļĩāļāļēāļĢāđ€āļ‚āđ‰āļēāđƒāļŠāđ‰āļ‡āļēāļ™āđ„āļŸāļĨāđŒ pfd0 āđāļĨāļ° pfd1 āļ”āļąāļ‡āļ™āļąāđ‰āļ™āđ€āļžāļ·āđˆāļ­āđƒāļŦāđ‰āļāļēāļĢāļŠāļ·āđˆāļ­āļŠāļēāļĢāļĢāļ°āļŦāļ§āđˆāļēāļ‡āļ—āļąāđ‰āļ‡āļŠāļ­āļ‡āļ­āļĒāļđāđˆāđƒāļ™āļāļāđ€āļāļ“āļ‘āđŒāļ‚āļ­āļ‡ pipe āļ„āļ·āļ­āđāļšāļšāļ—āļīāļĻāļ—āļēāļ‡āđ€āļ”āļĩāļĒāļ§ (unidirectional) āļˆāļ°āļ•āđ‰āļ­āļ‡āļāļģāļŦāļ™āļ”āļ§āđˆāļēāđ‚āļ›āļĢāđ€āļ‹āļŠāđƒāļ”āđ€āļ›āđ‡āļ™āļ„āļ™āļŠāđˆāļ‡āđāļĨāļ°āđ‚āļ›āļĢāđ€āļ‹āļŠāđƒāļ”āđ€āļ›āđ‡āļ™āļ„āļ™āļĢāļąāļš āļ”āļąāļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡āđāļŠāļ”āļ‡āđƒāļŦāđ‰āđ€āļŦāđ‡āļ™āļ–āļķāļ‡āļ•āļąāļ§āđ‚āļ›āļĢāđ€āļ‹āļŠāļĨāļđāļāļˆāļ°āļ—āļģāļŦāļ™āđ‰āļēāļ—āļĩāđˆāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨ (write) āļœāđˆāļēāļ™ pipe āđ„āļ›āļĒāļąāļ‡āđ‚āļ›āļĢ-āđ€āļ‹āļŠāđāļĄāđˆ (read)

āđāļ•āđˆāļ­āļĒāđˆāļēāļ‡āđ„āļĢāļāđ‡āļ•āļēāļĄāļāđ‡āļŠāļēāļĄāļēāļĢāļ–āļŠāļĢāđ‰āļēāļ‡āļāļēāļĢāļŠāļ·āđˆāļ­āļŠāļēāļĢāļ‚āđ‰āļ­āļĄāļđāļĨāđƒāļ™āļ­āļĒāļđāđˆāđƒāļ™āđāļšāļšāļŠāļ­āļ‡āļ—āļīāļĻāļ—āļēāļ‡āđ„āļ”āđ‰ (bi-directional) āđ‚āļ”āļĒāļāļēāļĢāđ€āļžāļīāđˆāļĄ pipe āļ‚āļķāđ‰āļ™āļĄāļēāļ­āļĩāļāļ—āđˆāļ­ āđ‚āļ”āļĒāļāļģāļŦāļ™āļ”āđƒāļŦāđ‰āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĄāđˆāđ€āļ›āđ‡āļ™āļ„āļ™āļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨ (write) āđ„āļ›āļĒāļąāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļĨāļđāļ (read) āđ„āļ”āđ‰āđ€āļŠāđˆāļ™āļāļąāļ™ āļ‹āļķāđˆāļ‡ system call āļ—āļĩāđˆāđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āđāļĨāļ°āļ­āđˆāļēāļ™āļ‚āđ‰āļ­āļĄāļđāļĨāļāđ‡āđƒāļŠāđ‰āļŠāļ·āđˆāļ­āļŸāļąāļ‡āļāđŒāļŠāļąāļ™āļ§āđˆāļē write() āđāļĨāļ° read() āļ•āļĢāļ‡āđ†āđ„āļ”āđ‰āļ—āļąāļ™āļ—āļĩ āļĒāļāđ€āļ§āđ‰āļ™āđ„āļĄāđˆāļŠāļēāļĄāļēāļĢāļ–āđƒāļŠāđ‰āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ lseek() āđ„āļ”āđ‰āļāļąāļš file descriptor āļ‚āļ­āļ‡ pipe āđ„āļ”āđ‰ āļ”āļąāļ‡āļ™āļąāđ‰āļ™āļ–āđ‰āļēāļ•āđ‰āļ­āļ‡āļāļēāļĢāđƒāļŠāđ‰āđ‚āļ›āļĢāđ€āļ‹āļŠāļŠāļēāļĄāļēāļĢāļ–āļŠāļ·āđˆāļ­āļŠāļēāļĢāļāļąāļ™āđ„āļ”āđ‰āļ—āļąāđ‰āļ‡āļŠāļ­āļ‡āļ—āļīāļĻāļ—āļēāļ‡ (bi-directional) āļˆāļ°āļ•āđ‰āļ­āļ‡āđƒāļŠāđ‰ pipe āļˆāļģāļ™āļ§āļ™ n*(n-1) āđ‚āļ”āļĒāļ—āļĩāđˆ n āļ„āļ·āļ­āļˆāļģāļ™āļ§āļ™āđ‚āļ›āļĢāđ€āļ‹āļŠ

āļŸāļąāļ‡āļāđŒāļŠāļąāļ™āđāļĨāļ°āļ•āļąāļ§āđāļ›āļĢāļ—āļĩāđˆāđ€āļāļĩāđˆāļĒāļ§āļ‚āđ‰āļ­āļ‡

  • āđ„āļĨāļšāļĢāļēāļĢāļĩāļ—āļĩāđˆāđ€āļāļĩāđˆāļĒāļ§āļ‚āđ‰āļ­āļ‡āļ„āļ·āļ­ unistd.h

  • āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ int pipe(int *fd_couple)

    • āļ—āļģāļŦāļ™āđ‰āļēāļ—āļĩāđˆāļŠāļĢāđ‰āļēāļ‡ pipe āđāļĨāļ°āđ€āļāđ‡āļšāļĢāļēāļĒāļĨāļ°āđ€āļ­āļĩāļĒāļ”āļ‚āļ­āļ‡ file descriptors āļ—āļąāđ‰āļ‡āļŠāļ­āļ‡

āļāļēāļĢāļžāļąāļ’āļ™āļēāđ‚āļ›āļĢāđāļāļĢāļĄāļŠāļ·āđˆāļ­āļŠāļēāļĢāļ”āđ‰āļ§āļĒāļ§āļīāļ˜āļĩāļāļēāļĢāļŠāļ•āļĢāļĩāļĄāļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļžāļĢāđ€āļ‹āļŠāļŠāļ™āļīāļ”āļ—āļēāļ‡āđ€āļ”āļĩāļĒāļ§āļ™āļĩāđ‰āļˆāļ°āđƒāļŠāđ‰āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ pipe āļ—āļĩāđˆāļ­āļĒāļđāđˆāļ āļēāļĒāđƒāļ™āđ„āļŸāļĨāđŒāđ„āļĨāļšāļĢāļēāļĢāļĩāļŠāļ·āđˆāļ­āļ§āđˆāļē unistd.h

#include   <unistd.h>
int pipe ( int pfd[2] );  <---  pfd āļ•āļąāļ§āđāļ›āļĢ array āļŠāļģāļŦāļĢāļąāļšāđ€āļāđ‡āļšāļ„āđˆāļē file descriptors                                                                                
                                          pfd[0] - file descriptor āļŠāļģāļŦāļĢāļąāļšāļāļēāļĢāļ­āđˆāļēāļ™
                                          pfd[1] - file descriptor āļŠāļģāļŦāļĢāļąāļšāļāļēāļĢāđ€āļ‚āļĩāļĒāļ™               

āļŠāļ–āļēāļ™āļ°āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡ pipe

0 - āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāđ„āļ”āđ‰āļŠāļģāđ€āļĢāđ‡āļˆ

-1 - āļāļēāļĢāļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļĨāđ‰āļĄāđ€āļŦāļĨāļ§

āļˆāļēāļāļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļšāļ™āđāļŠāļ”āļ‡āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ pipe āļ‹āļķāđˆāļ‡āļˆāļ°āļŠāļĢāđ‰āļēāļ‡āļŠāđˆāļ­āļ‡āļ—āļēāļ‡āļŠāļ·āđˆāļ­āļŠāļēāļĢāļŠāļ™āļīāļ”āļ—āļēāļ‡āđ€āļ”āļĩāļĒāļ§āļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđ‚āļ”āļĒāļˆāļ°āļ„āļ·āļ™āļ„āđˆāļē file descriptor āļ—āļąāđ‰āļ‡āļŠāļ­āļ‡āļāļąāđˆāļ‡ āļœāđˆāļēāļ™āļ•āļąāļ§āđāļ›āļĢ pfd[] āļāļĨāđˆāļēāļ§āļ„āļ·āļ­ āļ›āļĨāļēāļĒāļŦāļ™āļķāđˆāļ‡āļŠāļģāļŦāļĢāļąāļšāļ­āđˆāļēāļ™ (pfd[0]) āđāļĨāļ°āļ­āļĩāļāļ›āļĨāļēāļĒāļŦāļ™āļķāđˆāļ‡āļŠāļģāļŦāļĢāļąāļšāđ€āļ‚āļĩāļĒāļ™ (pfd[1]) āļ„āđˆāļēāļ‚āļ­āļ‡ file descriptor āđ€āļ›āđ‡āļ™āļŠāļ™āļīāļ”āļˆāļģāļ™āļ§āļ™āđ€āļ•āđ‡āļĄ (int) āļ—āļĩāđˆāļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāļĨāļĩāļ™āļļāļāļ‹āđŒāļˆāļ°āđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡āļ–āļķāļ‡āđāļŸāđ‰āļĄāļ—āļĩāđˆāļĄāļĩāļāļēāļĢāđ€āļ›āļīāļ”āđƒāļŠāđ‰āļ‡āļēāļ™āļ”āļąāļ‡āļ™āļąāđ‰āļ™āđ€āļĄāļ·āđˆāļ­āđ€āļĢāļĩāļĒāļāđƒāļŠāđ‰āļ‡āļēāļ™āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ pipe āļˆāļ°āļ•āđ‰āļ­āļ‡āļŠāđˆāļ‡āļ­āļēāđ€āļĢāļĒāđŒāļ‚āļ­āļ‡āļˆāļģāļ™āļ§āļ™āđ€āļ•āđ‡āļĄāļ—āļĩāđˆāļĄāļĩāļŠāļĄāļēāļŠāļīāļ 2 āļ•āļąāļ§āđƒāļŦāđ‰āđāļāđˆ āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ pipe()

 int  pfd[2];       // file descriptor āļ‚āļ­āļ‡ pipe

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ—āļĩāđˆ 1

āđāļŠāļ”āļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡ pipe āđ‚āļ”āļĒāđƒāļŠāđ‰āļ„āļģāļŠāļąāđˆāļ‡ pipe() āđ€āļžāļ·āđˆāļ­āđ€āļ›āđ‡āļ™āļ—āđˆāļ­āđ€āļŠāļ·āđˆāļ­āļĄāļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĄāđˆāđāļĨāļ°āđ‚āļ›āļĢāđ€āļ‹āļŠāļĨāļđāļ āđ‚āļ”āļĒāļ—āļĩāđˆāđ‚āļ›āļĢāđ€āļ‹āļŠāļĨāļđāļāļˆāļ°āļ—āļģāļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāđ„āļ›āļĒāļąāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĄāđˆ āđ‚āļ”āļĒāļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āļĨāļ‡ pfd[1] āļ”āļąāļ‡āļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡

// pipe1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
	pid_t pid;
	int pfd[2];
	int ret;
	char buf[20];

	ret = pipe(pfd);
	if (ret == -1) {
		perror("pipe");
		exit(1);
	}
	pid = fork();
	if (pid == 0) {
		/* Child Process */
		printf("Child Process\n");
		write(pfd[1], "Hello there!", 12);
	} else {
		/* Parent Process */
		printf("Parent Process\n");
		read(pfd[0], buf, 15);
		printf("buf : %s\n", buf);
	}
	return 0;
}

āļ„āļ­āļĄāđ„āļžāļĨāđŒāđ‚āļ›āļĢāđāļāļĢāļĄ pipe.c āđāļĨāļ°āļ—āļ”āļŠāļ­āļšāļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļ—āļąāđ‰āļ‡āļŠāļ­āļ‡ (āđ‚āļ›āļĢāđ€āļ‹āļŠ parent āđāļĨāļ°āđ‚āļ›āļĢāđ€āļ‹āļŠ child)

$ gcc -o pipe1 pipe1.c 
$ ./pipe1 
Parent Process
Child Process
buf : Hello there!

āļŸāļąāļ‡āļāđŒāļŠāļąāļ™āđāļĨāļ°āļ•āļąāļ§āđāļ›āļĢāļ—āļĩāđˆāđ€āļāļĩāđˆāļĒāļ§āļ‚āđ‰āļ­āļ‡

āļ™āļ­āļāļˆāļēāļāļ™āļąāđ‰āļ™āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡ pipe āļĒāļąāļ‡āļŠāļēāļĄāļēāļĢāļ–āļŠāļĢāđ‰āļēāļ‡āđ„āļŸāļĨāđŒ pipe āļŠāļģāļŦāļĢāļąāļšāļŠāļļāļ”āļ„āļģāļŠāļąāđˆāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļ—āļĩāđˆāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ‚āļ”āļĒāđƒāļŠāđ‰āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ popen() āļ‹āļķāđˆāļ‡āļ āļēāļĒāđƒāļ™āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ popen() āļˆāļ°āļĄāļĩāļāļēāļĢāđ€āļĢāļĩāļĒāļāļŸāļąāļ‡āļāđŒāļŠāļąāļ™ pipe() āļ­āļĒāļđāđˆāļ āļēāļĒāđƒāļ™ āđāļĨāļ°āļˆāļ°āļ•āđ‰āļ­āļ‡āļĢāļ°āļšāļļāļ”āđ‰āļ§āļĒāļ§āđˆāļēāļˆāļ°āđƒāļŦāđ‰āļŠāļļāļ”āļ„āļģāļŠāļąāđˆāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļ™āļĩāđ‰āđ€āļ›āđ‡āļ™āļ•āļąāļ§āļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨ (write) āļŦāļĢāļ·āļ­āļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨ (read) āđƒāļŦāđ‰āļŠāļąāļ”āđ€āļˆāļ™ āđ‚āļ”āļĒāļŠāļļāļ”āļ„āļģāļŠāļąāđˆāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļˆāļ°āļ–āļđāļāļ™āļģāđ„āļ›āļĢāļąāļ™āļ­āļĒāļđāđˆāļšāļ™ shell āļ‚āļ­āļ‡āļĢāļ°āļšāļšāļ­āļĩāļāļ—āļ­āļ”āļŦāļ™āļķāđˆāļ‡ āļ”āļąāļ‡āļ™āļąāđ‰āļ™āđ€āļĄāļ·āđˆāļ­āļĄāļĩāļāļēāļĢāļŠāļĢāđ‰āļēāļ‡ pipe āļ”āđ‰āļ§āļĒāļ„āļģāļŠāļąāđˆāļ‡ popen āļāđ‡āļˆāļ°āļ•āđ‰āļ­āļ‡āđƒāļŠāđ‰āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ pclose() āđ€āļžāļ·āđˆāļ­āļ—āļģāļĨāļēāļĒ pipe āļ—āļĩāđˆāļŠāļĢāđ‰āļēāļ‡āļ‚āļķāđ‰āļ™āļĄāļē

  • āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ FILE *popen(char *command, char *type)

  • āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ int pclose(FILE *stream)

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ—āļĩāđˆ 2

āđāļŠāļ”āļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡ pipe āļŠāļģāļŦāļĢāļąāļšāļŠāļļāļ”āļ„āļģāļŠāļąāđˆāļ‡ sort āļ—āļĩāđˆāļˆāļ°āļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨ (write) āļˆāļēāļāļ„āđˆāļēāđƒāļŠāđ‰āļ•āļąāļ§āđāļ›āļĢāļ­āļēāđ€āļĢāļĒāđŒ āđ€āļžāļ·āđˆāļ­āļ—āļģāļāļēāļĢāļˆāļąāļ”āđ€āļĢāļĩāļĒāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨ

/*****************************************************************************
 Excerpt from "Linux Programmer's Guide - Chapter 6"
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 Filename: pipe2.c
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#define MAXSTRS 5

int main(void) {
	int cntr;
	FILE *pipe_fp;
	char *strings[MAXSTRS] = { "echo", "bravo", "alpha", "charlie", "delta" };

	/* Create one way pipe line with call to popen() */
	if ((pipe_fp = popen("sort", "w")) == NULL) {
		perror("popen");
		exit(1);
	}

	/* Processing loop */
	for (cntr = 0; cntr < MAXSTRS; cntr++) {
		fputs(strings[cntr], pipe_fp);
		fputc('\n', pipe_fp);
	}

	/* Close the pipe */
	pclose(pipe_fp);

	return (0);
}
$ gcc -o pipe2 pipe2.c -Wall
$ ./pipe2 
alpha
bravo
charlie
delta
echo

āđ€āļ™āļ·āđˆāļ­āļ‡āļˆāļēāļāļŸāļąāļ‡āļāđŒāļŠāļąāļ™ popen() āļˆāļ°āđƒāļŠāđ‰ shell āđƒāļ™āļāļēāļĢāļĢāļąāļ™āļ„āļģāļŠāļąāđˆāļ‡āļ—āļĩāđˆāļĢāļ°āļšāļļāđ„āļ›āđƒāļŦāđ‰ āļ”āļąāļ‡āļ™āļąāđ‰āļ™āļāļēāļĢāđƒāļŠāđ‰āļŠāļļāļ”āļ„āļģāļŠāļąāđˆāļ‡āļāđ‡āļŠāļēāļĄāļēāļĢāļ–āļ—āļģāđ„āļ”āđ‰āđ€āļŦāļĄāļ·āļ­āļ™āļāļąāļšāļāļēāļĢāļžāļīāļĄāļžāđŒāļ„āļģāļŠāļąāđˆāļ‡āļšāļ™ shell (command line) āđ„āļ”āđ‰āđ€āļŠāđˆāļ™āļāļąāļ™ āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āđ€āļŠāđˆāļ™

popen("ls /usr/include", "r");
popen("sort > /tmp/foo", "w");
popen("sort | uniq | more", "w");

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ—āļĩāđˆ 3

āđāļŠāļ”āļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāđƒāļŠāđ‰ popen() āđ€āļžāļ·āđˆāļ­āļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāļ—āļĩāđˆāļ­āđˆāļēāļ™āđ„āļ”āđ‰āļˆāļēāļāļ„āļģāļŠāļąāđˆāļ‡ ls āđāļĨāđ‰āļ§āļŠāđˆāļ‡āļ•āđˆāļ­āđ„āļ›āđ€āļ›āđ‡āļ™āļ­āļīāļ™āļžāļļāļ—āđƒāļŦāđ‰āļāļąāļšāļ­āļĩāļāļ„āļģāļŠāļąāđˆāļ‡ (sort) āđ€āļžāļ·āđˆāļ­āļ—āļģāļāļēāļĢāļˆāļąāļ”āđ€āļĢāļĩāļĒāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļ•āđˆāļ­āđ„āļ›

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	FILE *pipein_fp, *pipeout_fp;
	char readbuf[80];

	/* Create one way pipe line with call to popen() */
	if ((pipein_fp = popen("ls /", "r")) == NULL) {
		perror("popen");
		exit(1);
	}

	/* Create one way pipe line with call to popen() */
	if ((pipeout_fp = popen("sort", "w")) == NULL) {
		perror("popen");
		exit(1);
	}

	/* Processing loop */
	while (fgets(readbuf, 80, pipein_fp))
		fputs(readbuf, pipeout_fp);

	/* Close the pipes */
	pclose(pipein_fp);
	pclose(pipeout_fp);

	return (0);
}
$ ls /
NAS   courseweb_backup  etc  initrd.img.old  lib64  mnt   root  selinux  tftpboot  var  bin   dev  home   lib    lost+found  opt   run   srv   tmp   vmlinuz  boot  ee_backup   initrd.img  lib32   media   proc  sbin  sys   usr   vmlinuz.old

$ gcc -o pipe3 pipe3.c -Wall
$ ./pipe3 
NAS
bin
boot
courseweb_backup
dev
ee_backup
etc
home
initrd.img
initrd.img.old
lib
lib32
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
selinux
srv
sys
tftpboot
tmp
usr
var
vmlinuz
vmlinuz.old

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ—āļĩāđˆ 4

āđāļŠāļ”āļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļĢāļąāļšāļ­āļēāļĢāđŒāļāļīāļ§āđ€āļĄāļ™āļ•āđŒāļˆāļēāļāļœāļđāđ‰āđƒāļŠāđ‰āļœāđˆāļēāļ™āļāļēāļĢāđ€āļĢāļĩāļĒāļāđ‚āļ›āļĢāđāļāļĢāļĄāļˆāļēāļ command line āđ‚āļ”āļĒāļ­āļēāļĢāđŒāļāļīāļ§āđ€āļĄāļ™āļ•āđŒāļ•āļąāļ§āđāļĢāļāļˆāļ°āđ€āļ›āđ‡āļ™āļ„āļģāļŠāļąāđˆāļ‡āļ—āļĩāđˆāļ•āđ‰āļ­āļ‡āļāļēāļĢāđƒāļŦāđ‰āļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨ (write) āđāļĨāļ°āļ­āļēāļĢāđŒāļāļīāļ§āđ€āļĄāļ™āļ•āđŒāļ—āļĩāđˆāļŠāļ­āļ‡āļˆāļ°āđ€āļ›āđ‡āļ™āđ„āļŸāļĨāđŒāļ‚āđ‰āļ­āļĄāļđāļĨāļ—āļĩāđˆāļ•āđ‰āļ­āļ‡āļāļēāļĢāļˆāļ°āđƒāļŦāđ‰āđ€āļ›āļīāļ”āļ­āđˆāļēāļ™ (rt)

/*****************************************************************************
 Excerpt from "Linux Programmer's Guide - Chapter 6"
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 Filename: popen.c
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
	FILE *pipe_fp, *infile;
	char readbuf[80];

	if (argc != 3) {
		fprintf(stderr, "USAGE: popen3 [command] [filename]\n");
		exit(1);
	}

	/* Open up input file */
	if ((infile = fopen(argv[2], "rt")) == NULL) {
		perror("fopen");
		exit(1);
	}

	/* Create one way pipe line with call to popen() */
	if ((pipe_fp = popen(argv[1], "w")) == NULL) {
		perror("popen");
		exit(1);
	}

	/* Processing loop */
	do {
		fgets(readbuf, 80, infile);
		if (feof(infile))
			break;

		fputs(readbuf, pipe_fp);
	} while (!feof(infile));

	fclose(infile);
	pclose(pipe_fp);

	return (0);
}
$ gcc -o popen popen.c -Wall

$ ./popen sort popen.c
$ ./popen cat popen.c
$ ./popen more popen.c
$ ./popen cat popen.c | grep main

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ—āļĩāđˆ 5

āđāļŠāļ”āļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļ›āļĢāļ°āļĒāļļāļāļ•āđŒāļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļ„āļ§āļēāļĄ (message) āļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļ•āđˆāļēāļ‡āđ† āđ‚āļ”āļĒāđ‚āļ›āļĢāđ€āļ‹āļŠāļ—āļĩāđˆ i āļ•āđ‰āļ­āļ‡āļāļēāļĢāļŠāđˆāļ‡āđ„āļ›āļĒāļąāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļ—āļĩāđˆ j āđ‚āļ”āļĒāļ–āđ‰āļēāđ‚āļ›āļĢāđ€āļ‹āļŠāđƒāļ”āļĢāļąāļšāļ‚āđ‰āļ­āļ„āļ§āļēāļĄāđ€āļžāļĩāļĒāļ‡āļŠāļ­āļ‡āļ„āļĢāļąāđ‰āļ‡āđāļĨāđ‰āļ§āļāđ‡āļˆāļ°āļŠāļīāđ‰āļ™āļŠāļļāļ”āļāļēāļĢāļ—āļģāļ‡āļēāļ™

/*
 *  pipe4.c
 *  
 *  A set of processes randomly messaging each the other, with pipes.
 *
 *
 *  Created by Mij <mij@bitchx.it> on 05/01/05.
 *  Original source file available on http://mij.oltrelinux.com/devel/unixprg/
 *
 */

#include <stdio.h>
/* for read() and write() */
#include <sys/types.h>
#include <sys/uio.h>
/* for strlen and others */
#include <string.h>
/* for pipe() */
#include <unistd.h>
/* for [s]random() */
#include <stdlib.h>
/* for time() [seeding srandom()] */
#include <time.h>
/* for signals */
#include <signal.h>

#define PROCS_NUM               15          /* 1 < number of processes involved <= 255 */
#define MAX_PAYLOAD_LENGTH      50          /* message length */
#define DEAD_PROC               -1          /* a value to mark a dead process' file descriptors with */

/*      ***                     DATA TYPES                  ***     */
/* a process address */
typedef char proc_addr;

/* a message */
struct message_s {
	proc_addr src_id;
	short int length;
	char *payload;
};
/*      ***                     FUNCTION PROTOTYPES         ***     */
/* send message to process with id dest */
int send_proc_message(proc_addr dest, char *message);
/* receive a message in the process' queue of received ones */
int receive_proc_message(struct message_s *msg);
/* mark process file descriptors closed */
void mark_proc_closed(proc_addr process);

/*              ***             GLOBAL VARS                 ***     */
/* they are OK to be global here. */
proc_addr my_address; /* stores the id of the process */
int proc_pipes[PROCS_NUM][2]; /* stores the pipes of every process involved */

int main(int argc, char *argv[]) {
	pid_t child_pid;
	pid_t my_children[PROCS_NUM]; /* PIDs of the children */
	int i, ret;
	char msg_text[MAX_PAYLOAD_LENGTH]; /* payload of the message to send */
	proc_addr msg_recipient;
	struct message_s msg;

	/* create a pipe for me (the parent) */
	pipe(proc_pipes[0]);

	/* initializing proc_pipes struct */
	for (i = 1; i < PROCS_NUM; i++) {
		/* creating one pipe for every (future) process */
		ret = pipe(proc_pipes[i]);
		if (ret) {
			perror("Error creating pipe");
			abort();
		}
	}

	/* fork [1..NUM_PROCS] children. 0 is me. */
	for (i = 1; i < PROCS_NUM; i++) {
		/* setting the child address */
		my_address = my_address + 1;

		child_pid = fork();
		if (!child_pid) {
			/* child */
			sleep(1);

			/* closing other process' pipes read ends */
			for (i = 0; i < PROCS_NUM; i++) {
				if (i != my_address)
					close(proc_pipes[i][0]);
			}

			/* init random num generator */
			srandom(time(NULL));

			/* my_address is now my address, and will hereby become a "constant" */
			/* producing some message for the other processes */
			while (random() % (2 * PROCS_NUM)) {
				/* interleaving... */
				sleep((unsigned int) (random() % 2));

				/* choosing a random recipient (including me) */
				msg_recipient = (proc_addr) (random() % PROCS_NUM);

				/* preparing and sending the message */
				sprintf(msg_text, "hello from process %u.", (int) my_address);
				ret = send_proc_message(msg_recipient, msg_text);
				if (ret > 0) {
					/* message has been correctly sent */
					printf("    --> %d: sent message to %u\n", my_address,
							msg_recipient);
				} else {
					/* the child we tried to message does no longer exist */
					mark_proc_closed(msg_recipient);
					printf("    --> %d: recipient %u is no longer available\n",
							my_address, msg_recipient);
				}
			}

			/* now, reading the first 2 messages we've been sent */
			for (i = 0; i < 2; i++) {
				ret = receive_proc_message(&msg);
				if (ret < 0)
					break;
				printf(
						"<--     Process %d, received message from %u: \"%s\".\n",
						my_address, msg.src_id, msg.payload);
			};

			/* i'm exiting. making my pipe widowed */
			close(proc_pipes[my_address][0]);

			printf("# %d: i am exiting.\n", my_address);
			exit(0);
		}

		/* saving the child pid (for future killing) */
		my_children[my_address] = child_pid;

		/* parent. I don't need the read descriptor of the pipe */
		close(proc_pipes[my_address][0]);

		/* this is for making srandom() consistent */
		sleep(1);
	}

	/* expecting the user request to terminate... */
	printf("Please press ENTER when you like me to flush the children...\n");
	getchar();

	printf("Ok, terminating dandling processes...\n");
	/* stopping freezed children */
	for (i = 1; i < PROCS_NUM; i++) {
		kill(my_children[i], SIGTERM);
	}
	printf("Done. Exiting.\n");

	return 0;
}

int send_proc_message(proc_addr dest, char *message) {
	int ret;
	char *msg = (char*) malloc(sizeof(message) + 2);

	/* the write should be atomic. Doing our best */
	msg[0] = (char) dest;
	memcpy((void*) &(msg[1]), (void*) message, strlen(message) + 1);

	/* send message, including the "header" the trailing '\0' */
	ret = write(proc_pipes[dest][1], msg, strlen(msg) + 2);
	free(msg);

	return ret;
}

int receive_proc_message(struct message_s *msg) {
	char c = 'x';
	char temp_string[MAX_PAYLOAD_LENGTH];
	int ret, i = 0;

	/* first, getting the message sender */
	ret = read(proc_pipes[my_address][0], &c, 1);
	if (ret == 0) {
		return 0;
	}
	msg->src_id = (proc_addr) c;

	do {
		ret = read(proc_pipes[my_address][0], &c, 1);
		temp_string[i++] = c;
	} while ((ret > 0) && (c != '\0') && (i < MAX_PAYLOAD_LENGTH));

	if (c == '\0') {
		/* msg correctly received. Preparing message packet */

		msg->payload = (char*) malloc(strlen(temp_string) + 1);
		strncpy(msg->payload, temp_string, strlen(temp_string) + 1);

		return 0;
	}

	return -1;
}

void mark_proc_closed(proc_addr process) {
	proc_pipes[process][0] = DEAD_PROC;
	proc_pipes[process][1] = DEAD_PROC;
}
$ gcc --ansi --pedantic -o pipe4 pipe4.c

$ ./pipe4 
    --> 1: sent message to 3
    --> 1: sent message to 8
    --> 2: sent message to 0
    --> 1: sent message to 5
    --> 2: sent message to 4
    --> 1: sent message to 10
    --> 2: sent message to 9
    --> 2: sent message to 0
    --> 3: sent message to 11
    --> 2: sent message to 6
    --> 1: sent message to 2
    --> 2: sent message to 12
    --> 3: sent message to 14
<--     Process 4, received message from 4: "hello from process 2.".
    --> 3: sent message to 13
    --> 1: sent message to 3
    --> 1: sent message to 9
    --> 1: sent message to 12
    --> 1: sent message to 3
    --> 2: sent message to 6
    --> 3: sent message to 7
<--     Process 5, received message from 5: "hello from process 1.".
    --> 2: sent message to 12
    --> 2: sent message to 5
    --> 1: sent message to 7
    --> 1: sent message to 4
<--     Process 4, received message from 0: "hello from process 1.".
# 4: i am exiting.
    --> 1: sent message to 12
    --> 1: sent message to 2
<--     Process 5, received message from 0: "hello from process 2.".
# 5: i am exiting.
    --> 2: sent message to 14
    --> 2: sent message to 1
    --> 2: sent message to 10
    --> 1: sent message to 8
    --> 1: sent message to 0
    --> 1: sent message to 0
    --> 6: sent message to 13
    --> 6: sent message to 8
    --> 6: sent message to 14
    --> 8: sent message to 14
    --> 6: sent message to 14
    --> 8: sent message to 8
    --> 8: sent message to 11
    --> 9: sent message to 14
    --> 10: sent message to 0
^C
ðŸ–Ĩïļ
āļ›āļĢāļ°āđ€āļ āļ—āļāļēāļĢāļāļĨāđ„āļāļŠāļ·āđˆāļ­āļŠāļēāļĢ IPC
āļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļˆāļēāļāđ‚āļ›āļĢāđ€āļ‹āļŠāđ„āļ›āļĒāļąāļ‡āđ„āļŸāļĨāđŒ fd
āđāļŠāļ”āļ‡āļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāđāļšāļšāļ—āļīāļĻāļ—āļēāļ‡āđ€āļ”āļĩāļĒāļ§āļ”āđ‰āļ§āļĒ Pipe
āļŠāļ·āđˆāļ­āļŠāļēāļĢāļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĄāđˆāđāļĨāļ°āđ‚āļ›āļĢāđ€āļ‹āļŠāļĨāļđāļ
āļāļēāļĢāļŠāđˆāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāđƒāļ™āļĢāļđāļ›āđāļšāļšāļ—āļēāļ‡āđ€āļ”āļĩāļĒāļ§āļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠ