This project must be done individually. It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub. It is academic...




    • This project must be done individually.




It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub.




It is academic misconduct for you to copy or use some or all of a program that has been written by someone else.












Learning GOALS

The purpose of this assignment is to gain insight into the asynchronous nature of interrupts and signals, as well as to expand your C programming skills.











A Periodic ALARM

For this part you'll be writing two programs calledintdate.candsendsig.c. The first programwill handle 3 signals: a periodic signal from an alarm, a keyboard interrupt signal and a user defined signal. The second program will be used to send signals to other programs including intdate.c.


Reminder: You are to do this work on the CS instructional lab machines. The setup and handling of signalsis different on different platforms and might not work the same on your machine as it does in the CS lab machines.



Setting up the Alarm


Write aprogramintdate.cwith just amain()function that runs an infinite loop such as:


while (1){
}

Now before entering the infinite loop themain()function has to do two things. First, you'll need to set up an alarm that will go off 3 seconds later, causing aSIGALRMsignal to be sent to the program. Second, you'll need to register a signalhandler to handle theSIGALRMsignal so that it can be received by the program. The signalhandler is just another function you need to write inside your program. This handler function should print the pid (process id) of the program and the current time (in the same format as the Unixdatecommand). It should also re-arm the alarm to go off again three seconds later, and then return back to the main function (which continues its infinite loop doing nothing).


At this stage, the output should look something like this:


[skrentny@jimbo] (49)$ ls
intdate* intdate.c
[skrentny@jimbo] (50)$ ./intdate
Pid and time will be printed every 3 seconds.
Enter ^C to end the program.
PID: 18238 | Current Time: Mon Apr 17 20:01:11 2017
PID: 18238 | Current Time: Mon Apr 17 20:01:14 2017
PID: 18238 | Current Time: Mon Apr 17 20:01:17 2017
PID: 18238 | Current Time: Mon Apr 17 20:01:20 2017
PID: 18238 | Current Time: Mon Apr 17 20:01:23 2017
^C
[skrentny@jimbo] (51)$

Notice that to stop the program from running, you type in aControl+cin the shell where the program is running. TypingControl+csends an interrupt signal (calledSIGINT) to the running program.


Since bothmain()and the alarm handler need to know/use the number of seconds in order to arm the alarm, make this value a global variable. Recall that signalhandlers are not called by a function in the program, so they cannot receive arguments from other functions in the program and must use global variables instead.


