Arc Forumnew | comments | leaders | submitlogin
2 points by stefano 6065 days ago | link | parent

On Anarki, you could try using:

  (require "ffi.arc")
  
  (w/ffi "libc.so"
    (cdef fork "fork" cint ()))
Now you should have imported fork.


2 points by eds 6064 days ago | link

Thanks for your suggestion.

I do have one question about loading libc. Since the system doesn't automatically find libc.so for you, you need to name the path explicitly. What worked for me was

  (w/ffi "/lib/libc.so.6"
    (cdef fork "fork" cint ()))
but I don't really know if this is the right way to do it. Is there a better way?

-----

2 points by elibarzilay 6064 days ago | link

The underlying foreign implementation is looking for the library. The problem is that /usr/lib/libc.so is not the library and dlopen() does not know how to handle this.

A better alternative with mzscheme's foreign interface is to use `#f' for the library -- that treats the mzscheme binary as the library to open (which includes the usual libc stuff.)

-----

1 point by sacado 6064 days ago | link

Interesting. Should be added to ffi.arc...

-----

2 points by eds 6064 days ago | link

Yeah, because currently if you attempt

  (w/ffi #f
    (cdef cfork "fork" cint ()))
arc will convert #f to nil, which mzcheme thinks is an unbound variable. There are work-arounds, like

  (w/ffi (read "#f")
    (cdef cfork "fork" cint ()))
but this is rather ugly.

-----

1 point by eds 6064 days ago | link

Also, I think I need to use wait() to wait for a child process to terminate, but I am somewhat at a loss as to how to accomplish this, since the manual says it takes an int pointer... Again, any help would be appreciate.

-----

2 points by stefano 6064 days ago | link

Try:

  (cdef _wait "wait" cint (cptr))

  (def wait (i)
    (let pi (cmalloc 4)
      (cpset pi cint i)
      (_wait pi)))
This creates a pointer, assign it a value and returns the value returned by _wait.

-----

1 point by eds 6059 days ago | link

Also, in your malloc() example, don't you have to explicitly free the memory afterwards? MzScheme's GC doesn't deal with memory explicitly allocated from C, does it? (I know the example itself uses only a tiny amount of memory, but still...)

-----

1 point by sacado 6059 days ago | link

Yes and no. I think mzscheme's GC can handle it without explicitly calling free at the right time, but you have to register a "finalizer" function to do so. That function will be called when the GC collects the object.

It does not know the size of malloced objects however, so be careful (no pb there, but if you allocate something very big, mzscheme will only see a new reference and will not necessarily call GC when you will actually lack memory).

-----

3 points by stefano 6058 days ago | link

If you alloc memory with cmalloc mzscheme will automatically delete it for you. Try

  (def infinite ()
    (cmalloc 4)
    (infinite))

  (infinite)
You shouldn't run out of memory. If you do, then I'm wrong.

-----

1 point by eds 6062 days ago | link

Just wondering: is there a way to malloc(sizeof(int)) from inside Arc instead of hard coding the size of an int?

-----

2 points by stefano 6061 days ago | link

You can make a C function like this:

  int size_of_int ()
  {
    return sizeof(int);
  }
and import it. I've followed this route while writing gtk.arc, but probably there's a better way to do it.

-----