linux - Linux 编译错误(关于管道概念的简单演示代码)

标签 linux compiler-errors pipe output sleep

所以当我尝试在 linux 上编译这个名为管道的代码时出现这个错误

pipe.c: In function ‘main’:
pipe.c:27:14: error: ‘Amsg’ undeclared (first use in this function)
 write(fd[1], Amsg, strlen (Amsg));
              ^
pipe.c:27:14: note: each undeclared identifier is reported only once for each function it appears in
pipe.c:30:41: error: expected ‘;’ before ‘:’ token
 printf("A got: %s and i = %d\n", buf, i):}

这是我的代码:

#define SIZE 1000
#include <stdio.h>
#include <string.h>


int main(void) {

int fd[2];
char buf[SIZE];

char *AMsg = "nodeA";
char *Bmsg = "nodeB";
int i = SIZE;
pipe(fd);


if( fork() == 0) {
sleep(1);
read(fd[0], buf, 100);
i++;
printf("B got: %s and i = %d\n", buf, i);
write(fd[1], Bmsg, strlen(Bmsg));
// sleep(10);
}

else {
write(fd[1], Amsg, strlen (Amsg));
wait(NULL);
while((i = read(fd[0], buf, 100)) != 0) {
printf("A got: %s and i = %d\n", buf, i);}
}
}

我该如何解决这个问题?让我困惑的是 sleep(1) 是什么意思?这是否意味着 1 是正确的,如果它是 1 它将进入休眠过程?

最佳答案

这似乎是一个简单的语法错误。 您声明了“char AMsg” 稍后尝试将变量称为 Amsg。 您只需将 m 更改为 M 即可。

关于linux - Linux 编译错误(关于管道概念的简单演示代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30835382/

相关文章:

linux - 使用 awk 和 sed 增加字符串中的数字

linux - 如何修改行首有字符的模式

c++ - 如何防止在 std header (在 Xcode 中)内发生编译错误?

gcc - 是否可以让 GCC 从管道中读取数据?

带 pipe 和 fork 的 C 作业

angular - 在 angular2 的自定义管道上扩展管道,如货币或数字

linux - 两个 open/proc 条目的处理方式不同

java - 为什么 JVM 消耗的内存比指定的 -Xms 少?

java - 如何从它本身(也是主类)更新GUI?

compiler-errors - 打包双字节文件名时出错