Create a new process
#include <sys/types.h> #include <process.h> pid_t fork( void );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The fork() function creates a new process. The new process (child process) is an exact copy of the calling process (parent process), except for the following:
In order to successfully call this function, your process must have the PROCMGR_AID_FORK ability enabled. For more information, see procmgr_ability().
The parent's ability configuration is copied to the child verbatim, regardless of the PROCMGR_AOP_INHERIT_NO status of each of the abilities.
You can use pthread_atfork() to register fork handler functions to be called before or after the fork occurs.
A value of zero to the child process; and the process ID of the child process to the parent process. Both processes continue to execute from the fork() function. If an error occurs, fork() returns -1 to the parent and sets errno.
/* * This program executes the program and arguments * specified by argv[1..argc]. The standard input * of the executed program is converted to upper * case. */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <ctype.h> #include <process.h> #include <sys/wait.h> int main( int argc, char **argv ) { pid_t pid; pid_t wpid; int fd[2]; char buffer[80]; int i, len; int status; if( pipe( fd ) == -1 ) { perror( "pipe" ); return EXIT_FAILURE; } if( ( pid = fork() ) == -1 ) { perror( "fork" ); return EXIT_FAILURE; } if( pid == 0 ) { /* This is the child process. * Move read end of the pipe to stdin ( 0 ), * close any extraneous file descriptors, * then use exec to 'become' the command. */ dup2( fd[0], 0 ); close( fd[1] ); execvp( argv[1], argv+1 ); /* This can only happen if exec fails; print message * and exit. */ perror( argv[1] ); return EXIT_FAILURE; } else { /* This is the parent process. * Remove extraneous file descriptors, * read descriptor 0, write into pipe, * close pipe, and wait for child to die. */ close( fd[0] ); while( ( len = read( 0, buffer, sizeof( buffer ) ) ) > 0 ) { for( i = 0; i < len; i++ ) { if( isupper( buffer[i] ) ) buffer[i] = tolower( buffer[i] ); } write( fd[1], buffer, len ); } close( fd[1] ); do { wpid = waitpid( pid, &status, 0 ); } while( WIFEXITED( status ) == 0 ); return WEXITSTATUS( status ); } }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |