Why we use threads and select together [duplicate] - c

This question already has answers here:
Select function in socket programming
(5 answers)
Closed 3 years ago.
I'm setting up a server with some clients using socket programming in c.
I found that in order to have more than one client, I should use threads or select or poll.
I know how should I use these function but when I searched I found that there is a way that used from thread and select together.
I have two questions:
1) what is the reason and benefits(using select and thread together)?
2) Is thread used for clients and select for reading the socket?

You can handle more than one socket/client in one thread by using select/epoll and non-blocking I/O. You can handle even more sockets/clients if you have multiple threads doing that.
Old but still relevant read The C10K problem.

Related

How to find out witch thead using specific file descriptor with C [duplicate]

This question already has answers here:
How to view thread id of a process which has opened a socket connection?
(2 answers)
Closed 3 years ago.
I have a process with 100 threads.
I know that only one thread is using a specific fd.
For example, this fd is a socket descriptor, and only one thread is using this socket with send() and receive().
How can I find out, with C, on Linux, the ID of this thread?
Is there a smarter way than attaching to each thread with ptrace and waiting until one of them will be detected?
File descriptors are part of the process. And since a file descriptor is just a nonnegative integer, and can be used by all threads of the same processes without explicit rebinding, asking "which thread holds an fd" is not a question applicable to the Linux process/threading model.
If you really want an answer then it would be: All the threads do!

How I can check if it something available in socket? [duplicate]

This question already has answers here:
How to check amount of data available for a socket in C and Linux
(6 answers)
Closed 7 years ago.
I write a simple application that provide connection between two computers in C. I have a problem with simultaneously reading and writing in socket. I am able to check if user press any key by getch(), but I don't know how to check if it something in socket buffer. When I use read() function it wait until be something in socket. I wanted to check socket buffer and then use read(), but I can't find any function/flag to check this. Maybe is different solution for this problem, perhaps use another thread to read?
You can use sys/ioctl.h file's method ioctl:
#include <sys/ioctl.h>
...
int count;
ioctl(fd, FIONREAD, &count);
Reference credit : This Answer

Using system call in c [duplicate]

This question already has answers here:
replace system() with non-blocking function
(5 answers)
Closed 8 years ago.
Please tell me how to open two files simultaneouly using system();
I want to open two file
and I am doing it like
system("C:\temp\file1.doc");
system("C:\temp\file2.doc");
But here till the file 1 is open file 2 is not opening as the control is not able to reach the second system call , Is there a way to do open them simultaneously.
Thanks
Shashank
I would suggest two ways.
Run two threads. Call system() from each thread.
Create a child process using fork() and run system() from both parent and child processes.

Find number of threads spawned by a process in Linux [duplicate]

This question already has answers here:
POSIX API call to list all the pthreads running in a process
(3 answers)
Closed 9 years ago.
I want to write a c function , which when called by the process returns number of threads created by that process.
I want to get the value not by counting but from the kernel structure ?
Which structure has this information ?
You can get a lot of information about your process by looking in /proc/$$ where $$ is your process ID.The number of threads is available atomically through /proc/$$/status.
My solution: You need write a function to analyse the file /proc/$$/status to get the number of threads.

semaphore unexpectedly getting set [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Interprocess semaphores sometimes not working as expected
In my application, I notice that a semaphore of type sem_t sometimes become 1 from 0 without executing sem_post. How come? What can cause this? The semaphore is used for inter-process communication and you can look at the code here.
The code that you are linking to doesn't capture the return values from the sem_t calls. If you look in the manual you can see that e.g sem_wait can return prematurely, so-called spurious wakeups.
Always check the return codes of these functions. If the return is -1 check errno for the corresponding error and decide if it is a transient error. If so, iterate.

Resources