Cプログラムで生成されたBashで名前付きパイプを使用する

Cプログラムで生成されたBashで名前付きパイプを使用する

かなり大きな入力を必要とするCコードがあります。各繰り返しごとに小さなチャンクを処理し、処理されたチャンクはカールを使用してS3に送信する必要があります。結局、多くの小さなチャンクが生成され、大きな入力が完全に処理されます。問題のある部分は、処理されたチャンクを個別にS3に送信するために使用される名前付きパイプを呼び出すbashスクリプトの一部です。

Cでbashスクリプトを呼び出し、bashを使用して名前付きパイプのデータをカリングする方法は?

簡潔さを維持するために、コードの関連部分のみを共有します。

#define FIFO_NAME "my_fifo"
int i,j,k;
char *d_ptr; //dummy pointer for frame set
int res;
/* create the FIFO (named pipe) */

if (access(FIFO_NAME, F_OK) == -1) {
    res = mkfifo(FIFO_NAME, 0777);
    if (res != 0)
    {
        fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
        exit(EXIT_FAILURE);
    }
}
res = open("test_fifo", O_WRONLY);
d_ptr=(char *) compressed_queue;
if(m_ptr-m_ptr_initial==0 | m_ptr-m_ptr_initial>MAX_MESSAGE_LENGTH-FLENGTH*sizeof(float) | number_of_frames>MAX_FRAME_NUMBER)//sends message when the chunk is ready and adds its prefix
{

    for(j=0;j<m_ptr-m_ptr_initial;j++)//sends message as stdout
    {

        /* write to the FIFO */
        write(res, m_ptr_initial[j], sizeof(char));

    }
    if (res != -1)
        (void)close(res);

また、bashスクリプトは次のようになります。

#!/bin/bash
while true
do
    TIMER=renesas-$(date +"%H-%M-%S").txt
    echo $TIMER
    ./Compression
    my_fifo| curl -X PUT --data-binary @- 54.231.19.24/{somePublicBucket}/device1/${TIMER}
done

ベストアンサー1

コードには2つのエラーがあります。

  1. write(…, sizeof(char));目的のバッファサイズではなく、「sizeof(char)」バイト(=ほとんど1または2バイト)のみがFIFOに書き込まれます。

  2. my_fifo |cat少なくとも必要に応じてFIFOからデータを読み取らないでください。

スクリプトがデータを読み取るまで(またはその逆)呼び出しがブロックされるため、書き込みwrite操作とスクリプト実行を2つの異なるプロセスまたはスレッドに配置することをお勧めします(非ブロック書き込みなどの他の可能性もあります)。違います)。次の例は、fork単純化のためにシステムコールを使用して原理を示しています。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>

#define FIFO_NAME "./my_fifo"

int main() {
    int res, fifo, pid;
    char *str = "Hello World!\n";

    /* Fork this process into a reader and a writer */
    fprintf(stderr, "Forking process\n");
    pid = fork();
    assert(pid >= 0);

    if (pid == 0) {
        /* The reader calls a shell script supposed to read the data from the FIFO */
        printf("Starting shell script\n");
        system("./tst.sh");
    }
    else {
        /* Create FIFO */
        fprintf(stderr, "Creating FIFO\n");
        remove(FIFO_NAME);
        res = mkfifo(FIFO_NAME, 0777);
        assert(res == 0);

        /* Open FIFO */
        fprintf(stderr, "Opening FIFO\n");
        fifo = open(FIFO_NAME, O_WRONLY);
        assert(fifo != 0);

        /* The writer opens the FIFO and writes some data to it */
        printf("Writing to FIFO\n");
        res = write(fifo, str, strlen(str));
        assert(res > 0);
        close(fifo);
    }
}

シェルスクリプトには以下tst.shが含まれます。

#!/bin/sh
echo "Started shell script"
cat ./my_fifo

Cプログラムの出力は次のとおりです。

Forking process
Creating FIFO
Opening FIFO
Starting shell script
Started shell script
Writing to FIFO
Hello World!

おすすめ記事