Threads and Locking Ioctl operations - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Threads and Locking Ioctl operations

Description:

... Echo Client. int sockfd; FILE *fp; main() pthread_t tid; fp = fopen ... Consumer has to inform producer when data is available. Without condition variables ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 27
Provided by: kart3
Category:

less

Transcript and Presenter's Notes

Title: Threads and Locking Ioctl operations


1
Threads and LockingIoctl operations
2
Threads
  • Lightweight processes
  • Whats wrong with processes?
  • fork() is expensive 10 to 100 times slower
  • Inter process communication
  • For returning information from child to parent

3
Threads
  • Shared components
  • Global memory
  • Instructions
  • Most data
  • Open descriptors (files, sockets etc)
  • Signal handlers
  • Not shared
  • Thread ID
  • Registers, Program counter, stack pointer
  • Stack
  • Errno
  • Signal mask
  • Priority

4
Creation
  • Thread equivalent of fork()
  • int pthread_create(
  • pthread_t thread,
  • pthread_attr_t attr,
  • void (start_routine)(void ),
  • void arg
  • )
  • Returns 0 is OK, and non-zero (gt 0) if error.

5
Termination
  • Return from initial fuction.
  • void pthread_exit(void status)
  • exit() called by any thread
  • main() returns

6
Waiting for child thread to exit
  • int pthread_join(pthread_t tid, void status)
  • Equivalent of waitpid()

7
Detaching a thread
  • The detached thread can act as daemon thread
  • The parent thread doesnt need to wait
  • int pthread_detach(pthread_t tid)
  • Detaching self
  • pthread_detach(pthread_self())

8
Echo client-server
Server
listenfd
S1
S2
Read
Write
Read
Write
Client2
Client1
Read Thread
Write Thread
Read Thread
Write Thread
9
Thread-based Echo Server
10
  • main()
  • int listenfd, connfd
  • int len
  • / Start the usual way /
  • listenfd Socket()
  • Bind(listenfd, )
  • Listen(listenfd, )
  • for ( )
  • len addrlen
  • connfd Accept(listenfd, )
  • / Create a thread in service_func routine /
  • Pthread_create(NULL, NULL, service_func, (void )
    connfd)

11
  • void service_func(void arg)
  • int local_connfd
  • / release parent from waiting /
  • Pthread_detach(pthread_self())
  • / extract connfd from argument /
  • local_connfd (int) arg
  • / receive and echo clients message /
  • str_echo(local_connfd)
  • / Terminate the connection /
  • Close(local_connfd)
  • return(NULL)

12
Thread-based Echo Client
13
  • int sockfd
  • FILE fp
  • main()
  • pthread_t tid
  • fp fopen()
  • / Start the usual way /
  • sockfd Socket()
  • Connect()
  • / Create a thread to send data /
  • Pthread_create(tid, NULL, write_func, NULL)
  • / read data from sockfd /
  • read_func()

14
  • void write_func(void arg)
  • char sendlineMAXLINE
  • while( more data in fp)
  • Read from fp into sendline
  • write sendline into sockfd
  • Shutdown(sockfd, SHUT_WR)
  • return(NULL)
  • void read_func()
  • char recvlineMAXLINE
  • while ( more data from sockfd)
  • read from sockfd into recvline
  • write from recvline to stdout

15
Mutex for mutual exclusion
  • int counter 0
  • void thread_func(void arg)
  • int val
  • / unprotected code why? /
  • val counter
  • counter val 1
  • return NULL

16
Mutex
  • int counter 0
  • ptread_mutex_t mutex PTHREAD_MUTEX_INITIALIZER
  • void thread_func(void arg)
  • int val
  • / protected by mutex /
  • Pthread_mutex_lock( mutex )
  • val counter
  • counter val 1
  • Pthread_mutex_unlock( mutex )
  • return NULL

17
Condition Variable for signaling
  • Think of Producer consumer problem
  • Producers and consumers run in separate threads.
  • Producer produces data and consumer consumes
    data.
  • Consumer has to inform producer when data is
    available

18
Without condition variables
19
  • / Globals /
  • int data_avail 0
  • int pthread_mutex_t data_mutex PTHREAD_MUTEX_INIT
    IALIZER
  • void producer(void )
  • Pthread_mutex_lock(data_mutex)
  • Produce data
  • Insert data into queue
  • data_avail
  • Pthread_mutex_unlock(data_mutex)
  • consume_data()

20
  • void consumer(void )
  • Pthread_mutex_lock(data_mutex)
  • while( !data_avail )
  • / do nothing keep looping!!/
  • Extract data from queue
  • if (queue is empty) data_avail 0
  • Pthread_mutex_unlock(data_mutex)
  • consume_data()

21
With condition variables
22
  • int data_avail 0
  • int pthread_mutex_t data_mutex PTHREAD_MUTEX_INIT
    IALIZER
  • int pthread_cont_t data_cond PTHREAD_COND_INITIA
    LIZER
  • void producer(void )
  • Pthread_mutex_lock(data_mutex)
  • Produce data
  • Insert data into queue
  • data_avail
  • Pthread_cond_signal(data_cond)
  • Pthread_mutex_unlock(data_mutex)
  • consume_data()

23
  • void consumer(void )
  • Pthread_mutex_lock(data_mutex)
  • while( !data_avail )
  • / sleep on condition variable/
  • Pthread_cond_wait(data_cond, data_mutex)
  • /woken up /
  • Extract data from queue
  • if (queue is empty) data_avail 0
  • Pthread_mutex_unlock(data_mutex)
  • consume_data()

24
ioctl()
25
ioctl()
  • Handles miscellaneous properties of a file/device
    referenced by a descriptor.
  • In our case, network interfaces.
  • int ioctl(int fd, int request, void arg)
  • Socket operations
  • File operations
  • Interface configuration
  • ARP cache
  • Routing table

26
  • SIOSPGRP/SIOGPGRP
  • set/get process/group ID of a socket
  • FIONREAD
  • Return number of bytes in socket buffer
  • SIOCGIFCONF
  • Get list of all interfaces
  • SIOCGIFBRDADDR/ SIOCSIFBRDADDR
  • Get/set broadcast address
  • SIOCGARP/SIOCSARP/SIOCDARP
  • Get/modify/delete ARP cache entry.
  • SIOCADDRT/SIOCDELRT
  • Add/delete routes
Write a Comment
User Comments (0)
About PowerShow.com