CSS430 - Operating and File Systems

Program 1C: Process Creation


The goal of process.cpp is to get familiar with with Linux programming using system calls such as fork, execlp, wait, pipe, dup2, and close processes.cpp simulates the command: ps -A | grep argv | wc -l where argv is an input string such as tty, user, or Sys, ect… As part of the class discussion on the process assignment, we graphed out the command process for the assignment.  This was the result.

ps -A | grep argv | wc -l

Most of the design of the program was extrapolated from the textbook (Silberschatz, et al, “Operating System Concepts”) example given on page 118:

    int main(){
        pid_t pid;
        pid = fork(); //fork a child process
	if (pid < 0) { //error occurred
		fprintf(stderr, "Fork Failed");
		return 1;
	}else if (pid == 0) { // child process
	} else { // parent process
		wait(NULL); // parent will wait for the child to complete
		printf("Child Complete");
	return 0;
The textbook example is for a single child process, so it needed to be modified to accommodate the three processes for the assignment (ps, grep, wc).  The three commands are hard coded into  execlp() in process.cpp, with only one input of argv[1] for grep so that it can be used with filter words. execlp("grep", "grep", argv[1], NULL);

A solid resource used was the Open Group Publication on the unistd library.  Specifically, the following pages:

  • close – The close() function will deallocate the file descriptor indicated by fildes
  • dup2 – The dup() and dup2() functions provide an alternative interface to the service provided by fcntl()
  • execlp – replace the current process image with a new process (ps)


How to run processes.cpp.

First ensure that the program is compiled.

g++ -o processes process.cpp

Then use the following commands in the shell:
./processes tty
ps -A | grep tty | wc -l
./processes Sys
ps -A | grep Sys | wc -l
./processes user
ps -A | grep user | wc -l>

Additionally, I wrote a shell script to test process.cpp automatically and output the results to a txt file, similarly to what we did in CSS 342/343. Simply run ./c1.sh > output.txt


Output via command line.
The output of the c1.sh script.
Operating Systems | Linux Pipe