Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c language Instructions. The exam must be completed in 2 hours. The exam has two

ID: 3734056 • Letter: C

Question

c language

Instructions. The exam must be completed in 2 hours. The exam has two problems and is worth 100 points. Problem 1 (50 points) ved cci, cht) Write a function cp/char "src, char "dst) thaty st, a) receives two filenames (strings) src and dst and copies the file identified by sre into fite identified by dst, using functions read) and write(). You can assume that the file identified by dst does not exist before cp) runs. b) uses calls to sigsetjmpl) and siglongimp) to perform a cleanup if the user hits Control- C during the time that the file is being copied. Specifically, if the user hits Control-C while function cp() is copying, then (before terminating) cp) should stop copying, should make sure that the partially written file dst is eliminated, and should close and free all resources Notes: The cleanup must be done by function Do not assume that the whole src file can fit into a memory buffer. (In fact, the are file can cp(). be arbitrarily large) not assume that the operating system will do the cleanu Do not use any global variables in your program except for the argument env to sigsetimeo and siglongimp) unction prototypes t gelpid void) int sigprocmask(int how, sigset I 'set, t gelppid void) sigset ! 'oldsel) int sigemptyset(sigset 1 'set), int sigfillset(sigset I 'set) int sigaddset(sigset I 'set, int signum), int sigdefset(sigset I 'set, int signum), int setimpjimp buf envy, înt sigsetjrnp(sigrnp but env, int savesigs), t fork(void) d, I wailpidf pid t pid, int 'status, int options) pitt wailt int 'status) unsigned int sleep(unsigned int secs) nt pause(void) item vechar lename, char .argv0.char'envpD, pid, t gelpgrp(void) /" use savesigs 1 int selpgidipid t pid, pid t pgid) int killipid t pid, int sig) void longimpljmp buf env, int relval), void siglongimp sigimp bul env, int retvaly untsigned int alarm(unsigned int secs) sighandler tsignal(int signum, sighandler_thandier)

Explanation / Answer

** Source code **

#define _POSIX_SOURCE

#include<stdio.h>

#include <setjmp.h>

#include <signal.h>

sigjmp_buf mark;

void cp(char *src, char *dst) {

                FILE *file_read, *file_write;

                char buf[1000];

                //Src file should be in same directory

                file_read = fopen(src,"r");           //Open file in "r" mode

                file_write = fopen(dst,"w"); //Create and open file in "w" mode

                if (!file_read)

                exit(1); //exit if file is empty

                while (fgets(buf,1000, file_read)!=NULL)

                                fputs(buf, file_write);                    //copy line from one file to another

                //Close files when no need it    

                fclose(file_read);

                fclose(file_write);

}

void signalHandler(void)

{

                siglongjmp(mark, -1);

}

void recover( void ) {

               

                printf( "taking recovery action " );

                remove("writeFile.txt");

}

void main()

{

                signal(SIGTERM, signalHandler);

                if (sigsetjmp(mark, 1) != 0) {

                                printf("siglongjmp() has been called ");

                                recover();

                                exit(1);

                }

                // copy from src file to destination file

                cp("readFile.txt", "writeFile.txt");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote