EmbLogic's Blog

THREADS AND SOCKETS

THREADS AND SOCKETS MECHANISMS

 INTRODUCTION OF THREADS: Multiple strands of execution in a single program are called threads. All processes have at least one thread. All threads in a program execute simultaneously and perform their function individually. Threads are the part of a program does not create new process context for multiples threads but shares the code segment , data segment part of a process context only different stacks is created for multiple threads of size of 8MB.

Threads shares file descriptors, signal handlers, current directory, global variables. Threads have it own stack and local variables. With respect to “forking” system does not initialize a new system virtual memory space and environment for a process.In forking with respect to each process a pid is given but in threads pid is given to a main process.

IMPLEMENTION OF  THREAD:

Header file for thread  #include<pthread.h> is included.

Thread is created by pthread_create(). pthread _create()  creates a new thread. The pthread_create() function starts a new thread in a calling process.

 int pthread_create(pthread_t * thread, pthread_attr_t   *attr,void*(*start_routine)(void *), void *arg);

There are four arguments in a pthread_create() function:

(1)pthread_t *thread :-pointer to new thread.

(2)pthread_attr_t *attr:- sets the thread attributes and the attr argument points to pthread_attr_t whose contents are used at the time of thread creation to determines attributes for a new thread. We take attr NULL that means thread is created with default attributes.

(3)void*(*start_routine)(void*):-the address of a function taking a pointer to void as a parameter & the function will return a pointer to void.

(4)void *arg:-The return value is 0 for success or an error if something going wrong.

When thread terminates it call exit function.

Void pthread_exit(void *retal);

this function terminates the thread executing ,the calling function return a pinter to an object.

 Threads are to be join to the main thread by a join calling function.

Int pthread_join(pthread_t th,void ** thread_return);

the first argument is the thread for which to wait and second argument is a pointer toa pointer that itself points to the return value from the thread.

This function returns 0 for success otherwise an error if something going wrong.

Advantages and disadvantages:-

overhead cost is low, better utilization, switching b/w threads are less demanding.

The disadvantage among threads debugging is difficult in multi-threaded program in comparison to single-threaded program because the interactions b/w the threads are very hard to control.

SOCKETS:-  Sockets provide intercommunication b/w server and client across a n/w of computers. Server create socket with (file descriptor) resource assigned using the system call function. A socket is named using the system call bind. Server waits for a client to connect to named socket.

 IMPLEMENTAION OF SOCKETS:-

 There are five call functions in server socket are socket call, bind call , listen call, connect call , accept call.

The header included for sockets are :-

#include<sys/types.h>

#include<sys/socket.h>

(1)Socket call:-

int socket (int domain, int type ,int protocol);

socket() creates an end point for communication &returns a descriptor.

the first argument int domain refers to communication domain which is of two types

domain() selects the protocol family which will be used for communication.

(i)AF_UNIX- local communication

(ii)AF_INET- ipv4 internet protocol

The second argument int type are of two types

(i)SOCK_STREAM- TCP/IP- full duplex byte streams,connection based byte stream, provides sequenced reliable two- way communication.

(ii)SOCK_DGRAM- UDP- not reliable and connectionless

Normally only a single protocol exists to support a particular socket type within a given protocol family thats why the third argument is taken zero. This function returns an file descriptor .

(2)Bind call:- Thisis to basically give name to socket & assign port to socket.

int bind(int sockfd, const struct sockaddr *addr ,socklen_t addrlen);

the sockaddr structure is defined as:-

 struct sockaddr{

sa_family_t_sa_family;

char sa_data[14];

purpose of this structure is to cast the structure pointer in addr in order to avoid compiler warnings. The first argument sockfd is the return fd from the socket call function.

(3)Listen call:- this creates queues in which clients wait for their connections.

int listen(int sockfd , int backlog);

the first argument is the return socket fd. The second argument shows how many number of request want to store in queue.

(1)if queue is full, client may receive an error with an indication of ECONNREFUSED.

(2)request ignored.

(3)on success zero is return and -1 for error is returned.

(4)Connect call:-

          int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

(5)Accept call:- This provide another socket descriptor through which server can chat with its client.

int accept( int sockfd,struct sockaddr *addr, socklen_t * addrlen);

(6)Closed call:-

int close(int fd);

Then for sever clients there are two system calls for are:

(1)Socket call:-To create socket.

(2)Connect call:-:This to provide socket descriptor through which it can chat with server.

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>