Network Programming - Unit - 1
Network Programming - Unit - 1
part-1
Overview of UNIX
Unix is a powerful, multiuser, multitasking operating system originally developed in the 1960s and 1970s
at Bell Labs, the research and development subsidiary of AT&T. It has since become a foundational
influence in modern computing, with many of its design principles and core concepts inspiring numerous
other operating systems, including Linux, BSD (Berkeley Software Distribution), and even macOS.
2. **Shell**: The shell is a command-line interface (CLI) that allows users to interact with the operating
system by entering text commands. Various shell types are available in Unix, such as the Bourne Shell
(sh), C Shell (csh), and Korn Shell (ksh).
3. **File System**: Unix organizes data in a hierarchical file system. Everything in Unix is considered a
file, including hardware devices. Files are organized into directories (folders), with a single root directory
("/") at the top.
5. **Portability**: Unix was one of the first operating systems written in the C programming language,
which made it highly portable. It can run on different types of hardware with minimal modifications.
6. **Security**: Unix has robust security features, including user-based permissions. Files and
directories can be assigned read, write, and execute permissions for the owner, group, and others. This
helps maintain security and privacy in a multiuser environment.
7. **Tools and Utilities**: Unix is known for its philosophy of providing small, modular utilities that
perform specific tasks well. These utilities can be combined using pipes (|) to accomplish more complex
tasks.
8. **Networking**: Unix was one of the first operating systems to include networking capabilities as a
core feature. It supports various networking protocols, making it suitable for networked environments.
9. **Scripting and Automation**: Unix allows users to write scripts in the shell to automate tasks. Shell
scripting is a powerful tool for system administration and repetitive tasks.
- **System V**: Another major branch of Unix, developed by AT&T, which introduced features like the
System V init system.
- **Linux**: While not technically Unix, Linux is a Unix-like operating system that follows Unix principles
and is widely used today.
- **Do One Thing Well**: Each Unix utility is designed to perform one task and do it well.
- **Everything is a File**: Devices, processes, and other resources are treated as files, providing a
uniform interface for interaction.
- **Write Programs to Work Together**: Unix encourages building small tools that can be combined to
perform more complex tasks.
- **Development**: Many software developers prefer Unix-based systems for their development
environments because of the availability of powerful tools and flexibility.
- **Education**: Unix is commonly used in computer science education to teach operating system
principles.
Unix's ability to evolve and adapt over the decades has solidified its place as a cornerstone of modern
computing.
In UNIX, each process runs in its own environment, which consists of several components that define
the context in which the process operates. Here's a breakdown of the environment of a UNIX process:
- Each process is assigned a unique Process ID (PID) that identifies it within the system.
- The PID of the process that created (or spawned) the current process. This parent-child relationship is
a key feature in UNIX process management.
- **Effective User ID (EUID):** Used for permission checks. It might differ from the UID if the process
has elevated privileges (e.g., setuid programs).
- **Group ID (GID):** The group ID associated with the process, which also influences access
permissions.
- The current state of the process, such as running, sleeping, stopped, or zombie.
- A set of key-value pairs that define the environment in which a process runs. These variables can
include paths to executables (`PATH`), configuration settings, and other dynamic values.
- A list of open files (or resources like sockets) that the process is currently using. Standard file
descriptors include:
- The current directory in which the process is operating. This is also known as the current working
directory (CWD).
- Processes can send and receive signals, which are used to notify a process of events like interruptions
or the need to terminate.
- Information about the process's priority, CPU usage, and other scheduling-related details.
- Limits on the resources that a process can use, such as CPU time, memory usage, and the number of
open files. These limits are often managed through tools like `ulimit`.
- The virtual memory space assigned to the process, which includes the text, data, heap, and stack
segments.
- UNIX processes can control other processes via system calls like `fork()`, `exec()`, `wait()`, and `kill()`.
- If the process is multithreaded, it may have multiple threads, each with its own thread ID (TID), stack,
and state, but sharing the same address space.
- Processes have associated timers (e.g., for time slices in CPU scheduling) and counters (e.g., for
measuring CPU time used).
These components collectively form the environment in which a UNIX process operates, allowing it to
interact with the system, manage resources, and perform tasks.
- **Forking**: The `fork()` system call is used to create a new process. When a process forks, it creates
a child process that is a duplicate of the parent. The child process gets its own unique process ID (PID).
- **Executing a Program**: The `exec()` family of system calls (e.g., `execl()`, `execv()`, `execvp()`) is
used to replace the current process image with a new program. After a successful exec call, the old
program is no longer running, and the new program takes over.
- **Sleeping**: The process is not running but is waiting for an event (e.g., I/O operation to complete).
- **Zombie**: The process has terminated, but its parent has not yet read its exit status.
- **PPID (Parent Process ID)**: The PID of the parent process that created the current process.
- **Foreground Process**: A process that runs in the foreground, interacting with the terminal.
- **Background Process**: A process that runs in the background, allowing the user to continue using
the terminal.
- Commands:
- `&`: Appending `&` to a command runs it in the background. For example, `./script.sh &`.
- **top**: Provides a dynamic, real-time view of running processes, along with system resource usage.
- **kill**: Sends a signal to a process, typically to terminate it. Usage: `kill PID`.
### 6. **Signals**
- **exit()**: A process can terminate itself using the `exit()` system call. The exit status can be checked
by the parent process.
- **wait()**: The parent process can wait for a child process to finish using the `wait()` system call. It
retrieves the child's exit status and releases the process resources.
These tools and concepts provide a robust framework for managing and controlling processes in a Unix
environment, allowing administrators and users to efficiently run, monitor, and control the tasks on
their systems.
- **Verbal Cues:** Listen to how your partner communicates. Are they open, honest, and
respectful? Or is there a lot of criticism, defensiveness, or stonewalling?
- **Tone and Language:** Pay attention to the tone of voice, choice of words, and frequency
of communication. A shift towards sarcasm, irritability, or avoidance can indicate underlying issues.
- **Affection and Care:** Positive signals like affection, acts of kindness, and expressions of
love are indicators of a healthy relationship. A decline in these behaviors can be concerning.
- **Consistency:** Observe any changes in behavior, such as becoming more distant, spending
less time together, or avoiding conversations.
- **Effort and Engagement:** A healthy relationship involves both partners putting in effort. If
one person seems less interested or disengaged, it could be a signal of dissatisfaction.
- **Body Language:** Non-verbal signals like eye contact, physical touch, and facial
expressions are important. Positive body language (e.g., leaning in, smiling) indicates a connection, while
negative body language (e.g., crossed arms, avoiding eye contact) can signal tension or discomfort.
- **Physical Proximity:** How close or distant someone is physically can reflect their
emotional state. Increased physical distance can be a red flag.
- **Reliability:** Pay attention to whether your partner follows through on promises and
commitments. Consistent reliability builds trust, while broken promises can erode it.
- **Transparency:** Openness and honesty are crucial. If you notice secrecy, avoidance, or
dishonesty, it may be a sign of deeper issues.
- **Shared Goals:** Discussing and planning for the future together is a positive signal. If one
partner is hesitant or avoids such conversations, it may indicate uncertainty about the relationship's
future.
- **Alignment of Values:** Ensure that both partners' values and goals align. Misalignment
can create tension and long-term dissatisfaction.
- **Social Circle and Family:** How your partner interacts with your friends and family can be
telling. A positive relationship with your social circle is a good sign, while avoidance or negativity can be
concerning.
- **Life Stressors:** External factors like work stress, health issues, or financial difficulties can
impact the relationship. Understanding these influences helps in processing signals that may be
situational rather than relational.
- **Intuition:** Trust your instincts. If something feels off, it’s worth exploring. Often, your
subconscious picks up on signals that your conscious mind may not immediately recognize.
- **Feedback:** Consider feedback from trusted friends or family who observe your
relationship. Sometimes an outside perspective can highlight things you may not see.
- **Self-Reflection:** Regularly assess your feelings, needs, and satisfaction within the
relationship. If you're consistently feeling unhappy or unfulfilled, it's a signal that something needs
attention.
### Conclusion:
- **Anonymous Pipes**: These are unidirectional and are used for communication between a parent
process and a child process.
- **Named Pipes (FIFOs)**: These can be used for communication between unrelated processes and
can be bidirectional.
2. **Message Queues**:
- Allow processes to send and receive messages in a queue format. This is useful for asynchronous
communication where processes don't need to wait for each other.
3. **Shared Memory**:
- This is the fastest form of IPC because it allows multiple processes to access the same memory space.
However, it requires explicit synchronization (e.g., semaphores) to avoid race conditions.
4. **Semaphores**:
- These are used to manage access to shared resources by multiple processes. They help in preventing
race conditions by allowing only one process to access a critical section of code at a time.
5. **Sockets**:
- Provide a way for processes to communicate over a network or on the same machine. They can be
used for both connection-oriented (TCP) and connectionless (UDP) communication.
6. **Signals**:
- A limited form of IPC where a process can send simple notifications to another process. Signals are
used primarily for interrupting or terminating processes.
- Enable processes to call functions or procedures on another process, often on a different machine.
This abstracts the details of the communication, making it look like a local function call.
8. **Memory-Mapped Files**:
- Allows multiple processes to access the same file as if it were memory, enabling fast sharing of data.
This is particularly useful when large datasets need to be accessed by multiple processes.
- **Parallel Processing**: Where processes need to share data or synchronize their actions.
- **Event Notification**: Where one process needs to notify another about a certain event (e.g., file
modification).
- **Access Control**: Ensuring that only authorized processes can communicate or access shared
resources.
- **Race Conditions**: Proper synchronization to avoid issues where processes access shared resources
simultaneously.
Different IPC mechanisms have their advantages and trade-offs in terms of performance, complexity,
and suitability for various applications. The choice of IPC method depends on the specific needs of the
system being designed.
- The TCP/IP model consists of four layers, each with specific functions and protocols. These layers
work together to handle different aspects of network communication.
- **Function:** This is the top layer where network applications (like web browsers, email clients,
and file transfer programs) operate.
- **Protocols:** HTTP (HyperText Transfer Protocol), FTP (File Transfer Protocol), SMTP (Simple Mail
Transfer Protocol), DNS (Domain Name System), etc.
- **Function:** Manages the end-to-end communication and data transfer between devices. It
ensures that data is delivered reliably and in the correct sequence.
- **Protocols:**
- **UDP (User Datagram Protocol):** Provides connectionless, faster, but less reliable
communication. It is used for applications like streaming where speed is prioritized over reliability.
**c. Internet Layer:**
- **Function:** Handles the logical addressing, routing, and forwarding of data packets across
networks.
- **Protocols:**
- **IP (Internet Protocol):** Responsible for addressing and routing packets. It defines the IP
addresses used to identify devices.
- **IPv6:** The newer version using 128-bit addresses, allowing for a much larger address space.
- **ICMP (Internet Control Message Protocol):** Used for diagnostic and error-reporting messages
(e.g., "ping" command).
- **Function:** Handles the physical transmission of data over network hardware. It deals with
hardware addressing and defines how data is transmitted over physical media (like Ethernet cables, Wi-
Fi, etc.).
- **Protocols:** Ethernet, Wi-Fi (IEEE 802.11), ARP (Address Resolution Protocol), etc.
- **Data Encapsulation:** As data moves down the layers of the TCP/IP model, it is encapsulated with
the necessary headers and trailers. For example, the application layer data is encapsulated in a transport
layer segment, which is further encapsulated in an IP packet, and then in a frame at the network access
layer.
- **Routing and Forwarding:** Routers use IP addresses to determine the best path for data packets
to travel across different networks until they reach their destination.
- **Error Checking and Retransmission (TCP):** TCP ensures that data is transmitted reliably by
checking for errors and retransmitting lost or corrupted packets.
- The TCP/IP model is often compared to the OSI (Open Systems Interconnection) model, which is a
more detailed seven-layer framework. However, TCP/IP is more widely used in practice because it aligns
closely with the protocols implemented on the internet.
4. **Importance of TCP/IP:**
- **Scalability:** TCP/IP allows networks of all sizes to interconnect, from small local networks to the
global internet.
- **Interoperability:** It provides a universal language for different types of devices to communicate,
regardless of their underlying hardware or operating systems.
- **Flexibility:** TCP/IP can support various types of communication (e.g., unicast, multicast,
broadcast) and can be adapted to different media and environments.
TCP/IP is the backbone of modern networking, facilitating communication between billions of devices
worldwide. Its layered structure, with specific protocols handling different tasks, ensures that data can
be transmitted reliably and efficiently across diverse networks.
Network architecture
Network architecture refers to the design and structure of a computer network, including how its
various components and devices (such as routers, switches, and servers) are connected and interact
with each other. It defines the layout, topology, protocols, and rules that govern communication
between devices in the network.
1. **Nodes (Devices):**
- **End Devices:** These are the devices that users interact with, such as computers, smartphones,
and printers.
- **Network Devices:** Devices like routers, switches, and hubs that manage and direct network
traffic.
2. **Network Topologies:**
- **Bus Topology:** All devices are connected to a single central cable, the bus.
- **Ring Topology:** Each device is connected to two other devices, forming a ring.
- **Mesh Topology:** Devices are interconnected, allowing for multiple paths for data.
3. **Protocols:**
- **TCP/IP:** The foundational protocol suite for the internet, governing how data is transmitted.
- **Data Link Layer:** Manages node-to-node data transfer and error detection.
5. **Network Types:**
- **LAN (Local Area Network):** A network covering a small geographic area, like an office or home.
- **WAN (Wide Area Network):** A network that spans large geographic areas, such as cities or
countries.
- **MAN (Metropolitan Area Network):** A network that covers a larger area than a LAN but smaller
than a WAN, such as a city.
- **PAN (Personal Area Network):** A network for personal devices, usually within a small range (e.g.,
Bluetooth).
6. **Network Security:**
- **Firewalls:** Devices or software that filter incoming and outgoing traffic based on security rules.
- **VPN (Virtual Private Network):** Extends a private network across a public network, enabling
secure communication.
- **Scalability:** The ability to expand the network to accommodate more devices and traffic.
- **Security:** Protecting the network from unauthorized access and cyber threats.
- **Client-Server Architecture:** Clients (end devices) request resources or services from a centralized
server.
- **Peer-to-Peer (P2P) Architecture:** All devices have equal status, sharing resources without a central
server.
- **Cloud-Based Architecture:** Resources and services are hosted in the cloud, accessible over the
internet.
Network architecture is crucial for ensuring efficient, secure, and reliable communication within and
between networks.
- UUCP operates by establishing a connection between two systems, often over dial-up modems, and
then transferring files or messages.
- File transfer.
- UUCP was instrumental in the early days of email and Usenet news distribution. It allowed
decentralized distribution of messages and news articles between different systems, forming a "store-
and-forward" network.
- UUCP networks were often built in a hierarchical manner, with smaller systems connecting to larger
hub systems.
- Routing was done manually by specifying a series of systems that messages or files would travel
through (e.g., `system1!system2!system3!destination`).
### 5. **Decline**:
- With the growth of the Internet and the availability of always-on TCP/IP networks, UUCP became less
common.
- By the mid-1990s, UUCP had largely been replaced by SMTP for email and FTP for file transfers.
### 6. **Legacy**:
- Although largely obsolete, UUCP played a crucial role in the early development of networked
communication and distributed computing. Its influence is seen in many modern networking concepts.
If you're dealing with older systems or looking into the history of Unix and early networking
technologies, UUCP remains a significant milestone.
1. **Layered Architecture**: XNS used a layered protocol architecture, similar to the OSI model. It
included several layers, such as the network layer, transport layer, and application layer, to handle
different aspects of network communication.
2. **Packet-Switching**: XNS employed packet-switching technology to break down data into packets
for transmission across the network, improving efficiency and reliability.
3. **Routing Protocols**: It included routing protocols to determine the best path for data to travel
across a network, ensuring efficient and accurate delivery.
4. **Name Resolution**: XNS provided mechanisms for name resolution, allowing systems to translate
human-readable names into network addresses.
5. **File and Print Services**: The system supported file sharing and print services, enabling users to
access files and printers on the network.
6. **Support for Local Area Networks (LANs)**: XNS was designed to work with various types of LAN
technologies, making it versatile for different networking environments.
7. **Network Control Protocols**: XNS included protocols for network control and management,
helping administrators monitor and manage network operations.
Overall, XNS was a pioneering effort in network protocol development, influencing subsequent
networking standards and technologies.
- **IPX (Internetwork Packet Exchange)**: This is the protocol used to route packets between network
nodes. It operates at the Network layer (Layer 3) of the OSI model and is responsible for addressing and
routing packets across a network.
- **SPX (Sequenced Packet Exchange)**: This is the transport protocol used in conjunction with IPX to
provide connection-oriented communication. It ensures reliable delivery of data between applications
on networked devices.
IPX/SPX was commonly used in Novell NetWare networks but has largely been replaced by TCP/IP due
to its broader compatibility and support.
### IP Header
The IP header is part of the Internet Protocol (IP) and is used to route packets of data across networks. It
contains information about the source and destination IP addresses, as well as various other details
required for routing and delivery. Here's a general overview of the fields found in an IPv4 header:
1. **Version**: Indicates the IP version (e.g., IPv4 or IPv6).
4. **Total Length**: The length of the entire IP packet, including header and data.
7. **Fragment Offset**: Indicates the position of the fragment in the original datagram.
8. **Time to Live (TTL)**: Limits the packet's lifetime to prevent it from circulating indefinitely.
9. **Protocol**: Indicates the protocol used in the data portion of the IP packet (e.g., TCP, UDP).
3. **Sequence Number**: Used for ordering data segments and managing data flow.
4. **Acknowledgment Number**: Indicates the next sequence number that the sender of the
acknowledgment expects to receive.
6. **Flags**: Various control flags (e.g., SYN, ACK, FIN) that manage the state of the connection.
7. **Window Size**: Specifies the size of the sender's receive window (flow control).
9. **Urgent Pointer**: Indicates if there is urgent data that needs immediate attention.
These headers work together to ensure that data is transmitted reliably and accurately over the
network.
- **Notation**: Divided into four 8-bit octets, usually written in decimal form, separated by dots.
- **Example**: `192.168.1.1`
- **Notation**: Divided into eight 16-bit blocks, written in hexadecimal and separated by colons.
- **Example**: `2001:0db8:85a3:0000:0000:8a2e:0370:7334`
- **Simplifications**:
- Consecutive blocks of zeros can be replaced with `::` (only once in an address).
Each version serves the same purpose of identifying devices on a network but differs in terms of address
length and features.