brand newbie question



Hi,
I'm a CS student who to this point has only programmed in java.  I
just got an internsip and i am learning c on a fedora core 4 system. 
The first task i was given was to take the output from the program i
have pasted at the end of this message, and, every time the program is
run, have it present the information in the form of a gnome window
with a chart plotting the output of the program, instead of just
printing it down the terminal screen as it does now.  I would
appreciate some tips on what would be the best way to approach this
task, as well as some pointers to some good tutorials or something
that will help me get started with gtk+ windows and such.  Also, what
would be the easist way to draw the graph?
thanks,
ryan

here is the complete program i'm working with; mtWtest.c
==============================================================================
#include <sys/types.h>
#include <sys/file.h>
/*#include <sys/buf.h> */
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <ctype.h>
#include <locale.h>
#include <dirent.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <glob.h>

#define EOS '\0'

int blocksize = 65536;
int rthreads  = 0;
int wthreads  = 4;
int rfilesz   = 1024*1024;
int wfilesz   = 1024*1024;
char *path; // glob for reads

int getOptions(int argc, char * argv[]);
int doGlob(char *);

void * reader(char *fn);
void * writer();


extern char *optarg;
extern int optind, opterr, optopt;

#define _GNU_SOURCE
#include <getopt.h>

//#include <pthread_exception.h>


//pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER;
//pthread_mutex_t hostMutex = PTHREAD_MUTEX_INITIALIZER;

char *fn;
int threads;
int debug;
glob_t GLOB;

