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

FIFO Programming

Named Pipes

ไปป์ระบุชื่อ (Named Pipes - FIFOs)

FIFO หรือย่อมาจาก "First In, First Out" (อ่านว่า Fy-Foh) ในอีกชื่อหนึ่งก็คือ named pipe ซึ่งหมายถึง pipe ที่มีชื่อไฟล์ของตัวเอง โดยตัว named pipe นี้จะเหมือน pipe ทั่วไปยกเว้นมันสามารถถูกตั้งชื่อได้ ซึ่งจะมีข้อดีคือสามารถระบุ pipe ได้และโปรเซสอื่นๆก็สามารถเรียกใช้ได้ง่ายขึ้นซึ่งจะคล้ายกับการเปิดไฟล์ทั่วไปด้วยคำสั่ง open() ดังนั้นก่อนที่จะใช้ named pipe (FIFO) จะต้องใช้คำสั่ง mknod() เพื่อสร้างไฟล์ชนิดนี้เป็นอย่างแรก ดังตัวอย่างข้างล่างนี้

mknod("myfifo", S_IFIFO | 0644 , 0);

จากตัวอย่างข้างล่างนี้ตัวไฟล์ FIFO ถูกตั้งชื่อว่า myfifo นอกจากนั้นจะมีการกำหนดค่าอาร์กิวเม้นต์เพื่อระบุโหมดของไฟล์เช่น S_IFIFO | 0644 ซึ่งเป็นการระบุว่าเป็นไฟล์ FIFO และมีสิทธิ์การเข้าถึง (access permission) เป็น 644 (เลขฐานแปด) หรือเทียบเท่ากับ rw-r--r-- ซึ่งรายละเอียดของโหมดนั้นจะถูกเก็บอยู่ในไฟล์ sys/stat.h ในอีกทางหนึ่งสามารถสร้างไฟล์ FIFO ได้ด้วยคำสั่ง mknod หรือ mkfifo ดังข้างล่างนี้

$ mknod myfifo p
$ chmod 0644 myfifo 
$ ls -al myfifo
prw-r--r-- 1 wiroon wiroon 0 Sep 18 14:06 myfifo

โดยที่ p เป็นตัวระบุให้เป็นไฟล์ชนิด pipe หรืออาจจะใช้คำสั่ง mkfifo ดังตัวอย่างข้างล่างนี้

$ mkfifo -m 0644 myfifo
$ ls -al myfifo
prw-r--r-- 1 wiroon wiroon 0 Sep 18 14:06 myfifo

ฟังก์ชันและตัวแปรที่เกี่ยวข้อง

  • ไลบรารีที่เกี่ยวข้องคือ sys/types.h และ sys/stat.h

  • ฟังก์ชัน int mkfifo(char *path, mode_t mode)

    • โดยที่ตัวแปรพอยเตอร์ path จะหมายถึงไฟล์ FIFO ที่ต้องการจะสร้างขึ้น และตัวแปร mode จะหมายถึงการระบุสิทธิ์การเข้าถึง (ดูเพิ่มได้จากคำสั่ง unmask และ chmod ซึ่งจะส่งค่ากลับที่ไม่เท่ากับศูนย์ในกรณีที่ไม่มีความผิดพลาดขณะสร้างไฟล์

ตัวอย่างที่ 1

แสดงตัวอย่างโปรแกรมสำหรับทดสอบการส่งข้อมูลผ่านไฟล์ FIFO ดังแสดงข้างล่างนี้

/*
 *  nampipes.c
 *  simply opens a pre-created named pipe (a "fifo") and reads
 *  stuff from it as soon as there's something available.
 *  Created by Mij <mij@bitchx.it> on 02/02/05.
 *  Original source file available on http://mij.oltrelinux.com/devel/unixprg/
 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

#define MAX_TEXT_LENGTH 20

int main(int argc, char *argv[]) {
	int pipe, count = 0;
	char ch;

	pipe = open("/tmp/myfifo", O_RDONLY);
	if (pipe < 0) {
		perror("Opening pipe");
		exit(1);
	}

	/* preparing to read from the pipe... */
	printf("Waiting data from the pipe... \n");

	/* reading one char a time from the pipe */
	while (1) {
		if (read(pipe, &ch, 1) < 0) {
			perror("Read the pipe");
			exit(2);
		}

		if (count < MAX_TEXT_LENGTH) {
			printf(“%c”, ch);
			count++;
		} else
			break;
	}

	/* leaving the pipe */
	printf("\n");
	close(pipe);

	return 0;
}

