Lab Assignment--System Calls for I/0
Lab Code
To get the sample and exercise code, please use the following commands in your cs330 directory:
wget www.labs.cs.uregina.ca/330/SystemCall_IO/Lab7.zip
unzip Lab7.zip
(Please do this lab on os1 or os2)
Exercise Description
The idea of this exercise is to communicate between a child and a parent
using a shared file that the user will specify.
Your program will require the name of the file to be sent as a command line
argument (argv[1])
A skeleton algorithm is provided below:
-
Check that the number of command line arguments is 2. If it isn't, produce an error message. Note: you cannot use perror here.
-
Before forking, declare all necessary variables for both parent and child, including a 8 byte character buffer for reads and writes.
-
Use fork to produce a child and parent
- Child will:
- open the file for writing (make sure to use the "create" flag
and permissions for the user to read and write)
- check for errors on opening and use perror to describe the error.
- read from standard input (until end of file,ie., you type CTRL-D on a blank line)
- write to the file
- when done, close the file
- Parent will:
- wait for the child
- open the file for reading
- check for errors on opening and use perror to describe the error.
- Call fstat() and print the following
information about the file the child created:
the file size (in bytes), the owner and group IDs, and the time it was last
modified. The output can be in the following format:
This is the status of file.txt
File Size Owner Group ID Last Modified
35 1005002087 1005000003 1722374520
If you want to do more, you can output in the following format:
This is the status of file.txt
File Size Owner Group ID Last Modified
35 nova csfac Tue Jul 30 15:18:48 2024
To do this, you can make use of library calls: getgrgid, getpwuid, and ctime.
Check out the man pages for these calls.
You do not have to use the write system call for this last step.
If you want, you can use printf to format your output nicely.
For more on printf(), click here.
- write a message "hello from the parent" to stdout
- read from the file (until end of file)
- write to standard output
- when done, close the file
Don't forget to handle the error situation
Deliverables
Submit 2 files to URCourses
- Your Code
- Script containing the following commands
(executable is called myIO):
- ls -l (Goal: show that no .txt files exist aside from your script file)
- ./myIO (Result: error message-not enough arguments)
- ./myIO file.txt (Result: no errors;
gets input from the keyboard until you type CTRL-D;
and creates file.txt with contents of what you typed)
- ls -l (Goal: show that file.txt has been
created)
- cat file.txt (Goal: display the contents of the output file)
- ./myIO /file.txt (Result: two error messages -
(1) permission denied on creating and
(2) no such file when opening for reading- on file.txt in the root directory)
Notes
- Do not create a zip file
- Submission is on UR Courses
- Submit your own work
- Please properly indent your work. I will deduct marks for ziggy-zaggy code.
- Be mindful that your assignment is due two hours before the start of your next lab