APUE – Chapter 15 – Interprocess Communication

You may find the reference given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture07.pdf (titled Interprocess Communication).

Reading Assignments

Entire Chapter

Report Submission

Run example programs 15.1 to 15.4 (Summary of UNIX System IPC, Two ways to view a half-duplex pipe, Half-duplex pipe after a fork, Pipe from parent to child – not programs), 15.5 (Send data from parent to child over a pipe), 15.6 (Copy file to pager program), 15.7 (Routines to let a parent and child synchronize), 15.11 (Copy file to pager program using popen), 15.12 (The popen and pclose functions) and verify that the programs work as expected. Write a report (C15report.txt) giving your observations. For each example typically limit the observation to two or three lines.

Also add observations relating to the assignments below in the report. The observations for these can be longer (they they do not have to be longer).

Assignment Submissions

  • 15A) Write a program which uses the pipe system call. Possible programs are: a) Extending the shell program written previously to handle piped commands b) Using pipe as a means of communicating tasks and results between mpcalc main program and worker processes.
  • 15B) Use fifo for implementing communication between mpcalc and worker processes.
  • 15C) Use Message Queues for implementing communication between mpcalc and worker processes.
  • 15D) Use Shared Memory (and Semaphores) for implementing communication between mpcalc and worker processes.

APUE – Chapter 13 – Daemon Processes

You may find the reference given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture09.pdf – (titled HTTP, Dæmon processes, System Logging, Shared Libraries) – see slides from slide 19 to slide 28 as they are on daemon processes and logging.

Reading Assignments

Sections 13.1 to 13.3 (Introduction, Daemon Characteristics, Coding Rules), 13.4 (Error Logging), 13.5 (Single-Instance Daemons)

Report Submission

Run example program 13.1 (Initialize a daemon process) and verify that the program works as expected. Write a report (C13report.txt) giving your observations. Limit the observation to two or three lines.

Also add observations relating to the assignments below in the report. The observations for these can be longer (they they do not have to be longer).

Assignment Submission

  • 13A) Write a simple version of the atd (at daemon program) and the at command. Call your atd implementation myatd and the command as myat. myatd should make an entry in the system log for each command executed.

APUE – Chapter 14 – Advanced I/O

You may find the reference given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture08.pdf (titled Advanced I/O / HTTP) – see slides 1 to 19 and 21 to 24, you may ignore slides from slide 25 onwards.

Reading Assignments

Sections 14.1 to 14.3 (Introduction, Nonblocking I/O, Record Locking), 14.5 (I/O Multiplexing), 14.9 (Memory-Mapped I/O).

Report Submission

Run example programs 14.1 (Large nonblocking write), 14.4 (File byte-range lock diagram – not a program), 14.5 (Function to lock or unlock a region of a file), 14.7 (Example of deadlock detection), 14.31 (Example of a memory-mapped file) and verify that the programs work as expected. Write a report (C14report.txt) giving your observations. For each example typically limit the observation to two or three lines.

Also add observations relating to the assignments below in the report. The observations for these can be longer (they they do not have to be longer).

Assignment Submission

  • Write one program that uses the select function call. Some possibilities are:
    1. Program that does a read with a timeout.
    2. Update the program used to test SIGPIPE. Let the writer check whether the pipe is full (using select) prior to writing. If it is full then the writer program should write a message to standard error, sleep for some time and then retry write to the pipe.
    3. Modifying program 2 above so that the writer also accepts commands from standard input. Now select will be required to wait for a ready event on either standard input or the pipe.

APUE – Chapter 10 – Signals

Powerpoint slides: AUP-SingalsTopicsOnly.ppt.

You may find the reference given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture06.pdf – (titled Process Groups, Sessions, Signals)
see slides from slide 34 onwards as they are on signals and relate to Chapter 10.

Reading Assignments

Sections 10.1 to 10.3 (Introduction, Signal Concepts, signal Function), 10.5 (Interrupted System Calls), 10.6 (Reentrant Functions), 10.8 to 10.19 (Reliable-Signal Terminology and Semantics, kill and raise Functions, alarm and pause Functions, Signal Sets, sigprocmask Function, sigpending Function, sigaction Function, sigsetjmp and siglongjmp Functions, sigsuspend Function, abort Function, system Function, sleep Function); Quick Browse through 10.4 (Unreliable Signals), 10.7 (SIGCLD Semantics), 10.20 (Job-Control Signals), 10.21 (Additional Features).

