Showing posts with label Operating System. Show all posts
Showing posts with label Operating System. Show all posts

Algorithms and Me: Anatomy of a Process



A process is a program in execution, with it associated are process context and executable instruction.
A process has its own data, code, stack, register and memory space. Every process has its own virtual memory address range, I/O resources, opened files etc.

Creation of a process
Most widely used method to create a process is to use 'fork' and 'exec' system calls. As mentioned earlier, every process has parent, parent uses fork system call to create exactly same copy of itself. Once new process is scheduled, it can use exec system call to execute any program it wants to.

fork is a call where one process goes in and two come out. They both start there execution from the statement just after fork call (Remember new process is exact copy, hence its PC will be same).

How to distinguish between parent and child process? fork comes to rescue there. Call to 'fork' return child process's PID to parent process while zero to child process. By having check on return value of 'fork' system call we can figure out which process is parent and which is child.

Now, fork can be a very expensive call as OS has to duplicate whole lot of information, especially the virtually memory and pages currently used by the parent process. There is one concept which is called 'Copy on Write', so fork system call will not copy any of the pages till the time one of the process tries to modify the page. This arrangement makes fork system call fast.


Other system call is exec(). It is used to start a new program, it will replace contents of process with of program binary. There are many versions of the same system call used for varying purposes.
  1. The calls with v in the name take an array parameter to specify the argv[] array of the new program.
  2. The calls with l in the name take the arguments of the new program as a variable-length argument list to the function itself.
  3. The calls with e in the name take an extra argument to provide the environment of the new program; otherwise, the program inherits the current process's environment.
  4. The calls with p in the name search the PATH environment variable to find the program if it doesn't have a directory in it (i.e. it doesn't contain a / character). Otherwise, the program name is always treated as a path to the executable
When a process creates a child process, it may or may not wait for return status of the child process.
To wait for the return status, parent process uses wait() system call. It blocks the parent process till the time one of its child returns status. Usually return status of child process is used to check if the child process terminated normally or abnormally. Child process can inform their exit status using SIGCHILD signal.
There are variants of wait() like wait3() and wait4() which are non blocking call on parent process.

Read full article from Algorithms and Me: Anatomy of a Process

What are the Zombie and the Orphan Processes and how to kill them? | LinuxG.net



the zombie (or defunct) processes are dead processes that still apear in the process table, usually because of bugs and coding errors. A zombie process remains in the operating system and does nothing until the parent process determines that the exit status is no longer needed.

When does a process turn into a zombie?
Normally, when a process finishes execution, it reports the execution status to its parent process. Until the parent process decides that the child processes exit status is not needed anymore, the child process turns into a defunct or zombie process. It does not use resources and it cannot be schuduled for execution. Sometimes the parent process keeps the child in the zombie stateto ensure that the future children processes will not receive the same PID.

You can find the zombie processes with ps aux | grep Z. The processes with Z in the STATE field are zombie processes:
$ ps aux | grep Z

http://www.geekride.com/zombie-process-defunct-linux
Killing a Zombie Process:
Well, before taking any decision of killing the Zombie process, you should wait, as it is possible that the parent process is intentionally leaving the process in a zombie state to ensure that future children that it may create will not receive the same pid. Or perhaps the parent is occupied, and will reap the child process momentarily.

If that didn’t happen then you can send a SIGCHLD signal to the parent process of zombie which will instruct parents to reap their zombie children.

# kill -s SIGCHLD <PPID>
or kill  -17 <PPID>
Even if this don’t work, then the last option you will have is to kill the parent process. You can easily find out the parent’s process ID with this command:

# ps aux -eo ppid | grep <Zombie Process ID>
# kill -9 <PPID>
So when a Zombie process loses it’s parent process, it becomes orphan and adopted by “init”. Init periodically executes the wait system call to reap any zombies with init as parent.

Q. Why I can’t kill a Zombie process with “kill” command ?
A. Zombie process is already dead, so killing them with “kill -9″ won’t help at all.
Q. Is it bad to have Zombie processes on your system ?
A. Well, as Zombie processes are not taking any resources of your system, leaving a small entry in process table, it’s not at all harmful to have Zombie processes in your system, but it may hurt you sometime under heavy load. So, it’s always better not to have them.
Q. Is Zombie process different from an Orphan process ?
A. Yes, Zombie is something which is already dead, but Orphan processes are those whose parents are dead.

Also refer to http://www.geekride.com/zombie-process-defunct-linux
Read full article from What are the Zombie and the Orphan Processes and how to kill them? | LinuxG.net

