??
字號:
//父進程通過管道向子進程傳送消息,然后子進程再向屏幕打印出所收到的字符串
#include <STDLIB.H>
#include <UNISTD.H>
#include <SYS types.h>
int main(void)
{
int n,
fd[2]; // 本例把fd[0]和fd[1]分別當做讀入管道和寫出管道。
pid_t pid;
char line[1024];
if (pipe(fd) < 0) // error process
exit(0);
pid = fork(); // 開一個進程
if (pid < 0) // 錯誤處理 // 執行fork(),若返回值pid<0,說明fork()執行失敗,失敗原因在errno中。
exit(0);
else if (pid > 0) { /* The code of parent */ // 執行fork(),若返回值pid>0,說明fork()執行成功,得到的值為新建立的子進程代碼(pid)。
close(fd[0]);
write(fd[1], "I am from parent!\n", 19);
}
else { /* The code of child */ // 執行fork(),若返回值pid==0,說明已經新建子進程
close(fd[1]);
n = read(fd[0], line, 1024);
write(STDOUT_FILENO, line, n);
}
exit(0);
}
//父進程通過管道向子進程傳送消息,然后子進程再向屏幕打印出所收到的字符串
#include
#include
#include
int main(void)
{
int n,
fd[2]; // 本例把fd[0]和fd[1]分別當做讀入管道和寫出管道。
pid_t pid;
char line[1024];
if (pipe(fd) < 0) // error process
exit(0);
pid = fork(); // 開一個進程
if (pid < 0) // 錯誤處理 // 執行fork(),若返回值pid<0,說明fork()執行失敗,失敗原因在errno中。
exit(0);
else if (pid > 0) { /* The code of parent */ // 執行fork(),若返回值pid>0,說明fork()執行成功,得到的值為新建立的子進程代碼(pid)。
close(fd[0]);
write(fd[1], "I am from parent!\n", 19);
}
else { /* The code of child */ // 執行fork(),若返回值pid==0,說明已經新建子進程
close(fd[1]);
n = read(fd[0], line, 1024);
write(STDOUT_FILENO, line, n);
}
exit(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -