LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2004, 01:48 PM   #16
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30

if you try running it some times and try pressing Ctrl + C
you 'll see that sometimes you get Broken pipe only
other times you get only Time: 0 sec etc..

and sometimes both like this:
Time: 0 sec ..
Broken pipe

I'm not sure but this should have sth to do with which process [child or parent] is in status "running" at the the time Ctrl - C is pressed.

Even if this is the case, I'm not sure what I should do. popen doesn't allow pid cheking or sth. strace -v -f didn't help me much. I only saw that with Ctrl - C
I sometimes detach 2 procs and sometimes only one.

you've been very helpful so far and I thank you for that. If you thought sth tell it
 
Old 05-01-2004, 02:14 PM   #17
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
i've been trying to reproduce that, but i cannot after many runs. what i would do is scratch popen and do everything urself manually as i outlined above. the broken pipe is happening b/c one of the process is quitting and the other isn't, so one end of the pipe gets "broken" as u prolly figured out.
 
Old 05-01-2004, 02:37 PM   #18
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
hmm.

I'm not sure what you descibe is possible. If parent proc dies, don't child procs die too?
do you refer to parent dies but child isn't ready yet?

anyways I noticed that when I get 'Broken pipe' then a fileout.gz is always created..
when I get successful Ctrl + C [without Broken pipe error] I don't have fileout.gz

Last edited by zeppelin; 05-01-2004 at 02:40 PM.
 
Old 05-01-2004, 02:45 PM   #19
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
If parent process terminates then the child becomes an orphan which then becomes a child of init.
 
Old 05-01-2004, 03:07 PM   #20
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
ok mr kooch. so that's the problem?

I'm guessing:

parent gets SIGINT it calls exit_status ALL OK

parent gets SIGINT but has previously forked . so parent calls exit_status but child reports broken pipe. [because init doesn't pipe anymore]

and now what happens when I only get broken pipe?
and even more importantly, how can I get out of this chaos

gdb strace -v -f won't help me much to understand that broken pipe thing with all this process detached I get.

and broken pipe is not so helpful. cause parent can break pipe and child can too right?

Last edited by zeppelin; 05-01-2004 at 03:11 PM.
 
Old 05-01-2004, 03:16 PM   #21
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
Is it possible for you to post or email the entire code? It might be easier to help fix that way. If not I'll take a look at the rest of this thread later -- I'm really busy at the moment.

-- email: lotusman (AT) nycap (DOT) rr (DOT) com --
 
Old 05-01-2004, 03:52 PM   #22
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
http://nk.unstable.nl/zipstats.c.html

infamous41md, you could catch maybe the SIGPIPE and assign (or is it register it) to exit_status().

I have gcc 3.4.0 glibc-2.3.2. maybe your version don't throw SIGPIPE.. [i 'm just guessing]


anyways. I could just catch SIGPIPE instead of SIGINT if I it 100% of the time that I press Ctrl - C a SIGPIPE was sent. But that's not the case here. If your press quickly Ctrl - C then you get only a SIGINT without a SIGPIPE

so I can't rely on Ctrl - C producing SIGPIPE all the time.

is there a way to tell to C that I want SIGPIPE and SIGINT to call exit_status
but I don't want two times exit_status to be called {so it's matter of who comes first}.


either this, or I could catch SIGPIPE and just do nothing with it?
would that be productive {and I mean let's say you press Ctrl - C and 20% of the filein wasn't read and passed to gzip} gzip would compress the 80% of the filein and output to fileout.gz or not?

too many questions. If you can't stand me, it's obvius [even to me] why!!

Last edited by zeppelin; 05-04-2004 at 03:42 AM.
 
Old 05-02-2004, 12:43 PM   #23
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
because I want also to have some data count in the output of gzip
and I think popen is too high-level for this, I will try the infamous41md way of 'writing the job that popen does' and I 'll hit back the forums if I encounter new problems.

so I'll be back
hehe ;(
 
Old 05-02-2004, 12:52 PM   #24
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
Quote:
Originally posted by zeppelin
[url]is there a way to tell to C that I want SIGPIPE and SIGINT to call exit_status
but I don't want two times exit_status to be called {so it's matter of who comes first}.


either this, or I could catch SIGPIPE and just do nothing with it?
would that be productive {and I mean let's say you press Ctrl - C and 20% of the filein wasn't read and passed to gzip} gzip would compress the 80% of the filein and output to fileout.gz or not?

