Dirrefent ways to run system command in C++ programme

Before proceding a short intro of forking

(o) fork()
The fork() system call will spawn a new child process which is an identical process to the parent except that has a new system process ID. The process is copied in memory from the parent and a new process structure is assigned by the kernel. The return value of the function is which discriminates the two threads of execution. A zero is returned by the fork function in the child’s process.

The environment, resource limits, umask, controlling terminal, current working directory, root directory, signal masks and other process resources are also duplicated from the parent in the forked child process.
** Problems
1) Some memory duplicated by a forked process such as file pointers, will cause intermixed output from both processes.
2) Race conditions can be created due to the unpredictability of when the kernel scheduler runs portions or time slices of the process.

(o) vfork()
Some systems have a system call vfork(), which was originally designed as a lower-overhead version of fork().The basic difference between the two is that when a new process is created with vfork(), the parent process is temporarily suspended, and the child process might borrow the parent’s address space. This strange state of affairs continues until the child process either exits, or calls execve(), at which point the parent process continues.
** Problems
1) A deadlock condition may occur if the child process does not terminate, the parent process will not proceed.

(o) int system ( const char * command )
Invokes the command processor to execute a command. Once the command execution has terminated, the processor gives the control back to the program, returning an int value, whose interpretation is system-dependent. C string containing the system command to be executed.
This function is implemented using fork(), exec() and waitpid(). The command string is executed by calling /bin/sh -c command-string. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored. The call “blocks” and waits for the task to be performed before continuing.
** Problems
1) Rrequired memory to fork, so in case of no memory this would not work.

(o) popen()
The popen() call opens a process by creating a pipe, forking, and invoking the shell (bourne shell on Linux). The advantage to using popen() is that it will allow one to interrogate the results of the command issued.

** Problems
1) Rrequired memory to fork, so in case of no memory this would not work.

(o) exec() and execve()
The exec() family of functions will initiate a program from within a program. They are also various front-end functions to execve().The functions return an integer error code. (0=Ok/-1=Fail).

more detailed info

** Problems
1) Rrequired memory to fork, so in case of no memory this would not work.

(o) By using kernel interface
A central theme in operating system design is the kernel interface. The kernel interface is the API of the operating system, and therefore the choice of API determines the structure, features and limitations of an operating system. We can use such interfaces to run some of commands we need in emergency.

e.g To run reboot command include and use int reboot(int howto);

This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s