Skip to main content

Posts

Showing posts from December, 2017

Threads

A thread of execution is often regarded as the smallest unit of processing that a scheduler works on. A process can have multiple threads of execution which are executed asynchronously. This asynchronous execution brings in the capability of each thread handling a particular work or service independently. Hence multiple threads running in a process handle their services which overall constitutes the complete capability of the process. Why Threads are Required? Suppose there is a process, that receiving real time inputs and corresponding to each input it has to produce a certain output. Now, if the process is not multi-threaded, then the whole processing in the process becomes synchronous. This means that the process takes an input, processes it and produces an output. The limitation in the above design is that the process cannot accept an input until its done processing the earlier one and in case processing an input takes longer than expected then accepting further inputs

Creating new process-fork()

System call  fork()  is used to create processes. It takes no arguments and returns a process ID.The purpose of  fork()  is to create a  new  process, which becomes the  child  process of the caller. After a new child process is created,  both  processes will execute the next instruction following the  fork()  system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of  fork() : If  fork()  returns a negative value, the creation of a child process was unsuccessful. fork()  returns a zero to the newly created child process. fork()  returns a positive value, the  process ID  of the child process, to the parent. The returned process ID is of type  pid_t  defined in  sys/types.h . Normally, the process ID is an integer. Moreover, a process can use function  getpid()  to retrieve the process ID assigned to this process. Therefore, after the system call to  fork() , a simple test can t

Chat server in python

# chat_server.py import sys import socket import select HOST = '' SOCKET_LIST = [] RECV_BUFFER = 4096 PORT = 9009 def chat_server():  server_socket=socket. socket (socket.AF_INET,socket.SOCK_STREAM)  server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)  server_socket. bind ((HOST, PORT))  server_socket. listen (10) # add server socket object to the list of readable connections SOCKET_LIST.append(server_socket) print "Chat server started on port " + str(PORT) while 1:    # get the list sockets which are ready to be read through select    # 4th arg, time_out  = 0 : poll and never block   ready_to_read,ready_to_write,in_error=select.select(SOCKET_LIST,[],[],0) for sock in ready_to_read:             # a new connection request recieved             if sock == server_socket:                 sockfd, addr = server_socket. accept ()                 SOCKET_LIST.append(sockfd)         print "Client (%s, %s)