gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:30 26 4
gpt4 key购买 nike

所以当我尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com