PDA

View Full Version : [C] Funzione clone


Njk00
08-02-2010, 13:01
Salve
non capisco bene come si dichiari la funzione clone: io ho fatto così

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sched.h>

int test(int);

int main(int argc, char** argv)
{
int* param = malloc(sizeof(int));
*param = 5;
int* stack = malloc(sizeof(int));

int c = 2;

/*int clone(int (*fn)(void *), void *child_stack,
int flags, void *arg, ...*/

int result = clone(test(c), stack + sizeof(int), CLONE_PARENT, c);

return 0;
}

int test(int a)
{
printf("In test function a = %d\n", a);
a = 19;
printf("a = %d\n", a);
return a;
}


Tuttavia mi da i seguenti warnings:


1.c: In function main:
1.c:21: warning: passing argument 1 of clone makes pointer from integer without a cast
1.c:21: warning: passing argument 4 of clone makes pointer from integer without a cast


Perché?

Grazie a tutti

fero86
08-02-2010, 18:21
per comiciare il prototipo della funzione test dovrebbe essere questo:
int test(void*)
cioé dovrebbe prendere un void*, non un int.

poi la clone dovresti chiamarla cosi:
clone(test, stack + sizeof(int), CLONE_PARENT, c)

ed infine ovviamente c dovrebbe essere di tipo void*, non int.

Njk00
09-02-2010, 08:35
Grazie mille.
L'errore dello stack non l'avevo proprio pensato... Davvero grazie