Create Process
Create Process
1) Fork() is used to create processes. It takes no arguments and returns a process ID.
If fork() returns a negative value, the creation of a child process was unsuccessful.
fork() returns a zero to the newly created child process.
fork() returns a positive value, the process ID of the child process, to the parent.
The returned process ID is of type pid_t defined in sys/types.h. Moreover, a
process can use function getpid() to retrieve the process ID assigned to this
process.
2) Vfork() is also system call which is used to create new process similar to fork().
New process created by vfork is child process.
Process that invoked vfork()system call is parent process.
Child process suspends execution of parent process, until child process
completes its execution.
Both processes share same address space.
3)Exec() is used to execute a file in active process.
When exec() is called, it replaces old program from the process with a new
program.
The new program is loaded in same process space with same PID, but data,
code, heap ,stack memory replaced with newly program.
Exec system call family:
int execl(const char* path, const char* arg, …)
int execlp(const char* file, const char* arg, …)
int execle(const char* path, const char* arg, …, char* const envp[])
int execv(const char* path, const char* argv[])
int execvp(const char* file, const char* argv[])
int execvpe(const char* file, const char* argv[], char *const envp[])
Fork() Vfork()
1)In fork()system call child &parent In Vfork() system call child & parent
process have separate memory space. process share same address space.
2)The child process and parent process get Once child process is executed then parent
executed simultaneously. process starts its execution.
3)Child process doesn’t suspend parent Child process suspend parent process
process execution in fork() system call. execution in vfork() system call.
Fork() Exec()
1)It is a command that allows a process to It is a command that makes a new process
copy itself. by replacing the existing process.
2)The parent and child processes are in The child address space replaces the
separate address space in the fork() parent address space in the exec()
3)There is a child process and a parent There is only a child process after calling
process after calling the fork() function. the exec().
4)The fork() makes a child's process equal The exec makes a child process and
to the parent's process. replaces it with the parent process.
// C Program to illustrate use of fork() and exec() system call for process creation
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<sys/wait.h>
Int main()
{
pid_t pid; //process id
int ret = 1,status;
pid = fork();
if(pid == -1) //-1 means error occurred in fork
{
printf("can't fork, error occurred\n");
exit(EXIT_FAILURE);
}
else if (pid == 0) // pid == 0 means child process created
{
printf("child process, pid = %u\n",getpid()); //returns
process id of calling process(child process)
printf("parent of child process, pid = %u\n",getppid());
//parent process id
char * argv_list[] = {"ls","-lart","/home",NULL};
execv("ls",argv_list);
exit(0);
}
else{
printf("Parent Of parent process, pid = %u\n",getppid());
printf("parent process, pid = %u\n",getpid());
if (waitpid(pid, &status, 0) > 0)
{
if (WIFEXITED(status) && !WEXITSTATUS(status))
printf("program execution successful\n");
else if (WIFEXITED(status) && WEXITSTATUS(status))
{
if (WEXITSTATUS(status) == 127)
{
printf("execv failed\n");
}
elseif
printf("program terminated normally,"
" but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else {
printf("waitpid() failed\n");
}
exit(0);
}
return 0;
}
Output: