SIMPLE SOCKET CREATION PROGRAMS
By karthick
, in
SIMPLE SOCKET CREATION PROGRAMS
,
0 Comments
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
main(int argc,char *argv[])
{
int i,pid,ppid,c,magic,sk[2];
struct
{
float t,h;
}num;
socketpair(AF_UNIX,SOCK_STREAM,0,sk);
if(fork()==0)
{
close(sk[0]);
pid=getpid();
while(read(sk[1],&magic,sizeof(magic))
>0)
{
printf("child [%d] the magic number
is %d \n:",pid,magic);
num.t=magic*magic;
num.h=num.t*magic;
write(sk[1],&num,sizeof(num));
}
close(sk[1]);
exit(1);
}
close(sk[1]);
ppid=getppid();
for(i=1;i<argc;i++)
{
magic=atoi(argv[i]);
write(sk[0],&magic,sizeof(magic));
}
for(i=1;i<argc;i++)
{
if(read(sk[0],&num,sizeof(num)) <
0)
break;
printf("Parent [%d] square =
%f,cube = %f \n",ppid,num.t,num.h);
}
close(sk[0]); }
OUTPUT
[11ca013@mcalinux network]$ cc exno2a.c
[11ca013@mcalinux
network]$ ./a.out 3 4 5 6
child
[22746] the magic number is 3
child [22746] the magic number is 4
child
[22746] the magic number is 5
child
[22746] the magic number is 6
Parent
[4041] square = 9.000000,cube = 27.000000
Parent
[4041] square = 16.000000,cube = 64.000000
Parent
[4041] square = 25.000000,cube = 125.000000
Parent
[4041] square = 36.000000,cube = 216.000000
0 Response to "SIMPLE SOCKET CREATION PROGRAMS"
Post a Comment