gpt4 book ai didi

C/UNIX 进程间通信用管道发送字符串

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:09 25 4
gpt4 key购买 nike

我正在编写一个代码,其中 child 和 parent 应该互相提供时间信息,接收者应该打印它。当我在父进程上找到时间并尝试在父进程上打印它时它工作正常。但是当我尝试要通过管道发送它,它会写一行带有奇怪的问号和一个 z 字母。我评论了最后一行,以防有人试图执行代码。抱歉,代码乱七八糟,但我无法在我当前的键盘上修复它。

  #include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include<signal.h>
#include<stdint.h>
#include<string.h>
#include<time.h>


void formatted_time(char* sender_name, char* receiver_name, char output[]);
void formatted_time( char* sender_name , char* receiver_name, char output[])
{
struct timeval tv;
time_t nowtime;
struct tm *nowtm;
char tmbuf[80];
gettimeofday(&tv, NULL);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf,80 , "%Y-%m-%d %H:%M:%S",
nowtm);
sprintf(output, "%s: Time at %s is %s.", receiver_name, sender_name, tmbuf);
}

int main(int argc, char** argv)
{
char* parent="Parent";
char* child1="Child1";
char* child2="Child2";
char result[80];
char buffer[80];
int firstchild,secondchild,read1,read2,read3;
firstchild=fork();
int mypipe[2];
int mypipe2[2];
int mypipe3[2];

if(pipe(mypipe) == -1) {
perror("Pipe failed");
exit(1);
}

if(firstchild == 0) //first child
{
close(mypipe[1]); //Closing the output of pipe
sleep(3);
read1=read(mypipe[0],buffer,sizeof(buffer));
printf("%s\n",buffer);



}else{
secondchild=fork(); //Creating second child

if(secondchild == 0) //2nd child
{

sleep(6);


}else{ //Parent
close(mypipe[0]); //Closing the input of pipe
formatted_time(parent,child1,result);
write(mypipe[1],result,strlen(result)+1);
//printf("%s\n",result);

最佳答案

您的问题是您在创建管道之前进行了fork 调用。所以实际上你 read 什么也没读到,而你的 printf 打印分配了 buffer 的堆栈上的垃圾。

您的代码已修复。我还在父级中添加了一个 wait 调用,以避免子级无缘无故地打印到控制台 :)

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>

void formatted_time(char *sender_name,char *receiver_name, char output[]) {
struct timeval tv;
time_t nowtime;
struct tm *nowtm;
char tmbuf[80];
gettimeofday(&tv, NULL);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf,80, "%Y-%m-%d %H:%M:%S", nowtm);
sprintf(output, "%s: Time at %s is %s.", receiver_name, sender_name, tmbuf);
}

int main(int argc, char** argv) {
char* parent="Parent";
char* child1="Child1";
char result[80];
char buffer[80];
int firstchild, secondchild, read1;

int mypipe[2];

if (pipe(mypipe) == -1) {
perror("Pipe failed");
exit(1);
}

firstchild=fork();
if (firstchild == 0) { // first child
close(mypipe[1]); //Closing the output of pipe
sleep(3);
read1 = read(mypipe[0], buffer, sizeof(buffer));
printf("%d %s\n", read1, buffer);
} else {
secondchild=fork(); //Creating second child
if(secondchild == 0) { //2nd child
sleep(6);
} else { //Parent
close(mypipe[0]); //Closing the input of pipe
formatted_time(parent, child1, result);
int w;
w = write(mypipe[1], result, strlen(result)+1);
printf("%d bytes written\n", w);
wait(NULL);
}
}
return 0;
}

关于C/UNIX 进程间通信用管道发送字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534391/

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