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.
Related
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!
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.
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.
This question already has answers here:
How to send a simple string between two programs using pipes?
(7 answers)
Closed 8 years ago.
I have created a named pipe (mkfifo) in process 1 and written something on it.
Now I can read the content written by process 1 in process 2.
Now I can to something (like listen) by which process 2 comes to know that process 1 has written some thing.
To complete the comment of Joachim.
You should consider using select or poll
What are the differences between poll and select?
I don't know if I really got your question but, if you want to process 2 know when process 1 write something, I suggest you use a signal from the process 1 to warn process 2 that things are ready to be read.(there are another ways to do this as well)
Since you got both process IDs(pid) you can use kill to send your ready message, so from the process that has done writting, it will be like that:
kill(pid, SIGINT)
And then handle Interrupt signal as you wish:
struct sigaction sigtohandle;
memset (&sigtohandle, 0, sizeof (sigtohandle));
sigtohandle.sa_handler = &read_process;
sigaction (SIGINT, &sigtohandle, NULL);
So you will have a function called read_process() on the signal receiver process that is supposed do the job you desire.
You can read more about processes and signals here:
http://advancedlinuxprogramming.com/alp-folder/alp-ch03-processes.pdf
This question already has answers here:
How to share memory between processes created by fork()?
(3 answers)
Closed 8 years ago.
I am a beginner on C program, and got a question between worker process.
I write a program which fork a child process, inside the process, it create 2 threads to run and get some values from DB; however, if I fork 2 process, it will create 4 threads totally
I wonder the "extern variables" will be share within this 2 processes? Or independent?
If the variables are not shared, how could I maintenance the consistency between the processes?
(Sorry for my poor english)
Thanks all!!
When you fork a process , both parent and child have their own separate address space and cannot communicate via variables.
When you launch two threads (thread is a lightweight process) inside a process they share the address space of the process and can communicate via variables.
So the "extern variables " will be duplicated twice in your case and you cannot use them to communicate between the processes. To synchronize the two processes you need to use an IPC (inter process communicatin) mechanism such as (for example) a Shared memory for the storage coupled probably to a semaphore to prevent concurrent access.