Notes

man 7 signal: Gives details about signals on Linux

Report Submission – Optional

Run example programs 10.1 (UNIX System signals – not a program), 10.2 (Simple program to catch SIGUSR1 and SIGUSR2), 10.4 to 10.8 (Reentrant functions that may be called from a signal handler – not a program, Call a nonreentrant function from a signal handler, System V SIGCLD handler that doesn’t work, Simple, incomplete implementation of sleep, Another (imperfect) implementation of sleep), 10.11 to 10.13 (Calling read with a timeout, using longjmp, An implementation of sigaddset, sigdelset, and sigismember, Ways to change current signal mask using sigprocmask – not a program), 10.14+10.10 (Print the signal mask for the process + Calling read with a timeout), 10.15 to 10.21 (Example of signal sets and sigprocmask, Option flags (sa_flags) for the handling of each signal – not a program, siginfo_t code values – not a program,An implementation of signal using sigaction, The signal_intr function, Example of signal masks, sigsetjmp, and siglongjmp, Time line for example program handling two signals – not a program) and verify that the programs work as expected. Write a report (C10report.txt) giving your observations. For each example typically limit the observation to two or three lines.

Also add observations relating to the assignments below in the report. The observations for these can be longer (they they do not have to be longer).

Assignment Submissions

  • 10A)- Optional: Write a program that creates a file and then goes into an infinite loop. Catch the following signals in the program:

a) SIGABRT, SIGBUS, SIGFPE, SIGHUP, SIGILL, SIGINT, SIGIO, SIGPWR, SIGQUIT, SIGSEGV, SIGTERM, SIGTRAP: For these signals program should print a message giving details about the signal caught and then delete the file created in the beginning and finally terminate.

b) SIGALRM, SIGCHLD, SIGCONT, SIGPIPE, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGUSR1, SIGUSR2, SIGWINCH: For these signals, print a message giving details about signal caught and then continue with program execution (don’t terminate).

For testing the program create conditions (may require program modification for some signals) which generate following signals: SIGABRT, SIGINT, SIGQUIT, SIGCHLD, SIGCONT, SIGPIPE, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGWINCH. For testing with other signals use kill command from another terminal.

  • 10B) In the shell program (written previously – assignment 8C) handle Ctrl+C (SIGINT) to terminate current command. Also print more meaningful messages for commands that have been terminated due to SIGSEGV and SIGFPE signals.
  • 10C) In mpcalc2 (assignment 8B) catch SIGCHLD and print termination status as well as output of comparison (if normal termination). Now we do not have to ask user to give a command to issue wait.
  • 10D) Redo mpcalc2 (call new solution mpcalc3) as a worker process pool solution instead of forking everytime a calculation has to be done. mpcalc3 should start off say 5 worker processes at the beginning. Then as the user keys in a calculation task it should be handed off to a free worker process. Use signals for IPC and files for sharing data between processes. If outstanding tasks become more than free processes then the tasks should be queued by mpcalc3 and handed over to worker processes once they become free. Gracefully shutdown all processes when user wants to quit.

APUE – Chapter 9 – Process Relationships

You may find the reference given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture06.pdf (titled Process Groups, Sessions, Signals)
– see slides from slide 1 to slide 34 as they are on process groups and sessions and relate to Chapter 9.

Reading Assignments

Sections 9.4 to 9.10 (Process Groups, Sessions, Controlling Terminal, tcgetpgrp, tcsetpgrp, and tcgetsid Functions, Job Control, Shell Execution of Programs, Orphaned Process Groups)

APUE – Chapter 8 – Process Control

Powerpoint slides: AUP-ProcessTopicsOnly.ppt and AUP-ProcessTopicsOnly2.ppt.

You may find the reference given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture05.pdf (titled Process Environment, Process Control)
– see slides from slide 52 onwards (on process ids, fork(2), exec(3) wait(2), waitpid(2)).

Reading Assignments

