Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #262589
| From | "Thomas Schmitt" <scdbackup@gmx.net> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: 12.2: fork() causing getline() to repeat stdin endlessly |
| Date | 2023-10-23 10:50 +0200 |
| Message-ID | <HrYNj-eS7-3@gated-at.bofh.it> (permalink) |
| References | <HrVmp-cVb-1@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
Hi,
i can reproduce the problem with the given example after changing
int main(int, char **) {
to
int main(int argc, char **argv) {
in order to get it through the compiler.
(There is also a memory leak about line_buf which does not matter now.)
Not only the read offset of stdin seems to get reset by fork() but also
the line_num conter begins again at 0. (Can be an illusion. See surprise
remedy below.)
This does not happen if i watch the program by gdb. Only if i let it
run freely and printf the line_num counter and the lines, i get after
this change
while (-1 < (num_read = getline(&line_buf, &stg_size, stdin))) {
++line_num;
- printf("%s", line_buf);
- if (num_read != 53) {
- printf("num_read: %ld at line %lu\n", num_read, line_num);
- }
+ printf("pid %d , line %d : %s", (int) getpid(), line_num, line_buf);
pid_t pid = fork();
if (0 == pid) {
exit(0);
}
- else if (pid > 0) {
+ printf("fork() = %d\n", (int) pid);
+ if (pid > 0) {
(void) waitpid(pid, NULL, 0);
}
this output
pid 25511 , line 1
pid 25511 , line 1
fork() = 25513
pid 25511 , line 2
pid 25511 , line 1
fork() = 25513
pid 25511 , line 2
fork() = 25514
pid 25511 , line 3
pid 25511 , line 1
fork() = 25513
pid 25511 , line 2
fork() = 25514
pid 25511 , line 3
fork() = 25515
pid 25511 , line 4
...
Riddles:
- Why no "fork() = " after the lines which show their number for the first
time ?
- Why is it always one line more ?
The phenomenon vanishes if i replace printf() by fprintf(stderr).
Total diff of the changes which did this trick:
------------------------------------------------------------------------
--- getline_fork_orig.c 2023-10-23 08:51:58.788992417 +0200
+++ getline_fork_2.c 2023-10-23 10:37:28.025016315 +0200
@@ -7,7 +7,7 @@
#include <sys/wait.h>
#include <unistd.h>
-int main(int, char **) {
+int main(int argc, char **argv) {
char * line_buf = NULL;
size_t stg_size = 0u;
ssize_t num_read = 0;
@@ -15,9 +15,9 @@ int main(int, char **) {
while (-1 < (num_read = getline(&line_buf, &stg_size, stdin))) {
++line_num;
- printf("%s", line_buf);
+ fprintf(stderr,"%s", line_buf);
if (num_read != 53) {
- printf("num_read: %ld at line %lu\n", num_read, line_num);
+ fprintf(stderr,"num_read: %ld at line %lu\n", num_read, line_num);
}
pid_t pid = fork();
------------------------------------------------------------------------
I am still puzzled ...
Have a nice day :)
Thomas
Back to linux.debian.user | Previous | Next — Next in thread | Find similar | Unroll thread
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 10:50 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 11:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 15:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly jleonard@slimy.com (Jon Leonard) - 2023-10-23 16:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-23 16:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 17:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly gene heskett <gheskett@shentel.net> - 2023-10-23 18:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly <tomas@tuxteam.de> - 2023-10-23 20:10 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-23 21:50 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 22:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 22:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly "Thomas Schmitt" <scdbackup@gmx.net> - 2023-10-23 22:50 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-23 23:00 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-24 07:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Max Nikulin <manikulin@gmail.com> - 2023-10-24 18:10 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Greg Wooledge <greg@wooledge.org> - 2023-10-24 21:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly conover@panix.com (John Conover) - 2023-10-25 01:00 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly jleonard@slimy.com (Jon Leonard) - 2023-10-25 04:20 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Max Nikulin <manikulin@gmail.com> - 2023-10-25 04:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly <tomas@tuxteam.de> - 2023-10-25 06:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-25 07:40 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly tom kronmiller <twk.personal@gmail.com> - 2023-10-25 08:00 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Max Nikulin <manikulin@gmail.com> - 2023-10-23 18:30 +0200
Re: 12.2: fork() causing getline() to repeat stdin endlessly Stefan Monnier <monnier@iro.umontreal.ca> - 2023-10-24 05:00 +0200
csiph-web