Fork: Difference between revisions

2,552 bytes added ,  2 years ago
Added Wren
(Adding R implementation)
(Added Wren)
Line 1,639:
prn.1)
prn.2</lang>
 
=={{header|Wren}}==
{{trans|C}}
The ability to call C library functions such as ''fork'' may be added to Wren-cli in the next release. In the meantime, we embed the following Wren script in a minimal C host (no error checking) to complete this task.
<lang ecmascript>/* fork.wren */
 
class C {
foreign static fork()
 
foreign static usleep(usec)
 
foreign static wait()
}
 
var pid = C.fork()
if (pid == 0) {
C.usleep(10000)
System.print("\tchild process: done")
} else if (pid < 0) {
System.print("fork error")
} else {
System.print("waiting for child %(pid)...")
System.print("child %(C.wait()) finished")
}</lang>
<br>
We now embed this in the following C program, compile and run it.
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "wren.h"
 
void C_fork(WrenVM* vm) {
pid_t pid = fork();
wrenSetSlotDouble(vm, 0, (double)pid);
}
 
void C_usleep(WrenVM* vm) {
useconds_t usec = (useconds_t)wrenGetSlotDouble(vm, 1);
usleep(usec);
}
 
void C_wait(WrenVM* vm) {
pid_t pid = wait(NULL);
wrenSetSlotDouble(vm, 0, (double)pid);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "fork()") == 0) return C_fork;
if (isStatic && strcmp(signature, "usleep(_)") == 0) return C_usleep;
if (isStatic && strcmp(signature, "wait()") == 0) return C_wait;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "fork.wren";
char *script = readFile(fileName);
wrenInterpret(vm, module, script);
wrenFreeVM(vm);
free(script);
return 0;
}</lang>
 
{{out}}
Sample output:
<pre>
waiting for child 15274...
child process: done
child 15274 finished
</pre>
 
=={{header|X86 Assembly}}==
9,476

edits