Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you spawn another process in C?

Tags:

c

process

How do you run an external program and pass it command line parameters using C? If you have to use operating system API, include a solution for Windows, Mac, and Linux.

like image 537
andrewrk Avatar asked Aug 19 '08 22:08

andrewrk


People also ask

Can a running process spawn another process?

A process can spawn another process dynamically using the posix_spawn() or posix_spawnp() function. This spawned process can be either a supervisor or a user process. Example 3-3 creates a new process.

What is spawning in C?

Spawn refers to a function that loads and executes a new child process. The current process may or may not continue to execute asynchronously. Creating a new subprocess requires enough memory in which both the child process and the current program can execute.

How do you spawn a process in Linux?

INIT is the first process that is fired up, which is run up by using systemd software. if you want to see the tree how this shell was spawned then you can execute “ps fx” command in your terminal which will show you following output. From above picture you can see how the current terminal shell was spawned.


3 Answers

It really depends on what you're trying to do, exactly, as it's:

  1. OS dependent
  2. Not quite clear what you're trying to do.

Nevertheless, I'll try to provide some information for you to decide.
On UNIX, fork() creates a clone of your process from the place where you called fork. Meaning, if I have the following process:

#include <unistd.h>
#include <stdio.h>

int main()
{
    printf( "hi 2 u\n" );
    int mypid = fork();

    if( 0 == mypid )
        printf( "lol child\n" );
    else
        printf( "lol parent\n" );

    return( 0 );
}

The output will look as follows:

hi 2 u
lol child
lol parent

When you fork() the pid returned in the child is 0, and the pid returned in the parent is the child's pid. Notice that "hi2u" is only printed once... by the parent.

execve() and its family of functions are almost always used with fork(). execve() and the like overwrite the current stackframe with the name of the application you pass to it. execve() is almost always used with fork() where you fork a child process and if you're the parent you do whatever you need to keep doing and if you're the child you exec a new process. execve() is also almost always used with waitpid() -- waitpid takes a pid of a child process and, quite literally, waits until the child terminates and returns the child's exit status to you.

Using this information, you should be able to write a very basic shell; one that takes process names on the command line and runs processes you tell it to. Of course, shells do more than that, like piping input and output, but you should be able to accomplish the basics using fork(), execve() and waitpid().

NOTE: This is *nix specific! This will NOT work on Windows.

Hope this helped.

like image 71
FreeMemory Avatar answered Oct 24 '22 22:10

FreeMemory


If you want to perform more complicated operations, like reading the output of the external program, you may be better served by the popen system call. For example, to programmatically access a directory listing (this is a somewhat silly example, but useful as an example), you could write something like this:

#include <stdio.h>

int main()
{
  int entry = 1;
  char line[200];
  FILE* output = popen("/usr/bin/ls -1 /usr/man", "r");
  while ( fgets(line, 199, output) )
  {
    printf("%5d: %s", entry++, line);
  }
}

to give output like this

1: cat1
2: cat1b
3: cat1c
4: cat1f
5: cat1m
6: cat1s
...
like image 18
Blair Conrad Avatar answered Oct 24 '22 20:10

Blair Conrad


#include <stdlib.h>

int main()
{
    system("echo HAI");

    return 0;
}
like image 12
wilhelmtell Avatar answered Oct 24 '22 22:10

wilhelmtell