(linux的进程间通信)
Linux下進程的創建與進程間通信?
代碼示例:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define READ_TERMINAL 0
#define WRITE_TERMINAL 1
int main() {
int file_descriptors;
pid_t pid_f;
char PipeBuf={‘a’,‘0’};
int read_ret=0;
pipe(file_descriptors);
pid_f=fork();
if (pid_f<0)
{
printf(“fork error!n”);
exit(1);
}
else if (pid_f==0)
{
//子進程向父進程發一則消息
printf(“Write in Pipe To FatherProcess!n”);
close(file_descriptors);
sleep(1);
write(file_descriptors,“Child Send”,sizeof(“Child Send”));
//open(file_descriptors);
}
else
{
//父進程接收(讀取)消息
printf(“Read in Pipe From ChildProcess!n”);
//通過fcntl()修改為使得讀管道數據具有非阻塞的特性
int flag=fcntl(file_descriptors,F_GETFL,0);
flag |= O_NONBLOCK;
if(fcntl(file_descriptors,F_SETFL,flag) < 0){
perror(“fcntl”);
exit(1);
}
close(file_descriptors);
read_ret=read(file_descriptors,PipeBuf,sizeof(PipeBuf));//沒阻塞的讀
printf(“Read Message are : %sn”,PipeBuf);
總結
以上是生活随笔為你收集整理的(linux的进程间通信)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 嘉兴购房备案流程(嘉兴购房备案)
- 下一篇: 三星安卓平板强制横屏(三星安卓平板)
