레이블이 Socket Programming (Linux)인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Socket Programming (Linux)인 게시물을 표시합니다. 모든 게시물 표시

2016년 3월 8일 화요일

Chatting Server based on MultiThread


















  • One process can implements multitasking as using multithread.
 Use 'clnt_cnt, clnt_socks[]' valuables in critical section because of synchronization.
 Critical section is implemented by using 'mutex'
 - pthread_mutex_lock(&mutex) : critical section start
 - pthread_mutex_unlock(&mutex) : critical section end





















  • Thread Context Switching is faster than Process Context Switching
 thread share heap, data(static), code except for stack, register.



Source Code



◈ references
   picture - http://fingerdev.tistory.com/24
   picture - http://www.ibm.com/

2016년 2월 19일 금요일

Chatting Program using EPOLL


  • Level Trigger Method



  • Edge Trigger Method


  • Test
if you compile 'chat_clnt.c', you will input 'gcc chat_clnt.c -D_REENTRANT -o c -lpthread'.
-D_REENTRANT is automatically turn funtion designated POSIX standard to safety function about thread.
-lpthread is POSIX pthread library linking option.
I will be posting Server & Client based on Thread next.

Source Code

2016년 2월 17일 수요일

Level Trigger VS Edge Tigger



  • Picture Compare






  • Level Trigger

- as using Level Trigger Method, Event is continually generated while input buffer is remaining extra space.

- I give 'BUF_SIZE' 5 size in order to compare Level Trigger to Edge Trigger.

ex) Level Trigger execution screen

- Also 'select' is a Level Trigger. => received data existance check

  • Edge Trigger

ex) This Test transform 'EPOLLIN' into 'EPOLLIN | EPOLLET'.


- Edge Trigger Mothod can generate one event.
  however, Server doesn't completely receive data which Client send.
     epoll_wait() call -> fetch 5byte -> event generation wait...


◈ Use 'fcntl' to use Non-blocking I/O
- int flag=fcntl(fd, F_GETFL, 0);    => fetch existing option information
- fcntl(fd, F_SETFL, flag|O_NONBLOCK);   => existing option add new attribute.

Why do Non-blocking mode use?
Answer:  As Edge Trigger characteristic, read & write Function Call stop executing Server for a long time.

◈ Use 'errno' which is extern valuable in the other file.
- 'errno' valuable value is different in functions whenever Error generated.

else if(str_len<0) // (1)
{
if(errno==EAGAIN) // (2)
break;
}

(1) What return value of read() have -1 is two meaning.
     Buffer is empty or Error generated
(2) EAGAIN mean that Buffer is empty because data don't exist in the buffer.


  • Echo Server & Client using Edge Trigger related to 'epoll' 





Source Code

2016년 2월 14일 일요일

'epoll' based on IO Multiplexing in Linux Environment

  • Echo Server & Client using 'epoll' Example Screen



  • select vs epoll

1. select
- advantage
 1. 'select' is independent about Operating System. Thus, It is operated by various OS.

- disadvantage
 1. All file desciptor is checking in the Loop. 
 ex) for(i=0; i<fd_max+1; i++)
 2. If 'select' is called, observe target which copied using 'fd_set' variable is sending to OS every time. 
 ex) cpy_reads=reads;
ex2) if((fd_num=select(fd_max+1, &cpy_reads, 0, 0, &timeout))==-1)
Inefficient!!


2. epoll ( equal to 'Windows - IOCP, BSD - kqueue, Solaris - /dev/poll' )

- advantage
 1. All file desciptor isn't checking in the Loop. 
 2. If 'epoll_wait' is called, observe target isn't sending to OS every time. 
Efficient!!

- disadvantage
 1. 'epoll' is dependent about Operating System. 

- add explanation
 1. If server has litter client, 'epoll' Function isn't unconditionally needed to use IO Multiplexing Model. 

  • Functions related 'epoll'
epoll_create : epoll file descriptor repository creation 
epoll_ctl : file descriptor enrollment & deletion in the repository
epoll_wate : file descriptor change waiting

2016년 1월 26일 화요일

Echo Client & Server based on MultiProcessing






This program operate at Linux Environment


  • Server provide Two or more client with service using Multiprocess.
       Use 'fork()' function.
       As using Multiprocess,
       One server can receive messages which Clients sended.

  • Input/Output Routine Division

       Client previously waits until Data sended or received once Data send or receive.
       As using Multiprocess,
       Client's parent process takes charge of 'Data Receive'.
       Client's child process takes charge of 'Data Send'.

OS : Linux
      => Windows don't have function such as 'fork()'
           Windows provide 'CrateProcess()' function. but It completely isn't same function.

Source Code


◈ references
   picture - http://fingerdev.tistory.com/24