Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operating system inside

I have three questions which are causing me a lot of doubts:

  1. If one thread in a program calls fork(), does the new process duplicate all threads, or is the new process single-threaded?

  2. If a thread invokes exec(), will the program specified in the parameter to exec() replace the entire process including ALL the threads?

  3. Are system calls preemptive? For example whether a process can be scheduled in middle of a system call?

like image 722
Utkarsh Srivastav Avatar asked Nov 30 '25 22:11

Utkarsh Srivastav


2 Answers

For exec, from man execve:

All threads other than the calling thread are destroyed during an execve().

From man fork:

The child process is created with a single thread — the one that called fork().

like image 115
Kerrek SB Avatar answered Dec 03 '25 14:12

Kerrek SB


W.r.t. #3: Yes, you can invoke a system call that directly or indirectly makes another thread ready to run. And if that thread has a greater priority than the current and the system is designed to schedule it right then, it can do so.

like image 28
Alexey Frunze Avatar answered Dec 03 '25 12:12

Alexey Frunze