public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* fork() + file descriptor bug in 1.7.27(0.271/5/3) 2013-12-09 11:54
@ 2014-01-13 16:31 tednolan
  2014-01-14 10:18 ` Corinna Vinschen
                   ` (3 more replies)
  0 siblings, 4 replies; 22+ messages in thread
From: tednolan @ 2014-01-13 16:31 UTC (permalink / raw)
  To: cygwin; +Cc: tednolan

Hello,

I'm running:

CYGWIN_NT-6.1 prog5 1.7.27(0.271/5/3) 2013-12-09 11:54 x86_64 Cygwin
gcc (GCC) 4.8.2

on a 64 bit Win7 system.

I have just run into an odd bug, which I have boiled down into the program
below (which started as a mod to tiff2ps).

If you compile this program:

=========================CUT HERE=============
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>


int main(int argc, char **argv)
{

	FILE *fp;
	char buf[4096];
	char infile[4096];
	char outfile[4096];
	int i = 0;
	int running_children = 0;
	int child_limit = 20;
	int wait_status;

	if (argc == 1) {
		fp = stdin;
	} else if (argc == 2) {

		fp = fopen(argv[1], "r");
		if (!fp) {
			fprintf(stderr, "Can't open input list %s\n", argv[1]);
			exit(1);
		}

	} else {
		fprintf(stderr, "Usage: multi_tiff2ps [spec_file]\n");
		exit(1);
	}

	while( fgets(buf, sizeof(buf), fp) ) {
		++i;

		if(sscanf(buf, "%s %s", infile, outfile) != 2) {
			fprintf(stderr, "Malformed spec line %d (%s)\n",
				i, buf);
			continue;
		}

		//fprintf(stderr, "(%s) (%s) %d %ld\n", infile,
		//	outfile, i, ftell(fp));

		fprintf(stderr, "Running %d\n", running_children);

		if (running_children >= child_limit) {
			fprintf(stderr, "Initial wait\n");
			wait(&wait_status);
			--running_children;
		}

		switch (fork()) {
			
			/* error */
			case -1:
				fprintf(stderr,
					"Can't fork new tiff2ps process!\n");
				exit(1);
				break;

			/* child */
			case 0:
				fprintf(stderr, "child\n"); fflush(stderr);
				exit(0);
				break;

			/* parent */
			default:
				++running_children;
				break;
		}
	}

	for(i = 0; i < running_children; i++) {
		fprintf(stderr, "Final wait\n");
		wait(&wait_status);
	}


	exit(0);

}

=========================End code=============

and run it with this data:

00.tif  00.eps
01.tif  01.eps
02.tif  02.eps

It will run forever.

However, if you uncomment the fprintf with the ftell(), it runs as
expected.

It works correctly on linux.

Anyone seen this before?

Is there a fix?

Thanks!

Ted Nolan

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2014-01-17 23:25 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-13 16:31 fork() + file descriptor bug in 1.7.27(0.271/5/3) 2013-12-09 11:54 tednolan
2014-01-14 10:18 ` Corinna Vinschen
2014-01-14 15:54 ` Eric Blake
2014-01-14 16:15   ` tednolan
2014-01-15  4:53     ` Lord Laraby
2014-01-15  5:40       ` Lord Laraby
2014-01-15  7:46       ` Peter Rosin
2014-01-15 15:53         ` tednolan
2014-01-15 16:33           ` Corinna Vinschen
2014-01-16  5:08             ` tednolan
2014-01-16  8:50               ` Corinna Vinschen
2014-01-17  5:19                 ` tednolan
2014-01-17  9:28                   ` Corinna Vinschen
2014-01-17 20:13           ` Eric Blake
2014-01-17 20:11       ` Eric Blake
2014-01-17 20:38     ` Eric Blake
2014-01-17 21:02       ` tednolan
2014-01-17 22:12       ` Eric Blake
2014-01-17 23:25         ` Lord Laraby
2014-01-15 16:40 ` Tom Honermann
2014-01-15 16:50   ` Corinna Vinschen
2014-01-17 20:06 ` Eric Blake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).