A dialog program that uses a well-known pipeline to implement a separate process

Source: Internet
Author: User
Tags strcmp

Beginners Linux system programming, some of the problems of the process is not very clear, recently learned the process of communication chapters, the evening and friends to discuss the way QQ communication, but is not very clear. It was not until we saw some of the best bloggers on the Internet that using the parent-child process could solve the problem effectively. So, I try to write two processes using a well-known pipeline can be a dialog program.

Special to share some, looking at Beginners Linux system programming for the mutual encouragement.

Mainly divided into server-side programs and client programs. And they all have a parent-child process. Use two well-known pipes to implement dialog communication.


Server-side Programs

#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <
	sys/stat.h> #include <string.h> #include <fcntl.h> int main (int argc,char *argv[]) {int fd,fd1;
	pid_t Fpid;
	Char buf[20],buf1[20];
	int bytes_read,bytes_write;
int bytes_read1,bytes_write1; /* if (argc<2) {printf ("Enter again.")
		\ n ");
	Exit (9); } */if ((Mkfifo ("Myfifo", 0666)) <0) {printf (creat pipe error!!
		\ n ");
	Exit (1); } if ((Mkfifo ("Yourfifo", 0666)) <0) {printf ("2___creat pipe
		\ n ");
	Exit (2);
} fpid=fork ();
		/* IF ((Fd=open ("Myfifo", O_RDWR)) <0) {printf ("Open pipe error!\n");
	Exit (2);
			* *////////////////* subprocess to receive the information sent by the client process/if (fpid==0) {while (1) {printf (">");
				if ((Fd1=open ("Yourfifo", O_RDWR)) <0) {printf ("Open Yourfifo error!\n");
			Exit (10);
				if (Bytes_read=read (fd1,buf1,sizeof (BUF1)) <0) {printf ("read Yourfifo error!\n");
			Exit (11);
			} buf1[bytes_read]= '; PrintF ("%s\n", BUF1);
		Close (FD1); }/* Parent process, used to send information to client/if (fpid>0) {while (1) {if (Fd=open ("Myfifo", O_RDWR) <0) {printf ("open pip
				e error!\n ");
			Exit (2);
			printf (">");
			scanf ("%s", buf);
			if ((strcmp (buf, "quit")) ==0) {exit (0); }/* If ((Bytes_read=read (fd,buf,sizeof (BUF)) <0) {printf ("read FIFO error!!
				\ n ");
			Exit (2);
				} if (bytes_read>0) {buf[bytes_read]= ' ";
			printf ("%s\n", buf); } */if ((Bytes_write=write (fd,buf,sizeof (BUF)) <0) {printf ("Write Pipe error!!
				\ n ");
			Exit (3);
		Close (FD);
	}//Close (FD);

return 0;
 }

Client programs

#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <
	sys/stat.h> #include <string.h> #include <fcntl.h> int main (int argc,char *argv[]) {int FD;
	int fd1,fpid;
	Char buf[20],buf1[20];
	int bytes_read,bytes_write;
int bytes_read1,bytes_write1; /* if (argc<2) {printf ("Enter again.")
		\ n ");
	Exit (9); }/* If ((Mkfifo ("Yourfifo", 0666)) <0) {printf (creat pipe error!!
		\ n ");
	Exit (1);
		}/* If ((Fd=open ("Myfifo", O_RDWR)) <0) {printf ("Open pipe error!\n");
	Exit (2);
	} */Fpid=fork ();
			/* subprocess, send message to server-side/if (fpid==0) {while (1) {printf (">");
			scanf ("%s", BUF1);
			if ((strcmp (BUF1, "quit")) ==0) {exit (0);
				} if ((Fd1=open ("Yourfifo", O_RDWR)) <0) {printf ("Open Yourfifo error!\n");
			Exit (10);
				if (Bytes_write1=write (fd1,buf1,sizeof (BUF1)) <0) {printf ("Write Yourfifo error!\n");
			Exit (11);
		Close (FD1); }}/* Parent process, receive information sent from server side.
	if (fpid>0) {while (1) {/* printf (">");
			scanf ("%s", buf);
			if ((strcmp (buf, "quit")) ==0) {exit (0);
				} */if ((Fd=open ("Myfifo", O_RDWR)) <0) {printf ("Open pipe error!\n");
			Exit (2); } if (Bytes_read=read (fd,buf,sizeof (BUF)) <0) {printf ("read FIFO error!!
				\ n ");
			Exit (2);
			} buf[bytes_read]= ';
			printf ("%s\n", buf);
Close (FD); /* IF ((Bytes_write=write (Fd,argv[1],strlen (argv[1))) <0) {printf ("Write Pipe error!!
				\ n ");
			Exit (3);
	} *//Close (FD);
	
return 0;
 }



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.