All assignments are to be submitted as submodules in the correct period directory for the assignment. Please see the resources page for help on linking your own repository as a submodule.

Last Class Today!

At the end of each class, 1 student will be chosen at random to 
post the day's notes online.
Notes will be posted in a document titled lct-[PERIOD].md in the dwsource 
repository on github (
Title the note [DAY] [DATE] [TOPIC/AIM] by [AUTHOR]
For example:   Wednesday, 9/14 Variable Types by JonAlf Dyrland-Weaver
Also include at the end of the note a link to an interesting 
piece of tech/science news.
Use markdown for formatting. For markdown help go here:

Work 13: Check your pipes for leaks.

posted Nov 20, 2017, 7:19 AM by JonAlf Dyrland-Weaver

Write a program that does the following:
  • Creates 2 unnamed pipes
  • Forks off a child process
  • Sends a number from the parent to the child
  • The child should perform some arithmetic operation on that number, then send the new value to the parent
    • Do whatever math your heart desires
  • Display messages for each step of the process
Example output:
[parent] sending: 15
[child] doing maths on: 15
[parent] received: 225

github link

Work 12: Please Fork Responsibly

posted Nov 16, 2017, 7:15 AM by JonAlf Dyrland-Weaver

With your table buddy, write a c program that does the following:
  • Forks off 2 child processes
  • Child process instructions:
    • At the start print its pid
    • Sleep for a random number of seconds in the range [5, 20]
      • Each child should sleep for a (potentially) different amount of seconds
    • At the end print a message that it is finished
  • Parent process instructions
    • Before forking, print out some initial message
    • After the fork, wait for any child to finish
    • Once a child has finished, print out the pid of the completed child as well as how many seconds it was asleep for.
      • Do not run any kind of timer, think about how the child could send this information back to the parent.
    • Print a message that the parent is done and then end the program
      • The other child might still be running, that's ok.
github clone link

Work 11: Don't get argumentative

posted Nov 10, 2017, 6:11 AM by JonAlf Dyrland-Weaver

With your table buddy, do the following:

strep is a function in string.h used to help parse strings
  • strsep( <SOURCE>, <DELIMETER> )
  • Locates the first occurrence of any character in the DELIMITER string and replaces it with NULL
  • Returns the beginning of the original string
  • sets the source string pointer  to point to the index 1 byte after the location of the new NULL
  • Since the source variable's value is changed, it must be a pointer to a string.
  • To test it out, try the following code:
  • char line[100] = "wow-this-is-cool";
    char *s1 = line;
    printf("[%s]\n", strsep( &s1, "-" ));
    printf("[%s]\n", s1);

Write a function that uses strep to parse a line of arguments
  • Use the following header: char ** parse_args( char * line );
  • It should take a string with the command line invocation of a program (like "ls -a -l")
    • Note that there is a " " between each argument
  • Return an array of pointers to each individual argument in the parameter string, that could be used for execvp()
  • For example, the following code should work (assuming all variables are declared appropriately:
    • char ** args = parse_args( line );
      execvp(args[0], args);
  • You can make the following assumptions:
    • There is only 1 space between each argument in the original string
    • The original string contains mo more than 5 arguments
    • The original string is correctly formatted

github link:

Work 10: Fire up the batSIGNAL

posted Nov 8, 2017, 6:36 AM by JonAlf Dyrland-Weaver

Write a C program with the following features:
  • Runs constantly inside a forever while loop.
  • Print out the process' PID inside the loop.
  • Use the sleep(1) function to have the program wait 1 second between inner loop iterations.
  • Catch the following signals
    • SIGINT
      • Before exiting, append a message to a file noting that the program exited due to SIGINT
      • To make your program quit, look up the exit function.
    • SIGUSR1
      • Print out the PID of the parent process.
      • Should not cause the program to exit.
github link

Work 09: Please follow the DIRections

posted Nov 1, 2017, 8:44 PM by JonAlf Dyrland-Weaver

You will write a program that will gather various information about a directory, specifically:
  • list all the files in the directory
  • specify which files are directories (if any)
  • show the total size of all the regular files the directory
    • note that you do not have to recursively go through any subdirectories to find their size for this part (unless you want to, but that is not a simple task)
  • Have the program scan the current directory
  • Possible enhancements:
    • Recursively list the files in any subdirectories, update the total to include the total size of subdirectories
    • Print out the size in a more readable format (like using KB, MB, GB for -byte prefixes)

Github links:

Below is sample output, yours may differ, but as long as it presents the information requested above you will receive full credit.
$ ./a.out
Statistics for directory: .
Total Diectory Size: 13453 Bytes
Regular files:

$ ./a.out
Statistics for directory: .
Total Diectory Size: 17766 Bytes
Regular files:

Work 08: I need information, STAT!

posted Oct 31, 2017, 7:13 AM by JonAlf Dyrland-Weaver

There is a function called stat. It is used to get information about a file. With your table buddy, write a program that uses stat to display the following information about a file:
  • file size
  • mode (permissions)
  • time of last access (displayed in the following format: Fri Oct 28 09:03:01 2016
Once you have that, try the following:
  • Modify the size so it is printed out in human readable form (that is using B, KB, MB, GB).
    • You may want to look into the sprintf function to help.
  • Attempt to print out the permissions in ls -l form, ignoring the early bits that include the file type information
    • Permissions are of type mode_t, which is an integer type.
    • The permissions section of the mode is stored in the last 9 bits of the mode_t data type.

Github clone url:

Work 07: /dev(ise a )/random( plan)

posted Oct 27, 2017, 5:47 AM by JonAlf Dyrland-Weaver

With your table buddy, write a c program with the following features:
  • A function that generates a random number by reading in the correct number of bytes from /dev/random.
  • A main function that:
    1. populates an array with 10 random numbers generated by your random function (print out each value)
    2. writes the array to a file
      • Do not use a loop when writing to the file
    3. reads that file into a different array
      • Do not use a loop when reading from the file
    4. prints out the contents of the second array to verify the random numbers are the same from step 1
  • Remember to use good practices, like checking return values for errors
  • Also remember to include a makefile that includes a run target (even though this is a single file program)
github clone links
Sample output:
Generating random numbers:
        random 0: 198116400
        random 1: 2592608753
        random 2: 2833624368
        random 3: 3273750590
        random 4: 3548697368
        random 5: 3876828898
        random 6: 2617393183
        random 7: 1720936907
        random 8: 682596330
        random 9: 2411695830

Writing numbers to file...

Reading numbers from file...

Verification that written values were the same:
        random 0: 198116400
        random 1: 2592608753
        random 2: 2833624368
        random 3: 3273750590
        random 4: 3548697368
        random 5: 3876828898
        random 6: 2617393183
        random 7: 1720936907
        random 8: 682596330
        random 9: 2411695830

Work 06: Enlist your C skillz

posted Oct 13, 2017, 5:36 AM by JonAlf Dyrland-Weaver   [ updated Oct 13, 2017, 12:35 PM ]

Write a simple linked list program. You will need a define a node struct with the following data members:
  • A piece of data (your choice)
  • A pointer to the next node
  • Put the struct declaration at the top of your code (after the include lines), or in a separate .h file
  • Here's an example of what the struct could look like:
    • struct node { int i; struct node *next };
Create the following functions:
  • void print_list(struct node *);
    • Should take a pointer to a node struct and print out all of the data in the list
  • struct node * insert_front(Struct node *, int);
    • Should take a pointer to the existing list and the data to be added, create a new node and put it at the beginning of the list.
    • The second argument should match whatever data you contain in your nodes.
    • Returns a pointer to the beginning of the list.
  • struct node * free_list(struct node *);
    • Should take a pointer to a list as a parameter and then go through the entire list freeing each node and return a pointer to the beginning of the list (which should be NULL by then).                
Don't forget to test out these various functions to ensure they work correctly. (Just to be clear, when you run the program and get "Segmentation fault" that means it's not working.)

Github link:

Work 05: If you like it then you should've put a string on it

posted Oct 2, 2017, 6:53 AM by JonAlf Dyrland-Weaver   [ updated Oct 2, 2017, 8:39 AM ]

This will be due Thursday, 10/5

You will be working with a partner on this assignment, please name your submodules as follows:

lastF-lastF e.g. brooksP-holmesD

First, acquaint yourself with the basic string functions: strlen, strcpy, strcat, strncat and strncpy. You can look at the man pages for each if you are unclear as to their uses. I encourage you to play around with each so you are comfortable with how to use them. Then write your own versions of the following:
  • Required (use the function headers I provide, they may differ from the standard headers).
    • strlen [ int strlen( char * ) ]
    • strcpy/strncpy (choose one)  [ char * strcpy( char *dest, char *source )  char * strncpy( char *dest, char *source, int n)  ]
    • strcat/strncat (choose one)  [ char * strcat( char *dest, char *source )  char * strncat( char *dest, char *source, int n)  ]
    • strcmp  [ int strcmp( char *s1, char *s2 ) ]
      • Your version does not need to return the same exact value as the system version, as long as it returns -, + or 0 when it should.
    • strchr  [ char * strchr( char *s, char c ) ]
    • You must test every function you write, you will not get full credit for an untested functions
    • You do not need to write 7 functions, only 5, but you need to write 1 or the "n" versions. 
      • (So if you do strcpy then you have to do strncat)
    • A makefile that includes a run target. Do not automatically call run in the first target.
      • This will be explained in class on Tuesday
  • Extra
    • strstr [ char * strstr( char *s1, char * s2 ) ]
    • The other strn/str version of the functions you wrote above
  • When you test your functions,  run them alongside the regular ones so you can see if they behave the same way or not.
  • Remember you should call them something other than the regular names (like, mystrlen...). 
Github link

Here's an example of what your code might look like when run:
s1: [?
s2: [hello]
s3: [goodbye]

Testing strlen(s2):
[standard]: 5
[mine]: 5

Testing strcpy(s1, s2):
[standard]: [hello]
[mine]: [hello]

Testing strncpy(s1, s3, 3):
[standard]: [goolo]
[mine]: [goolo]

Testing strcat(s1, s3):
[standard]: [goologoodbye]
[mine]: [goologoodbye]

Testing strncat(s1, s2, 3):
[standard]: [goologoodbyehel]
[mine]: [goologoodbyehel]

Testing strchr(s1, 'l'):
[standard]: [0x7fff57d70a13]
[mine]: [0x7fff57d70a13]

Testing strchr(s1, 0):
[standard]: [0x7fff57d70a1f]
[mine]: [0x7fff57d70a1f]

Testing strchr(s1, 'z'):
[standard]: [0x0]
[mine]: [0x0]

Testing strcmp
Comparting ab to abc:
[standard]: [-99]
[mine]: [-99]
Comparting abc to ab:
[standard]: [99]
[mine]: [99]
Comparting abc to abc:
[standard]: [0]
[mine]: [0]

Work 04: Demonstringing Code

posted Sep 28, 2017, 10:05 AM by JonAlf Dyrland-Weaver

In class, you were given a number, that number corresponds to a pair of c functions:
  • 0 : strcpy/strncpy
  • 1 : strcat/strncat
  • 2 : strcmp/strncmp
  • 3 : strchr/strstr
You will look up what your functions do, and then write a small c program that demonstrates the functions. Be prepared to describe your functions and explain your demo in class tomorrow.

Github link:

1-10 of 14