0% found this document useful (0 votes)
13 views7 pages

Unit5 Unix Half

The document provides an overview of UNIX system processes, detailing the definition of a process, its attributes, and the mechanisms for process creation, including forking, overlaying, and execution. It explains the parent-child relationship in processes, the use of the 'ps' command to display process information, and the concept of signals in process management. Additionally, it outlines the differences between the fork() and exec() system calls.

Uploaded by

ahassimon005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Unit5 Unix Half

The document provides an overview of UNIX system processes, detailing the definition of a process, its attributes, and the mechanisms for process creation, including forking, overlaying, and execution. It explains the parent-child relationship in processes, the use of the 'ps' command to display process information, and the concept of signals in process management. Additionally, it outlines the differences between the fork() and exec() system calls.

Uploaded by

ahassimon005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT 5

INTRODUCTION TO UNIX SYSTEM PROCESS

INTRODUCTION TO UNIX SYSTEM PROCESS

- A process is defined as a program in execution. Each process has a unique identification number
(PID) allocated to the process by the kernel.
- If process, considered as a tuple, the attributes can be as follows:

(process id, code, data, register values, pc values)


Where, process id : PID, is a unique identification number
code : is the program code under execution
data : is the data which is used during execution
register values : are the values from the CPU registers
pc values : address in the program counter from where the execution of the
program starts and continues.
- The process creates another process called ‘init’
o The PID of the init process if set to 0: creates another process
the first program starts running immediately
o The PID of the init process if set to 1: initializes all subsequent processes on the system.
- ‘init’ generates processes on log-ins

MECHANISM OF PROCESS CREATION

There are three distinct phases in creation of the processes.

1. Forking, uses the command fork ();


2. Overlaying and execution, which uses the command exec ();
3. Waiting, uses the command wait () respectively.

Forking:

- First phase in the creation of a process. fork() command is used to create separate, duplicate
process.
- Parent process makes the call to the system by using fork () command, which makes the exact
copy if itself.
- The copy will be of the memory of the calling process at the time of the fork () system call.
- after the fork () there will be two processes with identical memory images.
- Each one of these two processes has to return from the fork () system call. Thus, there will be
two return values.

Example:

Int main ()
{ fork();
echo hi }
Output: Hi Hi // first Hi belongs to parent process & second Hi belongs to the child process.

Example:

main
{
fork();
fork();
echo hi

Output: Hi Hi Hi Hi // each process creates another child process

Example:

main
{
fork();
fork();
fork()
echo hi

Output: Hi Hi Hi Hi Hi Hi // each process creates another child process

- Fork() command has 3 return values


o If the process returns value 0, the fork of the parent process creates a new process. i.e.,
the child process is successfully created.
o If the process returns value -1, the child process is not created.
o If the process returns value 1, the process returns to parent or the child. The value
contains the process Id of newly created child process.
Waiting:

- After forking, the parent makes a system call to wait() functions.


- the parent keeps waiting for the child process to complete its task.
- It awakens only when it receives a complete signal from the child, after which it will be free to
continue with its other functions.
- Then the child process inherits all the functions of the parent process except for the local
variables and will have different PID.

Overlaying and Execution:

- Second phase in creation of the process. Exec() command is used to replace the entire
process.
- This system call simply overwrites the text and data area of the child process.
- This process does the overwriting by using the text and the data of another program and then
starts executing the process.
exec() has 2 arguments:
o File / program
o Arguments of the file / program
- exec() system call has the following family functions
o cl
▪ Syntax:
Int execl (path of the file /file, arguments, …);
o clp
▪ Syntax:
Int execlp (path of the file /file, arguments, …);
o cle
▪ Syntax:
Int execle (path of the file /file, arguments, envp[] files);
o cv
▪ Syntax:
Int execv (path of the file /file, arguments, …);
o cvp
▪ Syntax:
Int execvp (path of the file /file, arguments, …);
o cvpe
▪ Syntax:
Int execvpe (path of the file /file, arguments, envp[] files);

Example

int main(int argc, int *argv[])


{
if (pid == -1)
printf(“process is not created”);
exit();
else if (pid == 0)
printf(“Hi, this is my first file = %u”, getpid());
char * argv_list[] = {“ls”, “/home”, NULL)
execv(“ls”, argv_list);
exit (0);
}

Output:

//replaces Hi – with ls command option, no change in PID

- At the end of the overlaying and execution, a call is made to the exit () function that terminates
the child process and sends a signal back to the parent process.

Difference between fork () system call and exec () system call.

Fork () Exec ()

It is a system call in C Language it is a system call in Operating System


fork () creates a new process exec () does not create a new process but runs
an executable file which already exists
PID changes for every child process execution PID does not change
Returns 3 values Returns with the replaced data of a new
- -1, if process is a failure program
- 0, if process is success
- 1, when process returns to
parent/child
PARENT AND CHILD PROCESS

- A process is responsible for generating another process.


- A process that generates another process is called the parent of the newly generated process,
called the child.
- When a parent process creates/generates a child process, a process is said to have born.
- As long as the process is active, it is said to be alive.
- Once the job of a process is over it becomes inactive and is said to be dead.

THE ‘ps’ COMMAND WITH ITS OPTIONS

- The ‘ps’ command stands for “process status”.


- The ‘ps’ command is used to display some process attributes.
- This command can be seen as the process counterpart of the file system’s ‘ls’ command.
- The command reads through the kernel’s data structures and process tables to fetch the
characteristics of processes.

Syntax:

$ps [options]

Example:

$ps

Output: PID TTY TIME CMD

<id num> console 0:00 bash

where PID : unique process ID


TTY : terminal type
TIME : cumulative time
CMD : command

476 tty03 00:00:01 login


659 tty03 00:00:01 sh
684 tty03 00:00:05 ps
Options for the ‘ps’ command:

Option Description

-a The process of all the users can be listed


-u All the processes of particular user only can
be listed, along with the user ID
-f A full listing of the processes can be obtained.
Once can trace the ancestry of different
processes also
-e All the processes, including the system
processes can be listed
-t terminal

Examples for each option:

Example 1: ps command by using the option -a (all users)


$ps -a

Output 1: PID TTY TIME CMD


476 tty03 00:00:01 login
659 tty03 00:00:01 sh
684 tty03 00:00:05 ps
717 tty03 00:00:07 ps -a

Example 2: ps command by using the option -u (user)


$ps -u

Output 2: UID PID PPID STIME TTY TIME CMD


usr 684 1010 15:12:36 tty03 00:00:05 ps
usr 717 1011 15:15:15 tty03 00:00:07 ps -a
usr 756 1011 15:30:30 tty03 00:00:10 ps -u

Example 3: ps command by using the option -f (full list)


$ps -a
Output 3: UID PID PPID STIME TTY TIME CMD
mgv 476 10111 14:38:15 tty03 00:00:01 login
bin 659 9999 14:58:59 tty03 00:00:01 sh
usr 684 1010 15:12:36 tty03 00:00:05 ps
usr 717 1011 15:15:15 tty03 00:00:07 ps -a
usr 756 1011 15:30:30 tty03 00:00:10 ps -u
usr 7 1011 15:32:30 tty03 00:00:15 ps -f

Example 4: ps command by using the option -e (every)


$ps -e
Output 4: PID TTY TIME CMD
0 ? 00:00:00 shel
1 ? 00:00:00 init
3 ? 00:00:00 swapper
476 tty03 00:00:01 login
659 tty03 00:00:01 sh
684 tty03 00:00:05 ps
717 tty03 00:00:07 ps -a

? – indicates the system processes

SIGNALS

A signal is a message sent to a program under execution, that is a process, on one of the following two
occasions.

1. When the kernel generates signals.


Eg: if error occurs / user interruption
2. During inter-process communication between any 2 processes.
Eg: child process sends signal to the parent process after termination.

Signals are identified by integers. “info kill” command can be used to see the full list of the SIGNAL
names. Some main names can be shown below.

Signal Number Name Function


1 SIGHUP Hangup: closes process communication links
2 SIGINT Interrupt: tells process to exit. Ctrl + C
3 SIGQUIT Quit: forces the process to quit. Ctrl - \
9 SIGKILL Sure kill: cannot be trapped or ignored
15 SIGTERM Software termination: default signal for the kill
command
24 SIGSTOP Stop: Ctrl - Z

You might also like