void * reader(char *fn) {
  struct timeval open_close_times[2];
  struct timeval blckopen_close_times[2];

  int fd;
  char *buf = malloc(blocksize);
  int rc;
  unsigned int i=0;
  float time;
  unsigned int j=0;
  struct stat stb;

  long long int k;
  int fd1, outfd;

  printf("child got FNAM ;%s; \n", fn);
  int numblocks = rfilesz / blocksize;

  fd = open(fn, O_RDONLY, 0666);

  if ( fd < 0 ) {
    fprintf(stdout, "open() %s failed\n",fn);
    pthread_exit(&fd);
  }

  snprintf(buf, 128,
           "outfd.%d", getpid());
  // open stats file
  outfd = open(buf, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  if ( outfd < 0 ) {
    fprintf(stdout, "open() %s failed\n",fn);
    pthread_exit(&fd);
  }

  sprintf(fn,
	  "PID %d\nBLOCKSIZE: %d\n",
	  getpid(), blocksize);
  write(outfd, fn, strlen(fn));

  gettimeofday(&(open_close_times[0]), NULL);

  while ( i < numblocks ) {
    gettimeofday(&(blckopen_close_times[0]), NULL);
    rc = read(fd, buf, blocksize);
    if ( rc < 0 ) {
      fprintf(stdout, "read() failed PID %d\n", getpid());
      exit(1);
    }
    gettimeofday(&(blckopen_close_times[1]), NULL);
    sprintf(buf,
	    "BLOCK %d %.3f\n", i++, time);
    //fprintf(stdout, "--> %s\n", buf);
    write(outfd, buf, strlen(buf));
  }

  gettimeofday(&(open_close_times[1]), NULL);
  time = ((open_close_times[1].tv_usec/1000000.0) +
open_close_times[1].tv_sec) -
    ((open_close_times[0].tv_usec/1000000.0) + open_close_times[0].tv_sec);

  sprintf(buf, "RD BW %.3f Time %.3f Size %llu %d\n",
	   ((i * blocksize) / (time*1048576)), time,
	   (unsigned long long)(i * blocksize), i );

  fprintf(stdout, "RD BW %.3f Time %.3f Size %llu %d\n",
	  ((i * blocksize) / (time*1048576)), time,
	  (unsigned long long)(i * blocksize), i );

  write(outfd, buf, strlen(buf));

  close(fd1);
  close(outfd);
  rc = close(fd);
  pthread_exit(&rc);
	
}

void * writer() {
  struct timeval open_close_times[2];
  struct timeval blckopen_close_times[2];

  int fd;
  char *buf = malloc(blocksize);
  int rc;
  unsigned int i=0;
  float time;
  unsigned int j=0;
  struct stat stb;

  long long int k;
  int fd1, outfd;
  char fn[256];

  int numblocks = wfilesz / blocksize;

  // read some shite
  fd1 = open("/dev/urandom", O_RDONLY);
  read(fd1, buf, blocksize);

  snprintf(fn, 128,
	   "tfile.%d", getpid());

  fd = open(fn, O_WRONLY| O_CREAT|O_TRUNC, 0666);
  if ( fd < 0 ) {
    fprintf(stdout, "open() %s  %s failed\n",fn, strerror(errno));
    pthread_exit(&fd);
  }

  snprintf(fn, 128,
           "outfd.%d", getpid());

  // open stats file
  outfd = open(fn, O_WRONLY| O_CREAT|O_TRUNC, 0666);
  if ( outfd < 0 ) {
    fprintf(stdout, "open() %s failed\n",fn);
    pthread_exit(&fd);
  }

  sprintf(fn,
	   "PID %d\nBLOCKSIZE: %d\n",
	   getpid(), blocksize);
  write(outfd, fn, strlen(fn));

  gettimeofday(&(open_close_times[0]), NULL);

  while ( i < numblocks ) {
    gettimeofday(&(blckopen_close_times[0]), NULL);
    rc = write(fd, buf, blocksize);
    if ( rc < 0 ) {
      fprintf(stdout, "write() failed PID %d\n", getpid());
      exit(1);
    }
    gettimeofday(&(blckopen_close_times[1]), NULL);
    time = ((blckopen_close_times[1].tv_usec/1000000.0) +
blckopen_close_times[1].tv_sec) -
      ((blckopen_close_times[0].tv_usec/1000000.0) +
blckopen_close_times[0].tv_sec);
    sprintf(fn,
	    "BLOCK %d %.3f\n", i++, time);
    fprintf(stdout, "--> %s\n", fn);
    write(outfd, fn, strlen(fn));
  }

  gettimeofday(&(open_close_times[1]), NULL);
  time = ((open_close_times[1].tv_usec/1000000.0) +
open_close_times[1].tv_sec) -
    ((open_close_times[0].tv_usec/1000000.0) + open_close_times[0].tv_sec);

  sprintf(fn,"WR BW %.3f Time %.3f Size %llu %d\n",
	   ((i * blocksize) / (time*1048576)), time,
	   (unsigned long long)(i * blocksize), i );

  fprintf(stdout, "WR BW %.3f Time %.3f Size %llu %d\n",
	  ((i * blocksize) / (time*1048576)), time,
	  (unsigned long long)(i * blocksize), i );

  write(outfd, fn, strlen(fn));

  close(fd1);
  close(outfd);
  rc = close(fd);
  pthread_exit(&rc);
}	



	
int main(int argc, char *argv[]) {
  int status;
  int numThreads, threads;
  int rc = 0;
  int *rcPtr = &rc;
  int err = 0;

  if ( getOptions(argc, argv) ) {
    fprintf(stderr, "bad opts");
    exit(1);
  }

  threads = rthreads+wthreads;
  pthread_t thread[threads];

  if ( rthreads > 0 ) {
    if ( (rc = doGlob(path)) ) {
      fprintf(stderr, "doGlob() failed %d\n", rc);
      exit(1);
    }
  }

  fprintf(stdout, "MAIN Process ID %d his parent %d PGRP %d\n",
	  getpid(), getppid(), getpgrp());

  char *arg;
  for (numThreads = 0; numThreads < rthreads; numThreads++) {
    if ( numThreads >= GLOB.gl_pathc ) {
      fprintf(stderr, "not enough files in glob? %d \n", GLOB.gl_pathc);
      exit(1);
    }
    arg = GLOB.gl_pathv[numThreads];
    fprintf(stdout, "PARENT run thread with ;%s;\n", arg);
    status = pthread_create(&thread[numThreads],
			    NULL,
			    reader,
			    (void *)arg);
  }

  for (numThreads = 0; numThreads < wthreads; numThreads++) {
    status = pthread_create(&thread[numThreads+rthreads],
                            NULL,
                            writer,
                            NULL);
  }

  for (numThreads = 0; numThreads < threads; numThreads++) {
    //sleep(1);
    pthread_join(thread[numThreads], (void*)&rcPtr);
    if (*rcPtr) {
      err++;
      fprintf(stdout, "thread failure : %d %s\n",
	      *rcPtr, strerror(*rcPtr));
    }
  }
  if ( err ) fprintf(stdout, "%d threads failed\n", err);
  exit(err);
}


int getOptions(int argc,  char *argv[])  {
#define ARGS "d:w:r:p:b:R:W:"
  int c, err = 0;
  optarg = NULL;

  while ( !err && ((c = getopt(argc, argv, ARGS)) != -1))
    switch (c) {

    case 'd' :
      debug = 1;
      break;

    case 'w' :
      wthreads = atoi(optarg);
      break;

    case 'r' :
      rthreads = atoi(optarg);
      break;

    case 'p' :
      // check this pathname for glob
      // this arg specifies which files to read
      path = optarg;
      break;

    case 'b'  :
      blocksize = strtol(optarg, NULL, 10);
      break;

    case 'R' :
      rfilesz  = strtol(optarg, NULL, 10);
      break;

    case 'W' :
      wfilesz  = strtol(optarg, NULL, 10);
      break;


    default :
      fprintf(stderr, "what?\n");
      err++;
    }
  return err;
}


int doGlob(char *globstr) {
  return (glob(globstr, GLOB_NOSORT|GLOB_MARK, NULL, &GLOB));
}


==============================================================================


--
He felt that his whole life was some kind of dream and he sometimes
wondered whose it was and whether they were enjoying it. - Douglas
Adams



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]