using pipe in a loop


 
Thread Tools Search this Thread
Top Forums Programming using pipe in a loop
# 1  
Old 01-02-2006
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.
# 2  
Old 01-03-2006
Please post the code if you can.
# 3  
Old 01-03-2006
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);
}
# 4  
Old 01-05-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop or while loop from a text file

Hi all, i developed a script to measure the uptime of a process in a Solaris 10/11 environments. All is well, but i came across a situation where there are multiple processes of the same name. Basically i have the following result file: beVWARS 13357 19592122 beVWARS 14329 19591910... (4 Replies)
Discussion started by: nms
4 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

5. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

6. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

7. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ... (4 Replies)
Discussion started by: brandono66
4 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies
Login or Register to Ask a Question