+ All Categories
Home > Documents > Network Programming – MC9241 -...

Network Programming – MC9241 -...

Date post: 12-Mar-2018
Category:
Upload: truonghuong
View: 244 times
Download: 8 times
Share this document with a friend
22
Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241 L T P C 3 0 0 3 UNIT I - INTRODUCTION 9 Introduction – Overview of UNIX OS - Environment of a UNIX process - Process control - Process relationships Signals – Interprocess Communication- overview of TCP/IP Protocols. UNIT II - ELEMENTARY TCP SOCKETS 9 Introduction to Socket Programming –Introduction to Sockets – Socket address Structures – Byte ordering functions – address conversion functions – Elementary TCP Sockets – socket, connect, bind, listen, accept, read, write , close functions – Iterative Server – Concurrent Server. UNIT III - APPLICATION DEVELOPMENT 9 TCP Echo Server – TCP Echo Client – Posix Signal handling – Server with multiple clients – boundary conditions: Server process Crashes, Server host Crashes, Server Crashes and reboots, Server Shutdown – I/O multiplexing – I/O Models – select function – shutdown function – TCP echo Server (with multiplexing) – poll function – TCP echo Client (with Multiplexing). UNIT IV - SOCKET OPTIONS, ELEMENTARY UDP SOCKETS 9 Socket options – getsocket and setsocket functions – generic socket options – IP socket options – ICMP socket options – TCP socket options – Elementary UDP sockets – UDP echo Server – UDP echo Client – Multiplexing TCP and UDP sockets – Domain name system – gethostbyname function – Ipv6 support in DNS – gethostbyadr function – getservbyname and getservbyport functions. UNIT V ADVANCED SOCKETS 9 Ipv4 and Ipv6 interoperability – threaded servers – thread creation and termination – TCP echo server using threads – Mutexes – condition variables – raw sockets – raw socket creation – raw socket output – raw socket input – ping program – trace route program. TOTAL = 45 REFERENCES: 1. W. Richard Stevens, “Advanced Programming in The UNIX Environment”, Addison Wesley, 1999. 2. W. Richard Stevens, “UNIX Network Programming - Volume 1”, Prentice Hall International, 1998.
Transcript
Page 1: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

1 CCET

NETWORK PROGRAMMING MC9241 L T P C

3 0 0 3

UNIT I - INTRODUCTION 9 Introduction – Overview of UNIX OS - Environment of a UNIX process - Process control - Process relationships Signals – Interprocess Communication- overview of TCP/IP Protocols. UNIT II - ELEMENTARY TCP SOCKETS 9 Introduction to Socket Programming –Introduction to Sockets – Socket address Structures – Byte ordering functions – address conversion functions – Elementary TCP Sockets – socket, connect, bind, listen, accept, read, write , close functions – Iterative Server – Concurrent Server. UNIT III - APPLICATION DEVELOPMENT 9 TCP Echo Server – TCP Echo Client – Posix Signal handling – Server with multiple clients – boundary conditions: Server process Crashes, Server host Crashes, Server Crashes and reboots, Server Shutdown – I/O multiplexing – I/O Models – select function – shutdown function – TCP echo Server (with multiplexing) – poll function – TCP echo Client (with Multiplexing). UNIT IV - SOCKET OPTIONS, ELEMENTARY UDP SOCKETS 9 Socket options – getsocket and setsocket functions – generic socket options – IP socket options – ICMP socket options – TCP socket options – Elementary UDP sockets – UDP echo Server – UDP echo Client – Multiplexing TCP and UDP sockets – Domain name system – gethostbyname function – Ipv6 support in DNS – gethostbyadr function – getservbyname and getservbyport functions. UNIT V ADVANCED SOCKETS 9 Ipv4 and Ipv6 interoperability – threaded servers – thread creation and termination – TCP echo server using threads – Mutexes – condition variables – raw sockets – raw socket creation – raw socket output – raw socket input – ping program – trace route program.

TOTAL = 45 REFERENCES: 1. W. Richard Stevens, “Advanced Programming in The UNIX Environment”, Addison Wesley, 1999. 2. W. Richard Stevens, “UNIX Network Programming - Volume 1”, Prentice Hall International, 1998.

Page 2: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

2 CCET

Introduction

UNIX

UNIX has been a popular OS for more than two decades because of its multi-user, multi-tasking

