Home Database Mysql Tutorial 2.进程间的管道通信

2.进程间的管道通信

Jun 07, 2016 pm 03:48 PM
about Simple pipeline write process communication

(一) 编写一个关于进程通信的简单程序,子进程送一串消息给父进程,父进程收到消息后把它显示出来。 要求: 两个子进程分别向管道写一句话: Child process 1 is sending a message! Child process 2 is sending a message! 而父进程则从管道中读出来自两

(一) 编写一个关于进程通信的简单程序,子进程送一串消息给父进程,父进程收到消息后把它显示出来。

要求:

两个子进程分别向管道写一句话:
Child process 1 is sending a message!

Child process 2 is sending a message!

而父进程则从管道中读出来自两个子进程的消息,显示屏幕上,且父进程要先接收子进程1的消息,在接受子进程2的消息。

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#define SIZE 50
int main()
{
	int p1, p2;
	int fd[2];
	char str[50];
	while((pipe(fd)) == -1);
	while((p1 = fork()) == -1);
	if(p1)
	{
		while((p2 = fork()) == -1);
		if(p2)
		{
			//parent
			wait(0);
			read(fd[0], str, SIZE);
			printf("%s\n", str);
			wait(0);
			read(fd[0], str, SIZE);
			printf("%s\n", str);
		}
		else
		{
			//child2
			lockf(fd[1], 1, 0);
			sleep(2);
			strcpy(str, "Child process 2 is sending messages!\n");
			write(fd[1], str, SIZE);
			lockf(fd[1], 0, 0);
		}
	}
	else
	{
		//child1
		lockf(fd[1], 1, 0);
		sleep(2);
		strcpy(str, "Child process 1 is sending messages!\n");
		write(fd[1], str, SIZE);
		//sleep(2);
		lockf(fd[1], 0, 0);

	}
}</unistd.h></string.h></stdio.h>
Copy after login

(二)父进程等待用户从控制台(键盘)输入字符串,通过管道传给子进程;子进程收到后,对字符串进行大小写转换后,输出到标准输出(显示器)。
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#define SIZE 50
int main()
{
	int p, fd[2];
	int i = 0;
	char str[SIZE];
	while((pipe(fd)) == -1);
	while((p = fork()) == -1);
	if(p)
	{
		//parent
		lockf(fd[1], 1, 0);
		printf("Please input the string:");
		scanf("%s", str);
		write(fd[1], str, SIZE);
		lockf(fd[1], 0, 0);
		wait(0);

		read(fd[0], str, SIZE);
		printf("reversed string is : %s\n", str);
	}
	else
	{
		read(fd[0], str, SIZE);
		while(str[i] != '\0')
		{
			if('A' <br>
<br>



</unistd.h></string.h></stdio.h>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
New generation of optical fiber broadband technology - 50G PON New generation of optical fiber broadband technology - 50G PON Apr 20, 2024 pm 09:22 PM

In the previous article (link), Xiao Zaojun introduced the development history of broadband technology from ISDN, xDSL to 10GPON. Today, let’s talk about the upcoming new generation of optical fiber broadband technology-50GPON. █F5G and F5G-A Before introducing 50GPON, let’s talk about F5G and F5G-A. In February 2020, ETSI (European Telecommunications Standards Institute) promoted a fixed communication network technology system based on 10GPON+FTTR, Wi-Fi6, 200G optical transmission/aggregation, OXC and other technologies, and named it F5G. That is, the fifth generation fixed network communication technology (The5thgenerationFixednetworks). F5G is a fixed network

Detailed explanation of Linux process priority adjustment method Detailed explanation of Linux process priority adjustment method Mar 15, 2024 am 08:39 AM

Detailed explanation of the Linux process priority adjustment method. In the Linux system, the priority of a process determines its execution order and resource allocation in the system. Reasonably adjusting the priority of the process can improve the performance and efficiency of the system. This article will introduce in detail how to adjust the priority of the process in Linux and provide specific code examples. 1. Overview of process priority In the Linux system, each process has a priority associated with it. The priority range is generally -20 to 19, where -20 represents the highest priority and 19 represents

How to use pipes to read and write files in Golang? How to use pipes to read and write files in Golang? Jun 04, 2024 am 10:22 AM

File reading and writing through pipes: Create a pipe to read data from the file and pass it through the pipe Receive the data from the pipe and process it Write the processed data to the file Use goroutines to perform these operations concurrently to improve performance

The development history of wireless mice The development history of wireless mice Jun 12, 2024 pm 08:52 PM

Original title: "How does a wireless mouse become wireless?" 》Wireless mice have gradually become a standard feature of today’s office computers. From now on, we no longer have to drag long cords around. But, how does a wireless mouse work? Today we will learn about the development history of the No.1 wireless mouse. Did you know that the wireless mouse is now 40 years old? In 1984, Logitech developed the world's first wireless mouse, but this wireless mouse used infrared as a The signal carrier is said to look like the picture below, but later failed due to performance reasons. It was not until ten years later in 1994 that Logitech finally successfully developed a wireless mouse that works at 27MHz. This 27MHz frequency also became the wireless mouse for a long time.

A brief history of broadband Internet technology A brief history of broadband Internet technology Apr 16, 2024 am 09:00 AM

In today's digital age, broadband has become a necessity for each of us and every family. Without it, we would be restless and restless. So, do you know the technical principles behind broadband? From the earliest 56k "cat" dial-up to the current Gigabit cities and Gigabit homes, what kind of changes has our broadband technology experienced? In today’s article, let’s take a closer look at the “Broadband Story”. Have you seen this interface between █xDSL and ISDN? I believe that many friends born in the 70s and 80s must have seen it and are very familiar with it. That's right, this was the interface for "dial-up" when we first came into contact with the Internet. That was more than 20 years ago, when Xiao Zaojun was still in college. In order to surf the Internet, I

Why do processes in Linux sleep? Why do processes in Linux sleep? Mar 20, 2024 pm 02:09 PM

Why do processes in Linux sleep? In the Linux operating system, a process can become dormant due to a number of different reasons and conditions. When a process is in a dormant state, it means that the process is temporarily suspended and cannot continue execution until certain conditions are met before it can be awakened to continue execution. Next, we will introduce in detail several common situations when a process enters hibernation in Linux, and illustrate them with specific code examples. Waiting for I/O to complete: When a process initiates an I/O operation (such as reading

Methods and techniques for implementing Socket communication in PHP Methods and techniques for implementing Socket communication in PHP Mar 07, 2024 pm 02:06 PM

PHP is a commonly used development language that can be used to develop various web applications. In addition to common HTTP requests and responses, PHP also supports network communication through Sockets to achieve more flexible and efficient data interaction. This article will introduce the methods and techniques of how to implement Socket communication in PHP, and attach specific code examples. What is Socket Communication Socket is a method of communication in a network that can transfer data between different computers. by S

How to detect if there is no PHP process in Linux system How to detect if there is no PHP process in Linux system Mar 16, 2024 am 11:42 AM

&quot;Detection method of no PHP process in Linux system, specific code examples are required&quot; When using Linux system for web development, we often rely on PHP process to handle dynamic pages and logic, and sometimes we may need to monitor whether there is a PHP process on the server. This article will introduce a method to detect whether there is a PHP process in a Linux system and give specific code examples. Why is it necessary to detect the PHP process? In web development, the PHP process plays a vital role. It is responsible for parsing and executing PHP processes.

See all articles