如何在不更改以前存储的值(linux)(命名管)的情况下附加到命名管道中
我正在为一个父母创造10个孩子。我希望所有的孩子都写入管道的管道上,因此我可以在父母的稍后阅读集体数据。 因此,第一个孩子写入管道“ 9-6”,然后第二个孩子写下“ 9-6”,使管道内的内容“ 9-6 9-6” 但是我在这里发现的是,每个孩子都打开管道写作时。它截断了管道。 有什么方法我可以继续将内容添加到管道中,最终只能阅读它。
#include <fcntl.h> //
#include <stdio.h> //
#include <stdlib.h> //
#include <string.h> //
#include <sys/types.h> //
#include <sys/wait.h> //
#include <sys/stat.h> //
#include <termios.h> //
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
pid_t pids[10]; //10 children
int i;
int n = 10;
int f1 = mkfifo("p", 0666); //making named pipe
if (f1 < 0)
std::cerr << "Pipe not created";
char str[256] = "9-6"; //character array that every child writes in the pipe
char read_char[256] = "";
/* Start children. */
for (i = 0; i < 10; ++i) {
if ((pids[i] = fork()) < 0) {
perror("fork");
abort();
}
else if (pids[i] == 0) {\
int fifo_write = open("p", O_WRONLY); //open the pipe for writing
if (fifo_write < 0)
{
std::cerr << "Pipe could not be created";
return 0;
}
else
{
write(fifo_write, str, sizeof(str)); //write char str[] and close the pipe
close(fifo_write);
exit(0);
}
}
}
/* Wait for children to exit. */
int count(0);
int fifo_read = open("p", O_RDONLY);
char str1[256] = "";
if (fifo_read < 0)
{
std::cerr << "Pipe could not be created";
}
else
{
read(fifo_read, str1, sizeof(str));
cout << str1 << endl;
count++;
close(fifo_read);
}
cout << "the count is " << count << endl;
unlink("p");
}
我还尝试了第一次阅读管道,并附加了带有“ 9-6”的管道中阅读的内容,然后再次在管道中写入。 看起来像这样的实现
#include <fcntl.h> //
#include <stdio.h> //
#include <stdlib.h> //
#include <string.h> //
#include <sys/types.h> //
#include <sys/wait.h> //
#include <sys/stat.h> //
#include <termios.h> //
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
pid_t pids[10];
int i;
int n = 10;
int f1 = mkfifo("p", 0666); //Making named pipe
if (f1 < 0)
std::cerr << "Pipe not created";
char str[256] = "9-6";
char read_char[256] = "";
/* Start children. */
for (i = 0; i < 10; ++i) {
if ((pids[i] = fork()) < 0) {
perror("fork");
abort();
}
else if (pids[i] == 0) {
//here I open the pipe for read so that I can read what inside and concatenate it with 9-6 //so every time it is concatenated with 9-6 and written back in the pipe
int fifo_read = open("p", O_RDONLY);
if (fifo_read < 0)
{
std::cerr << "Pipe could not be created";
return 0;
}
else
{
read(fifo_read, read_char, sizeof(read_char));
strcat(str, read_char);
close(fifo_read);
}
int fifo_write = open("p", O_WRONLY);
if (fifo_write < 0)
{
std::cerr << "Pipe could not be created";
return 0;
}
else
{
write(fifo_write, str, sizeof(str));
close(fifo_write);
exit(0);
}
}
}
/* Wait for children to exit. */
int count(0);
int fifo_read = open("p", O_RDONLY);
char str1[256] = "";
if (fifo_read < 0)
{
std::cerr << "Pipe could not be created";
}
else
{
read(fifo_read, str1, sizeof(str));
cout << str1 << endl;
count++;
close(fifo_read);
}
cout << "the count is " << count << endl;
unlink("p");
}
,但是当我这样做时,MKFIFO失败了,“未创建管道”打印了
I am creating 10 children to one parent. I want all the children to write into a pipe appending the pipe, so I can then read the collective data later in the parent.
So first child writes into the pipe "9-6" then the second child writes "9-6" making the contents inside the pipe "9-6 9-6"
but what I have discovered here is that Every child when open the pipes for writing inside it. It truncates the pipe.
Is there a way I can just keep on adding content into the pipe and eventually in the end just read it.
#include <fcntl.h> //
#include <stdio.h> //
#include <stdlib.h> //
#include <string.h> //
#include <sys/types.h> //
#include <sys/wait.h> //
#include <sys/stat.h> //
#include <termios.h> //
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
pid_t pids[10]; //10 children
int i;
int n = 10;
int f1 = mkfifo("p", 0666); //making named pipe
if (f1 < 0)
std::cerr << "Pipe not created";
char str[256] = "9-6"; //character array that every child writes in the pipe
char read_char[256] = "";
/* Start children. */
for (i = 0; i < 10; ++i) {
if ((pids[i] = fork()) < 0) {
perror("fork");
abort();
}
else if (pids[i] == 0) {\
int fifo_write = open("p", O_WRONLY); //open the pipe for writing
if (fifo_write < 0)
{
std::cerr << "Pipe could not be created";
return 0;
}
else
{
write(fifo_write, str, sizeof(str)); //write char str[] and close the pipe
close(fifo_write);
exit(0);
}
}
}
/* Wait for children to exit. */
int count(0);
int fifo_read = open("p", O_RDONLY);
char str1[256] = "";
if (fifo_read < 0)
{
std::cerr << "Pipe could not be created";
}
else
{
read(fifo_read, str1, sizeof(str));
cout << str1 << endl;
count++;
close(fifo_read);
}
cout << "the count is " << count << endl;
unlink("p");
}
I have also tried first reading the pipe and appending what is read from the pipe with "9-6" and then writing in the pipe again.
The implementation of that looks like this
#include <fcntl.h> //
#include <stdio.h> //
#include <stdlib.h> //
#include <string.h> //
#include <sys/types.h> //
#include <sys/wait.h> //
#include <sys/stat.h> //
#include <termios.h> //
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
pid_t pids[10];
int i;
int n = 10;
int f1 = mkfifo("p", 0666); //Making named pipe
if (f1 < 0)
std::cerr << "Pipe not created";
char str[256] = "9-6";
char read_char[256] = "";
/* Start children. */
for (i = 0; i < 10; ++i) {
if ((pids[i] = fork()) < 0) {
perror("fork");
abort();
}
else if (pids[i] == 0) {
//here I open the pipe for read so that I can read what inside and concatenate it with 9-6 //so every time it is concatenated with 9-6 and written back in the pipe
int fifo_read = open("p", O_RDONLY);
if (fifo_read < 0)
{
std::cerr << "Pipe could not be created";
return 0;
}
else
{
read(fifo_read, read_char, sizeof(read_char));
strcat(str, read_char);
close(fifo_read);
}
int fifo_write = open("p", O_WRONLY);
if (fifo_write < 0)
{
std::cerr << "Pipe could not be created";
return 0;
}
else
{
write(fifo_write, str, sizeof(str));
close(fifo_write);
exit(0);
}
}
}
/* Wait for children to exit. */
int count(0);
int fifo_read = open("p", O_RDONLY);
char str1[256] = "";
if (fifo_read < 0)
{
std::cerr << "Pipe could not be created";
}
else
{
read(fifo_read, str1, sizeof(str));
cout << str1 << endl;
count++;
close(fifo_read);
}
cout << "the count is " << count << endl;
unlink("p");
}
But when I do this mkfifo fails and "pipe not created" is printed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论