environment, stability, portability and powerful networking capabilities as shown in the below

figure.

In the late 1960s, researchers from General Electric, MIT and Bell Labs launched a joint project to

develop an ambitious multi-user, multi-tasking OS for mainframe computers known as MULTICS

(Multiplexed Information and Computing System). MULTICS failed, but it did inspire Ken

Thompson, who was a researcher at Bell Labs, to have a go at writing a simpler operating system

himself. He wrote a simpler version of MULTICS on a PDP7 in assembler and called his attempt

UNICS (Uniplexed Information and Computing System). Because memory and CPU power were at a

premium in those days, UNICS (eventually shortened to UNIX) used short commands to minimize

the space needed to store them and the time needed to decode them- hence the tradition of short

UNIX commands we use today, example: ls, cp, rm, mv etc. Ken Thompson then teamed up with

Dennis Ritchie, the author of the first C compiler in 1973.They rewrote the UNIX kernel in C - this

was a big step forwards in terms of the system's portability - and released the Fifth Edition of UNIX

to universities in 1974.

Page 3: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

3 CCET

The Seventh Edition, released in 1978, marked a split in UNIX development into two main

branches: SYSV (System5) and BSD (Berkeley Software Distribution). BSD arose from the

University of California at Berkeley where Ken Thompson spent a sabbatical year. Its development

was continued by students at Berkeley and other research institutions. SYSV was developed by

AT&T and other commercial companies. UNIX flavors based on SYSV have traditionally been more

conservative, but better supported than BSD-based flavors. The latest incarnations of SYSV (SVR4

or System 5 Release 4) and BSD Unix are actually very similar.

Overview of UNIX OS

Introduction

• UNIX is multi-user and multi-tasking operating system.

• Multi-tasking: Multiple processes can run concurrently.

• Example, different users can read mails, copy files, and print all at once.

Logging In

• Enter login name and password !

• System password file: /etc/passwd (usually).

• You can change password using the command: passwd.

Shell

• After a successful login, the shell program is run. The default shell of bingsuns: tcsh

• bingsun2% ps

PID TTY TIME CMD

2159 pts/2 0:00 tcsh

• Shell is a command line interpreter that reads user commands and executes them.

Unix Shells

• Common Shells: Bourne shell, the C shell, and the Korn shell.

• The shell on bingsuns is tcsh (tc shell).

• Users can switch between shells, using the commands bash, csh, ksh, sh.

• Control D (^d) to return back to original shell, or just use the command: exit.

Some shell commands

• Most Important command: man (manual pages).

• Help: unix commands, C functions.

Page 4: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

4 CCET

• Usage: man <command/function>

• Try “man man” !

• Example:

manls, man passwd, man printf.

• pwd: working directory (/u0/users/2/kliu1).

• ls: list contents of directory

• mkdir<dir-name>: make directory

• rmdir<dir-name>: remove an empty directory

• rm –r <dir-name>: remove a directory with all the contents

• cd <directory>: change directory, ~/ means your home directory

• cp<source><target>: copy command.

• chmod<mode><filename>: change mode of a file/directory

• ls –l <directory or filename>: long list with details

• 9 permission bits: d r w x r w x r w x

• 3 categories: user/group/all.

• Permissions: read/write/execute (r/w/x).

• E.g.: mode= 644 means r w _ r_ _ r _ _

• command: chmod 644 <filename>

• first 3 bits for user. Next group. Next all others

• rm<option><filename>: remove files

e.g.: rm –fr directory/filename

• mv <old><new>: change the name of a file

• Pipes: Connect the stdout of one command with the stdin of another command

e.g.: ls -l | more orls –l | less

File System

• Hierarchical arrangement of files and directories.

• Top level: root or /

e.g.: cd /

• . Current directory, .. One level higher directory

e.g.: cd .No change for it is current directory

or cd .. Change to parent directory.

Page 5: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

5 CCET

• Pathname: absolute and relative.

• Absolute pathname: /u0/users/2/kliu1

• Relative pathname: abc.

Editors

• Different editors: emacs, pico, vi

• emacs<filename>

• pico<filename>

• vi <filename>

The easiest editor: pico or nano

• pico<filename>

• Full screen editor

• Help on the bottom of the screen

• The nano is an extension to the pico

