C でアプリケーションを開発しています。パイプを使用して、あるプロセスから別のプロセスにデータを書き込みます。プロセスはリモートであり、プロセス 1 は毎回可変サイズのデータを書き込みます。char buf[4]
プロセス 1 は長さ 4 のbuf ( ) を書き込みます。プロセス 2 では、そのデータを読み取ります。サイズを決定するために、ioctl() 関数呼び出しを使用しました。
while(read(to_child[0], &byte, 1) == 1) // Read first byte as ioctl is not
{ //blocking and then allocate the
fprintf(fp,"\n Inside teh if block"); // buffer size = count+1;
ioctl(to_child[0], FIONREAD, &count);
buf = malloc(count+1); // count is 3 here
buf[0] = byte;
read(to_child[0], buf+1, count); // total length read is 4.
}
printf("count :%d, buf size: %d", count+1, strlen(buf));
Ioctl() 関数は、process-2() で 4 バイトを別の buf に読み込みます (予想どおり)。しかし、この後、strlen() を使用してバッファの長さを出力すると、長さが 1 になります。
OUTPUT:
count:4 buf size: 1
ここで何がうまくいかないのですか?変数のデータ型を間違っていますか?