shmget(); shmat();//assign "0" to it socket(); bind(); listenfd = listen(); for ( ; ; ) { sockaccepted = accept(); sockconnected = connect_to_thirdparty(); if ( (ret = fork()) == 0) { close(listenfd); rcvproc(sockconnected,sockaccepted); exit(0); } else if(ret >0) { close(sockaccepted); close(sockconnected); } else { exit(0); } if ( (ret = fork()) == 0) { close(listenfd); rcvproc(sockconnected,sockaccepted); exit(0); } else if(ret < 0) { exit(0); } } int rcvproc() { for ( ; ; ) { read(sockaccepted); send(sockconnected); } } int sndproc() { for ( ; ; ) { shmat();//这里连接父进程建立的共享内存并判断其内容是否为 "0"如果不是则循环判断直到其值为 "0" // 共享内存如果值 为 "0" 重新赋值为 "1" read(sockconnected); send(sockaccepted); //共享内存重新赋值为 "0" } } |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |