Presentation Topic:
Group:5,Topic:2
Kernel mode
Mode bit=0
User mode
Mode bit=1
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).
4
5
6
3
2
1Process Creation
Sleep Command
Wait Command
Sleep command using getpid
Signal Handling
Process Execution
Through appropriate system calls, such as fork or spawn,
processes used to create other processes. The process which
creates other process, is termed the parent of the other process,
while the created sub-process is termed as its child.
Fork:
• Forking refers to a function that copies the existing processes and
loads the copy and executes so that there are two identical
processes running.
Spawn:
• Spawning is the effective combination of forking + execing,
though it can be implemented with other system calls.
• A new process is created by the fork() system call. Both
processes start their execution right after the system call fork().
• Since both processes have identical but separate address
spaces, those variables initialized before the fork() call have the
same values in both address spaces.
• Since every process has its own address space, any modifications
will be independent of the others.
• Both processes (the parent and the child) continue execution at
the instruction after the fork system call, with one difference:
• The return code for the fork system call is zero for the
new(child) process. Whereas greater than zero process
identifier is returned to the parent.
System call
P r o c e s s c r e a t i o n
Algorithm for creating a process in Unix.
• STEP 1: Start the program.
• STEP 2: Declare pid as integer.
• STEP 3: Create the process using Fork command.
• STEP 4: Check pid is less than 0 then print error else if pid is equal to 0
then execute command else parent process wait for child process.
• STEP 5: Stop the program.
On Unix like operating systems, exec is a built-in command of the
Bash shell. It allows you to execute a command that completely
replaces the current process.
As a new process is not created, the process ID (PID) does not
change across an execute.
The first Unix shell was the Thompson shell, sh, written by ken-
Thompson at Bell Laboratory.
But the most widely distributed and influential of the early Unix
shells were the Bourne shell.
EXECUTING A COMMAND
(Using Shell)
First open the terminal.
write command given
below:
cat /etc/shells
This command shows the
shell type which your OS
support.
Now to know where bash
is located in yours system
write command:
which bash
echo “Program for
executing UNIX command
using shell programming”
echo “Welcome”
ps
exec wc “wc.sh”
echo “Program for
executing
UNIX command using
shell
programming”
echo “Welcome”
ps
exec wc
cs_2254_lab_manual.pdf”
A parent process usually needs to synchronize its actions by
waiting until the child process has either stopped or
terminated its actions.
The wait() system call allows the parent process to suspend
its activities until one of these actions has occurred.
• If the calling process does not have any child associated
with it, wait will return immediately with a value of -1.
• If any child processes are still active, the calling process
will suspend its activity until a child process terminates.
 Step 1: Start the execution.
 Step 2: Create process using fork and assign it to a variable.
 Step 3: Check for the condition pid is equal to 0.
 Step 4: If it is not a parent process has to wait until the child process.
 Step 5: If it is not a parent process has to wait until the child
teriminate.
 Step 6: Stop the execution.
SLEEP
SLEEP SYSTEM CALL
• On Unix-like operating systems, the sleep system
call is used to delay a process of system for a
specified amount of time.
• It takes time in seconds by default but a small
suffix(s, m, h, d) can be added at the end to
convert it into any other format.
SLEEP
• On Windows, the Sleep() function takes a
single parameter of the number of
milliseconds to sleep.
C examples
• In Windows OS:
Sleep(2*1000); // Sleep for 2 seconds
• In Unix:
sleep(2); // Sleep for 2 seconds
SLEEP
USING GETPID
SLEEP SYSTEM CALL
(using getpid)
• As we already know about sleep system call now we
have to discuss it with getpid and getppid commands.
• Both getppid() and getpid() are built-in functions.
Getppid():
Return type:
Getppid() returns the process ID of the parent of the calling
process.
SLEEP USING GETPID
For example:
when fork() function is use and the parent process still
exists at the time of the getppid function call, this
function returns the process ID of the parent process.
• Getpid() :
Returns the process ID of the calling process.
getpid() returns the process ID of the current process.
SIGNAL
HANDLING
Signal Handling
• A signal is a software interrupt delivered to a
process.
• The operating system uses signals to report
exceptional situations to an executing program.
• If you anticipate an event that causes signals, you
can define a handler function and tell the
operating system to run it when that particular
type of signal arrives.
#!/bin/bash
echo “program for performing
kill operation”
ps
echo “enter the pid”
read “pid”
echo “finished”
Thanks'

System calls

  • 1.
  • 2.
    Kernel mode Mode bit=0 Usermode Mode bit=1 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).
  • 3.
    4 5 6 3 2 1Process Creation Sleep Command WaitCommand Sleep command using getpid Signal Handling Process Execution
  • 4.
    Through appropriate systemcalls, such as fork or spawn, processes used to create other processes. The process which creates other process, is termed the parent of the other process, while the created sub-process is termed as its child. Fork: • Forking refers to a function that copies the existing processes and loads the copy and executes so that there are two identical processes running. Spawn: • Spawning is the effective combination of forking + execing, though it can be implemented with other system calls.
  • 5.
    • A newprocess is created by the fork() system call. Both processes start their execution right after the system call fork(). • Since both processes have identical but separate address spaces, those variables initialized before the fork() call have the same values in both address spaces. • Since every process has its own address space, any modifications will be independent of the others. • Both processes (the parent and the child) continue execution at the instruction after the fork system call, with one difference: • The return code for the fork system call is zero for the new(child) process. Whereas greater than zero process identifier is returned to the parent.
  • 6.
    System call P ro c e s s c r e a t i o n Algorithm for creating a process in Unix. • STEP 1: Start the program. • STEP 2: Declare pid as integer. • STEP 3: Create the process using Fork command. • STEP 4: Check pid is less than 0 then print error else if pid is equal to 0 then execute command else parent process wait for child process. • STEP 5: Stop the program.
  • 8.
    On Unix likeoperating systems, exec is a built-in command of the Bash shell. It allows you to execute a command that completely replaces the current process. As a new process is not created, the process ID (PID) does not change across an execute. The first Unix shell was the Thompson shell, sh, written by ken- Thompson at Bell Laboratory. But the most widely distributed and influential of the early Unix shells were the Bourne shell.
  • 9.
    EXECUTING A COMMAND (UsingShell) First open the terminal. write command given below: cat /etc/shells This command shows the shell type which your OS support. Now to know where bash is located in yours system write command: which bash
  • 10.
    echo “Program for executingUNIX command using shell programming” echo “Welcome” ps exec wc “wc.sh” echo “Program for executing UNIX command using shell programming” echo “Welcome” ps exec wc cs_2254_lab_manual.pdf”
  • 11.
    A parent processusually needs to synchronize its actions by waiting until the child process has either stopped or terminated its actions. The wait() system call allows the parent process to suspend its activities until one of these actions has occurred. • If the calling process does not have any child associated with it, wait will return immediately with a value of -1. • If any child processes are still active, the calling process will suspend its activity until a child process terminates.
  • 12.
     Step 1:Start the execution.  Step 2: Create process using fork and assign it to a variable.  Step 3: Check for the condition pid is equal to 0.  Step 4: If it is not a parent process has to wait until the child process.  Step 5: If it is not a parent process has to wait until the child teriminate.  Step 6: Stop the execution.
  • 14.
    SLEEP SLEEP SYSTEM CALL •On Unix-like operating systems, the sleep system call is used to delay a process of system for a specified amount of time. • It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format.
  • 15.
    SLEEP • On Windows,the Sleep() function takes a single parameter of the number of milliseconds to sleep. C examples • In Windows OS: Sleep(2*1000); // Sleep for 2 seconds • In Unix: sleep(2); // Sleep for 2 seconds
  • 17.
    SLEEP USING GETPID SLEEP SYSTEMCALL (using getpid) • As we already know about sleep system call now we have to discuss it with getpid and getppid commands. • Both getppid() and getpid() are built-in functions. Getppid(): Return type: Getppid() returns the process ID of the parent of the calling process.
  • 18.
    SLEEP USING GETPID Forexample: when fork() function is use and the parent process still exists at the time of the getppid function call, this function returns the process ID of the parent process. • Getpid() : Returns the process ID of the calling process. getpid() returns the process ID of the current process.
  • 21.
    SIGNAL HANDLING Signal Handling • Asignal is a software interrupt delivered to a process. • The operating system uses signals to report exceptional situations to an executing program. • If you anticipate an event that causes signals, you can define a handler function and tell the operating system to run it when that particular type of signal arrives.
  • 22.
    #!/bin/bash echo “program forperforming kill operation” ps echo “enter the pid” read “pid” echo “finished”
  • 23.