You are on page 1of 17

Process Control

system calls

Diksha Bobade.
System Call:-
 System call is a programmatic way in which a
computer program requests a service from the
kernel of the operating system it is executed on.
 A system call is a way for programs to interact
with the operating system.
 A computer program makes a system call when
it makes a request to the operating system’s
kernel.
 System call provides the services of the
operating system to the user programs via
Application Program Interface(API).
Types of System calls:-
 Process Control:-
These system calls deal with processes such as process
creation, process termination etc.
 File Management:-
These system calls are responsible for file manipulation such
as creating a file, reading a file, writing into a file etc.
 Device Management :- These system calls are used to
manage and manipulate I/O devices such as printers,
keyboards, and disk drives. Examples include ioctl() and
select().
 Information Management : These system calls are used to
retrieve information about the system, the processes running
on the system, and the status of various resources. Examples
include getuid(), getgid(), and getpid().
 Communication
These system calls are used for inter-process communication
(IPC) and resource working. Examples include socket(), bind(),
listen(), and accept().
Services Provided by System Calls:-

 Process creation and management


 Main memory management
 File Access, Directory, and File system

management
 Device handling(I/O)
 Protection
 Networking, etc.
Process Control System Calls:-
 Process control system calls are used to
create and manage processes.
 1.Fork

2.Wait
3.waitpid()
4.Wait 3
5.Waitid
fork() in C:-

 The Fork system call is used for creating a new


process in Linux, and Unix systems, which is called
the Child Process,
 which runs concurrently with the process that makes
the fork() call (parent process).
 After a new child process is created, both processes
will execute the next instruction following the fork()
system call.
 The child process uses the same pc(program
counter), same CPU registers, and same open files
which use in the parent process.
 It takes no parameters and returns an integer value.
 Below are different values returned by fork():-
• Negative Value: The creation of a child process was
unsuccessful.
• Zero: Returned to the newly created child process.
• Positive value: Returned to parent or caller. The
value contains the process ID of the newly created
child process.
 Example of fork() in C:-
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{

// make two process which run same


// program after this instruction
pid_t p = fork();
if(p<0){

{
perror("fork fail");
exit(1);
}
printf("Hello world!, process_id(pid) = %d \n",getpid());
return 0;
}
 Output:-
Hello world!, process_id(pid) = 31 Hello world!, process_id(pid) = 32
Wait System Call in C
 A call to wait() blocks the calling process until one
of its child processes exits or a signal is received.
 After child process terminates, parent continues its
execution after wait system call instruction.
 Child process may terminate due to any of these:
• It calls exit();
• It returns (an int) from main
• It receives a signal (from the OS or another process)
whose default action is to terminate.
Wait()
 // C program to demonstrate working of wait()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>

int main()
{
pid_t cpid;
if (fork()== 0)
exit(0); /* terminate child */
else
cpid = wait(NULL); /* reaping parent */
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);

return 0;
}
Waitpid():-

 The waitpid() system call suspends execution of the


current process until a child specified by pid argument
has changed state.
 By default, waitpid() waits only for terminated children,
but this behaviour is modifiable via the options argument.
 Waitpid Specifies what to wait for :-

• pid < -1Wait for a child whose GID is equal to the


absolute value of pid
• pid = -1Wait for any child to finish - same behavior as
wait( )
• pid = 0Wait for a child whose GID is the same as the
calling process
• pid > 0Wait for child whose PID equals pid
Syntax:-
Below, we can see the syntax for the waitpid() system call.
pid_t·waitpid(pid_t·pid,·int·*status_ptr,·int·
options);

Waitpid()
Waitid():-
 Waitid() - wait for a child process to change state

 SYNOPSIS
#include <sys/wait.h>
 int waitid(idtype_t idtype, id_t id, siginfo_t
*iThe waitid()

 function suspends the calling thread until one child of


the process containing the calling thread changes state.
 It records the current state of a child in the structure
pointed to by infop.
 If a child process changed state prior to the call
to waitid(), waitid() returns immediately.
 .
 If more than one thread is suspended in wait() or
waitpid() waiting termination of the same process,
exactly one thread will return the process status at
the time of the target process terminationThe idtype
and id arguments are used to specify which
children waitid() will wait for
• If idtype is P_PID, waitid() will wait for the child with a process ID
equal to (pid_t)id.
• If idtype is P_PGID, waitid() will wait for any child with a process
group ID equal to (pid_t)id.
• If idtype is P_ALL, waitid() will wait for any children and id is
ignored.
 RETURN VALUE:-
If waitid() returns due to the change of state of one of its
children, 0 is returned. Otherwise, -1 is returned
and errno is set to indicate the error.
Thank You !

You might also like