This site works best with JavaScript enabled
Sign In
Discussions
Activity
Sign In
All
003 Linux System Programming
Issues, queries and suggestions related to Linux, Linux Programming, IPC, Interprocess Communication, Synchronization, Semaphore, System Programming, Linux Software Development.
»
003.05.01.Inter-Process-Communication. Pipes
003.05.01.Inter-Process-Communication. Pipes
3. Parent to Child Communication
pravjot
June 4
3.
Parent to Child Communication
Write a program where the parent process writes a message to the pipe, and the child process reads the message from the pipe and prints it.
gaurav133232
June 28
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int pipefd[2];
int data;
char buf[BUFSIZ+1];
if(pipe(pipefd)==0)
{
pid_t ret = fork();
if(ret == (pid_t)-1)
{
perror("fork");
}
else if(ret == 0)
{
data = read(pipefd[0],buf,sizeof(buf));
printf("Child process having pid:%d",getpid());
printf("Read %d bytes:%s\n",data,buf);
exit(EXIT_SUCCESS);
}
else
{
printf("Parent process having pid:%d",getpid());
data = write(pipefd[1],"Hello world\n",14);
printf("wrote %d bytes:%s\n",data,buf);
exit(EXIT_SUCCESS);
}
printf("pipe created succesfully.\n");
exit(EXIT_SUCCESS);
}
else if(pipe(pipefd)!=0)
{
perror("pipe");
exit(EXIT_FAILURE);
}
return 0;
}
raj.kiran
July 23
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
2 int main()
3 {
4 int *pipes;
5 int ret,fret,wret;
6 char wbuff[]="Live Let Live",rbuff[50];
7 printf("FILE: %s -> %s:Begins\n",__FILE__,__func__);
8 pipes=(int*)malloc(sizeof(int)*2);
9 if(!pipe)
10 {
11 perror("malloc");
12 exit(EXIT_FAILURE);
13 }
14 ret=pipe(pipes);
15 if(ret==-1)
16 {
17 perror("pipe");
18 exit(EXIT_FAILURE);
19 }
20 printf("Pipe created successfully\n");
21 fret=fork();
22 if(fret==-1)
23 {
24 perror("fork");
25 exit(EXIT_FAILURE);
26 }
27 else if(fret==0)
28 {
29 printf("This is child process with pid:%d\n",getpid());
30 wret=read(*(pipes),rbuff,50);
31 if(wret==-1)
32 {
33 perror("write");
34 exit(EXIT_FAILURE);
35 }
36 printf("Read successfully from the pipe:%s\n",rbuff);
37 }
38 else
39 {
40 printf("This is Parent process with pid:%d\n",getpid());
41 wret=write(*(pipes+1),wbuff,strlen(wbuff));
42 if(wret==-1)
43 {
44 perror("write");
45 exit(EXIT_FAILURE);
46 }
47 printf("String:%s, successfully written into the pipe\n",wbuff);
48 }
49 printf("FILE: %s -> %s:Ends\n",__FILE__,__func__);
50 return 0;
51 }
bhatnagarharsh37
July 26
#include"headers.h"
2 #include"declarations.h"
3
4 int main()
5 {
6 int fret,wret;
7 int *pret;
8 char r;
9 char buff[]="she sells she shell on the she shore";
10 char rbuf[50];
11 printf("%s: __BEGIN__\n", __func__);
12
13 pret=createpipe();
14 fret=fork();
15 switch(fret)
16 {
17 case -1:
18 perror("fork");
19 exit(EXIT_FAILURE);
20 case 0:
21 printf("hello i am child\n");
22 printf("pipe rfd:%d\n",*(pret+0));
23 printf("pipe wfd:%d\n",*(pret+1));
24 memset(rbuf,'\0',50);
25 r=read(*(pret+0),rbuf,50);
26 if(!r)
27 {
28 perror("read");
29 exit(EXIT_FAILURE);
30 }
31 printf("read by child:%s\n",rbuf);
32 break;
33 default:
34 printf("hello i am parent\n");
35 printf("pipe rfd:%d\n",*(pret+0));
36 printf("pipe wfd:%d\n",*(pret+1));
37 wret=write(*(pret+1),buff,strlen(buff));
38 if(!wret)
39 {
40 perror("write");
41 exit(EXIT_FAILURE);
42 }
43
44 break;
45 }
46
47
48 printf("%s: __END__\n", __func__);
49 return 0;
50 }
abhinandan2971verma
July 26
main.c
#include"../common/headers.h"
#include"declaration.h"
int main()
{
int fret,ret;
int *pipe;
char buff[]="hello to world of programming ";
printf("%s__begin__\n",__func__);
pipe=creat_pipe();
printf("pipe read file descriptor===%d\n",*(pipe+0));
printf("pipe write file descriptor===%d\n",*(pipe+1));
switch(fret)
fret=fork();
{
case -1:
perror("fork");
free(pipe);
exit(EXIT_FAILURE);
case 0:
printf("this is child process \n");
printf("pid==%d \n",getpid());
ret=read(*(pipe+0),buff,50);
if(ret==-1)
{
perror("read");
free(pipe);
exit(EXIT_FAILURE);
}
printf("read %d bytes from parent process \n",ret);
printf("contents are %s\n",buff);
break;
abhinandan2971verma
July 26
main(continue)
default:
printf("this is parent process \n");
printf("pid==%d \n",getpid());
ret=write(*(pipe+1),buff,strlen(buff));
if(ret==-1)
{
perror("write");
free(pipe);
exit(EXIT_FAILURE);
}
printf("write %d bytes to child process \n",ret);
break;
}
p
rintf("%s__end__\n",__func__);
return 0;
}
abhinandan2971verma
July 26
creat_pipe.c
int* creat_pipe()
{
int ret;
int *arr;
printf("%s__begin__\n",__func__);
arr=(int*)malloc(sizeof(int)*2);
if(!arr)
{
perror("malloc");
exit(EXIT_FAILURE);
}
if(pipe(arr)==0)
printf("PIPE CREATED SUCCESFULLY \n");
else
{
perror("pipe");
free(arr);
exit(EXIT_FAILURE);
}
printf("%s__end__\n",__func__);
return arr;
}
Add a Comment
Powered by
Vanilla
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
0
000 Linux System Administration
25
001 C Programming, Data Structures
101
» 001.01.Introduction to C
1
» 001.16.Structures
21
002 OOPs using C++ with Eclipse on Linux
23
» 002.06.Streams and File Processing
1
003 Linux System Programming
375
» 003.01.Processes-and-Resources-Utilization
26
» 003.01.91.Interview-Questions-Introductory-Concepts
20
» 003.01.92.Interview-Questions-Introductory-Concepts
5
» 003.02.Process-Management. Introduction
21
» 003.02.91.Interview Questions. Intro-to-Processes
20
» 003.03.Process-Management - Process Duplication
91
» 003.03.92.Interview-Questions-Process-Duplication
20
» 003.03.93.Interview-Questions-Process-Duplication
20
» 003.03.94.Interview-Questions-Process-Duplication
20
» 003.03.95.Interview-Questions-Process-Duplication
20
» 003.03.96.Interview-Questions-Process-Duplication
10
» 003.04.Process-Management - Process Replacement
67
» 003.04.81.Assignment. Process Replacement
24
» 003.04.91.Interview-Questions-Process-Replacement
20
» 003.04.92.Interview-Questions-Process-Replacement
22
» 003.05.01.Inter-Process-Communication. Pipes
22
» 003.06.Inter Process Communication. FIFOs
21
» 003.07.Signals and Handlers
43
» 003.07.91.Assignment. Signals and Handlers
20
» 003.07.92.Assignment. Signals and Handlers
22
» 003.08.Inter Process Communication Message Queues
1
» 003.09.Inter Process Communication. Shared Memory
1
» 003.10.Synchronization Techniques. Semaphore
21
» 003.11.POSIX-Threads
21
004 Linux Network Programming
34
» 004.01.Introduction to Networks and Configurations
1
» 004.02.Networking Basics
1
» 004.03.Introduction-to-Sockets
21
» 004.04.Linux Network stack
1
» 004.05.Transmission Control Protocol
1
005 Character Device Drivers Development
32
» 005.01.Introduction To Device Drivers
11
006.Project Evolution with GitLab
2
» 01.Introduction To GitLab
1
008 Block Device Driver Development
4
» 008.01.Introduction to Block Device Drivers
1
009 Embedded Linux-ARM. Storage
91
» 009.01.Linux Boot Process
28
» 009.01.14.Linux-Boot-Process
5
» 009.01.15.Introduction to BIOS
0
» 009.01.16.Introduction-to-BIOS-IQs
5
» 009.01.18.Introduction-to-BIOS-IQs
5
» 009.01.20.BIOS CMOS UEFI. IQs
5
» 009.02.Introduction To Embedded Linux
1
» 009.03.01.ARM Processor Architecture
1
» 009.03.02.Programmers Model
1
» 009.04.Boot Loaders
50
» 009.05.Understanding-ARM-Board-Bringup
1
» 009.06-Board Bringup Raspberry Pi
1
» 009.06.Board Bringup. Raspberry Pi4
1
010 Embedded Linux ARM, Configuring and Porting using Storage
0
011 Shell Scripting using Bash
90
» 011.01.Introduction to Shells and Shell Scripts
1
» 011.02.Basics of Shell Scripting - Bash
1
» 011.03.Conditions and Branching
23
» 011.04.Loops and Iterations/
31
» 011.04.81.Assignment. Branching and Looping
10
» 011.05.Reserved-Words-Bullitin-Commands-Command-Line-Parsing
1
» 011.06.Parameters and Variables
1
» 011.07.Structured Scripting. Functions
11
» 011.08.Arrays-Strings-in-bash
11
» 011.09.File Operations and Commands
1
» 011.10.Writing-Manual-Pages
1
» 011.11.Makefile
1
» 011.12.sed-awk
1
012 Linux Kernel Architecture and Internals
3
014. Linux Network Administration
296
» 014.01.Intro to NW and Configurations
220
» 014.01.12.Introduction to Networking
10
» 014.01.14.Packets-IQs
10
» 014.01.16.NetworkLayers
10
» 014.01.18.The Internet Layer
20
» 014.01.20.routes and the kernel routing table
10
» 014.01.22.The Default Gateway
5
» 014.01.24.IPv6 Addresses And Networks
20
» 014.01.26.Basic ICMP And DNS Tools
10
» 014.01.28.The Physical Layer And Ethernet
5
» 014.01.30.Understanding Linux Network Interface
5
» 014.01.32.Intro To Network Interface Configuration
5
» 014.01.34.Boot Activated Network Configuration
5
» 014.01.36.Manual and Boot Activated Nw Config
5
» 014.01.38.Network Configuration Managers
10
» 014.01.40.Resolving Hostnames
19
» 014.01.42.The Transport Layer TCP UDP Services.
10
» 014.01.44.Understanding DHCP
5
» 014.01.46.Automatic IPv6 Network Configuration. IQs
5
» 014.01.48.Configuring Linux as Router
5
» 014.01.50.Private Networks IPv4
5
» 014.01.52.Network Address Translation. IP Masquerading
5
» 014.01.54.Routers And Linux
5
» 014.01.56.Linux Firewall Basics
5
» 014.01.58.Setting Firewall Rules
5
» 014.01.60.Firewall Strategies
10
» 014.01.62.Ethernet-IP-ARP-NDP-IQs
5
» 014.01.64.Wireless Ethernet
5
» 014.02.Network Applications and Services
11
» 014.02.12.Basics of Services
3
» 014.02.14.Introduction to Network Servers
2
» 014.02.16.Network Servers. Secure Shell
5
» 014.06.Network Protocol Telnet
38
» 014.06.12.Introduction-To-Telnet
3
» 014.06.14.General Working. Telnet
5
» 014.06.16.General-Working-Telnet
5
» 014.06.18.Network Virtual Terminal
3
» 014.06.20.More About Telnet
3
» 014.06.22.Installing Telnet on Fedora
5
» 014.06.24.Telnet Commands-Fedora
3
» 014.06.26.Using Telnet in Linux
5
» 014.06.28.Secure telnet with FirewallD. Fedora.
3
» 014.06.30.Using Telnet in Linux
2
» 014.06.DHCP. A network management protocol
26
015 Python with Eclipse on Linux
27
025.Rust Programming
3
» 025.03.Control Structures. Conditional Statements
0
101 Advanced Data Structures using C
18
104.Mastering Linux Network Stack
1
» 104.01.Linux Network Stack User-Space
1
105 Parallel Port Device Drivers Development
5
205 Serial Port Device Drivers Development
3
303 Linux System Programming
32
Project 22. Ethernet Network Device Driver Development
0
Query
557
Project.203 Linux System Programming. MySQL
0
Pravjot Sir Classes
161
Events at EmbLogic
2
Project 16: SPI Device Driver Development
2
Project 17: I2C Device Driver Development
0
Project 18: PCI Device Driver Development
0
Project 19: Embedded Linux on ARM Using Network TFTP
0
Project 20: CAN Bus Protocol and Driver Development
0
Project 21: USB Device Drivers Development
0
Embedded Linux
0
ARM Embedded Processor
0
Training
2
Members Area
1
Word From Admin
1