Orphan Process | Geek Ride




In Linux/Unix like operating systems, as soon as parents of any process are dead, re-parenting occurs, automatically. Re-parenting means processes whose parents are dead, means Orphaned processes, are immediately adopted by special process “init”.

A process can be orphaned either intentionally or unintentionally. Sometime a parent process exits/terminates or crashes leaving the child process still running, and then they become orphans.
Also, a process can be intentionally orphaned just to keep it running.
At the same time, when a client connects to a remote server and initiated a process, and due to some reason the client crashes unexpectedly, the process on the server becomes Orphan.

Finding a Orphan Process
ps -elf | head -1; ps -elf | awk '{if ($5 == 1 && $3 != "root") {print $0}}' | head
Is Orphan process different from an Zombie process ?
A. Yes, Orphan process are totally different from Zombie processes. Zombie processes are the ones which are not alive but still have entry in parent table.
Are Orphan processes harmful for system ?
A. Yes. Orphan processes take resources while they are in the system, and can potentially leave a server starved for resources. Having too many Orphan processes will overload the init process and can hang-up a Linux system. We can say that a normal user who has access to your Linux server is capable to easily kill your Linux server in a  minute.
Read full article from Orphan Process | Geek Ride

Operating system questions | TechInterviews



What resources are used when a thread created? How do they differ from those when a process is created?
When a thread is created the threads does not require any new resources to execute, the thread shares the resources like memory of the process to which they belong to. The benefit of code sharing is that it allows an application to have several different threads of activity all within the same address space.
Whereas a new process creation is very heavyweight because it always requires new address space to be created and even if they share the memory then the inter process communication is expensive when compared to the communication between the threads.

What is virtual memory?
Virtual memory is hardware technique where the system appears to have more memory that it actually does

What is the cause of thrashing? How does the system detect thrashing? Once it detects thrashing, what can the system do to eliminate this problem? 
Thrashing is caused by under allocation of the minimum number of pages required by a process, forcing it to continuously page fault.
The system can detect thrashing by evaluating the level of CPU utilization as compared to the level of multiprogramming.
It can be eliminated by reducing the level of multiprogramming.

What is Dispatcher?
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: Switching context, Switching to user mode, Jumping to the proper location in the user program to restart that program, dispatch latency time it takes for the dispatcher to stop one process and start another running.

What is CPU Scheduler?
Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them.

What is Context Switch?
Switching the CPU to another process requires saving the state of the old process and loading the saved state for the new process. This task is known as a context switch. Context-switch time is pure overhead, because the system does no useful work while switching.

What is cache memory?
Cache memory is random access memory (RAM) that a computer microprocessor can access more quickly than it can access regular RAM. As the microprocessor processes data, it looks first in the cache memory and if it finds the data there (from a previous reading of data), it does not have to do the more time-consuming reading of data from larger memory.

What is a Safe State and what is its use in deadlock avoidance?
When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a safe sequence of all processes. Deadlock Avoidance: ensure that a system will never enter an unsafe state.

What is fragmentation? Different types of fragmentation?
Fragmentation occurs in a dynamic memory allocation system when many of the free blocks are too small to satisfy any request.
External Fragmentation
External Fragmentation happens when a dynamic memory allocation algorithm allocates some memory and a small piece is left over that cannot be effectively used. If too much external fragmentation occurs, the amount of usable memory is drastically reduced. Total memory space exists to satisfy a request, but it is not contiguous.
Internal Fragmentation
Internal fragmentation is the space wasted inside of allocated memory blocks because of restriction on the allowed sizes of allocated blocks. Allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used

Read full article from Operating system questions | TechInterviews

Operating Systems - Interview Questions and Answers



What is thrashing?
It is a phenomenon in virtual memory schemes when the processor spends most of its time swapping pages, rather than executing instructions. This is due to an inordinate(excessive) number of page faults.

List the Coffman's conditions that lead to a deadlock.
Mutual Exclusion: Only one process may use a critical resource at a time.
Hold & Wait: A process may be allocated some resources while waiting for others.
No Pre-emption: No resource can be forcible removed from a process holding it.
Circular Wait: A closed chain of processes exist such that each process holds at least one resource needed by another process in the chain.

What are the typical elements of a process image?
User data: Modifiable part of user space. May include program data, user stack area, and programs that may be modified.
User program: The instructions to be executed.
System Stack: Each process has one or more LIFO stacks associated with it. Used to store parameters and calling addresses for procedure and system calls.
Process control Block (PCB): Info needed by the OS to control processes.

When is a system in safe state?
The set of dispatchable processes is in a safe state if there exists at least one temporal order in which all processes can be run to completion without resulting in a deadlock.

What is cycle stealing?
We encounter cycle stealing in the context of Direct Memory Access (DMA). Either the DMA controller can use the data bus when the CPU does not need it, or it may force the CPU to temporarily suspend operation. The latter technique is called cycle stealing. Note that cycle stealing can be done only at specific break points in an instruction cycle.

What is busy waiting?
The repeated execution of a loop of code while waiting for an event to occur is called busy-waiting. The CPU is not engaged in any real productive activity during this period, and the process does not progress toward completion.

What is a trap and trapdoor?
Trapdoor is a secret undocumented entry point into a program used to grant access without normal methods of access authentication. A trap is a software interrupt, usually the result of an error condition.

How are the wait/signal operations for monitor different from those for semaphores?
If a process in a monitor signal and no task is waiting on the condition variable, the signal is lost. So this allows easier program design. Whereas in semaphores, every operation affects the value of the semaphore, so the wait and signal operations should be perfectly balanced in the program

In loading programs into memory, what is the difference between load-time dynamic linking and run-time dynamic linking?
For load-time dynamic linking: Load module to be loaded is read into memory. Any reference to a target external module causes that module to be loaded and the references are updated to a relative address from the start base address of the application module.

With run-time dynamic loading: Some of the linking is postponed until actual reference during execution. Then the correct module is loaded and linked.

What are demand-paging and pre-paging?
With demand paging, a page is brought into memory only when a location on that page is actually referenced during execution. With pre-paging, pages other than the one demanded by a page fault are brought in. The selection of such pages is done based on common access patterns, especially for secondary memory devices.

What are the four layers that Windows NT have in order to achieve independence?
Hardware abstraction layer
Kernel
Subsystems
System Services.

What is SMP?
with SMP any process or threads can be assigned to any processor.

What is process spawning?
When the OS at the explicit request of another process creates a process, this action is called process spawning.

What is an idle thread?
The special thread a dispatcher will execute when no ready thread is found.

What are the reasons for process suspension?
swapping
interactive user request
timing
parent process request

List out some reasons for process termination.
Normal completion
Time limit exceeded
Memory unavailable
Bounds violation
Protection error
Arithmetic error
Time overrun
I/O failure
Invalid instruction
Privileged instruction
Data misuse
Operator or OS intervention
Parent termination.
Read full article from Operating Systems - Interview Questions and Answers

Operating System Interview Questions For Quick Reference



What is SMP?
A. SMP stands for Symmetric Multi-Processing.
in this mode of operation, any thread or process can be assigned to any processor, thereby enabling maximum reliability and efficiency. Each processor runs an identical copy of the operating system, and can communicate with the other processors as and when needed.

What are Time Sharing Systems?
A. When the CPU switches between multiple jobs in a multi-tasking fashion, it is a time-sharing system.

List some reasons for a process to terminate, besides successful completion.
A. A process could terminate early due to memory unavailability, timeout, protection error, arithmetic error, bounds violation, input-output failure, data misuse.

Read full article from Operating System Interview Questions For Quick Reference

What is the purpose of swapping?



the purpose of swapping, or paging, is to access data being stored in hard disk and to bring it into the RAM so that it can be used by the application program.

Excessive use of swapping is called thrashing and is undesirable because it lowers overall system performance, mainly because hard drives are far slower than RAM.

Read full article from What is the purpose of swapping?

Inter-process communication - Wikipedia, the free encyclopedia



inter-process communication (IPC) is a set of methods for the exchange of data among multiple threads in one or more processes. Processes may be running on one or more computers connected by a network. IPC methods are divided into methods for message passing, synchronization, shared memory, and remote procedure calls (RPC).

Anonymous Pipeline 
A two-way data stream interfaced through standard input and output and is read character by character.
a set of processes chained by their standard streams, so that the output of each process (stdout) feeds directly as input (stdin) to the next one. Each connection is implemented by an anonymous pipe.

ls -l | grep key | less

Named pipe
A pipe implemented through a file on the file system instead of standard input and output.

Named pipe makes use of the filesystem. It is explicitly created using mkfifo(), and two separate processes can access the pipe by name — one process can open it as a reader, and the other as a writer.

