C Language for pipeline communication

Source: Internet
Author: User

MKFIFO.C file

1#include <sys/types.h>2#include <sys/stat.h>3#include <stdio.h>4#include <errno.h>5 6 intMain ()7 {8     //int Mkfifo (const char *pathname, mode_t mode);9 Ten     intRet=mkfifo ("./test",0777); One     if(ret<0) A     { -         if(errno==eexist) -         { theprintf"Create error errno=%d\n", errno); -             return-1; -         } -     } +}

Link.h file

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <unistd.h>5typedefstructStudent6 {7     intID;8     Charname[ -];9     structStudent *Next;Ten}stu,*Pstu; One  A pstu Add_node (Pstu head); - voidShow_link (Pstu head); - voidWrite_link (Pstu Head,intFD); thePstu Read_link (intFD);

LINK.C file

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include"link.h"5 6 pstu Add_node (pstu head)7 {8Pstu temp= (PSTU)malloc(sizeof(STU));9printf"Please input ID:");Tenscanf"%d",&temp->ID); Oneprintf"Please input name:"); Ascanf"%s",temp->name); -temp->next=head; -     returntemp; the } -  - voidShow_link (pstu head) - { +printf"id\tname\n"); -      while(head!=NULL) +     { Aprintf"%d\t%s\n",head->id,head->name); atHead=head->Next; -     } - } -Pstu Read_link (intFD) - { -     intret; inPstu temp,head=NULL; -      while(1) to     { +temp= (PSTU)malloc(sizeof(STU)); -Ret=read (Fd,temp,sizeof(STU)); the         if(ret<0) *         { $printf"Read error\n");Panax Notoginseng             returnNULL; -         } the         Else if(ret==0) +             returnhead; A         Else the         { +temp->next=head; -Head=temp; $         } $     } - } - voidWrite_link (Pstu Head,intFD) the { -      while(head!=NULL)Wuyi     { theWrite (Fd,head,sizeof(STU)); -Head=head->Next; Wu     } -}

WRITE.C file

1#include <stdio.h>2#include <unistd.h>3#include <sys/types.h>4#include <sys/stat.h>5#include <fcntl.h>6#include <string.h>7#include"link.h"8 intMain ()9 {Ten     intFD; OneFd=open ("./test", o_wronly); A     if(fd<0) -     { -printf"Open filed\n"); the         return-1; -     } -Pstu head=NULL; -      for(intI=0;i<5; i++) +     { -Head=Add_node (head); +     } A Show_link (head); at  - Write_link (HEAD,FD); - Close (FD); -}

READ.C file

1#include <stdio.h>2#include <unistd.h>3#include <sys/types.h>4#include <sys/stat.h>5#include <fcntl.h>6#include"link.h"7 8 intMain ()9 {Ten     intFD; OneFd=open ("./test", o_rdonly); A     if(fd<0) -     { -printf"Open failed\n"); the         return-1; -     } -  -Pstu head=Read_link (FD); + Show_link (head); -  + Close (FD); A}

GCC mkfifo.c

./aou.t Generate Test

GCC link.c write.c-o Write

GCC link.c Read.c-o Read

Run read and write with effects such as:

C Language for pipeline communication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.