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
Related
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.
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 4 years ago.
Improve this question
Create child process by using fork() function.
The parent process runs change content of the process by execl() function which run cat f1.c command.
child process runs a traceroute www.google.com command.
Before asking questions here, please try it on your own and post what you have tried so far so we can guide you in the right direction. Also, it would be nice if you put more effort in asking better question. But to give you some guidance:
you can create child process by using fork. It returns an integer. If it is zero, that means you are in the child process. so you can do something like:
int pid;
if((pid=fork())==0){
// you are in child process
//use execl(constant char *path, constant char *commands); to run your commands
}
else {
//whatever you need to do in the parent process
}
You can find about execl() here :https://www.systutorials.com/docs/linux/man/3-execl/ It is basically a way to run a command. The first argument is a constant char pointer which points to the shell that you want to run the command in ("/bin/sh" etc.). The next arguments are the command it self ("cd", "mydir" etc.) terminated with null.
execl("/bin/sh","cd","mydir",NULL);
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.
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 7 years ago.
Improve this question
I need to do some operation at regular intervals in my thread. What is the best method to do this?
Like if, i have a local socket communication between linux deamon and a android application. In this case, if i want to send data periodically to the android app from deamon, how can i proceed?
If you use Linux why not try sleep() or usleep() functions of unistd.h?
sleep(5);
Will pause the thread for five seconds and then resume execution.
sleep(sec)
The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process.
usleep(usec)
usleep() function suspends execution of the calling thread for (at least) usec microseconds.
Emphasis mine.
Taken from here and here.
You should use Simple Signals - C programming and alarm function if you want truely accurate timing. If you don't do this, using sleep() etc will eventually result in your timer shifting due to the overhead of your code, the OS, etc.
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.