Convering fork-->vfork()

This is a discussion on Convering fork-->vfork() within the Linux Programming forums, part of the Platform Specific Boards category; Hi all. I have some code which does what i need it to do on a standard Linux platform. However ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    Convering fork-->vfork()

    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:

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    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/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by Zulu View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  2. Fork() not working.
    By BENCHMARKMAN in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2007, 12:28 AM
  3. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 01:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 05:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21