This article is written on the assumption that you know the concept of the Pointer

1. Introduction

In Linux (or Unix) System, ‘fd’ is a concept that ‘process’ handles a file. As if, abstract value when the ‘process’ accesses to a specific file.


2. What is File Descriptor (fd)?

The ‘fd’ is usually used for matching the File’s specific Number. The specific Number is given by OS system, and the Number is 0 or positive integer(0~ max).

2-1. Concept

With the Diagram.

Untitled


3. Example

’fd’ is a integer (0~max) value. But we can check the value of it is negative integer when it doesn’t work(~-1). So we can use its value for ‘if’ grammer.

#include <unistd.h>
...
fd = std::system("ls /dev | grep EtherCAT* > /dev/null");
    if(fd){
        printf( "Opening EtherCAT master...");
        std::system("cd ~; sudo ethercatctl start");
        usleep(2e6);
        fd = std::system("ls /dev | grep EtherCAT* > /dev/null");
        if(fd){
            printf( "Error : EtherCAT device not found.");
            return -1;
            }else {
                return 0 ;
            }
    }
    return 0 ;