2011年12月2日星期五

linux chapter12

进程间通信:管道
1.popen&pclose
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<memory.h>
#define BUFSIZE 100
int main(){
FILE *read_fp;
char buffer[BUFSIZE+1];
int chars_read;
memset(buffer,'\0',sizeof(buffer));
read_fp=popen("uname -a","r");
if(read_fp!=NULL){
chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);
if(chars_read>0)printf("output was \n%s\n",buffer);
pclose(read_fp);
exit(0);
}
exit(1);
}

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<memory.h>
#define BUFSIZE 100
int main(){
FILE *write_fp;
char buffer[BUFSIZE+1];
sprintf(buffer,"Once upon a time there was....\n");
write_fp=popen("od -c","w");
if(write_fp!=NULL){
fwrite(buffer,sizeof(char),strlen(buffer),write_fp);
pclose(write_fp);
exit(0);
}
exit(1);
}

2.pipe()
在原进程里先创建管道,再fork()一个新进程,则新老进程可以通过管道传递数据
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<memory.h>
int main(){
int data_processed;
int file_pipes[2];
const char some_data[]="123";
char buffer[BUFSIZ+1];
memset(buffer,'\0',sizeof(buffer));
pid_t fork_result;
if(pipe(file_pipes)==0){
fork_result=fork();
if(fork_result==-1){
fprintf(stderr,"fork failed");
exit(1);
}
if(fork_result==0){
data_processed=read(file_pipes[0],buffer,BUFSIZ);
printf("read %d bytes %s\n",data_processed,buffer);
exit(0);
}
else{
data_processed=write(file_pipes[1],some_data,strlen(some_data));
printf("wrote %d bytes\n",data_processed);
}
}
exit(0);
}

3.把管道用作标准输入
 #include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<memory.h>
int main(){
int stat;
int data_processed;
int file_pipes[2];
const char some_data[]="123";
char buffer[BUFSIZ+1];
memset(buffer,'\0',sizeof(buffer));
pid_t fork_result;
if(pipe(file_pipes)==0){
fork_result=fork();
if(fork_result==-1){
fprintf(stderr,"fork failed");
exit(1);
}
if(fork_result==0){
close(0);
dup(file_pipes[0]);
close(file_pipes[0]);
close(file_pipes[1]);
execlp("od","od","-c",(char *)0);
exit(1);
}
else{
close(file_pipes[0]);
data_processed=write(file_pipes[1],some_data,strlen(some_data));
close(file_pipes[1]);
printf("%d wrote %d bytes\n",getpid(),data_processed);
wait(&stat);
exit(0);
}
}
}

4.在无关的进程之间交换数据,用命名管道FIFO
用mkfifo
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<limits.h>
#define FIFO_NAME "/tmp/my_fifo1"
#define BUFFER_SIZE PIPE_BUF
#define TEN_M (1024*1024*10)
int main(){
int pipe_fd;
int open_mode=O_WRONLY;
int bytes_sent=0;
char buffer[BUFFER_SIZE+1];
int res;
//memset(buffer,'\0',sizeof(buffer));

res=mkfifo(FIFO_NAME,0777);
printf("process %d opening FIFO O_WRONLY\n",getpid());
pipe_fd=open(FIFO_NAME,open_mode);
printf("process %d opening result %d\n",getpid(),pipe_fd);
if(pipe_fd!=-1){
while(bytes_sent<TEN_M){
res=write(pipe_fd,buffer,BUFFER_SIZE);
bytes_sent+=res;
}
(void)close(pipe_fd);
}
else exit(1);
printf("process %d finished\n",getpid());
exit(0);
}

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<limits.h>
#define FIFO_NAME "/tmp/my_fifo1"
#define BUFFER_SIZE PIPE_BUF
int main(){
int pipe_fd;
int open_mode=O_RDONLY;
int bytes_read=0;
char buffer[BUFFER_SIZE+1];
int res;
memset(buffer,'\0',sizeof(buffer));
printf("process %d opening FIFO O_RDONLY\n",getpid());
pipe_fd=open(FIFO_NAME,open_mode);
printf("process %d opening result %d\n",getpid(),pipe_fd);
if(pipe_fd!=-1){
do{
res=read(pipe_fd,buffer,BUFFER_SIZE);
bytes_read+=res;
}while(res>0);
(void)close(pipe_fd);
}
else exit(1);
printf("process %d finished, read bytes %d\n",getpid(),bytes_read);
exit(0);
}

没有评论:

发表评论