Function system

Hellow,
When we use function system("ps -aux | wc -l"); to x =28.
As it does not use function system("ps -aux | wc -l"); it returns x = 38867.
Which is the correct count?
X is to be type long int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <signal.h>
#include <unistd.h>
#include <cstdlib>
#include <stdio.h>

int main(void)
{
	long int x = 0;
	alarm(1);
	while (1)
	{

printf("X=: ");
		printf("%li\n", x);
		x++;
printf("ID process: ");
printf( "pid %d ppid %d\n ", getpid(), getppid());

system("ps -aux | wc -l"); 
}
	return 0;
}
Last edited on
Your question is not clear.


Nevertheless, why do you spawn several processes to count running processes? Use the OS API.
For example, see https://stackoverflow.com/questions/939778/linux-api-to-list-running-processes
Topic archived. No new replies allowed.