C - printf 打印相同的语句两次



下一个代码应该写入"file.txt"PID编号,1表示父进程或0表示子进程。

我不确定代码是否正常工作,但我对 Printf() 有一个奇怪的问题,这带来了麻烦。 我不明白为什么,但 printf 打印了两次相同的语句。

法典:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void print_pids(int fd,int n){
int i,p;
char msg[99];
for(i=n;i>0;i--){
p=fork();
if(p>0){
sprintf(msg,"My generation is 1.My pid is %dn",getpid());
write(fd,msg,33);
wait();
}
if(p==0){
sprintf(msg,"My generation is 0.My pid is %dn",getpid());
write(fd,msg,33);
}
if(p<0){
printf("cannot fork");
exit(0);
}
}

}
void main(){
int fd;
char buf[99];
fd=open("file.txt",O_WRONLY,700);
print_pids(fd,1);
close(fd);
fd=open("file.txt",O_RDONLY,700);
read(fd,buf,35);
printf(" %sn",buf);
close(fd);
return;

}

而不是打印

My generation is 1.My pid is 8022

它打印

My generation is 1.My pid is 8
My generation is 1.My pid is 8

为什么?

谢谢!

孩子不会在print_pids()中退出,因此它会返回到main()并打开文件、读取、打印文件,然后退出。 父母也这样做,但只有在孩子去世之后。 如果您打印了执行打印操作的过程的 PID,您会更好地了解情况。

使用带有固定大小缓冲区的write()也令人担忧。 而且没有错误检查。

这是代码的固定版本 — 更相关的标头,正确调用wait()(您很不幸,您的代码没有崩溃),打印额外的诊断信息,写入消息的完整长度,读取和打印消息的完整长度(即使没有空终止符),使用八进制数 (0600) 而不是十进制数 (700) 作为权限, 等。

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
static void print_pids(int fd, int n)
{
int i, p;
char msg[99];
for (i = n; i > 0; i--)
{
p = fork();
if (p > 0)
{
sprintf(msg, "My generation is 1. My pid is %dn", getpid());
write(fd, msg, strlen(msg));
int status;
int corpse = wait(&status);
printf("Child %d exited with status 0x%.4Xn", corpse, status);
}
if (p == 0)
{
sprintf(msg, "My generation is 0. My pid is %dn", getpid());
write(fd, msg, strlen(msg));
}
if (p < 0)
{
printf("cannot fork");
exit(0);
}
}
}
int main(void)
{
int fd;
char buf[99];
fd = open("file.txt", O_WRONLY|O_CREAT|O_TRUNC, 0600);
print_pids(fd, 1);
close(fd);
fd = open("file.txt", O_RDONLY);
int nbytes = read(fd, buf, sizeof(buf));
printf("%.5d: %.*sn", (int)getpid(), nbytes, buf);
close(fd);
return 0;
}

示例输出:

33115: My generation is 1. My pid is 33112
My generation is 0. My pid is 33115
Child 33115 exited with status 0x0000
33112: My generation is 1. My pid is 33112
My generation is 0. My pid is 33115

请注意,获取消息的完整长度如何帮助您了解正在发生的事情。 您的消息正在截断输出,因此您看不到完整的 PID。 两个进程都写入文件(总共约 72 个字符)。 (可能会有一些时间问题来改变所看到的内容——我至少得到了一个异常结果,其中只有一个"我这一代"的消息,但我无法可靠地重现它。

最新更新