What is a main thread? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is a main thread in multithreading in a c program?
I need to create m threads and to execute different operations on main thread and on the created m threads. Is the main thread the main function maybe?

The main thread is the thread on which main() is called at program startup. Never end the main thread: on most platforms this ends the process quite rapidly.
There's a reason the alternative to multi-threaded programming is called single-threaded programming, not threadless programming. You always have at least one thread.
While some platforms allow you to create a process without any threads at all, that process isn't going to do anything.

Related

Execute program in C and don't wait it for finish [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to call a program in C on Windows and immediately close the main program. I've tried using system() like this:
system("SecondaryProgram.exe");
return 0;
But the "caller" program always waits for SecondaryProgram.exe to finish. I would like to avoid this and immediately return 0 before the "called" program closes, something like opening it in "another thread". Is there any other function that does this?
Usually Windows compilers have support for execXXX and spawnXXX functions. So something like this should work:
#include <process.h>
...
spawnl(P_NOWAIT, "prog.exe", "prog.exe", NULL);
exit(0);
Use the CreateProcess() API. When it successfully launches another process, it will return a handle to you that you can use to wait for the process to complete, or not, as you like.
system("cmd.exe /C SecondaryProgram.exe");
But you may have a DOS box floating around until SecondaryProgram finishes.

What is the purpose of condition variables in Monitors? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What is the purpose of condition variables in Monitors? Why really do we need the condition variables? It may sound something stupid to ask, but never mind I am really new at this. Thanks in advance
A condition variable is basically a container of threads that are waiting for a certain condition. Monitors provide a mechanism for threads to temporarily give up exclusive access in order to wait for some condition to be met, before regaining exclusive access and resuming their task.Reference link here.
Conceptually a condition variable is a queue of threads, associated with a monitor, on which a thread may wait for some condition to become true.

How to check if child process is finished in c by its pid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am building a very tiny shell in c,
I have the option on running programs in the background.
I keep a list of all my jobs meaning all the ones in the bg.
Now if i want to go and update this list, how can i check is a process is finished or if its still running.
ps
if i waited with waitpid for some process, will i still be able to check if the process is done? (i mean if i used waitpid it took the process of zombie state.
You should be able to call waitpid, passing it the process id and the WNOHANG option and call the WIFEXITED macro on the integer returned through status argument. See Just check status process in c.

How can I create a timer in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to create a timer that counts seconds and display them, but in parallel I want to do other actions. Can someone explain me how can I do that or if this is possible (in Visual Studio)?
This is one way to handle your desired actions.
in main thread,
-fork a child process. passing
1) a desired time interval
2) a callback function ptr
3) indication of oneShot or repeating timer
into the child process,
in the child process:
-begin loop
-sleep the passed-in-time-interval
-execute the call back function
-if non repeating timer
-then
-exit child process
-endif
-end loop
in main thread, in the call back function:
-get current time
-display the current time to user
-return
in main thread, when ready to exit, kill child thread

Is it possible to get parent threadID from child? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to add instrumentation into my code that will print out something like
'Thread 1 forks Thread 2'
Any suggestions on how I can achieve this?
Terminology correction: one thread may create another thread, not fork, which is usually
used to mention one process forking another.
No, a thread has no way to get another thread's identifier. On Linux, you can check if gettid() == getpid() to find if it's the main thread. Solaris has thr_main() to identify if the caller is main thread or not. FreeBSD has pthread_main_np() for the same purpose.
But there's no way to identify parent-child relationship between any threads. Any thread can create more threads. You'll have to use pass the thread identifiers around when creating threads or use global data structure to maintain this information.

Resources