zoukankan      html  css  js  c++  java
  • 命名管道--生产者

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    #define FIFO_NAME "/tmp/my_fifo"
    #define BUFFER_SIZE PIPE_BUF
    #define TEN_MEG		(1024 * 1024 * 10)
    
    int main(int argc, char *argv[]) {
    	int pipe_fd;
    	int res;
    	int open_mode = O_WRONLY;
    	int bytes_sent = 0;
    	char buffer[BUFFER_SIZE + 1];
    	
    	if(access(FIFO_NAME, F_OK) == -1) {
    		res = mkfifo(FIFO_NAME, 0777);
    		if(res != 0) {
    			fprintf(stderr, "Could not create fifo %s
    ", FIFO_NAME);
    			exit(EXIT_FAILURE);
    		}
    	}
    
    	printf("Process %d opening FIFO O_WRONLY
    ", getpid());
    	pipe_fd = open(FIFO_NAME, open_mode);
    	printf("Process %d result %d
    ", getpid, pipe_fd);
    	
    	if(pipe_fd != -1) {
    		while(bytes_sent <	TEN_MEG) {
    			res = write(pipe_fd, buffer, BUFFER_SIZE);
    			if(res == -1) {
    				fprintf(stderr, "Write error on pipe
    ");
    				exit(EXIT_FAILURE);
    			}
    			bytes_sent += res;
    		}
    		(void)close(pipe_fd);
    	} else {
    		exit(EXIT_FAILURE);
    	}
    	printf("Process %d finished
    ", getpid());
    	exit(EXIT_SUCCESS);
    }


  • 相关阅读:
    hadoop hdfs总结 NameNode部分 概述
    最近近况
    hadoop hdfs总结 NameNode部分 1
    rsync 使用
    SmartHost
    hadoop unit test 问题
    git 使用记录
    java 类内存分配计算
    hadoop hdfs总结 NameNode部分 2
    0417 430调试技巧
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3196623.html
Copyright © 2011-2022 走看看