delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/06/08/20:44:25

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <00af01c56c8c$54974c30$1f3ca8c0@AlohaSunset.com>
From: "Mark Pizzolato" <list-cygwin AT subscriptions DOT pizzolato DOT net>
To: <cygwin AT cygwin DOT com>
Subject: Multi Threaded programs deadlock doing simple I/O operations
Date: Wed, 8 Jun 2005 17:43:59 -0700
MIME-Version: 1.0
X-IsSubscribed: yes

------=_NextPart_000_00AC_01C56C51.A6898BB0
Content-Type: text/plain;
	format=flowed;
	charset="iso-8859-1";
	reply-type=original
Content-Transfer-Encoding: 7bit

There is a serious problem for multi threaded programs doing simple I/O 
operations in cygwin (open, dup, fdopen, fclose, and close).

The attached 81 line test program clearly demonstrates the issue (by hanging 
and no longer consuming CPU or performing any I/O operations).

I'm sure that anyone who ever encountered a stange hang in any program 
running under cygwin would appreciate a fix for this issue.

- Mark Pizzolato

------=_NextPart_000_00AC_01C56C51.A6898BB0
Content-Type: text/plain;
	format=flowed;
	name="iotest.c";
	reply-type=original
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="iotest.c"

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>

pthread_mutex_t log_mutex =3D PTHREAD_MUTEX_INITIALIZER;

void
logit(const char *fmt, ...) {
	va_list args;
	char buf[1024];
	int bytes;
=09
	buf[sizeof(buf)-1] =3D '\0';
	va_start(args, fmt);
	bytes =3D vsnprintf(buf, sizeof(buf)-1, fmt, args);
	va_end(args);
	pthread_mutex_lock(&log_mutex);
	printf("%d:", pthread_self());
	printf("%s", buf);
	pthread_mutex_unlock(&log_mutex);
}

struct TestIoInfo {
	int Iterations;
	int Progress;
};

void *
TestIoThread (void *arg) {
	struct TestIoInfo *t =3D (struct TestIoInfo *)arg;
	int i, j;
	int fd, fdd;
	char FileName[255];
	FILE *f;
=09
	logit("IO Thread %d starting...\n", pthread_self());
	snprintf(FileName, sizeof(FileName), "/tmp/TestIoThread-%d-%x", getpid(), =
pthread_self());
	sleep(1);
	for (j=3D0; j<t->Iterations; ++j) {
		if ((fd =3D open(FileName, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0=
) {
			logit("Error Opening File: %s - %d\n", FileName, errno);
			return;
		}
		fdd =3D dup(fd);
		if ((f =3D fdopen(fdd, "rb")) =3D=3D NULL) {
			logit("Can't open descriptor %d - %d\n", fd, errno);
			return;
		}
		fclose(f);
		close(fd);
		if (0 =3D=3D (j%t->Progress)) {
			logit("IO Thread %d - %d\n", pthread_self(), j);
		}
	}
	unlink(FileName);
	logit("IO Thread %d done.\n", pthread_self());
	return NULL;
}

main (int argc, char ** argv) {
	int threadcount =3D 4;
	int progress =3D 1;
	pthread_t tid[10];
	int i;
	struct TestIoInfo IoInfo;

	logit("Testing with %d concurrent threads\n", threadcount);
	logit("Progress indicated every %d operations...\n", progress);
	IoInfo.Iterations =3D 200;
	IoInfo.Progress =3D progress;
	for (i=3D0; i<threadcount; ++i) {
		pthread_create(&tid[i], NULL, TestIoThread, &IoInfo);
	}
	for (i=3D0; i<threadcount; ++i)
		pthread_join(tid[i], NULL);
	logit("All Io Threads completed\n");
}


------=_NextPart_000_00AC_01C56C51.A6898BB0
Content-Type: text/plain; charset=us-ascii

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019