Vous êtes sur la page 1sur 7

UNIX PROGRAMMING WITH SPECIAL EMPHASIS ON

PROCESSES

1 Unix Processes
2 Determining Information about Current Processes
3 Foreground/Background Processes
4 Terminating a Process

1 Unix Processes

 A process is an instance of running a program. If, for example, three people


are running the same program simultaneously, there are three processes
there, not just one. In fact, we might have more than one process running
even with only person executing the program, because the program can
“split into two,” making two processes out of one.
 A computer program is a passive collection of instructions, a process is the
actual execution of those instructions.
 At any given time, a typical Unix system will have many active processes,
some of which were set up when the machine was first powered up.
 Every time you issue a command, Unix starts a new process, and suspends
the current process until the new process completes.
 UNIX identifies every process by a Process Identification Number (pid)
which is assigned when the process is initiated.
 Multitasking is a method to allow multiple processes to share processors
(CPUs) and other system resources. Each CPU executes a single task at a
time.
 When we want to perform an operation on a process, we usually refer to it
by its pid.Unix is a timesharing system, which means that the processes
take turns running. Each turn is a called a time slice; on most systems this is
set at much less than one second. The reason this turns-taking approach is
used is fairness:

~1~
Process States:

Created Terminated

----------------------------------------------------------------------------------------------------
Running Main Memory

Waiting Blocked

----------------------------------------------------------------------------------------------------
Swapped out Swapped out
and Waiting and Blocked

Fig: The various process states with arrows indicating possible transitions between states

~2~
Important Process States:
1. Created: - When a process is first created, it occupies the "created" or "new"
state. In this state, the process awaits admission to the "ready" state.
2. Ready Or Running: - also called waiting or runnable. Process awaiting
execution on CPU. There may be more than one process waiting for CPU.
3. Waiting: - A process that is waiting for some event (such as I/O operation
completion or a signal).
4. Terminated Or Zombie:- process may be terminated, either from the
"running" state by completing its executing.

Additional Process States:-


There are two process states that support virtual memory. These are:-
1. Swapped out and waiting:- also called suspended and waiting. A process is
removed and placed in the virtual memory.
2. Swapped out and blocked :- also called suspended and blocked. A blocked
process can be swapped out .

Background Processing
Processing that does not take place on the screen. Background processing enables
you to process data in the background while executing other functions in parallel
on the screen. Although background processes are not visible to the user, they have
the same priority as online processes

Running background processes

To background a process, type an & at the end of the command line. For
example, the command sleep waits a given number of seconds before
continuing. Type

% sleep 10

~3~
This will wait 10 seconds before returning the command prompt %. Until the
command prompt is returned, you can do nothing except wait.

To run sleep in the background, type

% sleep 10 & [1] 6259

The & runs the job in the background and returns the prompt straight away,
allowing to run other programs while waiting for that one to finish.

Back grounding is useful for jobs which will take a long time to complete.

Foreground Processing
A foreground process is different from a background process in two ways:

1. Some foreground processes show the user an interface, through which the
user can interact with the program.
2. The user must wait for one foreground process to complete before running
another one.

To start a foreground process, enter a command at the prompt, e.g.,

$ command1

The next prompt will not appear until command1 finishes running.

 Note:- Foreground Processing is also known as Foreground Command.

Backgrounding a current foreground process


At the prompt, type

Sleep 1000

then to put it in the background, type


% bg

~4~
Listing Suspended and Background Processes
When a process is running, back grounded or suspended, it will be entered
onto a list along with a job number. Type

% jobs
An example of a job list:-

1 Suspended Sleep 1000


2 Running internet explorer
3 Running Ms Word

To restart (foreground) a suspended processes, type

% fg %jobnumber5.5 Killing a process

Terminating Or Killing A Process:-

kill (terminate or signal a process)

To terminate a running process w e use “KILL” command.


To kill a job running in the foreground, type ^C (control c). For example, run
% sleep 100
^C

To kill a suspended or background process, type

%kill %jobnumber

For example, run

%sleep 100&

%jobs

~5~
If it is job number 4, type

% kill % 4

ps (process status)

processes can be killed by finding their process numbers (PIDs) and using kill
PID_number

%sleep 1000&
%ps

PID TT S TIME COMMAND


20077 pts/5 S 0:05 sleep 1000
21563 pts/5 T 0:00 internet explorer
21873 pts/5 S 0:25 ms word

To kill the process sleep 1000, type

% kill 20077

and then type ps again to see if it has been removed from the list.

If a process refuses to be killed, uses the -9 option, i.e. type

%kill -9 20077

Then it is not possible to kill other users processes.

~6~
References:
1. http://heather.cs.ucdavis.edu/~matloff/UnixAndC/Unix/Processes.pdf
2. Wikipedia
3. Ibm.com
4. Linux.about.com

~7~

Vous aimerez peut-être aussi