1

If I execute the following code which is single threaded:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  char[] cmd1 = "cat /sys/class/thermal/thermal_zone0/temp > temp.txt";
  char[] cmd2 = "cat /sys/class/thermal/thermal_zone2/temp > temp2.txt";
  system(cmd1);
  system(cmd2);
  return 0;
}

I was under the assumption that the aforementioned code is a single threaded user-level application. When the program executes, especially the system() function, which requires to execute the shell command. So when this program calls two shell command functions are they being executed on two different threads (a thread for this program and another thread executed by shell)? Or when system() function is called the operation passes the control to shell, which is then preempted and executes the command and then hands back the operation to the program thread?

Can someone tell me how the aforementioned code works in thread level?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Somdip Dey
  • 3,346
  • 6
  • 28
  • 60
  • 1
    Shell commands are not "executed using kernel threads"... – user253751 Feb 14 '19 at 00:56
  • @immibis thank you for the reply. Could you tell me the task flow of the code? I thought the program uses system() function to execute the shell command, which is then executed by shell. This could be tracked using 'top' on Linux but when system() is called is it executed on a different thread than the thread of the program? – Somdip Dey Feb 14 '19 at 01:06
  • `system` is usually implemented using `fork` and `exec` so a separate process is created. The main process waits for `system` to complete – stark Feb 14 '19 at 01:12
  • @stark so when a separate process is created is it executed on a different thread or on the same thread but a different process/task? And when system completes, it gives the control back to the main thread of the program? – Somdip Dey Feb 14 '19 at 01:14
  • 2
    Threads are parts of processes. There's no such thing as "same thread but different process". – Barmar Feb 14 '19 at 01:38
  • 2
    Put another way, a process is a collection of threads that all share the same address space. – Barmar Feb 14 '19 at 01:39

2 Answers2

3

The system() function context means the main process is spawning a child process only to immediately wait for its termination. So we can think system() = fork() -> exec() -> waitpid(). In your situation:

char[] cmd1 = "cat /sys/class/thermal/thermal_zone0/temp > temp.txt";
char[] cmd2 = "cat /sys/class/thermal/thermal_zone2/temp > temp2.txt";
system(cmd1);
system(cmd2);

The main process will spawn new child process, execute the utility cmd1, wait for cmd1 termination. Then it will spawn another child process, execute utily cmd2, wait for cmd2 termination.

No thread level in this context. Thread is a unit of execution in a process. A process can contains one or more threads.

Loc Tran
  • 1,170
  • 7
  • 15
1

On Linux, threads and Processes are similar - they are called tasks. In Linux, a thread is simply a process sharing few things with other process(s). The system() function call is a blocking one and your program waits when the system() finishes its work. system() produces a new process which is killed when the work is completed.

You can say that total three processes were created in a sequential manner. But since nothing was shared between the processes - they were not threads.

Syed Waris
  • 1,056
  • 1
  • 11
  • 16