too many questions. If you can't stand me, it's obvius [even to me] why!!
what you could do is have a flag varialbe that each signal handler would try and set. if the flag is already set, that means the otehr signal handler has already run. to enforce synchronization, use the sigaction function and set the mask of blocked signals in SIGPIPE to block SIGINT, and the blocked mask in SIGINT to SIGPIPE. pseudo code:
Code:
int only_once = 0;

void sighandler(int signo)
{
   if(only_once)
          return;
   only_once = 1;
    do_ur_exit_stuff()
}

in main...

struct sigaction sapipe, saint;

sapipe.sa_handler = sighandler;
sigemptyset(&sapipe.sa_mask);
//block SIGINT when SIGPIPE handler urns
sigaddset(SIGINT, &sapipe.sa_mask);
sapipe.sa_flags = 0;
sigactoin(SIGPIPE, &sapipe);
///now whenever SIGPIPE handler runs, SIGINT will be blocked so u can
///safely test the shared global variable
//now repeat abovve steps for SIGINT handler
and dont worrry about bothering anyone. i enjoy trying to figure out problems, and it's cool that you are trying very hard to figure this out. we enjoy helping people like u.
 
Old 05-02-2004, 05:58 PM   #25
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
infamous41md, thank you man. this is nice pseudo-code you wrote.
eventhough I gave up on the idea of popen cause I can't figure out how much data did gzip write before someone hit Ctrl - C. [I don't want to just check the filesize of fileout.gz]

so if there isn't any way of figuring that out, I would go with your way of 'doing the job of popen' myself. So that way I believe I also won't have this SIGPIPE & SIGINT problems. [I'm not sure of course. If I encounter them, then I'll use your code]
What am I almost sure of, is that doing the popen's job on my own, will make me more dizzy trying to understand what doesn't go well. {of course I 'll try to manage to do the job I want w/out any problems}. I'll try tomorrow and I'll keep you informed [especially if I encounter problems that I don't understand/ don't know how to solve ]

Guys, you rock. You've been very helpful so far on enriching my C/Linux knowledge. Thank you!

Last edited by zeppelin; 05-02-2004 at 06:00 PM.
 
Old 05-02-2004, 06:52 PM   #26
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
hey have u read the book 'advanced programming in the unix environment' by W Ricard Stevens. it is a simply wonderful book, it's where most of us on here learned a lot of this. u seem like u would enjoy it very much. http://www.kohala.com/start/apue.html
 
Old 05-03-2004, 10:53 AM   #27
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
infamous41md, thank you for the recommendation.
hmm I think I did what you proposed but gzip doesn't like it much
I had to pass -f [force] because it can't understand that I have dup2'd stdout with fd_out.
apart from some strange symbols in the console before the new command I also have a broken .gz file.
this is the code I wrote: http://nk.unstable.nl/final.c.html

so, what did I write wrong?

Last edited by zeppelin; 05-04-2004 at 03:40 AM.
 
Old 05-04-2004, 07:33 AM   #28
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
plz. anyone???
 
Old 05-04-2004, 09:54 AM   #29
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
after school i'll have a look
 
Old 05-04-2004, 10:54 AM   #30
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
im at school now... so i can't test the code, but llooking at it, 2 tings:

1)didnt test error return for dup2
2)
Code:
while ((n = read(fd[0], buffer, BUFSIZE-1)) >0)
		//~ while(1)
		{ 
			bytes_out += n;
			
			//~ write(fd_middle, buffer, n);
			//~ write(0,buffer,n); //or I could dup2(fd[0],0);
			
			execl("/bin/gzip","gzip", "-fc", NULL);

		}
??? why the loop???? just execl() and thats it.
 
  


Reply



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't download Ubuntu .iso correctly - Checksum is all wrong Konradman Ubuntu 5 11-26-2005 03:59 PM
2-way pipe saiz66 Programming 3 11-22-2004 07:54 AM
GF4 170A TV OUT not working correctly - wrong freq. AndrewMSConvert Linux - Hardware 1 04-13-2004 05:07 AM
pipe jelly Slackware 2 03-05-2003 03:07 AM
pipe using no pipe soathana Programming 2 02-22-2003 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 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
Open Source Consulting | Domain Registration