CMSC 412
Operating Systems

Date: July 30, 2017
by Nicholas Duchon

Outline:

  • Processes C
  • Processes C++
  • PThreads C
  • PThreads C++

Java:

C/C++:


Processes C

#include <stdio.h>   // used in printf
#include <stdlib.h>  // used in atoi and wait
#include <unistd.h>  // used in fork and getpid
#include <time.h>    // used in nanosleep

// File: spawn.c
// Date: Oct 8, 2017
// Author: Nicholas Duchon
// Purpose: demonstrate process interleaving using fork()
// References: a variety of net searches

// stuff for nanosleep control structure
// uses time.h declarations
struct timespec tim0, tim1, tim2;

void pr (int v, int max) {
   int j;

   // wait before starting looping
//    nanosleep (&tim0, &tim2);

   for (j = 0; j < max; j++) {
      printf ("-- %3d -- %c: %d\n", v, 'A'+v, j);
   //       nanosleep (&tim1, &tim2);
      if (j%100 == 99)
         printf ("\n");
   } // end printing some different stuff in each child

} // end pr

int main (int argc, char *argv[]) {
   // wait before starting looping
   // makes it possible to start some/all processes before any loop
   tim0.tv_sec  = 2;
   tim0.tv_nsec = 0;

   // wait on each iteration
   // lets processes interleave
   // otherwise processors are so fast that interleaving hard to detect
   tim1.tv_sec  = 0;
   tim1.tv_nsec = 100000;

   pid_t pid;         // process id, used to tell child 0 from parent > 0
   int i;             // index in for loop used to create max children
   int max = 100;      // number of iterations for each child
   int nChildren = 5; // how many children to create

   if (argc > 1)
      nChildren = atoi (argv[1]);
   if (argc > 2)
      max = atoi (argv[2]);

   for (i = 0; i < nChildren; i++) {
      pid = fork(); // return 0 to child, child pid to parent
      if (pid == -1) {
            /* error handling here, if needed */
         return 1;
      }
      if (pid == 0) {
         // can make decisions based on i at this point, for example
         // getpid() returns child's real pid
         printf("Starting Child: %3d PID: %d\n",i, getpid());
         pr (i, max);
         printf ("Ending Child: %3d PID: %d -------------------------\n", i, getpid());
         // do not go back into the for loop in a child process
         // could also do a return (0) here as well
         return 0;
      } // in children
      else {
        // do something in main before next fork?
        // wait is probably not what you want to do here
      } // in parent
   } // end for

   printf("Starting Parent: PID: %d\n", getpid());
   pr (i, max);
   printf ("Ending Parent: PID: %d <<<<<<<<<<<<<<<<<\n", getpid());
   // The following will wait until all child processes finish
   // otherwise, end of parent will kill all children even if they are not finished
   wait (NULL);

   return 0;
} // end main


Processes C++

The following is just a template showing how to get process spawning to work in C++ - so getting this to work as the C example above will require adding significant code to this example.

// File: spawn.cpp
// Date: Oct 12. 2017
// Author: Nicholas Duchon
// Purpose: basic code that will compile in C++
//    spawning processes using fork

#include <unistd.h>  // used in fork and getpid
#include <stdio.h>   // used in printf
#include <stdlib.h>  // used in atoi, exit, and wait

int main () {
   int i, pid;
   int n = 5; // spawn 5 children.
   for (i = 0; i < n; ++i) {
      pid = fork();
      if (pid) {
         // print parent's value of pid from fork operation
         printf ("pid: %d\n", pid);
         continue;
      }
      else if (pid == 0) {
         // in the child
         break;
      }
      else {
         printf("fork error\n");
         exit(1);
      }
   }
} // end main

PThreads C

// File: threadDemoA.c
// Date: Aug 30, 2019
// Author: Nicholas Duchon
// Purpose demonstrate pthreads
//   No error handling or interesting code in thread here.

#include <stdio.h>   // used in printf
#include <pthread.h> // pthread stuff

void * f1 (void * x) {
  printf ("in f1\n");
  return NULL;
} // end f1, thread method

int main (int argc, char *argv[]) {
  printf ("This is in main\n");
  pthread_t ptt;
  int ec = pthread_create (&ptt, NULL, f1, NULL);
  // main will typically end before f1 runs
  // without the following join
  pthread_join (ptt, NULL);
  printf ("This is end of main\n");
} // end main


PThreads C++


+++++++++++++++++++++++++++++++
Dr. Nicholas Duchon
University of Maryland University College
Phone: 301-985-7793
e-mail: duchon@nova.umuc.edu
or:
duchon@nova.umuc.edu

+++++++++++++++++++++++++++++++
Last updated: 9.20.2017