Sections 8.1 to 8.6 (Introduction, Process Identifiers, fork Function, vfork Function, exit Functions, wait and waitpid Functions), 8.8 (wait3 and wait4 Functions), 8.9 (Race Conditions), 8.12 (Interpreter Files); Quick Browse through 8.7 (waitid Function), 8.10 (exec Functions) & 8.11 (Changing User IDs and Group IDs).

Report Submission

Run example programs (figures) 8.1 (Example of fork function), 8.3 + 8.4 (Example of vfork function, Macros to examine the termination status returned by wait and waitpid (not a program)), 8.5 (Print a description of the exit status), 8.6 (Demonstrate various exit statuses) (understand 8.7 (The options constants for waitpid (not a program))), 8.8 (Avoid zombie processes by calling fork twice), 1.5, 8.12 +8.13 (Program with a race condition + Modification of Figure 8.12 to avoid race condition), 8.14+8.15 (Differences among the six exec functions (not a program) + Relationship of the six exec functions (not a program) – so only understand – not run) and verify that the programs work as expected. Write a report (C8report.txt) giving your observations. For each example typically limit the observation to two or three lines.

Also add observations relating to the assignments below in the report. The observations for these can be longer (they they do not have to be longer).

Assignment Submissions

  • 8A) Write a program called mpcalc (multi process calc) that will compare two expressions of the form ab and cd and will print out which is less than (or equal to) which. These numbers (a, b, c & d) should be accepted from the user (standard input).

This program should be implemented using multiple processes so that the main process can spawn another process to do the calculation and allow the user to specify another such expression comparison. Instead of the simple ab and cd we might have a much more cumbersome and time consuming calculation which would mean that the program will not be able to take the next input till the time consuming expression is calculated. In fact you should simulate a time consuming calculation by making the child (expression calculator) process sleeping for 20 seconds before doing the calculation and printing the result.

The main process should exit when the user indicates that he wants to quit. Accept a maximum of 10 expression comparison commands (exit automatically after the 10th expression comparison is specified).

  • 8B) Copy mpcalc (assignment 8A) as mpcalc2. Instead of child process printing the result, modify it so that child process returns with a status which indicates comparison result.

When the parent process is prompting user for comparison expression input, it should also accept an option to check whether any previous comparison expression has completed and if so to print the result. If no previous process has completed (since last time we checked) then an appropriate message should be printed and the program should go once again to prompt user for input.

If any child process abnormally terminated then the parent process should report the abnormal termination with its termination/exit status. To allow you to test this code, increase the sleep in the child process to a larger value (say 60). Also (in the parent) print out the process id of each child. Now you can send signals to a child process using the kill command from another terminal. Send a child process one of the signals which terminates it and then check whether the parent reports the abnormal termination correctly. [Some signals which usually terminate a process are: SIGINT – 2, SIGQUIT – 3. A signal which always terminates a process is SIGKILL – 9]

Prior to exiting, the main process should wait for all children to complete and report their results.

  • 8C) Write a shell program. It should have the following features:
    1. You should be able to execute any commands (programs) with arguments (Figure 1.7 of APUE does not allow any arguments for a command).
    2. The termination status and exit status (if any) of the executed program should be reported after termination of the program.
    3. Optional: You should allow the user to execute programs in the background.
    4. Optional: You should allow user to create / modify environment variables and export them. You should test whether a newly created shell variable which is exported is getting specified in the environment variables of the programs executed from the shell.
    5. Any additional features from your side are very welcome 🙂

APUE – Chapter 7 – Process Environment

You may find the reference given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture05.pdf (titled Process Environment, Process Control) – see slides from beginning to slide 51, you may ignore, for now, slides from slide 52 onwards as they deal with Process Control which will be covered next in this course.

Reading Assignments

Sections 7.1 to 7.7 (Introduction, main Function, Process Termination, Command-Line Arguments, Environment List, Memory Layout of a C Program, Shared Libraries), 7.10 (setjmp and longjmp Functions); Quick Browse through 7.8 & 7.9 (Memory Allocation, Environment Variables)

Report Submission

Run example programs 7.1 (Classic C program), 7.3 (Example of exit handlers) and 7.4 (Echo all command-line arguments to standard output) and verify that the programs work as expected. Write a report (C7report.txt) giving your observations. For each example typically limit the observation to two or three lines.

Assignment Submissions

7A) Write a program called testexit that takes an integer value from the user and then immediately exits with that value. Write a shell script named calltexit which invokes testexit and after testexit returns prints its return value on standard output.
7B) Write a program called testenv that asks the user to specify an environment variable name. The program should then get the value for this environment variable name and print it on standard output. The program should do this in a loop till the user indicates that he is exiting (in some way you decide). After testing this program to check the standard environment variables, in your (login) shell create another environment variable called TESTEVAR and set its value to “my env data”. Now run your testenv program and see whether it can print out TESTEVAR’s data. It may not do so in most cases. Why?? How can you fix this? Write this answer also in your C7report.txt file.

APUE – Chapter 4 – Files and Directories

Powerpoint slides and example source file(s) in pdf format: AUP-FilesAndDirectories.ppt, createfile2.c.pdf, createfile3.c.pdf, listdir.c.pdf, readlink.c.pdf, rename.cpp.pdf and suid.c.pdf. Example source file(s) in original file format (my own, if I recall correctly, and so different from book example code) – zip file.

You may find the references given below to slides from a US university (links obtained via Google search and so presumed to be freely accessible to anybody on the net) worth viewing: http://www.cs.stevens.edu/~jschauma/810/lecture03.pdf (titled Files and Directories) and

http://www.cs.stevens.edu/~jschauma/810/lecture04.pdf (titled File Systems, System Data Files, Time & Date).

Self-Study Assignments

  • Entire chapter excluding sections 4.23, 4.24. Also study section 1.3 (Chapter 1).

Report Submission

Study and run example programs 4.3 (Print type of file for each command-line argument), 4.9 (Example of umask function), 4.12 (Example of chmod function), 4.16 (Open a file and then unlink it) and 4.22 (Recursively descend a directory hierarchy, counting file types) and verify that the programs work as expected. Program 4.22 is optional. Write a report (C4report.txt) giving your observations. For each example typically limit the observation to two or three lines.

Assignment Submissions

4a) Write a program that creates a file with ugo+rwx permissions.

4b) Write a program that takes in filenames (with or without a preceding path) as arguments and for each filename prints out the stat info.

4c) Write a program that takes in directory names (with or without a preceding path) as arguments and for each directory prints out the stat info for all the files in that directory.

4d) Write a program backupsrc which takes two arguments: source-directory and destination-directory. This program copies all .c, .h and mak* files from source-directory and its subdirectories (recursively) to destination-directory. Program should exit with an error if destination directory already exists.

Assignments Given in Previous Years – If you have the time you can do them as well

The objective of the assignments is to utilize the system calls that you have studied in programs (different from the example programs). As the time provided is limited I do not expect you to write programs for all the system calls studied (though if somebody does manage to do so, he is welcome).

The programs can be many small programs or a few large programs or any combination of small programs and large programs. A readme file (named C4readme.txt) should be provided which describes in short the program assignments you are submitting.

The evaluation will be based on how well you have explored the facilities provided by the system calls we have studied (I repeat you do not have to include all system calls in your programs). To aid you in choosing these program assignments I have listed below some sample program assignments. You are free to do to your own assignments instead of these.

Sample Assignments

  • Write a program that creates a temporary file. (A temporary file is one which gets deleted automatically by the system when the program exits).
  • Write a program called issymlink that takes a filename as an argument and checks whether the filename is a symbolic link. If so, it should read the symbolic link file (and not the file pointed to by the symbolic link) and print its contents to stdout. Else it just prints a message saying passed filename is not a symbolic link.
  • Write a simple touch program called mytouch which takes a filename as argument. The program should update the access and modification time to the current time.
  • Write a program called savecopy which takes arguments as follows:

savecopy dir file1 file2 …

The program should first create directory dir if it does not exist (otherwise it should go to the next step of copying). Then it should copy all the files specified after the dir argument into the directory dir. If a specified file does not exist it should print an error message and go to the next file. If in directory dir a file with the same name already exists it should give the user an interactive option to overwrite or skip. If any of the arguments after dir are directories the program should print a message and skip the directory from the copy.

  • Write a program called lsd which takes arguments as follows:

lsd [dir]

If an argument is passed lsd lists the sub-directories in that directory. If no argument is passed lsd lists the sub-directories in the current directory.

  • Write a program called mypwd which prints the current working directory.