Results 1 to 3 of 3
Hi everyone, I'm ertan and Im in new cpp programming in linux. I have one problem in my task.
Now,I created one parent and two children. I must create some ...
- 04-10-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 3
[SOLVED] I can't run second child in C (Fork Problem)
Hi everyone, I'm ertan and Im in new cpp programming in linux. I have one problem in my task.
Now,I created one parent and two children. I must create some value as random in child1 and child1 has to send these values to child2.Child2 must read them..
now,I cant create some random values and i can send them.But child2 doesnt work. I have two functions. One them is writing, another one is reading. Child2 uses reading function,but it doesnt work (child2 cant call it, because writing function in endless loop)
What i must do? I used wait,usleep... No way..
I attached my file and also there is code..
I'm waiting for your help..
Thank you..
PHP Code:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <string>
using std::ostringstream;
using namespace std;
void producer( int handle )
{
int count = 0, total = 0;
char buf[ 128 ];
int myVal=0;
while ( 1 )
{
myVal=rand()%100;
sprintf( buf, "%d\n",myVal );
int ret = write( handle, buf, strlen( buf ) );
printf("handle %d buf: %s \n",handle,buf);
//getchar();
if ( ret < 0 )
{
printf( "Writing Error in Pipe!" );
exit( 1 );
}
printf( "Sending Number: %s\n", buf );
}
}
void consumer( int handle )
{
// int total = 0;
char buf[ 128 ];
int tmp=0;
while ( 1 )
{
int ret = read( handle, buf, sizeof( buf ) );
if ( ret < 0 )
{
printf( "Reading Error in pipe!" );
exit( 1 );
}
buf[ ret ] = 0;
}
string str=(char*)buf;
size_t found=0;
int tmp1=0;
int total=0;
string str2;
do{
found=str.find_first_of("\n",found+1);
str2 = str.substr(tmp1,found-tmp1);
total += atoi(str2.c_str());
cout<<"Recieved Number: " <<str2<< "\n"<<"Sum of Numbers: "<<total<<"\n";
tmp1=found+1;
} while (found!=string::npos);
//semaphor
}
main()
{
int mypipe[ 2 ];
if ( pipe( mypipe ) < 0 )
{
printf("Creating pipe Error!" );
exit( 1 );
}
int myChild;
int myChild2;
myChild = fork();
if (myChild <0)
{
printf("can't fork");
exit(1);
}
else if (myChild == 0)
{ // Child 1
printf("child1\n");
close( mypipe[ 0 ] );
producer( mypipe[ 1] );
}
else
{
printf("Parent: my pid = %d, parent pid = %d \n ", getpid(), getppid());
myChild= fork();
if (myChild == 0)
{ // Child2
printf("child2\n");
close( mypipe[ 1 ] );
consumer( mypipe[ 0] );
}
}
return 0;
}
- 04-11-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
In your consumer() function, you aren't breaking out of the read loop, so you never get to the data processing part.
Code:void consumer( int handle ) { // int total = 0; char buf[ 128 ]; int tmp=0; while ( 1 ) { int ret = read( handle, buf, sizeof( buf ) ); if ( ret < 0 ) { printf( "Reading Error in pipe!" ); exit( 1 ); } buf[ ret ] = 0; } // You never get here . . .Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-12-2011 #3Just Joined!
- Join Date
- Apr 2011
- Posts
- 3
Hi,Thank you for your reply. I already have solve this problem. I made mistake and i realized it later. I have just edited "while loop" and it works now..
Thank you for your attention..
Have a nice days..


