People,
I am trying to interface a C program fifoToA.c to Axiom (friCAS)
by named pipes in Linux (Unix).
These pipes are created by the system commands > mkfifo toA
> mkfifo fromA
fifoToA builds n strings "ABC0", "ABC1", "ABC2", ... in a loop,
outputs each string to the pipe toA,
inputs the respond string from the pipe fromA.
fifoFromA.input is interpreted by Fricas.
It inputs a string iStr from toA,
converts each character in iStr to the lower case,
outputs the result string to fromA.
1. C <-> C
----------
First, I replace fifoFromA.input with the C program fifoFromA.c.
fifoToA opens toA once and only for writing,
opens fromA once and only for reading.
fifoFromA.c opens toA once and only for reading,
opens fromA once and only for writing.
A particular point is that in both C programs the function `open'
(`close') is envoced only once. And the C programs use read/write
(somewhat of a lower level) instead of fgets/fputs
(I do not know why fgets-fputs-fflush did not work as needed).
The source is presented below.
Compilation: gcc -O2 -o fifoToA fifoToA.c
gcc -O2 -o fifoToA fifoToA.c
First, run > ./fifoFromA
on terminal-2,
then, run > ./fifoToA
on terminal-1.
This C <-> C exchange performance (with no intermediate printing in
the loop) at 133000 strings/sec on a 2 GHz machine
(say, put n = 400000 in `main').
It is slow (I need to find out, why).
But so far, it looks admissible to start with and to experiment
further with parsing a string and applying algebra at the Axiom end.
2. C <-> Axiom
--------------
Only I failed to replace ./fifoToA.c with a correct code in
fifoFromA.input
(first, run on terminal-2 fricas and )r fifoFromA.input
Then, command > ./fifoToA
on terminal-1
).
Can you, please, fix the below code in fifoFromA.input ?
Thank you in advance for explanation,
------
Sergei
[email protected]
** fifoToA.c ********************************************************
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#define BOUND 64
char *convStr(int np1, int np2, char *line, char *res)
{
// here the pipes np1, np2 are already opened,
// the space at res is reserved
int numread;
write(np1, line, strlen(line));
numread = read(np2, res, BOUND);
res[numread] = '\0';
// printf("fifoA: Read from np2: %s\n", res);
return;
}
static char arr[BOUND] = "ABC";
static char res[BOUND];
void repeatConvStr(int np1, int np2, int n)
{
// It builds the strings str = "ABC0", "ABC1", ..., "ABC"<n>
// and applies convStr to each of them .
int i;
char *p = arr + strlen(arr); // next to end(arr)
for (i = 0; i < n; i++)
{
sprintf(p, "%d", i); // append `<i>"', returns length
convStr(np1, np2, arr, res); // returns res
if (res == NULL) {
fprintf(stderr, "convStr failed.\n");
printf("i = %d\n", i); break;
}
if (i == 0) {printf("pair(0) = %s, %s\n", arr, res);}
if (i == (n-1)) {printf("last pair = %s, %s\n", arr, res);}
}
return;
}
// The named pipes toA, fromA are created by > mkfifo toA
// > mkfifo fromA
main()
{
int n = 4; // edit it
int toA, fromA;
toA = open("toA", O_WRONLY); fromA = open("fromA", O_RDONLY);
printf("n = %d\n", n);
repeatConvStr(toA, fromA, n);
close(toA);
close(fromA);
return;
}
** fifoFromA.c ******************************************************
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#define BOUND 64
int main()
{
int toA, fromA, i, numread;
char buf[BOUND];
toA = open("toA", O_RDONLY); fromA = open("fromA", O_WRONLY);
for (;;)
{
numread = read(toA, buf, BOUND);
buf[numread] = '\0';
// printf("A: Read from toA: %s\n", buf);
i = 0; // convert the string to the lower case
while (i < numread) {buf[i] = tolower(buf[i]); i++;}
write(fromA, buf, strlen(buf));
}
}
** fifoFromA.input *****************************************************
toA: TextFile := open("toA", "input")
fromA: TextFile := open("fromA", "output")
repeat
iStr := readLine! toA
if iStr = "failed" then (output ""; output "input -> failed"; break)
output iStr
output ""
resStr := lowerCase! iStr
output resStr
output ""
writeLine!(fromA, resStr)
flush fromA -- ?
--
You received this message because you are subscribed to the Google Groups
"FriCAS - computer algebra system" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/fricas-devel?hl=en.