gpt4 book ai didi

c - 命名管道,使用 Fork()

转载 作者:行者123 更新时间:2023-11-30 20:36:00 25 4
gpt4 key购买 nike

我遇到了一个很大的问题。因此,对于我的作业,我必须使用命名管道(FIFO)并使用 Fork()。我有三个程序,lab6(这是我的主/父/打印答案),然后我有我的生命支持(这是一个 child ),然后我有我的导航(这也是一个 child )。两个 child 必须写信给 parent ,然后 parent 阅读并打印出来。确实很难解释答案应该如何。它很复杂,但我的数学是正确的,只有当我尝试将此文件移动到单独的 FIFO 文件中时才遇到问题。我收到段错误错误。所以我的问题是我做错了什么?我正在尝试将这段代码转换为 FIFO。附注不用担心任何时间或种子生成器或类似的东西。我只需要导航来计算其部分并将其发送给父级,生命支持也是如此,但我似乎无法让 FIFO 工作。

主文件-

  #include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<linux/stat.h>

#define FIFO_FILE "MYFIFO" //default is current directory


void getTime();
void writeToMain( char *, int );
void lifeSupport();
void navigation();
int generate(int);
void countdown( int );

//Global Variables
const int MAX_LINE = 80;
int ret, myPipes[2];
char *message;

int time_;
char myClock[25];

//Pipe variables
int totalTime = 0;
char toSend[120];



int main(void){

FILE *fpoint;
char readbuffer[80];
int again = 1;


mknod(FIFO_FILE, S_IFIFO | 0666, 0);

while(again){

fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;

return(0);

}


if( ret == 0 ){
if(fork() == 0){

while (repeat){

totalTime = 0;
getTime();
lifeSupport();
navigation();

getTime();
sprintf(toSend, "[ %s ] \tSleep \t\t[ %d second(s) ]\n", myClock, 30 - totalTime);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );

sleep(30 - totalTime);
}
}

else{

while(again){
read( myPipes[PipeStdIn], buffer, MAX_LINE);
again = strcmp(buffer, "Stop");


if (again == 0) ;
else
printf("%s \n", buffer);
}

}
}//exit main

child (生命支持)-

       #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO"



void getTime();
void writeToMain( char *, int );
void lifeSupport();
void navigation();
int generate(int);
void countdown( int );

void lifeSupport(){
getTime();
message = "Beginning life support systems \t";
sprintf(toSend, "[ %s ] %s ", myClock, message);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(1);
totalTime ++;

getTime();
time_ = generate(5);
message = "Adjusting breathing gas levels";
sprintf(toSend, "[ %s ] %s \t[ %d Second(s) ]", myClock, message, time_);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(time_);
totalTime += time_;

getTime();
time_ = generate(3);
message = "Adjusting environment";
sprintf(toSend, "[ %s ] %s \t\t[ %d Second(s) ]", myClock, message, time_);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(time_);
totalTime += time_;

getTime();
message = "Done. Life support adjusted. \t[ 1 Second(s) ]";
sprintf(toSend, "[ %s ] %s ", myClock, message);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(1);
totalTime ++;

getTime();
message = "\t\tExecution time";
sprintf(toSend, "%s [ %d Second(s) ]\n", message, (totalTime - 1));
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(1);
totalTime ++;


FILE *fpoint;
int again =1;
char strIn[80] = "Use message from command line";

if(argc !=2){
printf("USAGE: NamedPipeClient[string]\n");
exit(1);
}

while(again == 1){

if((fpoint = fopen (FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);

if((fpoint = fopen(FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
}
}

和最后一个 child (导航。记住两个 child 都会写信给 parent )-

       #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO"


void getTime();
void writeToMain( char *, int );
void lifeSupport();
void navigation();
int generate(int);
void countdown( int );

void navigation(){
getTime();
message = "Beginning navigation systems";
sprintf(toSend, "[ %s ] %s \t[ 1 Second(s) ]", myClock, message);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(1);
totalTime ++;

getTime();
time_ = generate(6);
message = "Adjusting navigation systems";
sprintf(toSend, "[ %s ] %s \t[ %d Second(s) ]", myClock, message, time_);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(time_);
totalTime += time_;
getTime();
message = "Done. Navigation systems adjusted \t[ 1 Second(s) ]";
sprintf(toSend, "[ %s ] %s ", myClock, message);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(1);
totalTime ++;

message = "\t\tExecution time";
sprintf(toSend, "%s [ %d Second(s) ]", message, (totalTime - 2));
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(1);
totalTime += 2;

getTime();
message = "\tTotal time";
sprintf(toSend, "[ %s ] %s \t[ %d Second(s) ]", myClock, message, totalTime);
write( myPipes[PipeStdOut], toSend, strlen(toSend) + 1 );
sleep(1);



while(again == 1){

if((fpoint = fopen (FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}

if((fpoint = fopen(FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
}
fputs(strIn,fpoint);
fclose(fpoint);

return(0);
}

我知道这很多,并且非常感谢任何帮助。如果这太多了,我会尊重规则并删除除此之外的问题,我认为这将是一个不错的挑战。哈哈提前致谢!

最佳答案

您似乎试图尽可能地向我们混淆您的问题。您的程序示例不是最小的 - 它归结为(主要)

int main (void){

mknod(FIFO_FILE, S_IFIFO | 0666, 0);

while(again){

fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);

// All the fork() stuff follows here
....
}

您创建 FIFO,打开它并从中读取(不检查您是否确实得到了某些内容)。当您的 child 处理实际上尚未开始的事情时,您希望谁在其中放入一些东西?

因为您没有检查 fgets() 的返回值,所以您将在未初始化的缓冲区 (readbuffer) 上工作,并且这里会发生严重破坏。

即使如果你有一些外部的东西神奇地写入了 FIFO,你需要检查 fgets() 的返回值,尤其是在多进程环境 - 当您读取时,您不能假设其他进程已经写了一些东西。

关于c - 命名管道,使用 Fork(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36930228/

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