#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int pid,p[2],p2[2],i,n,n2,num,check,a,j;
char buff1[100][100],buff2[100][100],ch,src[30],des[30];
FILE *source,*dest;
a = pipe(p);
if(a==-1)
{
perror("error pipe 1");
exit(1);
}
a = pipe(p2);
if(a==-1)
{
perror("error pipe 2");
exit(1);
}
pid = fork();
switch(pid)
{
case -1: perror("fork");
exit(1);
break;
case 0:
read(p[0],buff2,sizeof(buff2));
printf("In child process (PID : %d ) \n",pid);
int l,i;
strcpy(src,buff2[0]);
l = (int)buff2[1][0] - 48; //ASCII of 0 is 48
printf("\nsource file name : %s",src);
printf("\nNmber of line : %d",l);
printf("\nEnter destination file name : ");
gets(des);
source = fopen(src,"r");
dest = fopen(des,"w");
i=1;
l=2; //dummy
while(i<=l)
{
while( (ch=fgetc(source)) != '\n')
{
fputc(ch,dest);
}
fputc('\n',dest);
i++;
}
strcpy(buff1[0],des);
write(p2[1],buff1,sizeof(buff1));
exit(1);
break;
default: printf("In parent process (PID: %d) \n",pid);
printf("enter file name : ");
scanf("%s",buff1[0]);
printf("enter line limit : ");
scanf("%s",buff1[1]);
write(p[1],buff1,sizeof(buff1));
waitpid(pid,NULL,0);
printf("In parent process (PID: %d) \n",pid);
read(p2[0],buff2,sizeof(buff2));
printf("\nnew file created %s ",buff2[0]);
break;
}
close(p[0]);
close(p[1]);
close(p2[0]);
close(p2[1]);
return 0;
}
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int pid,p[2],p2[2],i,n,n2,num,check,a,j;
char buff1[100][100],buff2[100][100],ch,src[30],des[30];
FILE *source,*dest;
a = pipe(p);
if(a==-1)
{
perror("error pipe 1");
exit(1);
}
a = pipe(p2);
if(a==-1)
{
perror("error pipe 2");
exit(1);
}
pid = fork();
switch(pid)
{
case -1: perror("fork");
exit(1);
break;
case 0:
read(p[0],buff2,sizeof(buff2));
printf("In child process (PID : %d ) \n",pid);
int l,i;
strcpy(src,buff2[0]);
l = (int)buff2[1][0] - 48; //ASCII of 0 is 48
printf("\nsource file name : %s",src);
printf("\nNmber of line : %d",l);
printf("\nEnter destination file name : ");
gets(des);
source = fopen(src,"r");
dest = fopen(des,"w");
i=1;
l=2; //dummy
while(i<=l)
{
while( (ch=fgetc(source)) != '\n')
{
fputc(ch,dest);
}
fputc('\n',dest);
i++;
}
strcpy(buff1[0],des);
write(p2[1],buff1,sizeof(buff1));
exit(1);
break;
default: printf("In parent process (PID: %d) \n",pid);
printf("enter file name : ");
scanf("%s",buff1[0]);
printf("enter line limit : ");
scanf("%s",buff1[1]);
write(p[1],buff1,sizeof(buff1));
waitpid(pid,NULL,0);
printf("In parent process (PID: %d) \n",pid);
read(p2[0],buff2,sizeof(buff2));
printf("\nnew file created %s ",buff2[0]);
break;
}
close(p[0]);
close(p[1]);
close(p2[0]);
close(p2[1]);
return 0;
}
No comments:
Post a Comment