No doubt pthread_create() calls to clone, but is it possible to modify program having pthread_join()?
Actualy I am trying to modify this code to use clone()
#include <stdio.h>
#include <sched.h>
#include <pthread.h>
void *childfun (void *para)
{
sleep(2);
printf("child terminating\n");
}
int main (void)
{
void * stackptr;
pthread_t readthread;
pthread_create(&readthread,NULL,childfun,NULL);
pthread_join(readthread,NULL);
printf("exit\n");
}
first I confused which flag to use for clone,then i watched strace output of above code and replaced my main function by
int main (void)
{
int ctid;
void *stackptr;
stackptr = malloc(getpagesize());
ctid = clone(childfun , stackptr+getpagesize() , CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND| CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID,NULL);
printf("exit\n");
}
but here main thread is terminating before new thread. How to achieve pthread_join functioning?