semaphore and mutex/pthreads

Diff between semaphore and mutex.
Defn-1. Then a mutex is usually used for mutual exclusion of critical paths, while a semaphore is used for inter-thread synchronization and   event notifications.
Defn-2. A "mutex" (or "mutual exclusion lock") is a signal that two or more
asynchronous processes can use to reserve a shared resource for exclusive use.
 
The first process that obtains ownership of the "mutex" also obtains ownership of the shared resource. Other processes must wait for for the first process to release it's ownership of the "mutex" before they may attempt to obtain it.
 
Finally, a "semaphore" is a sort of "mutex" that is used to signal the availability of a plentiful resource. The source of the resource adds one to the semaphore for each unused resource available. As resources are taken away, the count in the semaphore is decremented (one for each removed resource) until it reaches zero (meaning that no more resources are available). At this
point, any remaining consumers of the resource must wait until the "semaphore" increments above zero, indicating that more resources are available.

Reference:
PThreads Primer
PThreads Manual

Diff between TCP and UDP

diff1=As a connectionless protocol, UDP does not incorporate any connection establishment, teardown, or maintenance logic. For example, a server supporting a UDP application with multiple simultaneous clients will need to allocate less memory to support these clients than would the corresponding TCP application server. Bandwidth throttling and in-order delivery features are properties of TCP, not UDP.

diff2= User Datagram Protocol (UDP)
The User Datagram Protocol (UDP) is a connectionless transport-layer protocol (Layer 4) that belongs to the Internet protocol family. UDP is basically an interface between IP and upper-layer processes. UDP protocol ports distinguish multiple applications running on a single device from one another.

Unlike the TCP, UDP adds no reliability, flow-control, or error-recovery functions to IP. Because of UDP's simplicity, UDP headers contain fewer bytes and consume less network overhead than TCP.

UDP is useful in situations where the reliability mechanisms of TCP are not necessary, such as in cases where a higher-layer protocol might provide error and flow control.
UDP is the transport protocol for several well-known application-layer protocols, including Network File System (NFS), Simple Network Management Protocol (SNMP), Domain Name System (DNS), and Trivial File Transfer Protocol (TFTP).

Core Java KB- Learning Journal



Aug 16 2006; Answers - last edit oct 15 2006


Diff between tcp and udp 
The 2 Transport layer protocols are, TCP and UDP. TCP provides connection-oriented, reliable, byte-stream packet delivery, while UDP provides connectionless, unreliable, byte-stream packet delivery. Also in both protocol forms,  the data payload are segmented into finite size fixed bytes packets.
What that means is connection-oriented protocol like TCP establish an end-to-end link along the intermediate nodes before any data moves. This ESTABLISHED link of nodes ( routers and switches) provides a secure and traceable pathway when data is moved in the form of electric signals along the wire over long distances.
A connectionless protocol [UDP] doesn't establish paths across the network before data can flow. Instead, the protocol routes connectionless packets or datagrams individually at each intermediate node.
  1. correct data corruption.
  2. packet arrive out of sequence at destination
  3. acknowledgements, retransmissions, wait timings
  4. duplicates
Reliable protocols safeguard against several forms of transmission mishaps/issues.

Transmission issue.1=data corruption
Solution.1=do checksum You can compare checksums included with a packet's data payload with a recalculation of the checksum algorithm at the destination to detect corrupted data. You must retransmit corrupted or lost data, so the protocol must provide methods for the destination to signal the source when retransmission is needed.

Transmission issue.2=packet arrive out of sequence at destination
Solution.2=buffer and order the packets correctly. Packetized data can arrive out of sequence, so the protocol must have a way to detect out-of-sequence packets, buffer them, and pass them to the Application layer in the correct order. 


Transmission issue.3=duplicates
solution.3=discard dups It must also detect and discard duplicate transmissions.


Transmission issue.4=acknowledgements, retransmissions, wait timings
solution.4=use timers. A collection of timers enables limiting the wait for various acknowledgements, so you can initiate retransmissions or link re-establishment. 

Byte-stream protocols don't specifically support data units other than bytes. TCP can't structure bytes of the data payload in a packet, nor can it cope with individual bits. As far as TCP is concerned, it's responsible for transporting an unstructured string of 8-bit bytes.

A connectionless protocol [UDP] doesn't establish paths across the network before data can flow. Instead, the protocol routes connectionless packets or datagrams individually at each intermediate node.


Without an end-to-end link, a connectionless protocol such as UDP isn't reliable. When a UDP packet moves into the network, the sending process can't know whether the packet arrives at its destination unless the Application layer acknowledges this fact. Nor can the protocol detect duplicate or out-of-sequence packets. The standard jargon describes UDP as "unreliable," though a more descriptive term might be "nonreliable." On modern networks, UDP traffic isn't prone to disruption, but you can't really call it "reliable," either. 



Aug 16 2006;
Questions and Answers

1. Explain in context of java  - final, finally, finalize
2. What is deadlock,cause of deadlock, steps to avoid to it.  
3. Diff between semaphore and mutex.
4. What is difference between object.wait() and thead.join()?
5. Diff between public static synchronized vs public synchronized
6. What is surrogate primary key? what is candidate key?
7. What is functional dependency? transitive dependency?
8. log4j vs java logging.
9. difference between
a.java.sql.statement
b. prepared statement
c. callable statement
10. a class does not marked as serializable participate in the ejb call as return object. caller fails with throwing exceptions. how to fix it?
a. java.rmi.remote interface.
11. what is difference between java.rmi.remote and java.lang.Serializable.
12. new string s;
a.  s.intern();
13. difference between java == and equals().
14. How do you make the software code you wrote is good?
a.      junit.
b.      basic smoke/functional testing testcases with inputs.
15.   what is your preferred OS development environment? what is your preferred editor vi or emacs?