โดยมีขั้นตอนการสร้างไฟล์ FIFO และส่งข้อมูลผ่านไฟล์ FIFO ไปยังโปรแกรม nampipes โดยมีขั้นตอนดังนี้

$ mkfifo -m 0644 /tmp/myfifo
$ ls -al myfifo
prw-r--r-- 1 wiroon wiroon 0 Sep 18 14:27 /tmp/myfifo
$ gcc -o nampipes nampipes.c -Wall
$ ./nampipes
$ echo “This message is for you” > /tmp/myfifo

เมื่อกลับมาดูหน้าต่าง Terminal 1 จะแสดงผลดังแสดงข้างล่างนี<หน้าต่างที่ 1>

$ ./nampipes
Waiting data from the pipe... 
This message is for you

ตัวอย่างที่ 2

แสดงตัวอย่างการสร้าง name pipe ด้วยการเรียกฟังก์ชัน mkfifo() ตาม path ที่กำหนดคือ /tmp/AtoB โดยโปรเซสแม่ จะทำการสร้างโปรเซสลูกขึ้นมา เพื่อให้โปรเซสลูกเป็นผู้เขียนข้อมูล (writer) เท่านั้น (O_WRONLY) แล้วโปรเซสแม่จะเป็นผู้อ่านข้อมูล (reader) เท่านั้น (O_RDONLY)ซึ่งโปรเซสลูกจะทำการคอยด้วยฟังก์ชัน wait(NULL); จนกว่าโปรเซสแม่ได้รับข้อมูลครบจึงจะลบ named pip ออกด้วยฟังก์ชัน unlink();

// fifo_parent_child.c 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>

#define FIFO_PATH "/tmp/AtoB" // Path to the named pipe

void writer_process() {
	int fd;
	char message[] = "Hello, named pipe!";

	// Open the named pipe for writing
	fd = open(FIFO_PATH, O_WRONLY);
	if (fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}

	// Write the message to the named pipe
	write(fd, message, strlen(message) + 1);
	printf("Message sent: %s", message);

	// Close the named pipe
	close(fd);
}

void reader_process() {
	int fd;
	char buffer[100];

	// Open the named pipe for reading
	fd = open(FIFO_PATH, O_RDONLY);
	if (fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}

	// Read the message from the named pipe
	read(fd, buffer, sizeof(buffer));
	printf("Message received: %s", buffer);

	// Close the named pipe
	close(fd);
}

int main() {
	pid_t pid;

	// Create the named pipe
	mkfifo(FIFO_PATH, 0666);

	// Fork a child process
	pid = fork();

	if (pid == -1) {
		perror("fork");
		exit(EXIT_FAILURE);
	} else if (pid == 0) {
		// Child process (writer)
		writer_process();
	} else {
		// Parent process (reader)
		reader_process();

		// Wait for the child process to finish
		wait(NULL);

		// Remove the named pipe
		unlink(FIFO_PATH);
	}
	return 0;
}
$ gcc -o fifo_parent_child fifo_parent_child.c 
$ ./fifo_parent_child
Message sent: Hello, named pipe!
Message received: Hello, named pipe!

ตัวอย่างที่ 3

แสดงตัวอย่างการดำเนินการทำงานในลักษณะของ FIFO โดยการสร้างไฟล์ pipe ในลักษณะไฟล์สตรีม (file stream) โดยใช้ฟังก์ชัน fopen() และ fclose() ซึ่งแตกต่างจากคำสั่ง open() เนื่องจากคำสั่ง open() นี้จะต้องมีการสร้างไฟล์ pipe ที่มีอยู่จริงๆบนระบบไฟล์ภายในฮาร์ดดิสของระบบ แต่ในขณะที่คำสั่ง fopen() จะเป็นการสร้างไฟล์ pipe อยู่ภายในตัวเคอร์เนลของระบบปฏิบัติการลีนุกซ์ ดังตัวอย่างข้างล่างนี้

