sources: forkng
This post has been edited by Jiro_: 05 December 2012 - 03:05 AM




Posted 05 December 2012 - 03:05 AM
This post has been edited by Jiro_: 05 December 2012 - 03:05 AM
Posted 05 December 2012 - 06:33 AM
pid_t pID = fork();
if (pID == 0) // child
{
// Code only executed by child process
}
Posted 05 December 2012 - 02:20 PM
if((childPID = fork()) < 0)
fprintf(stderr, "Bad Fork");
else if(childPid == 0) {
execl("echo", "Hello World", 0);
fprintf(stderr, "Bad Exec"); //should never get here
}
printf("Child %d is done.\n", wait3(NULL, 0, NULL));
Posted 05 December 2012 - 02:39 PM
Posted 06 December 2012 - 07:33 AM
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("Initial value of USER: \"%s\"\n", getenv("USER"));
if(putenv("USER=vivid") != 0)
{
fprintf("Error: putenv failed\n");
return EXIT_FAILURE;
}
execl("/usr/bin/printenv", "printenv", "USER", "SHELL", (char *) NULL);
/* If we get here, something went wrong */
fprintf(stderr, "Error: execl\n");
return EXIT_FAILURE;
}
This post has been edited by vividexstance: 06 December 2012 - 07:34 AM
Posted 07 December 2012 - 10:45 AM
vividexstance, on 06 December 2012 - 07:33 AM, said:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("Initial value of USER: \"%s\"\n", getenv("USER"));
if(putenv("USER=vivid") != 0)
{
fprintf("Error: putenv failed\n");
return EXIT_FAILURE;
}
execl("/usr/bin/printenv", "printenv", "USER", "SHELL", (char *) NULL);
/* If we get here, something went wrong */
fprintf(stderr, "Error: execl\n");
return EXIT_FAILURE;
}
//execute some code start child wait for child to exit //do some more code
Posted 07 December 2012 - 11:13 AM
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int system(const char *command)
{
int status, savedErrno;
pid_t childPid;
sigset_t blockMask, origMask;
struct sigaction saIgnore, saOrigQuit, saOrigInt, saDefault;
/* (1) Is shell available? */
if(command == NULL)
{
return system(":") == 0;
}
/* Block SIGCHLD */
sigemptyset(&blockMask);
sigaddset(&blockMask, SIGCHLD);
/* (2) Only block SIGCHLD in the parent process */
sigprocmask(SIG_BLOCK, &blockMask, &origMask);
/* Ignore SIGINT and SIGQUIT */
saIgnore.sa_handler = SIG_IGN;
saIgnore.sa_flags = 0;
sigemptyset(&saIgnore.sa_mask);
/* (3) SIGINT and SIGQUIT need to be ignored in the parent process */
sigaction(SIGINT, &saIgnore, &saOrigInt);
sigaction(SIGQUIT, &saIgnore, &saOrigQuit);
switch(childPid = fork())
{
/* Error: fork failed */
case -1:
status = -1;
/* Carry on to reset signal attributes */
break;
/* Child: exec command */
case 0:
saDefault.sa_handler = SIG_DFL;
saDefault.sa_flags = 0;
sigemptyset(&saDefault.sa_mask);
/* (4) If SIGINT or SIGQUIT are ignored, reset them to SIG_DFL */
if(saOrigInt.sa_handler != SIG_IGN)
{
sigaction(SIGINT, &saDefault, NULL);
}
if(saOrigQuit.sa_handler != SIG_IGN)
{
sigaction(SIGQUIT, &saDefault, NULL);
}
/* (5) No error checking is performed */
sigprocmask(SIG_SETMASK, &origMask, NULL);
execl("/bin/sh", "sh", "-c", command, (char *) NULL);
/* (6) We could not exec the shell */
_exit(127);
/* Parent: wait for our child to terminate */
default:
/* (7) Wait specifically for the child we created */
while(waitpid(childPid, &status, 0) == -1)
{
/* If error other than EINTR, then exit loop */
if(errno != EINTR)
{
status = -1;
break;
}
}
break;
}
/* Unblock SIGCHLD, restore dispositions of SIGINT and SIGQUIT */
/* (8) The following may change 'errno', so save it */
savedErrno = errno;
/* (9) */
sigprocmask(SIG_SETMASK, &origMask, NULL);
sigaction(SIGINT, &saOrigInt, NULL);
sigaction(SIGQUIT, &saOrigQuit, NULL);
/* (10) Restore errno */
errno = savedErrno;
return status;
}
This post has been edited by vividexstance: 07 December 2012 - 11:14 AM
Posted 07 December 2012 - 12:13 PM
int shell::system(const char *command, const char* arg)
{
int PID,exitStatus;
PID = fork(); //start the forking
if(PID == 0)
{
//child process, here we will execute the new program
execl(command,arg);
}
else
{
//parent process, gotta wait for completion here
pid_t stat = waitpid(PID,&exitStatus,0);
if(WIFEXITED(stat)) //something went wrong...
{
syntax_error(string(command));
argument_error(string(arg));
}
}
return exitStatus;
}
Posted 07 December 2012 - 02:07 PM
Posted 10 December 2012 - 01:22 PM
pid_t pid;
switch(pid = fork())
{
/* Deal with error */
case -1:
perror("fork");
exit(EXIT_FAILURE);
/* Deal with child */
case 0:
/* Do something in child */
_exit(EXIT_SUCCESS);
/* Parent */
default:
/* You can put code here or just call break to fall through */
break;
}
/* The parent's code could go here */
This post has been edited by vividexstance: 10 December 2012 - 01:26 PM
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
