System programming
system();
fork();
wait();
execl();
system()
[stdlib.h needs to be included to call system.]
system() used to invoke another C program or run commands that can run on terminal(ls,
pwd.. etc).
It takes string as argument in it.
For example system(“ls”);
If we call the system(“ls”) in between the C program it will immidiately invoke the “ls”
command and will appear the result on the terminal.
It return non zero value if the shell is available and 0 if no shell is available. But if the child
proccess is not created and its status couldnot be retrieved it return -1.
fork()
[sys/types.h and unistd.h files would required to call fork.]
Using fork(), we can make a copy of runing program. It has its own task structure that
contains PID, PPID and Program Counter but it shares the same process context with
parent process but at different logical address.The process priority vary and set by OS.
Some times the parent execute before the child process. It leads child process to orphan
state and then system Dadopt the child process. Shell dont have information about it So
shell stuck sometimes.
Fork return the child PID on success in the parent and 0 in the child. On failure it return -1.
wait()
[sys/wait.h to be included to work on wait call]
It holds the running process till the child process execute completely. So if we call wait in the
premises of parent process it will hold the parent process and let child process to be execute.
It takes one argument status and returns.
It return the pid of dead child. If any process does not have child process it will return -1
immediately.
cpid = wait(&wret);
Execl()
[unistd.h be included to call execl]
When execl calls in between any program it capture the Task Structure of the running
program and process context and reset the program counter to initial state and start another
program or commands passed as argument in the execl call.
If the execl calls in a child process it will capture the task structre but as we know fork call
wont create another process context so child process dont have its own process context. Due
to calling execl in child process created another process context. It wont overwrite the parent
process context. Remaining processes would run on thier own PID and PPID.
It takes type char string as argument. See below example to understand well:
excel(“path/filename”,”filename”,”alpha”,”ls”,”pwd”,NULL);
TEST FILE
#include<stdio.h>
printf("%s:pid:%d,ppid:%d,%s:BEGIN\n",__FILE__,getpid(),getppid(),__func__);
}
SYSTEM CALL FILE:
#include<stdio.h>
int main()
ret=fork();
switch(ret)
{
case 0:
printf("PRINT DUE TO child\n");
case -1:
perror("fork");
break;
default:
Here in this program we have used fork(),execl(),and wait() system
call all together , as soon as we call fork system call a child
process is created with ppid of the process its been created from and
pid of process next pid .
Case 1:
Now if we consider
that control is on child process then switch case 0 is executed at
switch 0 case we called execl system call and the very first argument
is file location , next is elf (executable linkable file) , next are
arguments and last is NULL.
As soon as the execl
is called in child process the TS of child is given to the new
process and a new process context (as child doesn’t have its own
process context and it shares process context of its parent) is
formed and child completely vanish .
Now control is on
parent and the parent executes till end.
Case 2.
If the control is on
parent then parent completes its execution and due wait system call
the parent pid is intact till child completes execution ( issue of :
shell getting handed doesn’t occur ).
fork(): system call creates a duplicate process known as child process.This
child process shares the same process context as of its parent
process(from where it generated).It does not have process context of its
own.The child process has its own TS(it contains pid ppid of the child process) as parent process .
fork()
system call is executed as the first command of the child process and
returns 0 and it simultaneously execute the rest of the code same as the
parent process.For example if the range of pc value is 1000 to
2000 for code segment and at 1500 the fork () system call is called then
the range of pc(process context) in code segment for child will be from
1500 to 2000.
The pid(process id) of child will be next if
available ie if pid of parent is 100 then pid of child will be 101 and
ppid(parent process id) of child will be pid of parent.
wait():-> It causes the parent process to pause its execution until a child process finishes its execution and gives an exit().
The exit code is stored out of the stack and data segment therefore it can't be accessed using pointer.
pid of child=wait(*ret);
FORK FUNCTION
Fork is used to
create child Process. A simple c program that is the parent and then
if we write fork so that the child process is generate.
In fork function
to include the headers file i.e.
#include <sys/types.h>
#include <unistd.h>
Fork
return the value
Zero(0)value +ve 1value -ve 1value
Zerovalue:- Return child
process.
+ve value:-Parent Process.
-ve value:- No child process is create due to value
Id operating system or
kernel is busy
FORK is used C1 and P1 is created
C1 P1
ORPHAN PROCESS
A process whose parent process no more exists. When the parent
process and child process executes both at a time the Parent process
will be finished or terminated without waiting for child process
completed execution. Then the child process to terminated into the
orphan process.
ADOPTION PROCESS
An orphan process is a running process whose parent process has
finished or terminated. In operating system any orphaned process will
be immediately adopted by the init system process. This is called
re-parenting.
It looks like you're new here. If you want to get involved, click one of these buttons!