# C program to demonstrate fork() and pipe()

Write Linux C program to create two processes P1 and P2. P1 takes a string and passes it to P2. P2 concatenates the received string with another string without using string function and sends it back to P1 for printing.

Examples:

```Other string is: forgeeks.org

Input  : www.geeks
Output : www.geeksforgeeks.org

Input :  www.practice.geeks
Output : practice.geeksforgeeks.org
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Explanation:

• To create child process we use fork(). fork() returns :

• <0 fail to create child (new) process
• =0 for child process
• >0 i.e process ID of the child process to the parent process. When >0 parent process will execute.
• pipe() is used for passing information from one process to another. pipe() is unidirectional therefore, for two-way communication between processes, two pipes can be set up, one for each direction.

``` Example:
int fd;
pipe(fd);
fd; //-> for using read end
fd; //-> for using write end
```

Inside Parent Process : We firstly close the reading end of first pipe (fd1) then write the string though writing end of the pipe (fd1). Now parent will wait until child process is finished. After the child process, parent will close the writing end of second pipe(fd2) and read the string through reading end of pipe (fd2).

Inside Child Process : Child reads the first string sent by parent process by closing the writing end of pipe (fd1) and after reading concatenate both string and passes the string to parent process via fd2 pipe and will exit.

 `// C program to demonstrate use of fork() and pipe() ` `#include ` `#include ` `#include ` `#include ` `#include ` `#include ` ` `  `int` `main() ` `{ ` `    ``// We use two pipes ` `    ``// First pipe to send input string from parent ` `    ``// Second pipe to send concatenated string from child ` ` `  `    ``int` `fd1;  ``// Used to store two ends of first pipe ` `    ``int` `fd2;  ``// Used to store two ends of second pipe ` ` `  `    ``char` `fixed_str[] = ``"forgeeks.org"``; ` `    ``char` `input_str; ` `    ``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; ` ` `  `        ``close(fd1);  ``// Close reading end of first pipe ` ` `  `        ``// Write input string and close writing end of first ` `        ``// pipe. ` `        ``write(fd1, input_str, ``strlen``(input_str)+1); ` `        ``close(fd1); ` ` `  `        ``// Wait for child to send a string ` `        ``wait(NULL); ` ` `  `        ``close(fd2); ``// Close writing end of second pipe ` ` `  `        ``// Read string from child, print it and close ` `        ``// reading end. ` `        ``read(fd2, concat_str, 100); ` `        ``printf``(````"Concatenated string %s "````, concat_str); ` `        ``close(fd2); ` `    ``} ` ` `  `    ``// child process ` `    ``else` `    ``{ ` `        ``close(fd1);  ``// Close writing end of first pipe ` ` `  `        ``// Read a string using first pipe ` `        ``char` `concat_str; ` `        ``read(fd1, 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); ` `        ``close(fd2); ` ` `  `        ``// Write concatenated string and close writing end ` `        ``write(fd2, concat_str, ``strlen``(concat_str)+1); ` `        ``close(fd2); ` ` `  `        ``exit``(0); ` `    ``} ` `} `

```Input : www.geeks
Output : Concatenated string
www.geeksforgeeks.org
```

References:

http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/wait.html
http://searchenterpriselinux.techtarget.com/definition/pipe
{"ft_lang_label":"__label__en","ft_lang_prob":0.5322609,"math_prob":0.72078854,"size":2989,"snap":"2021-43-2021-49","text_gpt3_token_len":806,"char_repetition_ratio":0.14941373,"word_repetition_ratio":0.047227927,"special_character_ratio":0.30612245,"punctuation_ratio":0.15449916,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.973122,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-09T09:30:09Z\",\"WARC-Record-ID\":\"<urn:uuid:62ff30c9-f2a8-4d86-b142-8f63522867af>\",\"Content-Length\":\"36706\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e4f6fe71-ed48-4700-a10b-e21f7f5271a7>\",\"WARC-Concurrent-To\":\"<urn:uuid:8ada8923-2312-4b35-8afd-81b4b2bd241d>\",\"WARC-IP-Address\":\"104.21.79.77\",\"WARC-Target-URI\":\"https://tutorialspoint.dev/language/cpp/c-program-demonstrate-fork-and-pipe\",\"WARC-Payload-Digest\":\"sha1:HYLCY3KIVI65SFDPOSHA2SQLFBAHLKZH\",\"WARC-Block-Digest\":\"sha1:2PYWBIVCV3NPPOIQXGQ7FWP5V7C5S2RZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363791.16_warc_CC-MAIN-20211209091917-20211209121917-00245.warc.gz\"}"}