Sockpair Implementing interprocess communication
We used to learn the use of pipelines (here is anonymous pipeline) to achieve interprocess communication, can only be one-way, can only read while the other side can only write, and can only be in a blood relationship between the process to communicate, if you want to achieve two-way communication must create two-way pipeline, and sockpair its implementation is two-way pipeline for communication. It can be used to create two-way communication pipelines
1 #include <stdio.h> 2 #include <unistd.h> 3 #include < string.h> 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include <stdlib.h> 7 #include <errno.h> 8 9 int main () 10 { 11 int sv[2]={0,0}; 12 char buf[1024]; 13 int sock_pair= Socketpair (AF_LOCAL,SOCK_STREAM,0,SV); 14 if (sock_pair < 0) 15 { 16 perror (" Socketpair "); 17 exit (1); 18 } 19 pid_t id=fork (); 20 if (id<0) 21 { 22 perror ("fork"); 23 return -1;
}else if (id==0) {//child
Close (sv[0]);
(1)
27 {
memset (buf, ' n ', sizeof (BUF));
strcpy (buf, "I ' m Child");
Write (Sv[1],buf,strlen (BUF));
ssize_t _size=read (sv[1],buf,sizeof (BUF)-1);
if (_size<0)
33 {
Perror ("read");
Return-2;
}else if (_size > 0)
37 {
"Buf[_size]=";
father->child:%s\n printf ("buf");
40}
Sleep (1);
42}
Close (sv[1]);
44
}else
46 {
Close (sv[1]);
while (1)
49 {
ssize_t _size=read (sv[0],buf,sizeof (BUF)-1);
Wuyi if (_size<0)
52 {
Perror ("read");
Wu exit (2);
}else if (_size >0)
56 {
"Buf[_size]=";
printf ("child->father:%s\n", buf);
59}
(buf, ' memset ', sizeof (BUF));
strcpy (buf, "I ' m father");
$ write (sv[0],buf,sizeof (BUF)-1);
63}
Close (sv[0]);
65}
0;
67}
Program Run Result:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/80/EC/wKioL1dE_Vfi_2JPAABDvwJt1NM805.png "title=" 7.png " alt= "Wkiol1de_vfi_2jpaabdvwjt1nm805.png"/>
Redirect: Redirect file descriptors
Example: Print the contents of a file to standard output, if the standard output file descriptor is turned off, then open a file, the file descriptor will be 1, and the contents of the first file will be printed to the file (that is, redirect)
What if a new process file descriptor is created from 3 (if 0, 1, 2 are not off)?
When a terminal is opened, the process is to create a session of the process, there will be a control process bash, that is, the session first process, the associated terminal terminal will default to fill the standard input, standard output and standard error, so the process is created in the present is a child process, The factor process inherits the file descriptor of the parent process when it is created, so the file descriptor starts at 3 when a process is created.
Using DUP redirection: function prototype int dup (int oldfd)
Example: Close the 1th file descriptor (standard output), the standard input should have been printed to the standard output, redirected to the log file
1 #include <stdio.h> 2 #include <unistd.h> 3 #include < stdlib.h> 4 #include <fcntl.h> 5 #include <string.h> 6 #include <errno.h> 7 #include <sys/stat.h> 8 9 int main () 10 { 11 int fd=open ("./log", O_CREAT| o_rdwr,0644); 12 if (fd<0) 13 { 14 perror ("open"); 15 exit (1); 16 } 17 Close (1); //must close the file descriptor 18 int newfd=dup (FD) that you want to redirect; 19 if (newfd <0) 20 { 21 &nbSp; perror ("DUP"); 22 exit (2); 23 }
-Char buf[1024];
25
(1)
27 {
memset (buf, ' n ', sizeof (BUF));
Fgets (Buf,sizeof (BUF), stdin);
if (strncmp ("Quit", buf,4) ==0)
31 {
break;
33}
printf ("%s\n", buf);
//printf ("Hello world\n");
Fflush (stdout);
37}
Close (NEWFD);
39
return 0;
41}
Input content:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/EE/wKiom1dFCJ7h5T7cAAApBTqsqZ4993.png "title=" Xie.png "alt=" Wkiom1dfcj7h5t7caaapbtqsqz4993.png "/>
Redirect to contents in log file
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/EC/wKioL1dFCgPgEbKXAAAfq9bHTDE341.png "title=" Log.png "alt=" Wkiol1dfcgpgebkxaaafq9bhtde341.png "/>
int dup2 (int oldfd,int newfd)
1 #include <stdio.h> 2 #include <unistd.h> 3 #include < stdlib.h> 4 #include <fcntl.h> 5 #include <string.h> 6 #include <errno.h> 7 #include <sys/stat.h> 8 9 int main () 10 { 11 int fd=open ("./log", O_CREAT| o_rdwr,0644); 12 if (fd<0) 13 { 14 perror ("open"); 15 exit (1); 16 } 17 Close (1); //may not have to close the 1th # file descriptor 18 int newfd=dup2 (fd,1); 19 if (newfd <0) 20 { 21 &Nbsp; perror ("DUP"); 22 exit (2); 23 }
-Char buf[1024];
while (1)
26 {
memset (buf, ' n ', sizeof (BUF));
Fgets (Buf,sizeof (BUF), stdin);
if ((STRNCMP ("Quit", buf,4)) ==0)
30 {
break;
32}
printf ("%s", buf);
//printf ("Hello world\n");
Fflush (stdout);
36}
PNS Close (NEWFD);
38
0;
40}
41
Input content:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/ED/wKioL1dFDAPysSryAAAqj1VQwnE276.png "title=" Xinxie.png "alt=" Wkiol1dfdapyssryaaaqj1vqwne276.png "/>
The contents of the log file:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/ED/wKioL1dFDB2A-U4tAAAW1ip_KVM125.png "title=" QQ picture 20160525101503.png "alt=" Wkiol1dfdb2a-u4taaaw1ip_kvm125.png "/>
Socketpair in advanced IO for interprocess communication and redirection