Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 27 Feb 2001 09:54:05 -0500 (EST)
From:      Peter Dufault <dufault@hda.hda.com>
To:        mouss <usebsd@free.fr>
Cc:        Marc W <mwlist@lanfear.com>, freebsd-hackers@FreeBSD.ORG
Subject:   Re: Where can I find out rules on blocking in threads?
Message-ID:  <200102271454.f1REs5j22343@hda.hda.com>
In-Reply-To: <4.3.0.20010227153039.00dd4f00@pop.free.fr> from mouss at "Feb 27, 2001 03:34:51 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
> can you provided a small piece of code that shows this behaviour?

Here's one without error checking.  Be sure to use -pthread.

Peter

--
Peter Dufault (dufault@hda.com)   Realtime development, Machine control,
HD Associates, Inc.               Fail-Safe systems, Agency approval


#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>

#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>

struct threadstuff {
	int rfid, wfid;
	const char *name;
};

static void *
reader(void *arg) {
	struct threadstuff *pts = (struct threadstuff *)arg;

	fprintf(stderr, "Opening %s for read...", pts->name);
	pts->rfid = open(pts->name, O_RDONLY);
	fprintf(stderr, " reader got fid %d.\n", pts->rfid);

	return 0;
}

static void *
writer(void *arg) {
	struct threadstuff *pts = (struct threadstuff *)arg;

	fprintf(stderr, "Opening %s for write...", pts->name);
	pts->wfid = open(pts->name, O_WRONLY);
	fprintf(stderr, " writer got fid %d.\n", pts->wfid);

	return 0;
}

int
main(int ac, char *av[]) {
	pthread_t r, w;
	void *value;

	struct threadstuff ts;

	ts.name =  "./my_fifo";
	ts.rfid = ts.wfid = -1;

	(void)unlink(ts.name);
	(void)mkfifo(ts.name, 0666);

	pthread_create(&r, 0, reader, (void *)&ts);
	pthread_create(&w, 0, writer, (void *)&ts);

	pthread_join(r, &value);
	pthread_join(w, &value);

	if (ts.rfid != -1)
		(void)close(ts.rfid);

	if (ts.wfid != -1)
		(void)close(ts.wfid);

	return 0;
}

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200102271454.f1REs5j22343>