Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-02-2014, 01:20 AM   #1
LQ Newbie
Registered: Feb 2014
Posts: 13

Rep: Reputation: Disabled
creating a child process in Linux using fork

em trying to evaluate an expression in the child process and print the result from the parent process but even after using pipes em unable to even just print a message sent by child process.
here is what em doing:
#include <iostream>
using namespace std;
int main()
int status,result=0;
int pfds[2]; //a file descriptor
char buf[30]="recieved value from child process"; //buffer for reading and writing
pid_t pid;
pid=fork(); //child's pid is returned to the parent. and child is returned 0 pid
//child process executes

result = ((2+3)-(9*7)/(3+2));
cout<<"Result is:"<<result<<endl;
read(pfds[0], buf, 30);
cout<<"I am a Parent"<<endl;


write(pfds[1],buf, 30);
return 0;
Old 03-03-2014, 10:14 AM   #2
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,890
Blog Entries: 13

Rep: Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934Reputation: 4934
I see that you marked the thread as solved. One thing I like to do if I self-solve is to post my result. In this case, the problem here was that you were waiting for a signal from the child process which would not occur unless that child exited; therefore the parent would never see a signal and end up sending data across the pipe. Another thing to note here is that your buffer of 30 characters was too short for the text string you created, and I'm very surprised that you didn't get a warning about that when you compiled; especially considering that it was C++ and those parsers are typically more strict than gcc.

Here are some minor modifications. I tend to do simple examples using solely C and also didn't do this in Windows, as I suspect you did with the namespace declaration, so sorry it's in C and for Linux. In this case for it works properly. It's style mostly, but I didn't see the need for an else clause since the child will exit and never enter that code portion.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
    int status, result=0;
    int pfds[2]; //a file descriptor
    char buf[64] = "recieved value from child process";
    pid_t pid;

    pid = fork();
    if(pid == -1) {

    if(pid == 0) {
        result = 3;
        printf("Result is: %d\n", result);
        read(pfds[0], buf, sizeof(buf));

    write(pfds[1],buf, sizeof(buf));
    while(wait(&status) != pid) {
    printf("Parent exiting\n");
    return 0;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I fork a foreground child process in a background process? henryyao Linux - Newbie 1 09-06-2013 03:32 PM
fork(), variable values between child and parent process???? dearvivekkumar Programming 6 02-24-2010 05:18 AM
Need solution to "failed to fork PTY" and "error creating the child process" problems Gnusboy Linux - Newbie 4 01-02-2010 07:54 AM
Under which circumstances a child process creates another child process using fork? mitsulas Programming 3 12-08-2009 08:16 AM
creating sequential child processes using fork() BrokenFighter Programming 1 03-06-2007 10:11 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:51 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration