DevNation
DevNation

DevNation

Linux Basic Programming-1

DevNation's photo
DevNation
·Aug 9, 2021·

3 min read

Process:-

Process is define as "an address space with one or more threads executing within that address space, and the required system resources for those threads."A multitasking operating system such as Linux lets many programs run at once. Each instance of a running program constitutes a process.

Process Structure:-

Each process is allocated a unique number, called a process identifier or PID. This is usually a positive integer between 2 and 32,768. When a process is started, the next unused number in sequence is chosen and the numbers restart at 2 so that they wrap around. The number 1 is typically reserved for the special init process, which manages other processes. Normally, a Linux process can’t write to the memory area used to hold the program code, so the code is loaded into memory as read-only. The system libraries can also be shared. Thus, there need be only one copy of printf , for example, in memory. Additionally, a process has its own stack space, used for local variables in functions and for controlling function calls and returns. It also has its own environment space, containing environment variables that may be established solely for this process to use. process must also maintain its own program counter, a record of where it has gotten to in its execution, processes can have more than one thread of execution. On many Linux systems, and some UNIX systems, there is a special set of “files” in a directory called /proc . These are special in that rather than being true files they allow you to “look inside” processes while they are run

The Process Table:-

The Linux process table is like a data structure describing all of the processes that are currently loaded with, for example, their PID, status, and command string, the sort of information output by ps . The operating system manages processes using their PIDs, and they are used as an index into the process table.

Starting New Processes:-

#include <stdlib.h>
int system (const char *string);
#include <stdio.h>
int main()
{
printf("Running ps with system\n");
system("ps ax ");//I pass
system("ps ax &");//IInd pass
system("ls -l");//IIIrd pass
printf("Done.\n");
exit(0);
}

Output:-

processnew.png

Explanation:-

int system (const char *string);

The system() function runs the command passed to it as a string and waits for it to complete.

You can cause a program to run from inside another program and thereby create a new process by using the system library function.

You can use system() to write a program to run ps.

The program calls system with the string “ps ax” , which executes the ps program. The program returns from the call to system when the ps command has finished. In the Ist pass, the call to system returns as soon as the shell command finishes. Because it’s a request to run a program in the background, the shell returns as soon as the ps program is started.

$ ps ax &

At a shell prompt. The IInd pass exits before the ps command has had a chance to finish all of its output. The ps output continues to produce output after IInd pass exits and in this case does not include an entry for IInd pass.

 
Share this