IPC Pipe sample code
This commit is contained in:
36
ipcPipe.c
Normal file
36
ipcPipe.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define BUFFER_SIZE 25
|
||||
#define READ_END 0
|
||||
#define WRITE_END 1
|
||||
|
||||
int main(){
|
||||
char write_msg[BUFFER_SIZE] = "Foxliver";
|
||||
char read_msg[BUFFER_SIZE];
|
||||
int fd[2];
|
||||
pid_t pid;
|
||||
|
||||
pipe(fd);
|
||||
pid = fork();
|
||||
|
||||
if(pid > 0){
|
||||
close(fd[READ_END]);
|
||||
write(fd[WRITE_END], write_msg, strlen(write_msg) + 1);
|
||||
close(fd[WRITE_END]);
|
||||
printf("Parent process\n");
|
||||
}else if(pid == 0){
|
||||
close(fd[WRITE_END]);
|
||||
|
||||
printf("Child process\n");
|
||||
|
||||
// pipe message read
|
||||
read(fd[READ_END], read_msg, BUFFER_SIZE);
|
||||
printf("Read : %s\n", read_msg);
|
||||
close(fd[READ_END]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user