สร้างโปรแกรมที่ทำตัวเองเป็น server โดยการเปิดไฟล์ pipe (MYFIFO) เพื่อรอรับข้อมูล โดยการเปิดอ่านไฟล์ pipe ที่ถูกสร้างขึ้นในเคอร์เนล ดังตัวอย่างข้างล่างนี้

หน้าต่างTerminal ที่ 1

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

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE       "MYFIFO"

int main(void) {
	FILE *fp;
	char readbuf[80];

	/* Create the FIFO if it does not exist */
	umask(0);
	mknod(FIFO_FILE, S_IFIFO | 0666, 0);

	while (1) {
		fp = fopen(FIFO_FILE, "r");
		fgets(readbuf, 80, fp);
		printf("Received string: %s\n", readbuf);
		fclose(fp);
	}

	return (0);
}

ทำการคอมไพล์โปรแกรม และรันโปรแกรม ซึ่งการทำงานของ FIFO นั้นจะทำงานรออยู่ตลอดเวลา

$ gcc -o fifoserver1 fifoserver1.c 
$ ./fifoserver1

สร้างโปรแกรมที่ทำหน้าที่ส่งข้อมูลไปยังโปรแกรม fifoserver1 โดยการส่งผ่าน (write) ไฟล์ pipe ชื่อ MYFIFO ที่ถูกเปิดรออยู่แล้วในเคอร์เนล ดังตัวอย่างข้างล่างนี้

หน้าต่างTerminal ที่ 2

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

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

#define FIFO_FILE       "MYFIFO"

int main(int argc, char *argv[]) {
	FILE *fp;

	if (argc != 2) {
		printf("USAGE: fifoclient [string]\n");
		exit(1);
	}

	if ((fp = fopen(FIFO_FILE, "w")) == NULL) {
		perror("fopen");
		exit(1);
	}

	fputs(argv[1], fp);

	fclose(fp);
	return (0);
}

ทำการคอมไพล์โปรแกรมและรันโปรแกรมเพื่อทำการส่งข้อความ "Hello from FIFO Client" ไปยังโปรแกรม fifoserver1 โดยการทำงานของ FIFO นั้นจะทำงานรออยู่ตลอดเวลา

$ gcc -o fifoclient1 fifoclient1.c 
$ ./fifoclient1 
USAGE: fifoclient1 [string]

$ ./fifoclient1 "Hello from FIFO Client"

ดังนั้นเมื่อมองกลับมาที่หน้าต่าง Terminal ที่ 1 ก็จะเห็นข้อมูลที่ตัวโปรแกรม fifoserver1 ได้รับมาจากไฟล์ pipe (MYFIFO) ดังแสดงข้างล่างนี้

$ gcc -o fifoserver1 fifoserver1.c 
$ ./fifoserver1
Received string: Hello from FIFO Client

ตัวอย่างที่ 4

แสดงตัวอย่างการประยุกต์ให้รองรับ multi-client แล้วตัว server จะส่งข้อความกลับไปยัง client ตัวนั้นๆผ่านไฟล์ MYFIFO2 ดังไฟล์ server แสดงข้างล่างนี้

/**********  Multi-Client Fifo Server!  **********/
// fifoserver2.c
#include <sys/types.h>  
#include <sys/stat.h>
#include <stdio.h>
#include <signal.h>  
#include <sys/fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h> 
#include <string.h>

#define FIFO_FILE       "MYFIFO2"

