2. 1. Demonstrate the use of fork system call.
• Fork system call is used to create a new process, called the child
process.
• Child process runs concurrently with the process that makes the
fork() call, i.e. parent process.
• After a new child process is created, both processes will execute the
next instruction followed by the fork() system call.
• The fork() takes no parameters and returns an integer value.
• If the value < 0, then the creation of the child process failed.
• If value = 0, then returned to the newly created child process
• If value > 0, then parent is returned, and value = PID of child
process.
3. What is the output of the
following program?
A)
int main()
{
fork();
printf("nHellon");
return 0;
}
5. B)
int main()
{
fork();
fork();
printf("nHellon");
return 0;
}
Parent
Process
Parent
Process
Child
Process
(C1)
Parent
Process
Child
Process
(C2)
Child
Process
(C1)
Child
Process
(C3)
14. 4.Demonstrate use of execl, execv, execve, execlp,
execvp, execle.
Execl :
execl() call replaces the image of the current process with
a new process image specified by the path i.e. the current
process code gets replaced by a new process code.
15. Execv(path,argv) :
causes the current process to abandon the program that it is
running and start running the program in file path. Parameter
argv is the argument vector for the command, with a null
pointer at the end. It is an array of strings.
Execve :
execve() executes the program pointed to by filename.
filename must be either a binary executable,
16. execlp :
The execlp function is most commonly used to overlay a
process image that has been created by a call to the fork
function. identifies the location of the new process image
within the hierarchical file system (HFS).
17. execvp and execle :
These two also serve the same purpose but the syntax of them
are a bit different from all the above members of exec family.
The syntaxes of both of them are shown below :
Syntax: int execvpe(const char *file, char *const argv[],char
*const envp[]);
Syntax:int execle(const char path, const char *arg, .../, (char
*) NULL, char * const envp[] */);
18. 5. Demonstrate Zombie Process and Orphan Process
Zombie Process: A Process Which Terminate But Whose
Parent Not Yet Called Wait() is Known As Zombie Process
Once The Parent Calls Wait() the Process Identifier of the
Process and its entry in the Zombie process the table is
released
A child Process Always First Becomes a Zombie Before
Removed From the Process Table
19. #include<stdio.h>
#include<unistd.h>
int main()
{
pid_t t;
t=fork();
if(t==0)
{
printf("Child having id %dn",getpid());
}
else
{
printf("Parent having id %dn",getpid());
sleep(15); // Parent sleeps. Run the ps command
}
}
21. Orphan Process :
A Process Whose Parent Process no More Exist i.e. either finished or
Terminated Without Waiting for Its Child Process To Terminate Is
Called an Orphan Process
The Init Process Is Assigned As The New Parent To Orphan Process
The Init Process Periodically Invokes wait(), There By Allowing The
Exit Status Of Any Orphaned Process to be collected and Relesing
Orphans Process identifier and Process Entry Table
22. #include<stdio.h >
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t p;
p=fork();
if(p==0)
{
sleep(5); //child goes to sleep and in the mean time parent
terminates
printf("I am child having PID %dn",getpid());
printf("My parent PID is %dn",getppid());
}
else
{
24. 6. Develop user defined function which allow to run
binary file and give control back to your parent process
similar to library function system
Ex1.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
Int main(int argc,char *argv[])
{
Printf(“PID of EX1.c= %dn”, getpid());
Char *args[] = {“Hello”,”Vahora”,”Irbaz”,NULL};
Execv(“./ex2”,args);
Printf(“Back To ex1.c”);
Return 0;
}
25. Ex2.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
Int main(int argc,char *argv[])
{
Printf(“We Are In Ex2.cn”)
Printf(“PID of EX2.c= %dn”, getpid());
Return 0;
}