PDA

View Full Version : Convering fork-->vfork()



Zulu
04-14-2007, 07:54 PM
Hi all. I have some code which does what i need it to do on a standard Linux platform. However i need to make it work on the uClinux kernel, which is used for microcontrollers. To do this, i have been told i will need to change fork() to vfork().

If someone would be able to help me, or guide me on how to make this work on uClinux using the vfork(), i would appreciate it.

Here is the code:



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

void termination_handler(int);

int test_pipe=1;
int main()
{
int pipedescriptor[2];
char buff[100];
alarm(10);
/*if (signal (SIGALRM, termination_handler) == SIG_IGN)*/
signal (SIGALRM, termination_handler);

pipe(pipedescriptor);
while (test_pipe>0)
{
if(!fork())
{
printf("This is the child writing to the pipe\n");
write(pipedescriptor[1], "TST", 4);
printf("This is the child exiting the pipe\n");
exit(0);

}
else
{
printf("This is the parent reading from the pipe\n");
fflush(stdout);
read(pipedescriptor[0], buff, 4);
printf("Parent reading form pipe: \"%s\"\n", buff);
test_pipe++;
wait(NULL);
}
}
while(1);
return 0;
}

void termination_handler (int signum)
{
printf("Total messeges sent: %d\n",test_pipe);
printf("Total bytes per second: %d\n",(test_pipe*4)/10);
exit(0);
}




Any help is appreciated.

Salem
04-15-2007, 12:39 AM
So does this mean you've already tried to use 'vfork()' and it's failed horribly?

How about - http://www.linuxjournal.com/article/7221


This is avoided by ensuring that the child never returns from the current stack frame once vfork() has been called and that it calls _exit when finishing-exit cannot be called as it changes data structures in the parent.


You're going to get a lot more detailed (and accurate) help by signing up to the forums / mailing lists at http://www.uclinux.org/

brewbuck
04-16-2007, 10:45 AM
Hi all. I have some code which does what i need it to do on a standard Linux platform. However i need to make it work on the uClinux kernel, which is used for microcontrollers. To do this, i have been told i will need to change fork() to vfork().

If someone would be able to help me, or guide me on how to make this work on uClinux using the vfork(), i would appreciate it.

In the code you posted, you can't. vfork() suspends the parent until the child either exits or execs. This is to avoid the page-copy penalty on systems that can't support copy-on-write. Your program will have to be redesigned. The child will have to reside in a separate program. You will vfork() and then the child will exec() this program.