通过管道传递整数
Passing integer through a pipe
我有问题通过管道发送整数或整数数组在Linux中,首先我试图发送一个整数,看看这个代码:
#include <unistd.h>
#include<iostream>
#include<cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main()
{
int count = 10 ;
//cout <<"Enter a digit : ";
//cin >> count ;
cout<<"n";
int pfd1[1];
pipe(pfd1);
int a=fork();
if(a==0)
{
close(pfd1[0]);
write(pfd1[1],&count,sizeof(count));
close(pfd1[1]);
exit(0);
}
else
{
wait(NULL);
int n =0;
close(pfd1[1]);
read(pfd1[0],&n ,sizeof(n));
close(pfd1[0]);
cout <<"N from parent = "<<n<<"nn";
}
return 0;
}
输出:N from parent = 4
输出应该是与声明的相同的10
,但它给了我4
,即使我取消注释注释代码以便用户输入一个数字,输出始终是4
,而它应该是用户输入的数字。
怎么了?如果我想发送一个整数数组呢?
您将超出数组边界,您应该将pfd1
声明为
int pfd1[2];
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- IPC使用多个管道和分支进程来运行Python程序
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 将整数数组写入管道
- 从用户中读取整数并写入子管道,然后父管道从中读取
- 通过管道传递整数