Does !fork() create a process? [closed] - 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 5 years ago.
Improve this question
For instance if I call
if (!fork())
does this create a new process like calling
if (fork())
or does it not?

It is the same, fork is evaluated before check is made.
if (!fork()) is the same as if (fork()) for function itself but not the same for if statement check.

Of course it does. In order to execute
if (! <expression>)
it first has to evaluate <expression>, so that it can then invert the boolean value of the expression and test it with if. When the expression is fork(), it calls the fork function, which creates a new process, and then returns a value in both the child and parent processes.
It's essentially just short for:
pid_t pid = fork();
if (!pid)

Related

What is a main thread? [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 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.

Creating child process in C linux [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 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);

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