I modified a fork() C template to execute ps -l in the child and I'm supposed to look at the ps lines and locate it's own process (to see that that it is the same id as the child before the launch of ps). I wrote this on Ubuntu and it worked just fine but now that I launched it on MacOS, I can't locate the ps line in its own list. Any idea of why is it behaving differently? Thanks!
Here is the code in C:
#include <stdio.h> /* Pour perror */
#include <stdlib.h> /* Pour exit */
#include <unistd.h> /* Pour fork, getpid, getppid, sleep */
#include <sys/types.h> /* Pour pid_t (fork, getpid, getppid) */
#include <sys/wait.h> /* Pour wait */
int main(void)
{
pid_t ident;
ident = fork();
if (ident == -1)
{
perror("fork");
return EXIT_FAILURE;
}
/* A partir de cette ligne, il y deux processus */
printf("Cette ligne sera affichee deux fois\n");
if (ident == 0)
{
/* Code execute uniquement par le fils */
printf("Je suis le fils, PID: %d, père: %d\n", getpid(), getppid());
execlp("ps", "ps", "-l", NULL);
}
else
{
/* Code execute uniquement par le pere */
printf("Je suis le pere\n, PID: %d, père: %d\n", getpid(), getppid());
wait(NULL);
}
return EXIT_SUCCESS;
}
By default, ps
on macOS lists your processes without controlling terminals. ps
is a privileged command with the sticky bit set on its executable file and runs as root. Adding a -a
switch to show the processes of other users will show the ps
process.