ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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 --
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!!
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.
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.
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!
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.