LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-02-2014, 01:20 AM   #1
mehreen124
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>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<stdlib.h>
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;
pipe(pfds);
pid=fork(); //child's pid is returned to the parent. and child is returned 0 pid
if(pid==-1){
cout<<"error";
}
//child process executes

if(pid==0)
{
result = ((2+3)-(9*7)/(3+2));
cout<<"Result is:"<<result<<endl;
close(pfds[1]);
read(pfds[0], buf, 30);
exit(0);
}
else
{
while(wait(&status)!=pid)
{
cout<<"I am a Parent"<<endl;

close(pfds[0]);

write(pfds[1],buf, 30);
}
}
return 0;
}
 
Old 03-03-2014, 10:14 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,265
Blog Entries: 12

Rep: Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862
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.
Code:
#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;

    pipe(pfds);
    pid = fork();
    if(pid == -1) {
        printf("error");
        return;
    }

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

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


Reply


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


All times are GMT -5. The time now is 10:01 PM.

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