user:kkrauss1:portfolio:mod_sys
Table of Contents
Project: Stack library
A project for C/C++, Data Structures, and System Programming by Karl Krauss for fall 2011.
Approximately 1 hour
Objectives
The purpose of this project was modify a program allowing for input from a file and sorting to monitor.
Attributes
Cprog attributes
- file i/o
Data Structures
- Pointers
- Sorts
- file i/o
Systems programming
- command line arg
- i/o redirection
- file io
- directories
- file attributes
- processes
- timer
- threads
Code
This is the code:
/* This is the latered version allowing for the input to come from a file and the output to be stdout * EoCEsrc.c - sample code for use with EoCE * * To compile: gcc -o eoceprog EoCEsrc.c -lpthread * To execute: ./eoceprog 55 44 33 22 11 12 23 34 45 5 1 3 4 2 * */ #include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<unistd.h> struct localinfo { unsigned char value; }; FILE *fPtr; int main(/*int argc, char **argv*/) // since the input is coming from a file there is no need for argc or argv { void *process(void *); unsigned char x; // simply changed this to an x(personal preference) and this will be used several times later on struct localinfo local; unsigned char valueCount =1; // since there is no argc I need a place to store how MANY values are in the file to be sorted // we are assuming at least one value in the file /*if (argc < 2) No longer needed since no argc { printf("Please provide a list of positive unique whole numbers,\n"); printf("between 1 and 255\n"); exit(1); } if (argc > 255) { printf("Please provide less than 256 arguments,\n"); printf("between 1 and 255\n"); exit(1); } no longer needed since no argc */ fPtr = fopen("valuefile", "r"); // changed to r since we are reading if(fPtr == NULL) { printf("Error opening file\n"); exit(1); } char c; // we are going to assume that values are seperated by a space not a newline and the following loop will count how many values we have while ((feof(fPtr)) == 0)// this checks if it is end of file if it is not then we want to try and count { if ((c = fgetc(fPtr)) == ' ') // this checks for spaces as each space would indicate a new value { valueCount++; // if the char grabbed by fgetc DOES equal ' '(a space) then we want to incriment the valueCount } } unsigned char value[valueCount]; // I am crating an array of the size value count to store all of the values from the file pthread_t threads[valueCount]; fclose(fPtr); fPtr = fopen("valuefile", "r"); // this is resetting the file descriptor to the beginning of the file. (many ways to do this) if(fPtr == NULL) { printf("Error opening file\n"); exit(1); } for(x=1; x<=valueCount; x++) { fscanf(fPtr, "%hhu", &value[x]); // this loop is how I copy each numeric value seperated by a space into my value array } fclose(fPtr); // the file is no longer needed, have stored how many values there are, and each value into an array. for(x=1; x<=valueCount; x++) // now we are getting back into the original code(minor changes) { local.value = /*(unsigned char)atoi*/(value[x]); // do to the fact I created the value array AS unsigned char I do not need to cast pthread_create(&threads[x], NULL, (void *) process, (void *) &local); usleep(32); } for(x=1; x<=valueCount; x++) { pthread_join(threads[x], NULL); } return(0); } void *process(void *foreign) { struct localinfo *thing = foreign; unsigned char data = thing -> value; sleep((int)(data & 0xFF)); printf("%hhu ", data); fflush(stdout); }
user/kkrauss1/portfolio/mod_sys.txt · Last modified: 2011/12/12 17:29 by kkrauss1