2015-10-14 45 views
0

我正在尝试在C中开发电梯模拟器应用程序,因此我即将使用共享内存和管道进行内部数据库通信。 为了让我的生活更轻松一点,我宣布了两个函数read_from_pipewrite_to_pipe不能让'孩子'和'父母'一起正确通信

下面是我main代码的一部分,我需要找出原因预期不会表现:

01 #include <stdio.h> 
02 #include <stdlib.h> 
03 #include <string.h> 
04 #include <sys/types.h> 
05 #include <sys/stat.h> 
06 #include <fcntl.h> 
07 #include <sys/mman.h> 
08 
09 #include "windows.h" 
10 
11 #define READ 0 
12 #define WRITE 1 
13 
14 typedef struct lift 
15 { 
16   int winch_control; 
17   int door_control; 
18   int call_buttons; 
19   int lift_buttons; 
20   double position; 
21   double door_ajar; 
22   int quit; 
23   int reset; 
24   int error; 
25 } lift; 
26 int main(void) 
27 { 
28 lift *pLift; 
29 pid_t pid; 
30 off_t off = 0; 
31 int liftfd, mmlen = sizeof(lift), FIFO[2];; 
32 
33 pid = fork(); 
34 liftfd = open("liftfile", (O_CREAT | O_RDWR), 0666); 
35 pLift = (lift *)mmap((caddr_t)0, mmlen, (PROT_READ | PROT_WRITE), MAP_SHARED, liftfd, off); 
36 
37 if (pipe(FIFO))     // create pipe failed 
38 { 
39  fprintf(stderr, "Pipe failed.\n"); 
40  return EXIT_FAILURE; 
41 } 
42 
43 if (pid == (pid_t)0)   // child process 
44 { 
45  close(FIFO[WRITE]);   
46  read_from_pipe(FIFO[READ]); 
47  close(FIFO[READ]); 
48 } 
49 else if (pid < (pid_t)0)  // create fork failed 
50 { 
51  fprintf(stderr, "Fork failed.\n"); 
52  return EXIT_FAILURE; 
53 } 
54 else       // parent process 
55 { 
56  close(FIFO[READ]);   
57  write_to_pipe(FIFO[WRITE],"UP3" , 56); 
58  close(FIFO[WRITE]); 
59 } 
60 } 

read_from_pipe子程序:

void read_from_pipe(int fileDescriptr) 
{ 
    FILE *stream; 
    int c; 
    stream = fdopen(fileDescriptr, "r"); 
    while ((c = fgetc(stream)) != EOF) 
     putchar(c); 
    fclose(stream); 
} 

write_to_pipe子程序:

void write_to_pipe(int fileDescriptr , char *stateName , int timerValue) 
{ 
    FILE *stream; 
    stream = fdopen(fileDescriptr, "w"); 
    fprintf(stream, "Current system state:\t%s\n", stateName); 
    fprintf(stream, "Timer value:\t\t%d\n",timerValue); 
    fflush(stream); 
    fclose(stream); 
} 

夫妻俩我窝我想指出:

  • 如果有人想要参考特定的 行,我会提供行号。我假设每个人都知道如何在代码 编辑器中使用列模式,并将其全部删除,以便编译成功。
  • 代码中的许多内容可能看起来多余,但实际上它们正在代码中的其他地方使用。因此,如果您选择了任何一项,请忽略 冗余。
  • 我在Windows上使用CygWin。

根据行号57,我的预期结果是:

Current system state:  UP3 
Timer value:    56 

不过,我得到一个空白屏幕。

任何想法我做错了什么?

回答

1

调用fork()后有3个possiblities

1)的返回值是< 0,表示该叉()失败

2)返回的值为0,表示子正在执行

3)返回的值大于0表示父节点正在执行。

假设没有发生故障,则父母和孩子在拨打fork()后执行该代码。所以父母和孩子都拨打open()mmap()

需要检查open()和mmap()的返回值,以确保操作成功。

mmap()结果未在发布的代码中使用。

open()结果未在发布的代码中使用。

该行:fprintf(stderr, "Fork failed.\n");应该可能是对perror()的调用,因此系统错误消息也会显示。

问题的根源似乎是一种竞争条件。

建议使用read()而不是fgetc()作为读取将等待所需数量的字符,因此将等待传递的数据可用。一定要检查从read()返回的值,然后再试一次,直到返回值为0(或小于0的错误)

+0

您是对的。我在'pipe()'调用后将所有内容放入'else'语句中,并且工作正常! – Bababarghi