ARCHITECTURE OF THE UNIX SYSTEM

The below figure depicts the high level architecture of UNIX system.

Page 6: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

6 CCET

The Kernel

The kernel is the part of the operating system that interacts directly with the hardware of a

computer, through device drivers that are built into the kernel. It provides sets of services that can

be used by programs, insulating these programs from the underlying hardware. The major

functions of the kernel are to manage computer memory, to control access to the computer, to

maintain the file system, to handle interrupts (signals to terminate execution), to handle errors, to

perform input and output services (which allow computers to interact with terminals, storage

devices, and printers), and to allocate the resources of the computer (such as the CPU or

input/output devices) among users.

Programs interact with the kernel through system calls. System calls tell the kernel to carry out

various tasks for the program, such as opening a file, writing to a file, obtaining information about a

file, executing a program, terminating a process, changing the priority of a process, and getting the

time of day Different implementations of a variant of the UNIX system may have compatible system

calls, with each call having the same functionality However, the internals, programs that perform

the functions of system calls (usually written in the C language), and the system architecture in two

different UNIX variants or even two different implementations of a particular UNIX variant may

bear little resemblance to one another.

SALIENT FEATURES OF UNIX

The system is written in a high level language, making it easy to read, understand, change

and move to other machine.

It has simpler user interface that has the power to provide the services that users want.

It provides primitives that permit complex programs to be built from the simpler programs.

It uses hierarchical file systems that allow easy maintenance and efficient implementation.

Multi-user Capability

Page 7: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

7 CCET

Multi-tasking Capability

It hides the machine architecture from the user, making it easier to write programs thatrun

on different hardware implementations.

It has excellent provision for communication with fellow users.

Environment of a UNIX Process

Program & Process

• Program is an executable file that resides on the disk.

• Process is an executing instance of a program.

• A Unix process is identified by a unique non-negative integer called the process ID.

• Check process status using the “ps” command.

Foreground/background processes

• A program run using the ampersand operator “&” creates a background process.

• E.g.:

bingsun2% back &

• otherwise it creates a foreground process.

• E.g.:

bingsun2% back

How to stop a process?

• Foreground processes can generally be stopped by pressing CONTROL C (^C).

• Background processes can be stopped using the kill command.

• Usage: kill SIGNAL <process id list>

• kill -9 <process id list> (-9 means no blocked)Or kill <process id list>.

• If a foreground process is not stopping by ^C, you can open another session and use

the kill command.

The Environment of a Unix Process

Objective:

• How main function is called when program is executed.

• How program terminates execution

• How command-line arguments are passed

• What the typical memory layout is

Alloc memory, use environment variables,….

main function

intmain(intargc, char *argv[]) -- prototype

• When program is started by kernel, a special start-up routine is called

before calling main(). Start-up routine is set up by the link editor as part of

Page 8: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

8 CCET

the executable. Start-up routine takes command-line arguments and

environment variable to prepare execution of main function.

How a C program is started and terminated ?

Historically, a C program has been composed of the following pieces:

Text segment, the machine instructions that the CPU executes. Usually, the text segment is

sharable so that only a single copy needs to be in memory for frequently executed

programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is

often read-only, to prevent a program from accidentally modifying its instructions.

Initialized data segment, usually called simply the data segment, containing variables that

are specifically initialized in the program. For example, the C declaration

int maxcount = 99;

appearing outside any function causes this variable to be stored in the initialized data

segment with its initial value.

Uninitialized data segment, often called the "bss" segment, named after an ancient

assembler operator that stood for "block started by symbol." Data in this segment is

initialized by the kernel to arithmetic 0 or null pointers before the program starts executing.

The C declaration

long sum[1000];

Page 9: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

9 CCET

appearing outside any function causes this variable to be stored in the uninitialized data

segment.

Stack, where automatic variables are stored, along with information that is saved each time

a function is called. Each time a function is called, the address of where to return to and

certain information about the caller's environment, such as some of the machine registers,

are saved on the stack. The newly called function then allocates room on the stack for its

automatic and temporary variables. This is how recursive functions in C can work. Each

time a recursive function calls itself, a new stack frame is used, so one set of variables

doesn't interfere with the variables from another instance of the function.

Heap, where dynamic memory allocation usually takes place. Historically, the heap has been

located between the uninitialized data and the stack.

