Work 17: Semaphone

posted Dec 12, 2018, 6:56 AM by JonAlf Dyrland-Weaver

This will be due Friday, 12/14

With your table buddy, write a telephone style game! It will use a semaphore to control access to a file and shared memory. 

The main program should work as follows:
  1. Print a message this it will check if resources are available.
  2. When they are, display the last line added to the story (or none, if it is new).
  3. Prompt the user to enter the next line
  4. Update the story
  5. quit

This game will require 2 files 
  1. A control program
    • Take a command line argument to determine if it is creating or removing the resources or viewing the story.
    • If creating (command line argument: -c):
      • Should make the shared memory, semaphore and file (open the file with the truncate flag). 
      • Set any values that are needed.
    • If removing (command line argument: -r)
      • Remove the shared memory, the semaphore and the story.
      • Display the full contents of the story.
      • This should wait until the semaphore is available.
    • If viewing (command line argument: -v)
      • Output the contents of the story file. 
      • This mode does not need to interact with the semaphore
  2. A main writing program
    • Will attempt to access the resources using the semaphore
      1. Once in, it should display the last line added to the file (use shared memory to contain the size of the last line).
      2. Prompt the use for the next line.
      3. Once a new line is read, write that to the file, update the shared memory and then release the semaphore
  3. You do not need a run target in your makefile
github link:

Work 16: Sharing is caring.

posted Dec 4, 2018, 6:39 AM by JonAlf Dyrland-Weaver

Shared memory is a thing, and it's exactly what it sounds like. You can learn more about it here:

Once you've learned a thing or two, write a program that does the following:
  1. Connects or creates a shared memory segment large enough to hold a 200 character string.
  2. Displays the contents of the shared memory.
    • If the segment was just created, instead show a message saying as much.
  3. Ask the user if they want to change the data in the segment.
    • If yes, prompt the user to enter a new string and write that to the segment.
  4. Ask the user if they want to delete the segment, if yes, then delete it.
Github submission link

Work 15: Please fork responsibly.

posted Nov 16, 2018, 7:48 AM by JonAlf Dyrland-Weaver

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 14: Get Argumentative

posted Nov 14, 2018, 5:50 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] = "woah-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 no more than 5 arguments
    • The original string is correctly formatted

github link:

Work 13: Fire up the bat signal!

posted Nov 8, 2018, 6:01 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 12: Please follow the dir(rections)

posted Nov 1, 2018, 7:04 AM by JonAlf Dyrland-Weaver   [ updated Nov 2, 2018, 5:45 AM ]

New and improved instructions:
  • Your program should handle user input as follows:
    • If a command line argument is entered, use that as the directory to scan.
    • If not, the program should ask the user to enter a directory to scan.
    • In either case, don't assume your user knows what they are doing, if an invalid directory is entered, take reasonable action (*ahem* errno *cough cough*)

With your *new* table buddy, 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 link:

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 11: I need information, STAT!

posted Oct 30, 2018, 5:32 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
  • Note
    • You may hardcode the file that your program uses, make sure the program works when I download and run it.
    • If only there were some file that you know I will have and where it is... hmm...
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 10: /dev(ise a)/random plan

posted Oct 26, 2018, 7:31 AM by JonAlf Dyrland-Weaver

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 separate run target 
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 09: File this under useful information.

posted Oct 23, 2018, 6:25 AM by JonAlf Dyrland-Weaver

  • Look at your computer number and mod it by 2.
  • Based on that number, look up and figure out what the following functions do
    • 0: open / close
    • 1: read / write
  • After 5 minutes or so, once you have a grasp on your functions, the parameters, return values and whatnot, talk to your table buddy and discuss the functions.
  • Once you and your table buddy have it all figured out, write a program that tests all 4 functions.
  • One submission per duo.
Github link:

Work 08: The Prime Directive

posted Oct 19, 2018, 7:17 AM by JonAlf Dyrland-Weaver   [ updated Oct 26, 2018, 7:36 AM ]

No, not that prime directive.

Use the Sieve of Eratosthenes to write a function that will find the nth prime number.

You can use the following driver file, especially if you plan on entering the contest of champions later next week:

Regardless, you must provide your sieve and associated code (I see you makefile!) on GitHub:

Kontest Requirements:
  • Submit a link to your repo here: (ONLY if you want to enter the competition)
  •  You must make a repo and place sieve.h and sieve.c inside that directory, not a subdirectory:
  • Your sieve.h should have the function:
    • int sieve(int targetPrime);
  • There should be no printed output when sieve() is called!
  • I will use a driver of mine.
  • I will compile with the -lm flag so you can use math.h.
  • Optionally:
    • In your makefile, you can pass args to the program:
    • run:
      • ./primetest $(args)
  • Then you can use the shell as follows: 
  • make run args="100000 10"

1-10 of 18