I have an assignment where i have to solve this problem...im a complete newbie to C and im still trying to learn C.
so here is the question
Write a program that creates a pipe and a child process. The parent repeatedly sets an alarm for 15 seconds. When the alarm is triggered, the parent calculates the number of seconds and microseconds elapsed since it started and sends these through the pipe to the child. On obtaining the information the child displays them on the screen. The whole continues for 2 minutes.
i have attempted this question, but i have got many errors..
here is my solution..
#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);
}
how do i make this run for 2 mins? how can i repeatedly run the alarm for 15 seconds? please help....