CS-332 Gerbessiotis, Alex         July 2, 2025
------------------------------------------------

Note: If you see reference to a Readme1st.txt
      or just Readme1st it is this file
      named Readmej3.txt and the 3 is a reference
      to Subject 3.

1.  Introduction

This Readme1st.txt file describes briefly the code in this 
directory.  More info on how to compile is available in 
Readme1st.txt related to j1srcXYZ.tar

 1. Compile all fork source code files + orphan.c + zombie.c

 The first time do a

 % cp Makefile.fork Makefile

 Then 

 % make clean; make all

 If you forget the cp step then

 % make clean -f Makefile.fork ; make all -f Makefile.fork



or if you know what you are doing,

 % make all


 2. Compile the zombie and orphan files separately

To compile the orphan and zombie files execute

 % make clean; make other

or if you know what you are doing,

 % make other


 3. Description of some of the source files included

Makefile   : Be careful with editing the Makefile
             TABs are important and if you turn a TAB into 
             space or delete it you will regret it.

Some of the fork[1234].c files are described in Subject 3.

AVOID WRITING A FORK the way it is exhibited in fork1.c-fork2.c

fork1.c    : A minimal C code program having a fork 
fork2.c    : A less minimal C code program having a fork 

fork3.c    : A complete C code program using  a fork, 
             inspecting properly the return value of the fork,
             and 
             having the parent process waiting for the child 
             process to complete its execution,
             thus
             capturing the return value of the child process 
             properly.
fork4.c    : A variation of fork3.c where all the fork related 
             code is isolated in a function other than main.
             BE CAREFUL WITH return values!
             THE RETURN value of the PARENT or a CHILD PROCESS is
             the return value of the main function not the function
             that contains the fork system function call!
fork5.c    : Child process runs another program (in fact a script)! 
fork6.c    : Array differences for parent and child!
fork7.c    : Try to reason with this program's output
fork8.c    : Use of shmem ; the parent passed to its child its pid;
             child negates pid of parent!
If there are other fork source code files explore them!


orphan.c   : Creating an orphan process. 
             It will take one minute or so to complete. 
             The child is going to print twice a line
             containing fval,PID (of the child), PPID (of the child).
             Observe the PPID (of the child) in the two output lines.
zombie.c   : Creating a zombie process temporarily
             A zombie process is indicated in linux with the
             state known as defunct. To appreciate this
             open two (terminal) sessions. In the first you run 
             the zombie executable
             (rename it also to  myzombie if you want) and in the
             second session run continuously  
                % ps -ef | egrep yourUCID
             where yourUCID is your NJIT UCID also indicated in the 
             first column of the output.

mysleep.c  : Process blocks (sleeps) for a number of seconds
             (command line controlls the number of seconds).
spawn.c    : No comment provided. Explore it!


Other additional files are auxiliary. Explore them, if any!


******************************************************
HOW to KILL a Process in Linux/Unix 
******************************************************
        Disclaimer: KILL means TERMINATE :-)
------------------------------------------------------

CTRL-C : with the CTRL button depressed you also press 
         the c button
CTRL-Z : with the CTRL button depressed you also press 
         the z button

1. Process runs on the foreground of the bash shell


  Press CTRL-C
  It sends the process the interrup signal (SIGINT 2)


2. Process runs on the foreground 
   (but you have on another window a instance of bash running)
   or the process runs in the background

NOTE: % ps -ef | egrep yourUCID 
      shows in the second column a process's PID. 
      If it is for a process 5678 a

      % kill -9 5678
      would kill that process.

   9 is the SIGKILL signal


3. Process runs in the background

   % jobs
[1]+  Running                 ./mysleep 180 &
   Select process (number in brackets is the job number
   associated with a process and in this case
   process mysleep with argument 180)
   % kill %1
% [1]+  Terminated              ./mysleep 180


  Be careful if
   % jobs
[1]   Running                 ./mysleep 360 &
[2]-  Running                 ./mysleep 240 &
[3]+  Running                 ./mysleep 180 &

   Pick the appropriate job number
   % kill %1
   vs
   % kill %3




******************************************************
HOW to SUSPEND, STOP, START, RESTART, KILL 
a Process in Linux/Unix 
******************************************************

 Do a
 % gcc mysleep.c -o mysleep
 Then 


1. Run a process in the foreground
-------------------------------

 %  ./mysleep 180
 Process mysleep runs in the foreground and will
 run for 180 seconds and then will terminate.
 Running in the foreground it means that the process
 blocks further user interaction with the bash shell.
 No % prompt is being displayed by  bash just the
 cursor that indicates where no action is!

2. Stop  a process in the foreground
-------------------------------
CTRL-Z: If you press CTRL-Z you STOP the process and
        a 

[1]+  Stopped                 ./mysleep 180

        is then displayed. [1] is the job number 
associated with the stopped process. The plus
indicates it is the more recent one.

3. Continue running the  process in the background
-------------------------------
A Prompt+cursor are now visible but mysleep is stopped. 
Do a 

   % bg %1

and mysleep continues it execution in the background
Confirm it with a

   % jobs

that now shows

 [1]+  Running                 ./mysleep 180 &

Note the ambersand at the end ! It indicates it
runs on the background

4. Bring it into the foreground again
-------------------------------
Run in foreground:  do a cautious and confirming

 % jobs
[1]+  Running                 ./mysleep 180 &

 to confirm it is still running and identify the
job number, and then

 % fg %1

to bring it to the foreground.

5. Changed your mind: Back to the background!
-------------------------------
  Go to step 2 and then step 3

6. Run to the background directly !
-------------------------------
After compilattion, run it in the  background 
directly with: 

 %  ./mysleep 180  &

 Note the ambersand at the end !  Compare with 
 a previous 

 %  ./mysleep 180  

7. Kill a process
-------------------------------

If there are multiple processes running in
the background

   % jobs
[1]   Running                 ./mysleep 360 &
[2]-  Running                 ./mysleep 240 &
[3]+  Running                 ./mysleep 180 &

 the + and - indicate the more recently activated
and second most recently one.
