1. 进程间通信之pipe
2.单向通道
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
//child--->pipe-->parent
-
int main ( int argc, char *argv[] )
-
{
-
int fd[2];
-
pid_t pid;
-
char buf[1024];
-
if(pipe(fd) < 0)
-
{
-
perror("pipe error\n");
-
return -1;
-
}
-
-
printf("next fork\n");
-
pid = fork();
-
if(pid<0)
-
{
-
perror("fork error\n");
-
return -1;
-
} else if(pid==0) //child
-
{
-
close(fd[0]); //close read
-
//write data to parent
-
write(fd[1], "hello,world\n", strlen("hello,world\n")+1);
-
} else if(pid > 0) //parent
-
{
-
close(fd[1]); //close write
-
//read data frome child
-
read(fd[0], buf, sizeof(buf));
-
printf("recv_buf=%s", buf);
-
}
-
return EXIT_SUCCESS;
-
}
2.1 模拟僵死的子进程
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <signal.h>
-
-
//typedef void (*sighandler_t)(int);
-
//sighandler_t signal(int signum, sighandler_t handler);
-
-
//child--->pipe-->parent
-
pid_t child_pid;
-
-
void sig_child_process(int signo)
-
{
-
waitpid(child_pid, NULL, 0);
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int fd[2];
-
char buf[1024];
-
//signal(SIGCHLD, sig_child_process); //不打开这个,即不等侍child进程的pid,则子进程会出现死锁
-
if(pipe(fd) < 0)
-
{
-
perror("pipe error\n");
-
return -1;
-
}
-
-
printf("next fork\n");
-
child_pid = fork();
-
if(child_pid<0)
-
{
-
perror("fork error\n");
-
return -1;
-
}else if(child_pid==0) //child
-
{
-
close(fd[0]); //close read
-
sleep(3);
-
//write data to parent
-
write(fd[1], "hello,world\n", strlen("hello,world\n")+1); //write完子进程就退出了,但是父进程没有等侍子进程的退出信号
-
}else if(child_pid > 0) //parent //此时子进程处于僵死状态
-
{
-
close(fd[1]); //close write
-
//read data frome child
-
read(fd[0], buf, sizeof(buf));
-
printf("recv_buf=%s", buf);
-
sleep(5);
-
}
-
return EXIT_SUCCESS;
-
}
3.双向通道
流程如下:
cwrite-->pread-->pwrite-->cread-->cexit
同时pwait ----> pwait_over
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
//child--->pipe-->parent
-
//child<---pipe<--parent
-
void parent_process(int readfd, int writefd);
-
void child_process(int readfd, int writefd);
-
-
int main ( int argc, char *argv[] )
-
{
-
int pipe1[2];
-
int pipe2[2];
-
pid_t pid;
-
char buf[1024];
-
if(pipe(pipe1) < 0)
-
{
-
perror("pipe1 error\n");
-
return -1;
-
}
-
if(pipe(pipe2) < 0)
-
{
-
perror("pipe2 error\n");
-
return -1;
-
}
-
-
printf("next fork\n");
-
if( (pid = fork()) < 0)
-
{
-
perror("fork error\n");
-
return -1;
-
} else if(pid==0) //child
-
{
-
printf("child\n");
-
close(pipe1[1]); //close pipe1 write
-
close(pipe2[0]); //close pipe2 read
-
//write data to parent
-
child_process(pipe1[0], pipe2[1]);
-
} else if(pid > 0) //parent
-
{
-
printf("parent\n");
-
close(pipe1[0]); //close pipe1 read
-
close(pipe2[1]); //close pipe2 write
-
//read data frome child
-
parent_process(pipe2[0],pipe1[1]);
-
printf("next waitpid\n");
-
waitpid(pid, NULL, 0);
-
printf("waitpid over\n");
-
}
-
return EXIT_SUCCESS;
-
}
-
-
void parent_process(int readfd, int writefd)
-
{
-
char buf[1024];
-
printf("next parent read\n");
-
read(readfd, buf, sizeof(buf));
-
printf("parent read_buf=%s",buf);
-
-
sleep(1);
-
write(writefd, "hello,world222\n",strlen("hello,world222\n")+1);
-
}
-
-
void child_process(int readfd, int writefd)
-
{
-
char buf[1024];
-
sleep(1);
-
printf("next child write\n");
-
write(writefd, "hello,world111\n",strlen("hello,world111\n")+1);
-
-
printf("next child read\n");
-
read(readfd, buf, sizeof(buf));
-
printf("child read_buf=%s",buf);
-
}
4.代码打包
pipe.rar(下载后改名为pipe.tar.gz)
5. socketpair
linux提供了另外一种实现双向管道的方法socketpair
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <sys/types.h> /* See NOTES */
-
#include <sys/socket.h>
-
-
//child--->pipe-->parent
-
//child<---pipe<--parent
-
void parent_process(int readfd, int writefd);
-
void child_process(int readfd, int writefd);
-
-
int main ( int argc, char *argv[] )
-
{
-
int sockfd[2];
-
pid_t pid;
-
char buf[1024];
-
if((socketpair(AF_LOCAL,SOCK_STREAM,0, sockfd)) < 0)
-
{
-
perror("socketpair error\n");
-
return -1;
-
}
-
-
printf("next fork\n");
-
if( (pid = fork()) < 0)
-
{
-
perror("fork error\n");
-
return -1;
-
} else if(pid==0) //child
-
{
-
printf("child\n");
-
close(sockfd[1]);
-
child_process(sockfd[0], sockfd[0]); //既可读也可以写
-
} else if(pid > 0) //parent
-
{
-
printf("parent\n");
-
close(sockfd[0]);
-
//read data frome child
-
parent_process(sockfd[1], sockfd[1]); //既可读也可以写
-
printf("next waitpid\n");
-
waitpid(pid, NULL, 0);
-
printf("waitpid over\n");
-
}
-
return EXIT_SUCCESS;
-
}
-
-
void parent_process(int readfd, int writefd)
-
{
-
char buf[1024];
-
printf("next parent read\n");
-
read(readfd, buf, sizeof(buf));
-
printf("parent read_buf=%s",buf);
-
-
sleep(1);
-
write(writefd, "hello,world222\n",strlen("hello,world222\n")+1);
-
}
-
-
void child_process(int readfd, int writefd)
-
{
-
char buf[1024];
-
sleep(1);
-
printf("next child write\n");
-
write(writefd, "hello,world111\n",strlen("hello,world111\n")+1);
-
-
printf("next child read\n");
-
read(readfd, buf, sizeof(buf));
-
printf("child read_buf=%s",buf);
-
}
socketpair更简洁明了
阅读(1561) | 评论(0) | 转发(0) |