Figure shows the typical arrangement of these segments. This is a logical picture of how a

program looks; there is no requirement that a given implementation arrange its memory in

this fashion. Nevertheless, this gives us a typical arrangement to describe. With Linux on an

Intel x86 processor, the text segment starts at location 0x08048000, and the bottom of the

stack starts just below 0xC0000000. (The stack grows from higher-numbered addresses to

lower-numbered addresses on this particular architecture.) The unused virtual address

space between the top of the heap and the top of the stack is large.

Several more segment types exist in an a.out, containing the symbol table, debugging information, linkage tables for dynamic shared libraries, and the like. These additional sections don't get loaded as part of the program's image executed by a process.

Page 10: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

10 CCET

Process Control

PROCESS RELATED SYSTEM CALLS

In addition to the process ID, there are other identifiers for every process. The following functions

return these identifiers.

It is very simple to print the PID of a running process programmatically. Here is the program that

achieves this…

/* pid.c */

int main( )

{

printf ( "Process ID = %d", getpid( ) ) ;

}

Here getpid( ) is a library function which returns the process ID of the calling process. When the

execution of the program comes to an end the process stands terminated. Every time we run the

program a new process is created. Hence the kernel assigns a new ID to the process each time. This

can be verified by executing the program several times—each time it would produce a different

output.

fork Function

As we know, our running program is a process. From this process we can create another process.

There is a parent-child relationship between the two processes. The way to achieve this is by using

a library function called fork( ). This function splits the running process into two processes, the

existing one is known as parent and the new process is known as child.

An existing process can create a new one by calling the fork function.

#include <unistd.h>

pid_t fork(void);

Returns: 0 in child, process ID of child in parent, 1 on error

The only difference in the returns is that the return value in the child is 0, whereas the return value

in the parent is the process ID of the new child. The reason the child's process ID is returned to the

parent is that a process can have more than one child, and there is no function that allows a process

to obtain the process IDs of its children. The reason fork returns 0 to the child is that a process can

have only a single parent, and the child can always call getppid to obtain the process ID of its

parent. (Process ID 0 is reserved for use by the kernel, so it's not possible for 0 to be the process ID

of a child.)

Page 11: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

11 CCET

Both the child and the parent continue executing with the instruction that follows the call to fork.

The child is a copy of the parent. For example, the child gets a copy of the parent's data space, heap,

and stack. Note that this is a copy for the child; the parent and the child do not share these portions

of memory. The parent and the child share the text segment

/* forkexample.c */

# include <sys/types.h>

int main( )

{

printf ( "Before Forking\n" ) ;

fork( ) ;

printf ( "After Forking\n" ) ;

}

Here is the output of the program…

Before Forking

After Forking

After Forking

Watch the output of the program. You can notice that all the statements after the fork( ) are

executed twice—once by the parent process and second time by the child process. In other words

fork( ) has managed to split our process into two. Indeed, one characteristic of fork is that all file

descriptors that are open in the parent are duplicated in the child. We say "duplicated" because it's

as if the dup function had been called for each descriptor. The parent and the child share a file table

entry for every open descriptor.

Consider a process that has three different files opened for standard input, standard

output, and standard error. On return from fork, we have the arrangement shown in

below figure.

Page 12: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

12 CCET

If both parent and child write to the same descriptor, without any form of synchronization, such as

having the parent wait for the child, their output will be intermixed (assuming it's a descriptor that

was open before the fork). Although this is possible we saw it in above figure but it's not the normal

mode of operation.

There are two normal cases for handling the descriptors after a fork.

1. The parent waits for the child to complete. In this case, the parent does not need to do anything

with its descriptors. When the child terminates, any of the shared descriptors that the child

read from or wrote to will have their file offsets updated accordingly.

2. Both the parent and the child go their own ways. Here, after the fork, the parent closes the

descriptors that it doesn't need, and the child does the same thing. This way, neither interferes

with the other's open descriptors. This scenario is often the case with network servers.

vfork Function

The function vfork has the same calling sequence and same return values as fork. But the semantics

of the two functions differ. The vfork function is intended to create a new process when the purpose

of the new process is to exec a new program. The vfork function creates the new process, just like

Page 13: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

13 CCET

fork, without copying the address space of the parent into the child, as the child won't reference

that address space; the child simply calls exec (or exit) right after the vfork.

Instead, while the child is running and until it calls either exec or exit, the child runs in the address

space of the parent. This optimization provides an efficiency gain on some paged virtual-memory

implementations of the UNIX System. Another difference between the two functions is that vfork

guarantees that the child runs first, until the child calls exec or exit. When the child calls either of

these functions, the parent resumes. (This can lead to deadlock if the child depends on further

actions of the parent before calling either of these two functions.)

/* vforkexample.c */

#include <stdio.h>

#include<sys/types.h>

int glob=6;

int main(void)

{

int var=88;

pid_t pid;

printf("\nVfork Function");

printf("\n~~~~~~~~~~~~~~~");

printf("\nBefore vfork\n");

if((pid=vfork())<0)

printf("\nvfork error");

else if(pid==0)

{

glob++;

var++;

_exit(0);

}

printf("\npid=%d\nglob=%d\nvar=%d\n",getpid(),glob,var);

exit(0);

}

exit Functions

A process can terminate normally in five ways:

1. Executing a return from the main function.

2. Calling the exit function..

3. Calling the _exit or _Exit function.

4. Executing a return from the start routine of the last thread in the process.

5. Calling the pthread_exit function from the last thread in the process.

The three forms of abnormal termination are as follows:

1. Calling abort. This is a special case of the next item, as it generates the SIGABRT

signal.

Page 14: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

14 CCET

2. When the process receives certain signals. The signal can be generated by the

process itself for example, by calling the abort function by some other process,

or by the kernel. Examples of signals generated by the kernel include the process

referencing a memory location not within its address space or trying to divide by 0.

3. The last thread responds to a cancellation request. By default, cancellation occurs

in a deferred manner: one thread requests that another be canceled, and sometime

later, the target thread terminates.

Regardless of how a process terminates, the same code in the kernel is eventually executed. This

kernel code closes all the open descriptors for the process, releases the memory that it was using,

and the like.

wait and waitpid Functions

When a process terminates, either normally or abnormally, the kernel notifies the parent by

sending the SIGCHLD signal to the parent. Because the termination of a child is an asynchronous

event it can happen at any time while the parent is running this signal is the asynchronous

notification from the kernel to the parent. The parent can choose to ignore this signal, or it can

provide a function that is called when the signal occurs: a signal handler. The default action for this

signal is to be ignored. A process that calls wait or waitpid can

· Block, if all of its children are still running

· Return immediately with the termination status of a child, if a child has terminated and is waiting

for its termination status to be fetched

· Return immediately with an error, if it doesn't have any child processes If the process is calling

wait because it received the SIGCHLD signal, we expect wait to return immediately. But if we call it

at any random point in time, it can block.

#include <sys/wait.h>

pid_t wait(int *statloc);

pid_t waitpid(pid_t pid, int *statloc, int options);

Both return: process ID if OK, 0 (see later), or 1 on error. The interpretation of the pid argument for

waitpid depends on its value:

pid == 1 Waits for any child process. In this respect, waitpid is equivalent to wait.

pid > 0 Waits for the child whose process ID equals pid.

exec Functions

As we mentioned in earlier that one use of the fork function is to create a new process (the child)

that then causes another program to be executed by calling one of the exec functions. When a

process calls one of the exec functions, that process is completely replaced by the new program, and

the new program starts executing at its main function.

The process ID does not change across an exec, because a new process is not created; exec merely

replaces the current process its text, data, heap, and stack segments with a brand new program

Page 15: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

15 CCET

from disk.pid == 1 Waits for any child process. In this respect, waitpid is equivalent to wait. pid == 0

Waits for any child whose process group ID equals that of the calling process. pid < 1 Waits for any

child whose process group ID equals the absolute value of pid.

process Relationships signals

Process relationships Two major relations govern individual processes

Process groups Sessions

Information on these relationships is available through the ps command when using the proper option flags.

Relationship of the six exec functions

Example Program 10 */ /* Prepared by Mr.A.Saleem Raja on 04.12.10 */ /* execlexample.c */ # include <unistd.h> int main( ) { int pid ; pid = fork( ) ; if ( pid == 0 ) { execl ( "/bin/ls","-al", "/etc", NULL ) ; printf ( "Child: After exec( )\n") ; } else printf ( "Parent process\n" ) ; }

system Function It is convenient to execute a command string from within a program. For example, assume that we want to put a time-and-date stamp into a certain file. system("date > file"); ISO C defines the system

Page 16: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

16 CCET

function, but its operation is strongly system dependent. POSIX.1 includes the system interface, expanding on the ISO C definition to describe its behavior in a POSIX environment.

Changing User IDs and Group IDs We can set the real user ID and effective user ID with the setuid function. Similarly, we can set the real group ID and the effective group ID with the setgid function.

#include <unistd.h> int setuid(uid_t uid); int setgid(gid_t gid); Both return: 0 if OK, 1 on error

User Identification The system normally keeps track of the name we log in with help of the two data files that have been provided with most UNIX systems are the utmp file, which keeps track of all the users currently logged in, and the wtmp file, which keeps track of all logins and logouts. The get login function provides a way to fetch that login name. #include <unistd.h> char *getlogin(void); Returns: pointer to string giving login name if OK, NULL on error

This function can fail if the process is not attached to a terminal that a user logged in to. We normally call these processes daemons.

Inter process Communication

Inter-process communication (IPC) is the transfer of data among processes. Processes incomputer

memory are said to be communicating with one process passes data to anotheror vice versa. UNIX

provides a number of ways to share and communicate data. They are

• Pipes

• Signals

• Message Queues

• Semaphores

• Shared Memory

• Sockets

Pipes

We use the word pipe when we connect a data flow from one process to another. Shellcommands

can be linked together so that the output of one process is fed straight to theinput of another.Pipes

are the oldest form of UNIX System IPC and are provided by all UNIX systems.Pipes have two

limitations.

Historically, they have been half duplex (i.e., data flows in only one direction).Some systems now

provide full-duplex pipes, but for maximum portability, weshould never assume that this is the

case.2. Pipes can be used only between processes that have a common ancestor.Normally, a pipe is

Page 17: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

17 CCET

created by a process, that process calls fork, and the pipe isused between the parent and the child.

Despite these limitations, half-duplex pipes are still the most commonly used form ofIPC. Every

time you type a sequence of commands in a pipeline for the shell to execute,the shell creates a

separate process for each command and links the standard output ofone to the standard input of

the next using a pipe.

Signals

Signals are one of the oldest inter-process communication methods used by Unix systems. They are

used to signal asynchronous events to one or more processes. A signal could be generated by a

keyboard interrupt or an error condition such as the process attempting to access a non-existent

location in its virtual memory. Signals are also used by the shells to signal job control commands to

their child processes. There are a set of defined signals that the kernel can generate or that can be

generated by other processes in the system, provided that they have the correct privileges. You can

list a system's set of signals using the kill command (kill -l:)

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL

5) SIGTRAP 6) SIGIOT 7) SIGBUS 8) SIGFPE

9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2

13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD

18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN

22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO

30) SIGPWR

Message Queues

A message queue is a linked list of messages stored within the kernel and identified by a message

queue identifier. Message queues provide a way of sending a block of data from one process to

another. Message queues allow one or more processes to write messages, which will be read by one

or more reading processes. i.e. Two (or more) processes can exchange information via access to a

common system message queue. The sending process places via some (OS) message-passing

module a message onto a queue which can be read by another process. Each message is given an

identification or type so that processes can select the appropriate message. Process must share a

common key in order to gain access to the queue in the first place.

Page 18: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

18 CCET

Each queue has the following msqid_dsstructure associated with it:

Semaphores

Semaphores are a programming construct designed by E. W. Dijkstra in the late 1960s.Dijkstra's

model was the operation of railroads: consider a stretch of railroad in which there is a single track

over which only one train at a time is allowed. Guarding this track is a semaphore. A train must wait

before entering the single track until the semaphore is in a state that permits travel. When the train

enters the track, the semaphore changes state to prevent other trains from entering the track.

A train that is leaving this section of track must again change the state of the semaphore to allow

another train to enter. In the computer version, a semaphore appears to be a simple integer. A

process (or a thread)waits for permission to proceed by waiting for the integer to become 0. The

signal if it proceeds signals that this by performing incrementing the integer by 1. When it is

finished, the process changes the semaphore's value by subtracting one from it. Here are the signal

notations:

• P ( Semaphore variable) for wait.

• V ( Semaphore variable) for signal.

