This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

fork() + file descriptor bug in 1.7.27(0.271/5/3) 2013-12-09 11:54


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]