Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
5
Добавлен:
26.05.2014
Размер:
2.33 Кб
Скачать
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#define BUFFERSIZE 100



int main(int argc,char *argv[])
{

static int in_fd1,in_fd2,out_fd,n_char1,n_char2,k=0,p=0,l1=0,l2=0;
static char buf1[BUFFERSIZE];
static char buf2[BUFFERSIZE];

static  void putpot(int);
static  void putpr(int);
static  int set_ticker(int);


if (argc!=4)
 {
   printf("Forget input all parameties!\n");
   exit(1);
 }


// Make result file
if((out_fd = open("fileresult.txt",O_WRONLY | O_CREAT | O_TRUNC | O_APPEND,0744)) == -1)
 {
   printf("Can't creat result file!\n");
 }

// Open the first file
if((in_fd1 = open(argv[1],O_RDONLY)) == -1)
 {
   printf("Can't open 1 file!\n");
 }

// Open the second file
if((in_fd2 = open(argv[2],O_RDONLY)) == -1)
 {
   printf("Can't open 2 file!\n");
 }

// Read from 1 file
if((n_char1 = read(in_fd1,buf1,BUFFERSIZE)) == -1)
 {
   printf("Can't read 1 file!\n");
 }
buf1[n_char1+1]='\0';

// Read from 2 file
if((n_char2 = read(in_fd2,buf2,BUFFERSIZE)) == -1)
 {
   printf("Can't read 2 file!\n");
 }
buf2[n_char2+1]='\0';

//************************************************
static int n,i=0;
n=atoi(argv[3]);
//************************************************
  if (fork()==0)
  {
  printf("\nsleep for 2 seconds in potomke1\n");
  signal(SIGALRM,putpot);
	while(1)
{
  set_ticker(1001);

//alarm(2);  
pause();
}  
  }
  if (fork()==0)
  {
  printf("sleep for 1 seconds in potomke2\n");
  signal(SIGALRM,putpr);
	while(1)
{
  set_ticker(1000);

//alarm(1);  
pause();
}  
  }

close(in_fd1);
close(in_fd2);
close(out_fd);



int set_ticker(int n_msecs)
{
	struct itimerval new_timeset; 
 	long n_sec,n_usecs;
	n_sec=n_msecs/1000;
	n_usecs=(n_msecs%1000)*1000L;
	new_timeset.it_interval.tv_sec=n_sec;
	new_timeset.it_value.tv_sec=n_sec;
	new_timeset.it_value.tv_usec=n_usecs;
	return setitimer(ITIMER_REAL,&new_timeset,NULL);
} 


void putpr(int signum)
{
l1++;
 printf("1Put N(bate) in file result!\n");
for(i=0;i<n;i++)
{
 write(out_fd,&buf1[i+k],1);
 if ((i+k)==n_char1)
 exit(0);
}
k=n*l1;
printf("OK(1).\n");
}

void putpot(int signum)
{
l2++;
 printf("2Put N(bate) in file result!\n");
for(i=0;i<n;i++)
{
 write(out_fd,&buf2[i+p],1);
if ((i+p)==n_char2)
exit(0);
}
p=n*l2;
printf("OK(2).\n");
}

return 0;
} 
Соседние файлы в папке Nsouz