You'll use library functions and system calls to write the above program. It is important to check the return values of these functions, sothat your program detects error conditions and acts in response to those errors. Refer the man pages of the following functions that you will use. To use the man pages, just typeman
at the Linux prompt. The manual section numbers will be either 2 (system calls) or 3 (C library functions). Read more on how to use man pageshere(Links to an external site.)Links to an external site..







    • time()andctime(): are library calls to help your handler function obtain and print the time in the correct format.


    • getpid(): is a system call to help your handler function obtain the pid of the program.


    • alarm(): activates theSIGALRMsignal to occur in a specified number of seconds.


    • sigaction(): registers your handler function to be called when the specific type of signal (specified as the first parameter) is sent to the program. You are particularly interested in setting thesa_handlerfield of the structure thatsigaction()needs; it specifies the handler function to run upon receiving the signal.DO NOT USEthe signal() system call; instead, use sigaction() to register your handler.



    Note:Make sure to initialize the sigaction struct viamemset()so that it is cleared (i.e, zeroed out) before you use it.


    struct sigaction act;
    memset (&act, 0, sizeof(act));


    User Defined Signals


    Linux has two basic user defined signals,SIGUSR1andSIGUSR2. As the name suggests these signals are defined by a user program, which is free to choose whatever action it should take on catching these signals.


    Extend the implementation ofintdate.cso that it prints a message on receiving aSIGUSR1signal. It should alsoincrement a global counter to keep tally of the number of times it receivedSIGUSR1. For achieving this you will need to write another signalhandler and register it to handle theSIGUSR1signal (usingsigaction() again).


    You can test your implementation by using thekillcommand at the Linux prompt to send aSIGUSR1signal tointdate.c. Later, wewill implement our own small program that can be usedto send signals to other programs.



    Note:If you are working remotely, make sure that different ssh sessions are made on the same machine (example,rockhopper-04) otherwise sendingsignals from one session to the other is going to fail.



    Handling the Keyboard Interrupt Signal


    In this section, you'll modify your program so that it does something different other than exiting after aControl+cis typed. The program should print the number of times it received theSIGUSR1signal before callingexit(0). You will need to write another signalhandler and register it to handle theSIGINTsignal (usingsigaction()just like you did forSIGALRM). With this addition the output of the program should look something like this:


    [skrentny@jimbo] (67)$ ./intdate
    Pid and time will be printed every 3 seconds.
    Enter ^C to end the program.
    PID: 18163 | Current Time: Mon Apr 17 20:00:03 2017
    PID: 18163 | Current Time: Mon Apr 17 20:00:06 2017
    SIGUSR1 caught!
    PID: 18163 | Current Time: Mon Apr 17 20:00:09 2017
    PID: 18163 | Current Time: Mon Apr 17 20:00:12 2017
    SIGUSR1 caught!
    PID: 18163 | Current Time: Mon Apr 17 20:00:15 2017
    PID: 18163 | Current Time: Mon Apr 17 20:00:18 2017
    PID: 18163 | Current Time: Mon Apr 17 20:00:21 2017
    ^C
    SIGINT received.
    SIGUSR1 was received 2 times. Exiting now.
    [skrentny@jimbo] (68)$


    Sending Signals


    In this section, you'll write a simple programsendsig.cwhich can send signals (SIGINTandSIGUSR1) to other programs using their pid. For this, you will need to use the system callkill().


    Your programshould take two command line arguments: the type of signal (-i forSIGINTand -u forSIGUSR1) and the pid of the program to which the signal needs to be sent. The output should look like the following:


    [skrentny@jimbo] (67)$ sendsig
    Usage:

    [skrentny@jimbo] (68)$ sendsig -u 18163
    [skrentny@jimbo] (69)$ sendsig -u 18163
    [skrentny@jimbo] (70)$ sendsig -i 18163

    You should usesendsig.calong withintdate.cand make sure that both programs work as expected.











    Divide by ZERO

    Write a programdivision.cthat does the following in an infinite loop:






      • Prompt for and read in one integer value

      • Prompt for and read in a second integer value

      • Calculate the quotient and remainder of doing the integer division operation:int1 / int2

      • Print these results

      • Keep a total count of how many division operations were successfully completed.


      Usefgets()to read each line of input (use a buffer of 100 bytes).DO NOT USEfscanf()orscanf()for this assignment. Then, useatoi()to translate that C string to an integer.


      Users tend to type in bad inputs occasionally. For ease of programming, ignore error checking on the input. If the user enters a bad integer value don't worry about it. Just use whatever valueatoi()returns.


      At this stage the sample run of the program would appear as:


      [skrentny@jimbo] (118)$ ls
      division* division.c
      [skrentny@jimbo] (119)$ ./division

      Enter first integer: 12
      Enter second integer: 2
      12 / 2 is 6 with a remainder of 0
      Enter first integer: 100
      Enter second integer: -7
      100 / -7 is -14 with a remainder of 2
      Enter first integer: 10
      Enter second integer: 20
      10 / 20 is 0 with a remainder of 10
      Enter first integer: ab17
      Enter second integer: 3
      0 / 3 is 0 with a remainder of 0
      Enter first integer: ^C
      [skrentny@jimbo] (120)$

      Please note the behavior of the program for a non-numeric input ‘ab17’. Handle similar inputs in the same way.


      Try giving the input for the second integer as 0. This will cause a divide by zero exception. The hardware traps when this unrecoverable arithmetic error occurs, and the program crashes, because it did not (catch and) handle theSIGFPEsignal.


      To remedy this situation, modify your program to set up a handler that will be called if the program receives theSIGFPEsignal. In the signal handler you will print a message stating that a divide by 0 operation was attempted, print the number of successfully completed division operations, and then exit the program usingexit(0)(gracefully, instead of crashing). Below is a sample output of how your program should behave:


      [skrentny@jimbo] (124)$ ./division

      Enter first integer: 1
      Enter second integer: 2
      1 / 2 is 0 with a remainder of 1
      Enter first integer: 1
      Enter second integer: 0
      Error: a division by 0 operation was attempted.
      Total number of operations completed successfully: 1
      The program will be terminated.
      [skrentny@jimbo] (125)$

      The count of the number of completed divisions needs to be a global variable so it can be used by bothmain()and your signalhandler.


      Lastly your program should have a separate handler to handle the keyboard interruptControl+cjust like theintdate.cprogram did. Except in this program on the firstControl+csignal, the handler should print the number of successfully completed division operations, and then exit the program usingexit(0).
      Here is the sample output fordivision.cthat shows the graceful exit of the program in case of aSIGINTsignal.


      [skrentny@jimbo] (128)$ ./division

      Enter first integer: 1
      Enter second integer: 2
      1 / 2 is 0 with a remainder of 1
      Enter first integer: 3
      Enter second integer: 4
      3 / 4 is 0 with a remainder of 3
      Enter first integer: ^C
      Total number of operations successfully completed: 2
      The program will be terminated.
      [skrentny@jimbo] (129)$


      Note:Do not place the calls tosigaction()within the loop. These calls should be completed before entering the loop that requests and does division on the two integers. Implement 2 independent handlers; do not combine the handlers.











      REQUIREMENTS



      • Your program must follow theseStyle Guidelines.

      • Your program must follow theseCommenting Guidelines.Include a comment at the top of each source code file with yourname and lecture number.You must add a header comment for every function.

      • Your programs should operate exactly as the sample outputs shown above.

      • We will compile each of your programs with

        gcc -Wall -m32 -std=gnu99

        on the Linux lab machines. So, your programs must compile there, andwithout warnings or errors. It is your responsibility to ensure that your programs compile on the department Linux machines, andpoints will be deductedfor any warnings or errors.

      • Remember to do error handling in all your programs.











      SUBMITTING Your Work

      Submit the followingsource filesunder Project 6 in Assignments on Canvas on or before the deadline:






        1. intdate.c

        2. sendsig.c

        3. division.c


        It is your responsibility to ensure your submission is complete with the correct file names having the correct contents. The following points will seem obvious to most, but we've found we must explicitly state them otherwise some students will request special treatment for their carelessness:




        • You will only receive credit for the files that you submit.You will not receive credit for files that you do not submit. Forgetting to submit, not submitting all the listed files, or submitting executable files or other wrong files will result in you losing credit for the assignment.


        • Do not zip, compress, submit your files in a folder, or submit each file individually.Submit only the text files as listed as a single submission.


        • Make sure your file names exactly match those listed.If you resubmit your work, Canvas will modify the file names by appending a hyphen and a number (e.g., intdate-1.c) and these Canvas modified names are accepted for grading.



        Repeated Submission:You may resubmit your work repeatedly until the deadline has passed.We strongly encourage you to use Canvas as a place to store a back up copy of your work.If you resubmit, you must resubmit all of your work rather than updating just some of the files.

        Dec 06, 2019
        SOLUTION.PDF

        Get Answer To This Question

        Related Questions & Answers

        More Questions »

        Submit New Assignment

        Copy and Paste Your Assignment Here