Linux C: An "interactive session" with separate named read and write channels?

I am trying to work with " Introduction to interprocess communication using named pipes - full duplex communication using named pipes ", link ; in particular fd_server.c(see below for reference)

Here is my info and compilation line:

: ~ $ cat / etc / issue
Ubuntu 10.04 LTS \ n \ l
: ~ $ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
: ~ $ gcc fd_server.c -o fd_server

fd_server.ccreates two named pipes: one for reading and one for writing. What can be done is: in one terminal, start the server and read (through cat) your write channel:

: ~ $ ./fd_server & 2> / dev / null 
[1] 11,354
: ~ $ cat / tmp / np2 

and in another, write (using the echo) to the server’s read channel:

: ~ $ echo "heeellloooo"> / tmp / np1

Returning to the first terminal, you can see:

: ~ $ cat / tmp / np2 
HEEELLLOOOO
0 [1] + Exit 13 ./fd_server 2> / dev / null

, "" ( "" ) ; , cat echo - . , screen /dev/ttyS0 38400, , , , /dev/ttyS0, . , , screen, , , , screen .

"" ( /)?

:

#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <fullduplex.h> /* For name of the named-pipe */
#define NP1     "/tmp/np1"
#define NP2     "/tmp/np2"
#define MAX_BUF_SIZE    255
#include <stdlib.h> //exit
#include <string.h> //strlen

int main(int argc, char *argv[])
{
    int rdfd, wrfd, ret_val, count, numread;
    char buf[MAX_BUF_SIZE];

    /* Create the first named - pipe */
    ret_val = mkfifo(NP1, 0666);

    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (1);
    }

    ret_val = mkfifo(NP2, 0666);

    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (1);
    }

    /* Open the first named pipe for reading */
    rdfd = open(NP1, O_RDONLY);

    /* Open the second named pipe for writing */
    wrfd = open(NP2, O_WRONLY);

    /* Read from the first pipe */
    numread = read(rdfd, buf, MAX_BUF_SIZE);

    buf[numread] = '0';

    fprintf(stderr, "Full Duplex Server : Read From the pipe : %sn", buf);

    /* Convert to the string to upper case */
    count = 0;
    while (count < numread) {
        buf[count] = toupper(buf[count]);
        count++;
    }

    /*
     * Write the converted string back to the second
     * pipe
     */
    write(wrfd, buf, strlen(buf));
}

Edit:

, - , , - , - script ( " , script ..." ) :

# stty raw # 
( ./fd_server 2>/dev/null; )&
bgPidS=$!
( cat < /tmp/np2 ; )&
bgPid=$!
# Read commands from user, send them to device
echo $(kill -0 $bgPidS 2>/dev/null ; echo $?)
while [ "$(kill -0 $bgPidS 2>/dev/null ; echo $?)" -eq "0" ] && read cmd; do
   # redirect debug msgs to stderr, as here we're redirected to /tmp/np1
   echo "$? - $bgPidS - $bgPid" >&2
   echo "$cmd"
   echo -e "\nproc: $(kill -0 $bgPidS 2>/dev/null ; echo $?)" >&2
done >/tmp/np1
echo OUT
# Terminate background read process - if they still exist
if [ "$(kill -0 $bgPid 2>/dev/null ; echo $?)" -eq "0" ] ;
then
    kill $bgPid
fi
if [ "$(kill -0 $bgPidS 2>/dev/null ; echo $?)" -eq "0" ] ;
then
    kill $bgPidS
fi
# stty cooked

, script, starter.sh , :

$ ./starter.sh 
0
i'm typing here and pressing [enter] at end
0 - 13496 - 13497
I'M TYPING HERE AND PRESSING [ENTER] AT END
0~ .N= ( ~       }    @      ~  [garble]
proc: 0
OUT

" " ( ) - , ; (, , , script). , , , ( , - , stty raw , : Enter, Ctrl - C:))

, ( screen , ), "" "", ; , , / "", .

+3
1

, , . /, ( ):

while( 1 ) {
    numread = read(rdfd, buf, MAX_BUF_SIZE);

    fprintf(stderr, "Full Duplex Server : Read From the pipe : %sn", buf);

    /* Convert to the string to upper case */
    count = 0;
    while (count < numread) {
        buf[count] = toupper(buf[count]);
        count++;
    }

    /*
     * Write the converted string back to the second
     * pipe
     */
    write(wrfd, buf, strlen(buf));
}

, , , EOF .. , , :

numread = read(rdfd, buf, MAX_BUF_SIZE);
while( numread > 0) {
    /* ... etc ... */
    numread = read(rdfd,buf, MAX_BUF_SIZE);
}
if( numread == 0 )  {
    /* ... handle eof ... */
}
if( numread < 0 ) {
    /* ... handle io error ... */
} 

man-, 0 EOF -1 ( , ? http://linux.die.net/man/2/read). , , , EOF , () . , , EOF, .

, , . :

cat - > /tmp/np1

"-" cat stdin ( , ). , , , . EOF Ctrl + D, - stdin. , , EOF .

, , io, ( stdio echo), :

const int stdin_fd = 0;  // known unix constant!
int readpipe_fd = open the read pipe, as before 
int writepipe_fd =  open the write pipe, as before 
read stdin into buffer
while( stdin is reading correctly ) {
     write data from stdin to read pipe
         check write is successful
     read write pipe into buffer
         check read is successful
     write buffer to stdout (fprintf is fine)
     read stdin into buffer.
}

, stdin, , stdio. , , , / .

+3
source

Source: https://habr.com/ru/post/1744334/


All Articles