9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Axel Belinfante <Axel.Belinfante@cs.utwente.nl>
To: Russ Cox <rsc@swtch.com>,
	Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu>
Subject: Re: [9fans] thread confusion
Date: Thu, 22 Sep 2005 00:34:53 +0200	[thread overview]
Message-ID: <200509212234.j8LMYrG01220@zamenhof.cs.utwente.nl> (raw)
In-Reply-To: Your message of "Wed, 21 Sep 2005 16:37:21 -0400." <ee9e417a050921133714f0e59a@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1085 bytes --]

> Again, it sounds like you're
> not closing all the references to one end of the pipe.
> If multiple programs have references to a pipe end,
> they *all* need to close them.  Make sure that the
> proc running tlsClient doesn't have a reference too.

Just checking: I assume by 'references' you mean
file descriptors in the process' fd table?

ehm... I just realized:
I create the procs with proccreate,
so all procs share the same references,
and when I close them in the main proc,
they go away in the other ones as well,
or so it seems, at least according to
cat /proc/*/fd.
nevertheless, without the zero-write
before one of the closes they just keep
hanging in the read, even when their fd
table no longer shows the pipe file
descriptors in any of the fd tables.

If I don't share the fd table,
and just give each of the sub processes
their own end of the pipe, I'll never be
able to close those from within those
processes, because they are both locked
up in a read (essentially waiting for each other).

I have attached a silly program.

Axel.


[-- Attachment #2: Type: text/plain, Size: 3820 bytes --]

#include <u.h>
#include <libc.h>
#include <thread.h>
enum {
	STACK = 16*2048,
};

typedef struct State {
	int id;
	int fd, tobeclosed;
	Channel *c;
} State;

static void
subproc(void *arg)
{
	State *m;
	char buf[1024];
	int n;

	m = arg;
	print("subproc tid=%d tobeclosed=%d\n", threadid(), m->tobeclosed);
	sleep(15000);
//	print(" subproc tid=%d pid=%d\n", m->id, threadpid(m->id));
//	close(m->tobeclosed);
//	sleep(15000);

	print("subproc writing fd=%d n=%d\n", m->fd, n);
	n = write(m->fd, buf, 5);
	print("subproc written fd=%d n=%d\n", m->fd, n);
	while((n = read(m->fd, buf, sizeof(buf))) > 0)
		print("subproc fd=%d] read n=%d\n", m->fd, n);
	print("subproc eof fd=%d n=%d\n", m->fd, n);
	sleep(15000);
	sendul(m->c, 0);
	print("exiting subproc tid=%d pid=%d\n", m->id, threadpid(m->id));
	threadexits(nil);
}

static void
mainproc(void *arg)
{
	State *m;
	char buf[1024];
	int n;

	m = arg;
	print("mainproc tid=%d tobeclosed=%d\n", threadid(), m->tobeclosed);
	sleep(15000);
//	print(" mainproc tid=%d pid=%d\n", m->id, threadpid(m->id));
//	close(m->tobeclosed);
//	sleep(15000);

	print("mainproc writing fd=%d n=%d\n", m->fd, n);
	n = write(m->fd, buf, 7);
	print("mainproc written fd=%d n=%d\n", m->fd, n);

	while((n = read(m->fd, buf, sizeof(buf))) > 0)
		print("mainproc fd=%d n=%d\n", m->fd, n);
	print("mainproc  fd=%d eof n=%d\n", m->fd, n);
	sleep(15000);
	sendul(m->c, 0);
	print("exiting mainproc tid=%d pid=%d\n", m->id, threadpid(m->id));
	threadexits(nil);
}

void
threadmain(int argc, char *argv[])
{
	State mainState, *m;
	State subState, *s;
	char buf[256];
	int i, j, k, l, n, N, maineof, subeof, ret, hang;
	int p[2];
	Alt a[] = {
	/*	 c			v		op   */
		{nil ,	nil,	CHANRCV},
		{nil,	nil,	CHANRCV},
		{nil,			nil,	CHANEND},
	};

	hang = 0;		//change to 1 to hang
	N = 1;

	m = &mainState;
	s = &subState;
	print("threadmain tid=%d pid=%d\n", threadid(), threadpid(threadid()));
	memset(m, 0, sizeof(State));
	m->c = chancreate(sizeof(int), 0);
	a[0].c = m->c;
	s->c = chancreate(sizeof(int), 0);
	a[1].c = s->c;

	for (l=0; l < N; l++) {
		print("for %d\n", l);
		if (pipe(p) < 0) {
			fprint(2, "pipe failed: %r\n");
			threadexitsall("pipe failed");
		}
		m->fd = p[0];
		m->tobeclosed = p[1];
		s->fd = p[1];
		s->tobeclosed = p[0];
	
	
//		m->id = procrfork(mainproc, m, STACK, RFFDG);
		m->id = proccreate(mainproc, m, STACK);
		print("started mainproc tid=%d pid=%d\n", m->id, threadpid(m->id));

//		s->id = procrfork(subproc, s, STACK, RFFDG);
		s->id = proccreate(subproc, s, STACK);
		print("started subproc tid=%d pid=%d\n", m->id, threadpid(m->id));

		sleep(60000);
		print("manthread after sleep\n");

		i = 0;
		j = 1;
		print("threadmain closing %d\n", p[j]);
		close(p[j]);
		print("threadmain closed %d\n", p[j]);

		if (!hang) {
			print("threadmain writing zero to %d\n", p[i]);
			n = write(p[i], buf, 0);
			print("threadmain write %d returns %d\n", p[i], n);
			sleep(15000);
		}

		print("threadmain closing %d\n", p[i]);
		close(p[i]);
		print("threadmain closed %d\n", p[i]);
		sleep(15000);

		maineof = 0;
		subeof = 0;
		while(!maineof || !subeof) {
			print("threadmain while alt maineof=%d subeof=%d\n", maineof, subeof);
			switch(ret = alt(a)){
			case 0:
				print("main eof\n");
				maineof = 1;
				break;
			case 1:
				print("sub eof\n");
				subeof = 1;
				break;
			default:
				print("should not happen ret=%d\n", ret);
				sysfatal("should not happen");
			}
		}
		print("threadmain while alt done\n");
	
	//	print("threadmain threadint mainid\n");
	//	threadint(m->mainid);
	//	print("threadmain done threadint mainid\n");

	}
	print("threadmain exiting\n");

	threadexits(nil);
}

  reply	other threads:[~2005-09-21 22:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-21 14:44 Fco. J. Ballesteros
2005-09-21 15:05 ` Axel Belinfante
2005-09-21 15:42   ` Russ Cox
2005-09-21 20:25     ` Axel Belinfante
2005-09-21 20:32       ` Axel Belinfante
2005-09-21 20:37       ` Russ Cox
2005-09-21 22:34         ` Axel Belinfante [this message]
2005-09-21 22:44           ` Russ Cox
2005-09-26 18:40       ` rog
2005-09-26 18:52         ` Russ Cox
2005-09-26 19:20           ` rog
2005-09-27 10:12         ` Axel Belinfante
  -- strict thread matches above, loose matches on Subject: below --
2005-09-21 15:48 Fco. J. Ballesteros
2005-09-21 13:53 Fco. J. Ballesteros
2005-09-21 14:32 ` Axel Belinfante
2005-09-21 13:25 Axel Belinfante

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200509212234.j8LMYrG01220@zamenhof.cs.utwente.nl \
    --to=axel.belinfante@cs.utwente.nl \
    --cc=9fans@cse.psu.edu \
    --cc=rsc@swtch.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).