Pre-Lab 1

Goals

  • Start developing proficiency with Unix system calls for process control.

Credits

The material developed for this lab was developed by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this “credits” note is not removed. Additional student files associated with this lab, as well as any existing solutions can be provided upon request by e- mail to:

perrone[at]bucknell[dot]edu


Preparing for Lab 1

  • In the Silberschatz, Gavin, and Gagne (SGG) text: read Chapter 3 up to and including section 3.3.
  • In the the Stevens and Rago (SR) text: read chapter 8 up to 8.6, inclusively.
  • Read the manual pages for the items below :
    • ps (1)
    • kill (1)
    • fork (2)
    • exit (2)
    • wait (2)
    • waitpid (2)
    • execv (3)
    • system (3)
  • In addition, you have worked with file I/O in CSCI 206. Please review the following manual pages.
    • open (2)
    • read (2)
    • write (2)
    • close (2)

Problem 1 (10 points)

In your ~/csci315/Lab1 directory, create a C program called cmds.c - this program should ask for 4 command line arguments of data types as follows: char, int, float, string. Your program won’t need to do much: upon starting, it must check if the number of parameters provided by the user is exactly four and terminate with an error message otherwise. If the number of command line parameters is four, your code should assign each of the values read in from the command line to a variable of the appropriate type. Finally, have the program print to the terminal each of the four variables and terminate.

When you are confident that your program works do:

  • cd ~/csci315/Labs
  • git add Lab1
  • git add Lab1/cmds.c
  • git commit -m “Pre-lab 1.1 completed”
  • git push

Problem 2 (10 points)

In your ~/csci315/Lab1 directory, create a C program called cmdreverse.c - This program should ask for 1 command line argument of type string. Your program should check if the number of parameters provided by the user is exactly 1 and terminate with an error message otherwise. If all is well, your program should reverse the entire string passed in as command line argument and print it out to the terminal. Note that your program must work with strings that contain any type of blank spaces.

When you are confident that your program works do:

  • cd ~/csci315/Labs
  • git add Lab1
  • git add Lab1/cmdreverse.c
  • git commit -m “Pre-lab 1.2 completed”
  • git push

Problem 3 (10 points)

In your ~/csci315/Lab1 directory, create a program called myprog.c that spawns two child processes. The parent should run an infinite loop printing to the terminal the string “parent:” followed by an increasing counter value, and a new line character. The children should also run an infinite loop printing to the terminal the string “child1:” or “child2:“ followed by an increasing counter value, and a new line character. The parent and the children should have separate counter variables! At the end of each iteration of their respective loops, each process’ counter variable gets incremented by one. The output of the program should resemble what appears in the box below. Note that the specific values of the counters will be different in your program and that the output will scroll by fast.

parent: 1
parent: 2
parent: 3
parent: 4

child1: 1
child1: 2
child2: 1

parent: 321
parent: 322

child2: 143
child1: 142

Very important: you must make sure that child1 and child2 are processes spawned by the same parent process.

Make sure that your problem compiles – don’t expect to receive much (if any) partial credit, if you ever turn in code that does not compile. Additionally, to earn full credit, make sure to debug your code so that it runs as expected.

When you are done with this problem, you need to:

  • cd ~/csci315/Labs/Lab1
  • git add myprog.c
  • git commit -m “Pre-lab 1.3 completed”
  • git push

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Print Friendly