使用pipe和fork的C语言程序?
我有一个任务,我必须解决这个问题…我是一个完全的C新手,我仍然在努力学习C 这就是问题所在 编写一个程序来创建管道和子进程。家长反复设置警报15秒。触发警报时,父级计算警报启动后经过的秒数和微秒数,并通过管道将这些秒数和微秒数发送给子级。获取信息后,孩子会在屏幕上显示这些信息。整个过程持续2分钟 我试过这个问题,但我有很多错误 这是我的解决办法使用pipe和fork的C语言程序?,c,linux,fork,pipe,C,Linux,Fork,Pipe,我有一个任务,我必须解决这个问题…我是一个完全的C新手,我仍然在努力学习C 这就是问题所在 编写一个程序来创建管道和子进程。家长反复设置警报15秒。触发警报时,父级计算警报启动后经过的秒数和微秒数,并通过管道将这些秒数和微秒数发送给子级。获取信息后,孩子会在屏幕上显示这些信息。整个过程持续2分钟 我试过这个问题,但我有很多错误 这是我的解决办法 #include <string.h> #include <stdio.h> #include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int fd[2], nbytes;
int fd2[2];
pid_t childpid;
char readbuffer1[80];
char readbuffer2[80];
clock_t start, stop;
long count;
double time_sec, time_milli;
const char* time1, time2;
pipe(fd);
pipe(fd2);
childpid = fork();
if(childpid == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[1]);
/* Read in a string from the pipe */
read(fd[0], readbuffer1, sizeof(readbuffer1));
read(fd2[0], readbuffer2, sizeof(readbuffer2));
printf("Received string 1: %s", readbuffer1);
printf("Received string 2: %s", readbuffer2);
}
else
{
start = clock();
/* Parent process closes up output side of pipe */
alarm(15);
/* Send "string" through the output side of pipe */
stop = clock();
time_sec = (double)(stop-start)/CLOCKS_PER_SEC;
time_milli = time_sec*1000;
sprintf(&time1,"%f",time_sec);
sprintf(&time2,"%f",time_milli);
close(fd[0]);
write(fd[1], time1, (strlen(time1)+1));
write(fd2[1], time2, (strlen(time2)+1));
}
return(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
int-fd[2],n字节;
int-fd2[2];
pid_t childpid;
char readbuffer1[80];
char readbuffer2[80];
时钟没有启动,停止;
长计数;
双时间秒,时间毫秒;
常量字符*time1,time2;
管道(fd);
管道(fd2);
childpid=fork();
if(childpid==-1)
{
佩罗尔(“福克”);
出口(1);
}
if(childpid==0)
{
/*子进程关闭管道的输入端*/
关闭(fd[1]);
/*从管道中读入字符串*/
read(fd[0],readbuffer1,sizeof(readbuffer1));
read(fd2[0],readbuffer2,sizeof(readbuffer2));
printf(“收到的字符串1:%s”,readbuffer1);
printf(“收到的字符串2:%s”,readbuffer2);
}
其他的
{
开始=时钟();
/*父进程关闭管道的输出端*/
警报(15);
/*通过管道的输出端发送“字符串”*/
停止=时钟();
时间/秒=(双精度)(停止/启动)/时钟/秒;
时间_milli=时间_秒*1000;
sprintf(&time1,“%f”,时秒);
sprintf(&time2,“%f”,time_milli);
关闭(fd[0]);
写入(fd[1],time1,(strlen(time1)+1));
写入(fd2[1],time2,(strlen(time2)+1));
}
返回(0);
}
我该如何跑2分钟?我如何重复运行警报15秒?请帮助……您只需等待15秒,然后将其包装成一个循环
for (int i = 0; i < 8; ++i) {
sleep(15);
/* do something, talk to your child, ... */
}
for(int i=0;i<8;++i){
睡眠(15);
/*做点什么,和你的孩子谈谈*/
}
您只需等待15秒,然后将其包装成一个循环
for (int i = 0; i < 8; ++i) {
sleep(15);
/* do something, talk to your child, ... */
}
for(int i=0;i<8;++i){
睡眠(15);
/*做点什么,和你的孩子谈谈*/
}
首先,如果运行代码,会出现哪些错误
我在代码中看到的第一个问题是,您正在创建两个管道,而不是问题中提到的一个
这将创建一个合适的管道,在那里你可以做你的事情
编辑:我刚刚在代码中添加了一次简单的通信。你所要做的就是把所有东西都包装在一个循环中,循环运行2分钟,并在正确的时间发出警报
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pipefd[2];
int rt;
pid_t child = 0;
// An array with some values
char charBuffer[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
// A buffer we use to communicate over the pipe
char outputBuffer[10];
if( pipe(pipefd) == -1 ) // Create new pipe
{
fprintf(stderr, "Failed to create pipe\n");
exit(1);
}
child = fork(); // Create new Child
if( !child )
{
printf("child created\n");
close(pipefd[1]); // Child is read only
// Wait for your alarm here
while( read(pipefd[0], (void*)&outputBuffer, 10) > 0 )
{
printf("Child received something over the pipe\n");
// Write output direct to stdout
rt = write(STDOUT_FILENO, &outputBuffer, 10);
rt = write(STDOUT_FILENO, "\n", 1);
}
}
else if( child == -1 )
{
fprintf(stderr, "Error on fork - no child\n");
exit(2);
}
else if( child > 0 )
{
close(pipefd[0]); // Parent is write only
// Write our values to the pipe
rt = write(pipefd[1], &charBuffer, 10);
printf("Parent finished writing\n");
close(pipefd[1]); // Signal writing is finished
}
return 0;
}
#包括
#包括
#包括
int main()
{
int-pipefd[2];
int-rt;
pid_t child=0;
//具有某些值的数组
char charBuffer[10]={'0','1','2','3','4','5','6','7','8','9'};
//我们用来通过管道进行通信的缓冲区
字符输出缓冲区[10];
if(pipe(pipefd)==-1)//创建新管道
{
fprintf(stderr,“未能创建管道”\n);
出口(1);
}
child=fork();//创建新的子对象
如果(!child)
{
printf(“创建的子项”);
关闭(pipefd[1]);//子项为只读
//在这里等你的闹钟
而(读取(pipefd[0],(void*)和输出缓冲区,10)>0)
{
printf(“Child通过管道接收到某物\n”);
//将输出直接写入标准输出
rt=写入(标准输出文件号和输出缓冲区,10);
rt=写入(标准输出文件号,“\n”,1);
}
}
else if(子项==-1)
{
fprintf(stderr,“fork上的错误-无子\n”);
出口(2);
}
else if(子项>0)
{
close(pipefd[0]);//父级为只读
//将我们的值写入管道
rt=写入(pipefd[1],&charBuffer,10);
printf(“父级已完成写入\n”);
关闭(pipefd[1]);//信号写入完成
}
返回0;
}
首先,如果运行代码,会出现哪些错误
我在代码中看到的第一个问题是,您正在创建两个管道,而不是问题中提到的一个
这将创建一个合适的管道,在那里你可以做你的事情
编辑:我刚刚在代码中添加了一次简单的通信。你所要做的就是把所有东西都包装在一个循环中,循环运行2分钟,并在正确的时间发出警报
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pipefd[2];
int rt;
pid_t child = 0;
// An array with some values
char charBuffer[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
// A buffer we use to communicate over the pipe
char outputBuffer[10];
if( pipe(pipefd) == -1 ) // Create new pipe
{
fprintf(stderr, "Failed to create pipe\n");
exit(1);
}
child = fork(); // Create new Child
if( !child )
{
printf("child created\n");
close(pipefd[1]); // Child is read only
// Wait for your alarm here
while( read(pipefd[0], (void*)&outputBuffer, 10) > 0 )
{
printf("Child received something over the pipe\n");
// Write output direct to stdout
rt = write(STDOUT_FILENO, &outputBuffer, 10);
rt = write(STDOUT_FILENO, "\n", 1);
}
}
else if( child == -1 )
{
fprintf(stderr, "Error on fork - no child\n");
exit(2);
}
else if( child > 0 )
{
close(pipefd[0]); // Parent is write only
// Write our values to the pipe
rt = write(pipefd[1], &charBuffer, 10);
printf("Parent finished writing\n");
close(pipefd[1]); // Signal writing is finished
}
return 0;
}
#包括
#包括
#包括
int main()
{
int-pipefd[2];
int-rt;
pid_t child=0;
//具有某些值的数组
char charBuffer[10]={'0','1','2','3','4','5','6','7','8','9'};
//我们用来通过管道进行通信的缓冲区
字符输出缓冲区[10];
if(pipe(pipefd)==-1)//创建新管道
{
fprintf(stderr,“未能创建管道”\n);
出口(1);
}
child=fork();//创建新的子对象
如果(!child)
{
printf(“创建的子项”);
关闭(pipefd[1]);//子项为只读
//在这里等你的闹钟
而(读取(pipefd[0],(void*)和输出缓冲区,10)>0)
{
printf(“Child通过管道接收到某物\n”);