A traditional pipe is "unnamed" because it exists anonymously and persists only for as long as the process is running. A named pipe is system-persistent and exists beyond the life of the process and can be deleted once it is no longer being used. Processes generally attach to the named pipes (usually appearing as a file) to perform inter-process communication.

mkfifo my_pipe
gzip -9 -c < my_pipe > out.gz &

cat file > my_pipe
rm my_pipe

A named pipe can be used to transfer information from one application to another without the use of an intermediate temporary file.

In windows, anonymous pipes used in pipelining are actually named pipes with a random name.

Socket
We can use socket to send data over network interface to a different process on the same computer or to another computer.

Message queues
Message queues provide an asynchronous communications protocol, meaning that the sender and receiver of the message do not need to interact with the message queue at the same time. Messages placed onto the queue are stored until the recipient retrieves them. 
Examples of informations include Apache ActiveMQ, IBM WebSphere MQ.

Shared memory
Shared memory is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies

Semaphore
a semaphore is a variable or abstract data type that is used for controlling access, by multiple processes, to a common resource in a parallel programming environment.
Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores.

Counting semaphores are equipped with two operations, historically denoted as V (also known as signal) and P (or wait). Operation V increments the semaphore S, and operation P decrements it.

Signal
A signal is an asynchronous notification sent to a process or to a specific thread within the same process in order to notify it of an event that occurred.
When a signal is sent, the operating system interrupts the target process's normal flow of execution to deliver the signal. Execution can be interrupted during any non-atomic instruction. If the process has previously registered a signal handler, that routine is executed. Otherwise, the default signal handler is executed.

For example, the kill command send a specified signal to a specified process. 
Ctrl-C (in older Unixes, DEL) sends an INT signal (SIGINT); by default, this causes the process to terminate.

File
We can use file to communicate between multiple process.

Memory-mapped file
A file mapped to RAM and can be modified by changing memory addresses directly instead of outputting to a stream, shares same benefits as a standard file.

Semaphores vs. mutexes
Mutexes have a concept of an owner, which is the process that locked the mutex. Only the process that locked the mutex can unlock it. In contrast, a semaphore has no concept of an owner. Any process can unlock a semaphore.

Unlike semaphores, mutexes provide priority inversion safety. Since the mutex knows its current owner, it is possible to promote the priority of the owner whenever a higher-priority task starts waiting on the mutex.

Mutexes also provide deletion safety, where the process holding the mutex cannot be accidentally deleted. Semaphores do not provide this.

Read full article from Inter-process communication - Wikipedia, the free encyclopedia

Difference Between Semaphore and Monitor



What is a Semaphore?
Semaphore is a data structure that is used to provide mutual exclusion to critical sections.
Semaphores mainly support two operations called wait (historically known as P) and signal (historically known as V). The wait operation blocks a process until the semaphore is open and the signal operation allows another process (thread) to enter.

There are two types of semaphores called mutex semaphores and counting semaphores. Mutex semaphores allow a single access to a resource and counting semaphores allow multiple threads to access a resource (which has several units available).

Each semaphore is associated with a queue of waiting processes.

When the wait operation is called by a thread, if the semaphore is open, the thread can continue. If the semaphore is closed when the wait operation is called by a thread, the thread is blocked and it has to wait in the queue.

The signal operation opens a semaphore and if there is a thread already waiting in the queue, that process is allowed to proceed and if there are no threads waiting in the queue the signal is remembered for the next threads.

What is a Monitor?
A monitor is a programming language construct that is used to control access to shared data. Monitors encapsulate shared data structures, procedures (that operate on shared data structures) and synchronization between concurrent procedure invocations. A monitor makes sure that its data is not faced with unstructured accesses and guarantees that treads (which access monitor’s data through its procedures) interact in a legitimate manner.

A monitor guarantees mutual exclusion by allowing only one thread to execute any monitor procedure at a given time. If another thread tries to invoke a method in the monitor, while a thread is already executing a procedure in the monitor, then the second procedure is blocked and it has to wait in the queue.

What is the difference between Semaphore and Monitor?
Even though both the semaphores and monitors are used to achieve mutual exclusion in parallel programming environments, they differ in the techniques used to achieve this task. In monitors, the code that is used to achieve mutual exclusion is in a single place and is more structured, while code for semaphores are distributed as wait and signal function calls. Also, it is very easy to make mistakes when implementing semaphores, while there is a very little chance to make mistakes when implementing monitors. Further, monitors use condition variables, while semaphores do not.
Read full article from Difference Between Semaphore and Monitor

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts