I have one process which has two thread main thread and event thread ,can i use shared memory between two different thread in a single process for sending the data from one thread to Other ?
If I am using shmget() in two different thread in same process ,one shmget() get the shmid and other return with -1 and failed ,so need to know either its a valid scenario or not ?
Related
I have to code a C program that runs several processes.
If one of these processes stops, I need them all other processes to exit properly (free everything + close every opened semaphores).
Processes can stop either because an error occured or because they died (a specific condition related to my program's purpose).
I only can use semaphores (no mutexes) and the pthread_create, pthread_join, ptthread_detach functions, no signals allowed !
What I did: I run 3 threads per process:
1 thread to execute the main purpose of my process (M),
1 thread to check for any other process' death (D),
1 thread to check for any other process' error (E),
Thread M just runs by default.
For threads D and E, I have a semaphore named /death (or /error) that is worth 0 when initialized. Whenever a process dies (or encounters an error), it posts this semaphore so it will enter in the respective thread, free everything, then posts it again to allow any other process to free again and quit and etc...
Here is an example for the death thread:
void *D_thread(void *args)
{
sem_wait(death_sem);
frees_everything();
sem_post(death_sem);
exit(0);
}
I have 2 issues:
While entering the D thread for example, my process will start freeing all its resources, while its M thread is still running and trying to access to these resources. Could this be a source of errors and should I then protect this with another semaphore ?
My processes will free one after the other, resulting potentially in a slow exit if many processes run together.
Do you think this is a good method or can you think of something else more efficient ? Again: I only can use semaphores (no mutexes) and the pthread_create, pthread_join, pthread_detach functions, no signals allowed !
How can I see the number of threads placed in a process in C ?
Actually, What I find out is whether newly-created process has one thread or no thread.(I am working in Linux)
The pthread_create() function starts a new thread in the calling process.So technically yes you can say there are two threads.The main thread,however remains the dominating one ,returning from main() causes the termination of all threads in the process.
I have an setup with multiple (roughly 32) processes each with 2 threads. I would like to send a message from thread 0 of process A to thread 1 of process B. So, should the message be sent specifically to the thread id or to the process id. If the message is sent to the process, by default which thread will service the message?
There are lots of ways possible. Just search for IPC. For example, you could use shared memory, synchronized by a set of semaphores.
I have used fork() to create 2 different processes operating on 2 different address spaces.
Now, in parent process I need the value of a variable from child's address space or if the child process can modify the variable in parent's address space.
Is this possible?
No, once you've forked, each process gets its own address space and you'll have to look into either:
some form of IPC between the processes to access each others data (such as shared memory or message queues).
some more lighweight variant of fork that allows sharing of data (including possibly threading).
Once you have two processes, sharing data needs interprocess communication: file, pipe or shared memory.
If you mean exchanging data between these two processes you can not. You can do it by system APIs like SharedMemory, Message Passing, Pipeline, Socket, ...
As you have created two process using fork command Both Process will be in different address space so they will only communicate by IPC, message passing ,Piping , Shared Memory etc. otherwise one process can't access other process data as thay do have Process specific data
and similarly threads also have thread specific data
So I have an application which uses threads. Now when the program first starts up, I want it to go through setting up database connections and whatnot before it backgrounds itself so that whatever/whoever starts the program can know if there was an error starting up.
I did some looking around and have found some resources that say 'do not mix fork and threads', while others say that forking in linux will only duplicate the main thread and leave the others alone.
In the case of the latter (where it just duplicates the main thread), how then do the threads access file level (global) variables? Will the threads not be able to access the variables that are now in the forked process's address space?
Ultimately the goal is to have the application background itself after threads have been created. If this is not possible, I can put the fork before the thread creation, just would like to do it as late as possible.
Note: at the time of the fork, the threads will be doing a sleep() loop until the main thread puts data into a shared variable for them to process. So if the sleep gets interrupted, they wont be harmed.
There is no way to duplicate threads as part of the fork, and the parent's threads will all terminate when the parent exits, so even if they could access the child's memory, it wouldn't help you. You need to either create your threads after forking, or use pthread_atfork to register handlers that will recreate them in the child process. I would recommend just waiting until after forking to create your threads since it's a lot simpler and more efficient.
Why is it that you want to delay forking as long as possible? If you want to maintain connection to a terminal or something until initialization is finished, you can just have the parent process wait to terminate until the child process (with its threads) is done initializing and ready to be "in the background". Various synchronization tools could be used to accomplish this. One simple one would be opening a pipe through which the child sends its output back to the parent to display; the parent could simply exit when it receives EOF on this pipe.
Forking a process creates two different processes and threads in one process will not be able to access memory in the second process. If you want different processes to access the same memory, you want something called shared memory.
When a thread in a process calls fork(), a new process is created by copying, among other things, (1) the full address space of the process and (2) the (one) thread that called fork. If there are other threads in the process, they don't get copied. This will almost certainly lead to bugs in your program. Hence the advice not to mix threads and forks.
If you want to create a background process with many threads, you must fork it before spawning any other thread. Then, the two processes behave normally, like any two isolated processes: threads within one process share the same memory, but your background threads and your foreground process won't share any memory (by default).