Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 1 -copying files from the OS point of view using system-calls – C program – Operating Systems Fundamentals

Objectives: The aim of this assignment is to help students understand the main principles of copying files from the OS point of view, i.e. via using system-calls. Students will obtain hands on experience in copying files, system-calls, and working with APIs.
Tasks:
Section 2.3 of the textbook and Chapter 2 of the class slides describe a program that copies the contents of one file to a destination file. This program works by first prompting the user for the name of the source file and destination files. Write this program using an API of your choice, either Windows API, POSIX API, of Java API; though, I prefer that you use the POSIX API. Make sure you include all necessary error checking, including ensuring that the source file exists.
The system-call sequence for this program (which copies the contents a source file to the destination file) is shown below.

Once you have correctly designed and tested the program, if you used system that supports it, run the program using a utility that traces system-calls. Linux systems provide the strace utility, and Solaris and Mac OS X systems use the dtrace command. As Windows systems do not provide such features, you will have to trace through the Windows version of this program using a debugger (or via the debugger/IDE you are using).
Submission:

  1. You must submit:
  2. a short report (in PDF or word), which contains the details of the implementation and a sample input/output,
  3. the source code of your program. You should upload all these files to the Blackboard, as a single zip file.
  4. In your report, you must provide details about the implementation, including the programs’ output, as required. Marks will be deducted if explanations/outputs are missing.
  5. Add the following note at the beginning of your report: “I confirm that I will keep the content of this assignment confidential. I confirm that I have not received any unauthorized assistance in preparing for or writing this assignment. I acknowledge that a mark of 0 may be assigned for copied work.” + Name + SID
  6. Any submission after the deadline will receive a penalty of 10% for the first 24 hrs, and so on, for up to seven days. After seven days, the mark will be zero.
  7. Unlimited resubmissions are allowed. But keep in mind that we will consider/mark the last submission. This means that if you resubmit after the deadline, a penalty will be applied, even if you submitted an earlier version before the deadline.

Answer


<span style="color: #000000; font-family: verdana, geneva, sans-serif;"><!-- /wp:paragraph --></span>

<span style="color: #000000; font-family: verdana, geneva, sans-serif;"><!-- wp:syntaxhighlighter/code --></span>
<pre class="wp-block-syntaxhighlighter-code"><span style="color: #000000; font-family: verdana, geneva, sans-serif;">//C-Preprocessor Directives
#include <stdio.h>
#include <stdlib.h> 
 
int main()
{
    FILE *ptr1, *ptr2;
    char file1[200],file2[200], c;
 
    printf("Please enter the file name to open for reading \n");
    scanf("%s", file1);
	printf("Please enter the file name to open for writing \n");
	scanf("%s", file2);
	// Open file1 for reading
	if (ptr1 = fopen(file1, "r")){	
	 
		// Open file2 for writing
		if (ptr2 = fopen(file2, "r")){
			printf("file already exists \n");
			exit(0);
		}else{
			ptr2 = fopen(file2, "w");
			if (ptr2 == NULL)
			{
				printf("Cannot open file %s \n", file2);
				exit(0);
			}
		 
			// Read contents from file1 and write to file 2
			c = fgetc(ptr1);
			while (c != EOF)
			{
				fputc(c, ptr2);
				c = fgetc(ptr1);
			}
		 
			fclose(ptr1);
			fclose(ptr2);
			printf("\nContents copied to %s", file2);
		}
	}else{
	   printf("file doesn't exists \n");
	   exit(0);
	}
    
    return 0;
}


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!