C程序演示fork()和pipe()

fork() 用于创建子进程。此子进程是原始(父)进程的副本。它是在类Unix操作系统上创建进程的主要方法。(见 本文 供参考)。

null

语法:

fork(); // It does not take any parameter, it returns // integer values. It may return negative, // positive or zero integer values.

管道:它 用于Linux中的进程间通信。这是一个系统功能。(见 本文 (供参考)

语法:

int pipe(int pipefd[2]);

演示fork()和pipe()的C程序:

编写LinuxC程序来创建两个进程P1和P2。P1获取一个字符串并将其传递给P2。P2在不使用字符串函数的情况下将接收到的字符串与另一个字符串连接起来,并将其发送回P1进行打印。

例如:

Other string is: forgeeks.orgInput  : www.geeksOutput : www.geeksforgeeks.org        Input :  www.practice.geeksOutput : practice.geeksforgeeks.org

说明:

  • 要创建子进程,我们使用fork()。fork()返回:
    • <0 无法创建子(新)进程
    • =0 子进程
    • >0 i、 将子进程的进程ID转换为父进程。当>0时,将执行父进程。
  • pipe()用于将信息从一个进程传递到另一个进程。pipe()是单向的,因此,对于进程之间的双向通信,可以设置两个管道,每个方向一个。

例子:

int fd[2];pipe(fd);fd[0]; //-> for using read endfd[1]; //-> for using write end

在父进程内部: 我们首先关闭第一个管道的读取端(fd1[0]),然后通过写入管道的读取端(fd1[1])写入字符串。现在家长会 等待 直到子进程完成。在子进程之后,父进程将关闭第二个管道(fd2[1])的写入端,并通过读取管道(fd2[0])的读取端读取字符串。

子进程内部: 子进程通过关闭管道的写入端(fd1[1])读取父进程发送的第一个字符串,读取后连接两个字符串,并通过fd2管道将字符串传递给父进程,然后退出。

输入

www.geeks

C

// C program to demonstrate use of fork() and pipe()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
// We use two pipes
// First pipe to send input string from parent
// Second pipe to send concatenated string from child
int fd1[2]; // Used to store two ends of first pipe
int fd2[2]; // Used to store two ends of second pipe
char fixed_str[] = "forgeeks.org" ;
char input_str[100];
pid_t p;
if (pipe(fd1) == -1) {
fprintf (stderr, "Pipe Failed" );
return 1;
}
if (pipe(fd2) == -1) {
fprintf (stderr, "Pipe Failed" );
return 1;
}
scanf ( "%s" , input_str);
p = fork();
if (p < 0) {
fprintf (stderr, "fork Failed" );
return 1;
}
// Parent process
else if (p > 0) {
char concat_str[100];
close(fd1[0]); // Close reading end of first pipe
// Write input string and close writing end of first
// pipe.
write(fd1[1], input_str, strlen (input_str) + 1);
close(fd1[1]);
// Wait for child to send a string
wait(NULL);
close(fd2[1]); // Close writing end of second pipe
// Read string from child, print it and close
// reading end.
read(fd2[0], concat_str, 100);
printf ( "Concatenated string %s" , concat_str);
close(fd2[0]);
}
// child process
else {
close(fd1[1]); // Close writing end of first pipe
// Read a string using first pipe
char concat_str[100];
read(fd1[0], concat_str, 100);
// Concatenate a fixed string with it
int k = strlen (concat_str);
int i;
for (i = 0; i < strlen (fixed_str); i++)
concat_str[k++] = fixed_str[i];
concat_str[k] = ' ' ; // string ends with ' '
// Close both reading ends
close(fd1[0]);
close(fd2[0]);
// Write concatenated string and close writing end
write(fd2[1], concat_str, strlen (concat_str) + 1);
close(fd2[1]);
exit (0);
}
}


输出:

Concatenated string www.geeksforgeeks.org

本文由 卡提克·阿胡亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享