Example #1
0
File: userapp.c Project: am5/MP2
///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME:  main
//
// PROCESSING:
//
//    This function tests the register, unregister, and yield functions from 
//    user space. 
//
// INPUTS:
//
//    argc - the number of arguments passed to the program
//    argv - the command line vector that contains the arguments passed to the program
//
// RETURN:
//
//    int - (0) default with no errors
//          (-1) unable to register the PID 
//
// IMPLEMENTATION NOTES
//
//   The main function tests all the functions that were implemented in kernel 
//   space that the user-space application will use in order to register and 
//   unregister with the kernel module, and to change it status using yield.  
//
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
  pid_t mypid;
  int j;

  long period = 250;		// in milliseconds
  long processTime = 10;	// in milliseconds
  
  // get our PID so that we can register
  mypid= syscall(__NR_gettid);

  // register with the module
  if(!try_register(mypid, period, processTime)){
    printf("Unable to register this PID %ld\n", mypid);
    return -1;
  }

  struct timeval tv;
  gettimeofday(&tv, NULL);
  printf("Current time %d\n", tv.tv_sec);
  // writes yield to proc/mp2/status file for this pid
  try_yielding(mypid);
  gettimeofday(&tv, NULL);
  printf("Current time %d\n", tv.tv_sec);

  for(j=1; j<100; j++)
  {
	printf("Factorial %u: %llu\n",j, factorial(10));
        try_yielding(mypid);
  	gettimeofday(&tv, NULL);
        printf("Current time %d\n", tv.tv_sec);
  }
  // unregister from the module
  if(try_unregister(mypid)){
    printf("We successfully unregistered from the module!\n");
  }else{
    printf("We are still registered...\n");
  }

}
			cv_cancellation_guard(const cancellation_token& token, Handler& handler) :
				_token(token), _handler(handler)
			{ _registered = try_register(_token, _handler); }