The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 09:49 AM
unix pipe in C meh High Level Programming 1 10-16-2006 04:34 PM
PIpe Spy jodders High Level Programming 11 02-18-2004 07:44 AM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 08:09 AM
pipe help bb666 High Level Programming 5 02-26-2002 01:07 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-02-2006
Registered User
 

Join Date: Jan 2006
Posts: 5
using pipe in a loop

Hi

I am working with ipc functions in linux such as pipe()
well I have a program that must be used in a loop and a pipe is used in
this program but when I use this program in aloop program never ends I think
it is because of the fork function that is located in the program but I don't know
how to control them I used free function but nothing happened and the program was in
error.
so how can I kill a process like that and is it a good way to control them?

would you please help me to solve this problem?

thanks for your help and attention

Best Regards.
Reply With Quote
Forum Sponsor
  #2  
Old 01-02-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
Please post the code if you can.
Reply With Quote
  #3  
Old 01-03-2006
Registered User
 

Join Date: Jan 2006
Posts: 5
Hi

thanks for your attention

here I have written a sample code that works
only for one time I mean if I ommit the line exit(EXIT_SUCCESS) in default part the activity of
client never ends with the wrong output it means that only it printfs:

child: the name is %s !!

for unlimited times but when I use this line every thing is ok but just for one data just for
"afi" in array names and then every thing ends.

well what do you think about it ? what is my problem?

one another time thanks for your attention

Best Regards.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>

static void sigchld_handler(int sig) { int stat; wait(&stat); }

int main()
{

char names[7][10]={"afi","shin","pedi","arin","nazin","mam","bab"};
int fd[2];
int nread;
int i,j,rc;
pid_t pid;
char buf[1024];

struct sigaction sigchldr;
memset(&sigchldr, 0, sizeof(struct sigaction));
sigchldr.sa_handler = sigchld_handler; sigaction(SIGCHLD, &sigchldr, NULL);

printf("\n start");


if ((rc=pipe(fd))<0)
{
printf("\n pipe error");
exit(0);
}

i=0;
while(i<7)
{

printf("\n enter while %d",i);

switch((pid=fork()))
{

case -1:

printf("\n fork error");
exit(EXIT_FAILURE);


case 0: //child

printf("\n child");
close(fd[1]);
while((nread=read(fd[0],buf,1024)!=0))
{
char c=getchar();

printf("\n child: the name is %s !!",buf);
}
close(fd[0]);
exit(EXIT_SUCCESS);

default: //parent

printf("\n parent");
char c=getchar();

close(fd[0]);
printf("\n strart parent %s",names[i]);
write(fd[1],names[i],1024);
printf("\n writing %s is finished $",names[i]);
close(fd[1]);
exit(EXIT_SUCCESS);


}
i++;
}
exit(EXIT_SUCCESS);
}
Reply With Quote
  #4  
Old 01-04-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
i assume you had missed out /usr/include/signal.h by mistake

Quote:
but when I use this line every thing is ok but just for one data just for "afi" in array names and then every thing ends.

by the flow of the program what output you are getting is correct,
you have an
exit(EXIT_SUCCESS);
in parent process hence after reaching at that point execution of the parent is terminated.
By this time you have only one value written to the pipe by the parent to be read by the child and parent terminates.

Hence the one vaue ( the string that is printed )
"afi"
is read from the pipe and printed by the child.

you can verify this by,
run the program and check for the binary you are running
ps -ef | grep <binary_name>

once the parent is terminated , INIT process would adopt the child which is left without its parent (this can be verified with current PPID of the child as 1)

Quote:
the activity of client never ends with the wrong output it means that only it printfs:
child: the name is %s !!
for unlimited times

Code:
close(fd[0]);
printf("\n strart parent %s",names[i]);
write(fd[1],names[i],1024);
printf("\n writing %s is finished $",names[i]);
close(fd[1]);


you had closed the read end of the pipe in parent as the parent is going to flush the data and not read -- close(fd[0])
after writing the first data even the write descriptor of the parent's pipe is closed. -- close(fd[1])

you can check this,
retval=write(fd[1],names[i],1024);

for the first time you would receive positive value indicating SUCCESS
and for subsequent writes a negative value indicating FAILURE

hope this clarifies.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0