Hawassa University Chapter 2 Linux Operating System Project
Hawassa University Chapter 2 Linux Operating System Project
For Example:
lets create a single process in c language .
# include <stdio.h>
# include <sys/types.h>
# include <unistd.h>
Int main ()
Then after that in the terminal by typing gcc(it is the compiler used to
compile c language in linux operating system) and let say the above c
program is saved in linux.c folder in the desktop so when we apply
the gcc command and pressing enter the c file would be compiled
and an executable file would generated (it is the output file) so
inorder to run this program we should type this command ./a.out
and press enter then we get this:
// output
Now let’s add the fork system call in the main function:
# include <stdio.h>
# include <sys/types.h>
# include <unistd.h>
Int main ()
Fork ();
Printf(“ hello Linux ! \n PID =%d\n”, getpid());
// output
Finally let’s call fork system call three times in main function.
# include <stdio.h>
# include <sys/types.h>
# include <unistd.h>
Int main ()
Fork ();
Fork ();
Fork ();
// output
Killing commands:
Killall
Kill-9
Xkill
Pkill
ps –ef command
This command is used to find the PID (Process ID, Unique
number of the process) of the process. Each process will have
the unique number which is called as PID of the process.
Top command
This command shows all running process with their
cpu usage and other resources that they used.
2.6 process Resource allocation and
context switching in linux.
The Operating System allocates resources when a program needs
them. When the program terminates, the resources are de-allocated,
and allocated to other programs that need them. Now the question
is, what strategy does the operating system use to allocate these
resources to user programs?
Advantages:
Allocated resources are not wasted.
Any resource requirement can be fulfilled if the resource is free
(unlike Partitioning approach)
Disadvantages:
Overhead of allocating and de-allocating the resources on
every request and release.
Context switching
Interrupts cause the operating system to change a CPU from its
current task and to run a kernel routine. Such operations happen
frequently on general-purpose systems. When an interrupt occurs,
the system needs to save the current context of the process
currently running on the cpu so that it can restore that context
when its processing is done, basically suspending the process and
then resuming it.
The context is represented in the PCB of the process.
Switching the cpu to another process requires performing a state
save of the current process and a state restore of a different
process.
Part 2: threads
2.7 Introduction to Linux threads
A thread is a basic unit of CPU utilization which comprises a thread
id, a program counter, a register set and a stack. It shares with
other threads belonging to same process its code section, data
section, and other operating system resources, such as open files
and signals. A traditional (heavyweight) process has a single thread
of control. But if a process has multiple threads of control, it can
perform more than one task at a time.
A thread can also explicitly terminate itself or terminate any other thread
in the process, using a mechanism called cancelation. Because all
threads share the same data space, a thread must perform cleanup
operations at termination time; the threads library provides cleanup
handlers for this purpose.
Exiting a thread
A process can exit at any time when a thread calls the exit subroutine.
Similarly, a thread can exit at any time by calling
the pthread_exit subroutine.
Implementation of threads
User - Level Threads
The user-level threads are implemented by users and the kernel is not
aware of the existence of these threads. It handles them as if they were
single-threaded processes. User-level threads are small and much faster
than kernel level threads. They are represented by a program
counter(PC), stack, registers and a small process control block. Also,
there is no kernel involvement in synchronization for user-level threads.
Advantages of User-Level Threads
Some of the advantages of user-level threads are as follows −
User-level threads are easier and faster to create than kernel-level
threads. They can also be more easily managed.
User-level threads can be run on any operating system.
There are no kernel mode privileges required for thread switching in
user-level threads.
Disadvantages of User-Level Threads
Some of the disadvantages of user-level threads are as follows −
Multithreaded applications in user-level threads cannot use
multiprocessing to their advantage.
The entire process is blocked if one user-level thread performs
blocking operation.
Kernel-Level Threads
Kernel-level threads are handled by the operating system directly and
the thread management is done by the kernel. The context information
for the process as well as the process threads is all managed by the
kernel. Because of this, kernel-level threads are slower than user-level
threads.
Advantages of Kernel-Level Threads
Some of the advantages of kernel-level threads are as follows −
Multiple threads of the same process can be scheduled on different
processors in kernel-level threads.
The kernel routines can also be multithreaded.
If a kernel-level thread is blocked, another thread of the same
process can be scheduled by the kernel.
Disadvantages of Kernel-Level Threads
Some of the disadvantages of kernel-level threads are as follows −
A mode switch to kernel mode is required to transfer control from
one thread to another in a process.
Kernel-level threads are slower to create as well as manage as
compared to user-level threads.