using signals,along with pipes and files

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

struct itimerval timer;
int which = ITIMER_REAL;
int fd[2];
char line [100];
int timex;

void  f0();
void f1();

int main(){
pipe(fd);
int p=fork();

int t=rand();
t=(t % 1000);
printf(“time interval:%d”,t);

struct itimerval v;
v.it_interval.tv_sec = 0;
v.it_interval.tv_usec = t;  /* random milliseconds */
v.it_value.tv_sec = 0;           /* Zero seconds */
v.it_value.tv_usec = t;

if (p==0){

int f=open(“intext.txt”,O_RDONLY);
int i=0;

//–  the loop —
for(i;i< 5;i++){
int t=rand();
t=(t % 1000);
timex=* t;
read(f,line,5);
line[6]=i;

signal(SIGALRM,f0);
setitimer( which, &v, NULL );
int k=0;
for(k;k<5;k++) pause();

}//for
}//if

else{
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec =500000;  /* five hundred milliseconds */
timer.it_value.tv_sec = 0;           /* Zero seconds */
timer.it_value.tv_usec =500000;

signal(SIGALRM,f1);
setitimer( which, &timer, NULL );
int j=0;
for(j;j<5;j++){ pause();}

}//else
return 1;
}//main

void  f0(){
close (fd[0]);
write(fd[1],line,6);
printf(“writing in pipe at random time %d\n”,timex);

}

void  f1(){
int z= open(“outext.txt”,O_WRONLY|O_APPEND);

char l[50];
close (fd[1]);
int s=read(fd[0],l,6);
write(z,l,6);
if(s>0){
printf(“writing in output file \n”);}

else{ printf(“not read\n”);}

}

Leave a Reply