Thursday, April 11, 2013

Thread synchronization

Q: How to use pthread_cond_wait();
Ans :  
       int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 
It's used to block on a condition variable.Its called with mutex locked by the
calling thread. It atomically release mutex and cause the calling thread to block on the condition variable cond;
atomically here means "atomically with respect to access by another thread to the mutex and then the condition 
variable".Upon successful return,the mutex has been locked and is owned by the calling thread.
Spurious wakeups from the pthread_cond_wait() or pthread_cond_timedwait()functions may occur.Since the return
from pthread_cond_wait() or pthread_cond_timedwait() does not imply anything about the value of this predicate,the
predicate should be re-evaluated upon such return. That's why  conditional wait is put in while Loop to check DATA instead
of if condition. This make sures that threads comes out only id DATA has met condition,
else go in wait again.......  
 
Usage:
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 int avail = 0; 
 
Worker  Thread: 
 pthread_mutex_lock(&mutex);
 avail++; /* Let consumer know another unit is available */
 pthread_mutex_unlock(&mutex);
 if(avail == THRESHOLD)
     pthread_cond_signal(&cond); /* Wake sleeping consumer */
 
Reader Thread:
pthread_mutex_lock(&mutex);
while (avail != THRESHOLD)/* Check that shared variable is not in state we want */
 pthread_cond_wait(&cond, &mutex);
}
/* Now shared variable is in desired state; do some work */
 pthread_mutex_unlock(&mutex);
-------------------------------------------------------------------------------------
 pthread_cond_wait internally does three things :
 1) unlock the mutex specified by mutex;
 2) block the calling thread until another thread signals the condition variable cond; and
 3) relock mutex.
-------------------------------------------------------------------------------------
We can’t make any assumptions about the state of the predicate upon return from pthread_cond_wait(), 
for the following reasons:
1   Other threads may be woken up first: Perhaps several threads were waiting to acquire the mutex associated with
  the condition variable. Even if the thread that signaled the mutex set the predicate to the desired state, it is still 
  possible that another thread might acquire the mutex first and change the state of the associated shared variable(s),
  and thus the state of the predicate may be changed.

2   Designing for “loose” predicates may be simpler. Sometimes, it is easier to design applications based on condition 
  variables that indicate possibility rather than certainty. In other words, signaling a condition variable would mean
  "there may be something” for the signaled thread to do, rather than “there is something” to do. Using this approach,
  the condition variable can be signaled based on approximations of the predicate’s state, and the signaled thread 
  can ascertain if there really is something to do by rechecking the predicate.

3   Spurious wake-ups can occur. On some implementations, a thread waiting on a condition variable may be woken
  up even though no other thread actually signaled the condition variable. Such spurious wake-ups are a (rare) consequence
  of the techniques required for efficient implementation on some multiprocessor systems, and are explicitly permitted by SUSv3.
TO  Wake up ALL threads waiting use 
pthread_cond_broadcast() this will wake all threads.

Friday, April 5, 2013

exec dup2

Not all properties of the process would change across an exec call. In other words, the new process inherits a number of properties from the calling process:
  • process ID and parent process ID
  • real user ID and reall group ID
  • supplementary group IDs
  • process group ID
  • session ID
  • controlling terminal
  • time left on alarm clock
  • current working directory
  • root directory
  • file mode creation mask
  • file locks
  • process signal mask
  • pending signals
  • resource limits
  • file descriptors without close-on-exec flag set
Obviously, here we care the most about open file descriptors. While we say that exec replaces the old process with a new one, most open file descriptors would remain open.

Thursday, April 4, 2013

Zombie and Orphan Process

Zombie Process : A  child  that terminates, but has not been waited for becomes a "zombie".
 Parent does not grab status of child process using wait() or waitpid() system call.  The kernel maintains a  minimal set of information about the zombie  process  (PID,  termination  status,  resource  usage  information)  in order to allow the parent to later perform a wait to obtain information about the  child.  As long as a zombie is not removed from the system via a wait, it will consume a  slot  in   the  kernel process table, and if this table fills, it will not be possible to create further process   If a parent process terminates, then its  "zombie"  children  (if  any)  are  adopted  by
       init(8), which automatically performs a wait to remove the zombies.


Orphan Process:
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” PID: 1 . Thing to notice here is that even after re-parenting, the process still remains Orphan as the parent which created the process is dead.

Orphan process are totally different from Zombie processes. Zombie processes are the ones which are not alive but still have entry in parent table.
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.

Wednesday, April 3, 2013

EOF end of file

There is no special character as EOF at the end of ascii files
Both Windows and Linux have file systems that always know the exact length in bytes of the contents of a file, and have absolutely no need of any special character marking the file’s end.

Long back system used to read in BLOCK fashion and then they needed special character Control-Z character decimal code 26, hex 1A

 In C and C++  in file stdio.h we do have
#define EOF (-1) 
It is used as the return value of functions like int getchar(void)




.http://latedev.wordpress.com/2012/12/04/all-about-eof/

Links soft and hard

Soft Link (Synbolic link):
 Symbolic links are names that reference other files. It can refer to other directory also. Soft link can be made across file systems .
Hard Link : 
When a hard link is made, then the i-numbers of two different directory file entries point to the same inode. Thats why hard links cannot span across file systems.
Unlink command is used to delete link.
unlink on hard link decreases i -count and if only one refrence is there it will delete file
-------------------------------------------------------------
$ touch foo
$ ls -i
538639 foo
$ ln -s  foo bar   // make soft link
$ ls -i
538643 bar  538639 foo    // inode no are different
$ ln foo bar_h    // make hard link
538643 bar  538639 bar_h  538639 foo      // inode no for hard link is same as source.

$ ls -l bar_h
-rw-r--r-- 2 user1 user1 0 2011-04-03 11:18 bar_h
 2  here means no of Hard links
----------