0

I'm working on a hw assignment and was wondering if someone with a bit more Unix experience could explain what is meant by the following question:

Run sample program 1 in the background with &
Using the ps utility with appropriate options, observe and report the PIDs and the status  of your processes.

Sample program 1 is:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int
main()
{
    puts("Before fork");
    fork();
    sleep(10);
    puts("After fork");
}

I compiled and ran:

./fork_demo &

What is the best way to use ps with this running in background? I'm only looking for guidance here and this is a tiny portion of a HW assignment. I've already done the major stuff!

David Bejar
  • 512
  • 4
  • 20
Jordan
  • 2,070
  • 6
  • 24
  • 41
  • Just type `ps`. What's the problem? The forked program will sleep for 10 seconds, so there is plenty of time for typing `ps` command. – nhahtdh Jun 13 '12 at 02:36
  • I figure there is a better way of reporting them than simply ps? – Jordan Jun 13 '12 at 02:40
  • 2
    You might want to use ps to look at the process tree, as I believe the assignment is trying to show you the relationship between the PIDs of the parent and child. Just type `man ps` in a terminal to get more info. – Alex W Jun 13 '12 at 02:42
  • You can print the PID of the process right after `fork` and before `sleep` with `getpid`: http://linux.die.net/man/3/getpid – nhahtdh Jun 13 '12 at 02:42
  • I also didn't know if "status" of processes had a significant meaning other than just the existence of the processes. I suppose I am over thinking the problem. – Jordan Jun 13 '12 at 02:42

2 Answers2

4
./fork_demo &
ps

Explanation:

& forces the process before it (fork_demo) into the background. At that point, you can run another command (like ps).

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
3
ps -ef | grep fork_demo

or

ps aux | grep fork_demo

Not knowing which are the "right parameters" for your assignment, I took some guesses.

xelco52
  • 5,257
  • 4
  • 40
  • 56