9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] C-program error..
@ 2000-06-16 19:50 dhog
  0 siblings, 0 replies; 3+ messages in thread
From: dhog @ 2000-06-16 19:50 UTC (permalink / raw)
  To: 9fans

In your example, s in recvit() is expected (by recv()) to point to
storage for the data sent on the channel (all 32 bytes worth),
but you have left it uninitialized.

Also, print() has an internal buffer of 4096 bytes; you need to
increase the stack size or you'll get mysterious errors caused
by stack corruption.

Try this version:

#include <u.h>
#include <libc.h>
#include <thread.h>

#define STACKSIZE (8*1024)

void
recvit(void *arg)
{
	char *s;
	Channel *in = arg;

	recv(in, &s);
	print("%s\n", s);
	free(s);
}

void
threadmain(int argc, char *argv[])
{
	char *m;
	Channel *sch;

	sch = chancreate(sizeof(char*), 0);
	m = strdup("hello world!");
	print("main: %s\n", m);
	proccreate(recvit, (void *)sch, STACKSIZE);

	send(sch, &m);

	print("done\n");
}



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

* Re: [9fans] C-program error..
@ 2000-06-16 19:52 rsc
  0 siblings, 0 replies; 3+ messages in thread
From: rsc @ 2000-06-16 19:52 UTC (permalink / raw)
  To: 9fans

A few problems.  The main one is that you
sent a 32-byte array and tried to read it
into what was pointed at by an uninitialized pointer.
Compiling with -w would catch this.

The second is that you need to use threadprint
instead of print to avoid stack problems.
Threadprint is like fprint, so you need
to give a file descriptor.

The below works for me.

Russ


#include <u.h>
#include <libc.h>
#include <thread.h>

#define STACKSIZE (2*2048)

void
recvit(void *arg)
{
	char *s;
	Channel *in;	/* of char* */

	in = arg;
	s = recvp(in);
	threadprint(1, "%s\n", s);
	threadexits(0);
}

void
threadmain(int argc, char *argv[])
{
	Channel *sch;	/* of char* */

	sch = chancreate(sizeof(char*), 0);
	proccreate(recvit, (void *)sch, STACKSIZE);
	sendp(sch, "hello world");
	threadprint(1, "done\n");
	threadexitsall(nil);
}



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

* [9fans] C-program error..
@ 2000-06-16 18:57 Ish Rattan
  0 siblings, 0 replies; 3+ messages in thread
From: Ish Rattan @ 2000-06-16 18:57 UTC (permalink / raw)
  To: 9fans

Hello,

Just tried translation of an old alef program. The program
aborts with run time error. Source and output given below.

Any pointers?
- ishwar
----
/* First C program, uses channel and procs..
	dies horribly..
*/

#include <u.h>
#include <libc.h>
#include <thread.h>

#define STACKSIZE (2*2048)

void recvit(void *arg)
{
   char *s;
   Channel *in = arg;

   recv(in, s);
   print("%s\n", s);
}

void threadmain(int argc, char *argv[])
{
   char m[32];
   Channel *sch;

   sch = chancreate(sizeof(m), 0);
   strcpy(m, "hello world!");
   print("main: %s\n", m);
   proccreate(recvit, (void *)sch, STACKSIZE);

   send(sch, m);

   print("done\n");
   exits(nil);
}
---
main: hello world!
 done
hello world!
panic: D2B called on non-block 510a8
8.out 513: suicide: sys: trap: fault read addr=0x0 pc=0x00003fd1
---


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

end of thread, other threads:[~2000-06-16 19:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-06-16 19:50 [9fans] C-program error dhog
  -- strict thread matches above, loose matches on Subject: below --
2000-06-16 19:52 rsc
2000-06-16 18:57 Ish Rattan

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).