- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
(警告 - 不是完美的英语!)
在拼贴画上,他们给了我一个任务,我苦苦挣扎了几个小时,却无法解决。 我在将数据从一个进程发送到另一个进程时遇到问题。 在下面的代码中,我将输入数据发送到子进程,但我没有从子进程接收到它。你们能帮忙吗?
程序描述:我的工作是从用户那里获取输入,使用管道将其发送到子进程,进行一些计算,然后将这些计算进一步发送到下一个进程。最后一个过程对传入的数据求和并将其分配给最终结果。更具体地说,第一个子进程计算麦克劳林级数的单项并将其发送给对其求和的进程。这是代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
int mkfifo(const char *s, mode_t m) {
return 0;
}
int i = 1;
double FINAL_RESULT = 0 ;
double factorial(int N){
if(N==1)return 1.0;
return N*factorial(N - 1);
}
double Maclaurine(int i , double x , double eps){
double result;
result = (pow(x,(double)i) / (double)factorial(i));
if( result < eps) return 0;
return result;
}
void print(char * tab, int size){
printf("\n");
for( int i = 0; i < size ; i++){
printf("%c" , tab[i]);
}
printf("\n");
}
int main(){
int pd;
if(mkfifo("tmp/myfifo1", 0666) == -1){
printf("Failed to create myfifo2\n");
}
if(mkfifo("tmp/myfifo2", 0666) == -1){
printf("Failed to create myfifo2\n");
}
pd = open("tmp/myfifo1", O_WRONLY);
double x1,eps1;
printf("X Value : \n");
scanf("%lf", &x1);
printf("EPS Value : \n");
scanf("%lf", &eps1);
char tab1[128], tab2[128];
//converting to char*
sprintf(tab1,"%lf",x1);
sprintf(tab2,"%lf",eps1);
//printing to standard output
print(tab1, strlen(tab1));
print(tab2, strlen(tab2));
//creating new process
if(fork() == 0){
//creating another process
if(fork() == 0){
int pd3;
pd3 = open("tmp/myfifo2", O_RDONLY);
char tab5[128];
double x3;
//reading data from pipe , myfifo2
read(pd3,tab5,sizeof(tab5));
//converting char* to float
x3 = atof(tab5);
FINAL_RESULT += x3;
printf("\nCurrent result = %lf" , FINAL_RESULT);
}
int pd2;
//opening myfifo1 for reading
pd2 = open("tmp/myfifo1", O_RDONLY);
if(pd2 == - 1){
printf("\nFailed to open myfifo1 for reading!\n");
}
double x2, eps2;
char tab3[128], tab4[128];
//reading inputed parameters
read(pd2,tab3, sizeof(tab3) );
read(pd2,tab4, sizeof(tab4) );
//converting to floats
x2 = atof(tab3);
eps2 = atof(tab4);
printf("\nReceived initial data x=%f, eps = %f \n" , x2, eps2);
double singleTerm = 0;
int pd4;
char wyraz[128];
//opening myfifo2 for writing
pd4 = open("tmp/myfifo2", O_WRONLY);
if(pd4 == -1 ){
printf("\nfailed to open myfifo2\n");
}
do{
//calculate single term of Maclaurin series
singleTerm = Maclaurine(i,x2,eps2);
printf("\n%d. Series term = %lf\n", i,singleTerm);
sprintf(wyraz,"%lf",singleTerm);
if(write(pd4, wyraz, sizeof(wyraz)) == -1 ){
printf("\nfailed to send singleTerm to myfifo2\n");
}
i++;
//if its less than eps, loop ends
}while(singleTerm);
printf("\nFinal Result : %lf", FINAL_RESULT);
close(pd4);
close(pd2);
unlink("tmp/myfifo2");
}
//write data to myfifo1
write(pd, tab1, sizeof(tab1));
write(pd, tab2, sizeof(tab2));
close(pd);
unlink("tmp/myfifo1");
return 0;
}
这里是代码的结果:
pawel@IMPERIUMVB:~$ gcc pipes.c -lm -o x
pawel@IMPERIUMVB:~$ ./x
X Value :
10
EPS Value :
0.001
10.000000
0.001000
pawel@IMPERIUMVB:~$
Failed to open myfifo1 for reading!
Received initial data x=0.000000, eps = 0.000000
failed to open myfifo2
1. Series term = 0.000000
failed to send singleTerm to myfifo2
Final Result : 0.000000
Current result = 0.000000
Failed to open myfifo1 for reading!
Received initial data x=0.000000, eps = 0.000000
failed to open myfifo2
1. Series term = 0.000000
failed to send singleTerm to myfifo2
Final Result : 0.000000
最佳答案
好的,我解决了那个问题,程序给了我预期的结果。第一个关键要素是删除
int mkfifo(const char *s, mode_t m) { return 0; }
就像 Jonathan Leffler 所说 *“在你的程序中包含 int mkfifo(const char s, mode_t m) { return 0; } 意味着你不会创建 FIFO,所以你不能使用它们。你需要删除它以使用系统调用。”
第二件事是支持我的 fifo 在 lseek() 函数单次使用后一遍又一遍地开始。我还必须在使用 memset() 后清除所有缓冲区。
现在这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
int i = 0;
double FINAL_RESULT = 1.0 ;
double factorial(int N){
if(N==1 || N == 0)return 1.0;
return N*factorial(N - 1);
}
double Maclaurine(int i , double x , double eps){
double result;
result = (pow(x,(double)i) / (double)factorial(i));
if( result < eps) return -1.0;
return result;
}
void print(char * tab, int size){
printf("\n");
for( int i = 0; i < size ; i++){
printf("%c" , tab[i]);
}
printf("\n");
}
int main(){
umask(0);
if(mkfifo("myfifo1", 0666) == -1){
printf("Failed to create myfifo2\n");
}
if(mkfifo("myfifo2", 0666) == -1){
printf("Failed to create myfifo2\n");
}
if(fork() == 0){
int pd = open("myfifo1", O_WRONLY);
double x1,eps1;
char tab1[256];
//assigning tab1 data to 0's
lseek(pd,0,0);
memset(tab1, 0, 256);
printf("X Value : \n");
scanf("%lf", &x1);
sprintf(tab1,"%lf",x1);
//write data to myfifo1
write(pd, tab1, strlen(tab1));
printf("EPS Value : \n");
scanf("%lf", &eps1);
lseek(pd,0,0);
memset(tab1, 0, 256);
//converting to char*
sprintf(tab1,"%lf",eps1);
write(pd, tab1, strlen(tab1));
lseek(pd,0,0);
memset(tab1, 0, 256);
close(pd);
unlink("myfifo1");
//printing to standard output
//print(tab1, strlen(tab1));
//print(tab2, strlen(tab2));
//creating new process
}
else if(fork() == 0){
int pd2, pd3;
//opening myfifo1 for reading
pd2 = open("myfifo1", O_RDONLY);
//opening myfifo2 for writing
pd3 = open("myfifo2", O_WRONLY);
if(pd2 == - 1){
printf("\nFailed to open myfifo1 for reading!\n");
}
if(pd3 == -1 ){
printf("\nfailed to open myfifo2\n");
}
double x2, eps2;
char tab2[256];
lseek(pd2,0,0);
memset(tab2, 0, 256);
//reading inputed parameters
read(pd2,tab2, sizeof(tab2) );
//converting to floats
x2 = atof(tab2);
lseek(pd2,0,0);
memset(tab2, 0, 256);
read(pd2,tab2, sizeof(tab2) );
//converting to floats
eps2 = atof(tab2);
lseek(pd2,0,0);
memset(tab2, 0, 256);
//printf("\nReceived initial data x=%f, eps = %f \n" , x2, eps2);
double singleTerm = 0.0;
char wyraz[256];
while(1){
//calculate single term of Maclaurin series
singleTerm = Maclaurine(i,x2,eps2);
if(singleTerm > 0.0)printf("\n%d. Term of Mac Series = %lf", i+ 1,singleTerm);
sprintf(wyraz,"%lf",singleTerm);
lseek(pd3,0,0);
if(write(pd3, wyraz, sizeof(wyraz)) == -1 ){
printf("\nfailed to send singleTerm to myfifo2\n");
}
if(singleTerm < 0.0){
//printf("\nI just broke the 1 loop\n");
memset(wyraz, 0, 256);
break;
}
i=i+1;
}
sleep(1);
close(pd3);
close(pd2);
unlink("myfifo2");
unlink("myfifo1");
}//creating another process
else if(fork() == 0){
int pd4;
pd4 = open("myfifo2", O_RDONLY);
char tab3[256];
double x3 = 0.0;
while(1){
x3=0.0;
lseek(pd4,0,0);
memset(tab3, 0, 256);
//reading data from pipe , myfifo2
read(pd4,tab3,sizeof(tab3));
//converting char* to float
x3 = atof(tab3);
FINAL_RESULT += x3;
if(x3<=0.0){
printf("\nFinal Result : %lf", FINAL_RESULT );
break;}
//else
//printf("\n x = %lf, Current result = %lf" ,x3, FINAL_RESULT);
}
sleep(1);
printf("\nFinal Result : %lf", FINAL_RESULT);
close(pd4);
unlink("myfifo2");
}
sleep(7);
unlink("myfifo1");
unlink("myfifo2");
while(1)pause();
return 0;
}
现在我可以看到使用麦克劳林级数对 e^x 函数进行数学估计的美妙之处。以下是结果:
pawel@IMPERIUMVB:~$ gcc rury.c -lm -o zad1
pawel@IMPERIUMVB:~$ ./zad1
X Value :
8
EPS Value :
0.0001
1. Term of Mac Series = 1.000000
2. Term of Mac Series = 8.000000
3. Term of Mac Series = 32.000000
4. Term of Mac Series = 85.333333
5. Term of Mac Series = 170.666667
6. Term of Mac Series = 273.066667
7. Term of Mac Series = 364.088889
8. Term of Mac Series = 416.101587
9. Term of Mac Series = 416.101587
10. Term of Mac Series = 369.868078
11. Term of Mac Series = 295.894462
12. Term of Mac Series = 215.195972
13. Term of Mac Series = 143.463982
14. Term of Mac Series = 88.285527
15. Term of Mac Series = 50.448873
16. Term of Mac Series = 26.906065
17. Term of Mac Series = 13.453033
18. Term of Mac Series = 6.330839
19. Term of Mac Series = 2.813706
20. Term of Mac Series = 1.184718
21. Term of Mac Series = 0.473887
22. Term of Mac Series = 0.180529
23. Term of Mac Series = 0.065647
24. Term of Mac Series = 0.022834
25. Term of Mac Series = 0.007611
26. Term of Mac Series = 0.002436
27. Term of Mac Series = 0.000749
Final Result : 2980.957900 // after adding all of above
e^8 ~ 2980.9579870417
估计满意:)
关于c - Linux。 C 中的管道。将数据发送到另一个进程。写作和阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41184710/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!