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)
- Additionally, review what you have learned about C file I/O in CSCI 206. You should revisit the following manual pages to get reacquainted.
- 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 requires 4 command line arguments of data types in this specific order: char, int, float, string. Your program must work as follows:
- Check if the number of parameters provided by the user is exactly four and terminate with an error message otherwise.
- When 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, print to the terminal the values in 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)
SUPER IMPORTANT HERE: This program involves forking! Given oft-made mistakes here, please avoid the normal linuxremote systems. Please either use linuxremote-csci315.bucknell.edu or specific lab machines: acet116-lnx-#.bucknell.edu, where # is a number in the range 2 to 25. For the best programming experience, be at the physical machine: that way, if (or when) you fork inside an infinite loop, you’ll have any chance to shut it down on your own.
In your ~/csci315/Lab1 directory, create a program called myprog.c that spawns two child processes.
- The parent process 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 processes 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.
Important: 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: 2
parent: 3
parent: 4
…
child1: 1
child1: 2
child2: 1
…
parent: 321
parent: 322
…
child2: 143
child1: 142
…
Very important: 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
Grading Rubric
Problem 1 [10 points total]
- [4 points] Program reads in four command line arguments of types char, int, float, and string.
- [2 points] Program terminates with an error message if fewer than four or more than four command line arguments are provided.
- [4 points] Program prints to the terminal all the four command line arguments provided.
Problem 2 [10 points total]
- [2 points] Program reads in one command line argument of type string.
- [2 points] Program terminates if fewer than one or more than one command line argument is provided.
- [6 points] Program prints to the terminal the correct reversal of the string provided as command line argument.
- [3 points, partial credit] Program prints to the terminal characters that were not part of the original string.
Problem 3 [10 points]
- [5 points] Program creates two children process with the same parent.
- [3 points] Each of the three processes (parent, first child, and second child) runs on an infinite loop that prints their identification and an integer value to the terminal.
- [2 points] Each of the three processes (parent, first child, and second child) maintains its own integer counter independently of the other processes.