int main(void) {
	int readfifo, writefifo, dummyfd;
        char buf[80], outbuf[80], filename[80], *fifoid, *message;
        void handler(int signum);

	signal(SIGPIPE, handler); /******  In case client disappears ******/
	signal(SIGTTOU, handler); /******  To keep server alive on printf *****/

	sprintf(filename, FIFO_FILE);
	if ((mkfifo(filename, S_IRUSR | S_IWUSR) < 0) && (errno != EEXIST)) {
		printf("Cannot make server fifo!");
		exit(1);
	}

	if ((readfifo = open(filename, O_RDONLY)) < 0) {
		printf("Cannot open server fifo!");
		exit(2);
	}
	if ((dummyfd = open(filename, O_WRONLY)) < 0) {
		printf("Cannot open server fifo dummy!");
		exit(3);
	}

	while (read(readfifo, buf, 80) == 80) {
		fifoid = strtok(buf, "\040");
		message = strtok(NULL, "\n");
		printf("Got %s from client\n", buf);
		if ((writefifo = open(fifoid, O_WRONLY)) < 0) {
			printf("Cannot open fifo to client!");
			continue;
		}
		memset(outbuf, 0, 80);
		sprintf(outbuf, "%s\n", message);
		write(writefifo, outbuf, 80);
		close(writefifo);
	}
	return 0;
}

void handler(int signum) {
	printf("Fifo has been closed in reader!");
	exit(0);
}

ไฟล์ fifo client ดังแสดงข้างล่าง

/**********  Fifo Client  ***********/
// fifoclient2.c
#include <sys/types.h>  
#include <sys/stat.h>
#include <stdio.h>
#include <signal.h>  
#include <sys/fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define FIFO_FILE       "MYFIFO2"

int main() {
        int clientfifo, serverfifo;
        char buf[80], filename[80], outbuf[256], inbuf[80], fifoname[80];

        sprintf(filename, FIFO_FILE);
        if ((serverfifo = open(filename, O_WRONLY)) < 0) {
                perror("Cannot open server's fifo!");
                exit(3);
        }

        sprintf(fifoname, "fifo.%d", getpid());
        if ((mkfifo(fifoname, S_IRUSR | S_IWUSR) < 0) && (errno != EEXIST)) {
                perror("mkfifo error!");
                exit(1);
        }

        memset(buf, 0, 80);
        printf("Enter string: ");
        while (fgets(buf, 80, stdin) && strcmp(buf, "quit\n") != 0) {
                /*************  Tell server who we are
                 *************/
                memset(outbuf, 0, 256);
                sprintf(outbuf, "fifo.%d %s", getpid(), buf);
                write(serverfifo, outbuf, 80);

                /*************  Open must be done here!  It blocks if done above loop
                 *************  because opens for read block until data arrives!!
                 *************/
                memset(inbuf, 0, 80);
                if ((clientfifo = open(fifoname, O_RDONLY, 0)) < 0) {
                        perror("Could not open client fifo!");
                        exit(2);
                }
                read(clientfifo, inbuf, 80);
                printf("From server: %s", inbuf);
                memset(buf, 0, 80);
                printf("Enter string: ");
                close(clientfifo);
        }
        unlink(fifoname); /******  Get rid of filesystem entry.  ******/
}
$ gcc -o fifoserver2 fifoserver2.c -Wall
$ ./fifoserver2 &
$ gcc -o fifoclient2 fifoclient2.c -Wall
$ ./fifoclient2                                                                                                                                  ─╯
Enter string: Hi, There ... I'm from Client 1
From server: Hi, There ... I'm from Client 1
$ ./fifoclient2                                                                                                                                  ─╯
Enter string: Hi, There ... I'm from Client 2
From server: Hi, There ... I'm from Client 2

เมื่อกลับมาดูหน้าต่าง Terminal 1 จะแสดงผลดังแสดงข้างล่างนี้<หน้าต่างที่ 1>

$ ./fifoserver2 &
Got fifo.26203 from client <-- Client ตัวที่ 1 จากหน้าต่าง Terminal 2
Got fifo.26203 from client <-- Client ตัวที่ 2 จากหน้าต่าง Terminal 3
PreviousPipe ProgrammingNextMsg. Queue Programming

Last updated 1 year ago

Was this helpful?

🖥️