Semaphore Definition A binary semaphore is a variable that can take only the values 0 and 1.

The definition of p and v are surprisingly simple. Suppose we have a semaphore variable,sv. The

two operations are then defined as:

Page 19: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

19 CCET

Shared Memory

Shared memory allows two or more processes to share a given region of memory. This isthe fastest

form of IPC, because the data does not need to be copied between the clientand the server. The only

trick in using shared memory is synchronizing access to a givenregion among multiple processes. If

the server is placing data into a shared memoryregion, the client shouldn't try to access the data

until the server is done. Often,semaphores are used to synchronize shared memory access.

Overview of TCP/IP Protocols

TCP/IP Protocol Suite

The TCP/IP protocol suite was first defined in 1974

The TCP/IP protocol suite is the protocol architecture of the Internet

The TCP/IP suite has four layers:

Application, Transport, Internet, and Network Interface Layer.

Page 20: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

20 CCET

Internet is viewed as a communication infrastructure that enables distributed

applications like e mail, games, and e –commerce. The protocol used in the internet is

TCP/IP.TCP/IP is a suite of protocols, also known as the Internet Protocol Suite. It should

not be confused with the OSI reference model, although elements of TCP/IP exist in OSI.

The transmission control protocol and the internet protocol are fundamental to the suite,

hence the TCP/IP title. The suite was originally developed for the US Department of

Defense Advanced Research Project Agency (DARPA) network, but it is now the basis for

the Internet and many intranets.

As with the OSI model, the TCP/IP suite uses a layered model. The OSI model has

seven layers while the TCP/IP model has four or five – some authors call it a four layer

suite - Application, Transport, Internet and Network Access layers and others split the

Network Access layer into its Physical and Data link components. The functionality of each

layer and some of the protocols that comprise the TCP/IP model are explained here.

Page 21: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

21 CCET

Layers 1 and 2 - Network Access:

The combination of physical and data link layers deals with pure hardware (wires,

satellite links, network interface cards, etc.) and access methods such as CSMA/CD (carrier

sensed multiple access with collision detection). Ethernet exists at the network access layer

- its hardware operates at the physical layer and its medium access control method

(CSMA/CD) operates at the data link layer. Each system has a physical address for the

operation by this layer.

Layer 3 – Internet:

This layer is responsible for the routing and delivery of data across networks. It

allows communication across networks of the same and different types and carries out

translations to deal with dissimilar data addressing schemes. IP (Internet Protocol) and

ARP (Address Resolution Protocol) are both to be found at the Internet layer. Each system

has a physical address corresponds to the operation at this layer. Each system has a logical

address called as IP address used by this layer.

Layer 4 – Transport:

The transport layer is similar to the OSI transport model, but with elements of the

OSI session layer functionality. This layer provides an application layer delivery service.

The two protocols found at the transport layer are TCP (Transmission Control Protocol)

and UDP (User Datagram Protocol). Either of these two protocols are used by the

application layer process depends on the application's transmission reliability

requirements. Each system has many port addresses corresponding to the various services

provided at this layer.

TCP is a reliable, connection-oriented protocol that provides error checking and

flow control through a virtual link that it establishes and finally terminates. This gives a

reliable service, therefore TCP would be utilized by FTP and SNMP - file transfer and email

delivery have to be accurate and error free.

UDP is an unreliable, connectionless protocol that provides data transport with

lower network traffic overheads than TCP - UDP does not error check or offer any flow

Page 22: Network Programming – MC9241 - chettinadtech.ac.inchettinadtech.ac.in/storage/13-12-05/13-12-05-16-43-31-2347... · Network Programming – MC9241 1 CCET NETWORK PROGRAMMING MC9241

Network Programming – MC9241

22 CCET

control, this is left to the application process. SNMP uses UDP - SNMP is used to monitor

network performance, so its operation must not contribute to congestion.

Layer 5 – Application:

This layer is broadly equivalent to the application, presentation and session layers

of the OSI model. It gives an application access to the communication environment.

Examples of protocols found at this layer are Telnet, FTP (File Transfer Protocol), SNMP

(Simple Network Management Protocol), HTTP (Hyper Text Transfer Protocol) and SMTP

(Simple Mail Transfer Protocol).

Comparison of OSI Model and TCP/IP Suite


Recommended