Google Answers Logo
View Question
 
Q: Operating Systems ( Answered,   0 Comments )
Question  
Subject: Operating Systems
Category: Computers > Operating Systems
Asked by: axmedeey-ga
List Price: $30.00
Posted: 10 Mar 2004 06:05 PST
Expires: 09 Apr 2004 07:05 PDT
Question ID: 315226
1:  

This question is concerned with UNIX systems programming. It involves
a data-file that consists of records, as defined in the following
header-file:
/* myheader.h
*/

#define MY_FILE "myfile"

struct myrecord {
	char fstname[10];
	char surname[10];
	int  id;
};
Consider the following C program that allows records to be input into
the data-file:
/* inputrecords.c
 */

#include <stdio.h>
#include <fcntl.h>
#include "myheader.h"

int main()
{
	int fd;
	int c;
	struct myrecord r; 
	int rlen = sizeof(r);

#if 1
	if ( (fd = creat(MY_FILE, 0644)) == -1 ) {
		perror( MY_FILE );
		exit(1);
	}

#else
	if ( (fd = open(MY_FILE, O_WRONLY | O_CREAT | O_APPEND, 0644)) == -1 ) {
		perror( MY_FILE );
		exit(1);
	}
#endif

	printf("Enter first record: ");
	while ( (c = scanf("%s%s%d", r.fstname, r.surname, &r.id)) != EOF ) {
		if ( c != 3 ) {printf("Re-enter record: "); }
		else { 	if ( write(fd, &r, rlen) != rlen ) {
				perror( MY_FILE );
				exit(1);
			} else printf("Enter next record: ");
		}
	}

	Close(fd);
	return 0;
}
a)	Two kinds of #include-statement have been used: <header-file> and
"header-file".  Can you Explain to me please how this difference
affects file inclusion?
b)	What is the name of the data-file that is to be used for storing records?
c)	Suppose that the program is compiled as is into an executable-file.
If the data-file does not exist, then it will be created when the
executable-file is run. What will happen if the data-file already
exists?
d)	Suppose instead that 1 is changed to 0 in the #if-statement prior
to compilation. What effect, if any, does this have as far as the
data-file is concerned? (Consider both the case in which the data-file
does not exist and the case in which it does.)
e)	Which C-function is accessing the standard input stream? Which is
accessing the standard output stream? Which is accessing the standard
error stream?
f)	 Can you provide for me an example of a session in which you have
checked the size of the data-file, input a number of records and
finally re-checked its size?
g)	Are all records of the same size? 
h)	What gets stored when a record that you enter includes a very long first name? 
i)	 Can you provide for me a listing of a program outputrecords.c that
you have written? It should output the records stored in the data-file
one line at a time.


2:
This part requires you to write two versions of a Java program that
performs a standard data-processing task, namely, to read and modify
the contents of one file, writing the results to a second file. In
particular:
·	The user on the command line provides the names of the input file
and output file when the program is run. Your program should check
that these two arguments are provided, that the first is a file that
is not a directory, and that the second is not an existing file. Hint:
the File class provides suitable methods.
·	The contents of the input file should be read in one byte at a time.
Hint: read () either returns an integer between 0 and 255 (the value
of an unsigned byte) or -1 (upon reaching the end of the input
stream).
·	Each byte read should be written to the output file as soon as it is
available, except for the following three transformations:
a)	The value 36 should be changed to the value 163, 
b)	The value 10 should be inserted after the value 12, and 
c)	If the value 111 is immediately followed by the value 101, they
should be replaced by the single value 248.
  Can you make the first version of your program single-threaded, and
the second version of your program multi-threaded:

Request for Question Clarification by maniac-ga on 10 Mar 2004 15:44 PST
Hello Axmedeey,

I suggest you break this into at least two questions and adjust your
price appropriately. You are asking for answers to eight questions as
well as three separate programs:
 - the C file outputrecords.c
 - the single threaded java application
 - the multiple threaded java application
That way, it would be possible that one researcher answers your C
related question and another handle the Java question.

I also suggest you view
  https://answers.google.com/answers/pricing.html
to determine if the effort you are asking for is comparable to the price offered.
  --Maniac

Clarification of Question by axmedeey-ga on 12 Mar 2004 13:58 PST
hello thanks for the Suggestion, I agree I should break it into some parts.
I suggest you do it the way you think should be done.
By the way I adjusted my price appropriately i think, and thanks for
everything, I really appreciate it.

thank you very much.

Request for Question Clarification by theta-ga on 13 Mar 2004 10:06 PST
Hi axmedeey-ga,
  If you intend to break this question up into two parts (as you
indicated in your clarification post), I suggest you limit this
question to only the first part, and post the second part(the Java
programs) as a new question on Google Answers.
  Also, it would help if you could clarify part 1f of your question.
What exactly do you mean by 'an example of a session'?
  Awaiting your response.
Regards,
Theta-ga

Clarification of Question by axmedeey-ga on 14 Mar 2004 02:39 PST
ok thanks for Suggestion I will limit this
question to only the first part, and post the second part(the Java
programs) as a new question on Google Answers.

Clarification of Question by axmedeey-ga on 14 Mar 2004 02:55 PST
hi if you don not understand a question don not worry, just do what
you can please thank you, i really really appreciate it.
by

Clarification of Question by axmedeey-ga on 23 Mar 2004 10:52 PST
This question is concerned with UNIX systems programming. It involves
a data-file that consists of records, as defined in the following
header-file:
/* myheader.h
*/

#define MY_FILE "myfile"

struct myrecord {
	char fstname[10];
	char surname[10];
	int  id;
};
Consider the following C program that allows records to be input into
the data-file:
/* inputrecords.c
 */

#include <stdio.h>
#include <fcntl.h>
#include "myheader.h"

int main()
{
	int fd;
	int c;
	struct myrecord r; 
	int rlen = sizeof(r);

#if 1
	if ( (fd = creat(MY_FILE, 0644)) == -1 ) {
		perror( MY_FILE );
		exit(1);
	}

#else
	if ( (fd = open(MY_FILE, O_WRONLY | O_CREAT | O_APPEND, 0644)) == -1 ) {
		perror( MY_FILE );
		exit(1);
	}
#endif

	printf("Enter first record: ");
	while ( (c = scanf("%s%s%d", r.fstname, r.surname, &r.id)) != EOF ) {
		if ( c != 3 ) {printf("Re-enter record: "); }
		else { 	if ( write(fd, &r, rlen) != rlen ) {
				perror( MY_FILE );
				exit(1);
			} else printf("Enter next record: ");
		}
	}

	Close(fd);
	return 0;
}
a)	Two kinds of #include-statement have been used: <header-file> and
"header-file".  Can you Explain to me please how this difference
affects file inclusion?
b)	What is the name of the data-file that is to be used for storing records?
c)	Suppose that the program is compiled as is into an executable-file.
If the data-file does not exist, then it will be created when the
executable-file is run. What will happen if the data-file already
exists?
d)	Suppose instead that 1 is changed to 0 in the #if-statement prior
to compilation. What effect, if any, does this have as far as the
data-file is concerned? (Consider both the case in which the data-file
does not exist and the case in which it does.)
e)	Which C-function is accessing the standard input stream? Which is
accessing the standard output stream? Which is accessing the standard
error stream?
f)	 Can you provide for me an example of a session in which you have
checked the size of the data-file, input a number of records and
finally re-checked its size?
g)	Are all records of the same size? 
h)	What gets stored when a record that you enter includes a very long first name? 
i)	 Can you provide for me a listing of a program outputrecords.c that
you have written? It should output the records stored in the data-file
one line at a time.


hi this is a new clarification if you can do this, i would like to
recieve the answer as soon as possible. thank you very much
by
Answer  
Subject: Re: Operating Systems
Answered By: maniac-ga on 23 Mar 2004 20:09 PST
 
Hello Axmedeey,

Sorry for the delay - I was "off line" when your previous clarification was posted.

Based on your update - I have prepared a set of answers to your
questions below. If you have any problems understanding the answer or
they are incomplete in any way - please use a clarification request so
I can make the needed correction.

I also noted that someone answered your Java question as a comment. If
that comment is acceptable - I suggest you close the question so you
are not charged for an answer. If that comment is NOT acceptable
(e.g., you need two or more tasks and use a queue or protected
object), then make a clarification so a proper answer can be produced.

  --Maniac

a)      Two kinds of #include-statement have been used: <header-file> and
"header-file".  Can you Explain to me please how this difference
affects file inclusion?

From "The C Programming Language" by Kernighan and Ritchie, they state:
  #include "filename"
causes the replacement of that line by the entire contents of the file
filename. The named file is searched first in the directory of the
original source file, and then in a sequence of standard places.
Alternatively, a control line of the form
  #include <filename>
searches only the standard places, and not the directory of the source file.

b)      What is the name of the data-file that is to be used for storing records?
From myheader.h:4, the file will be named myfile.

c)      Suppose that the program is compiled as is into an executable-file.
If the data-file does not exist, then it will be created when the
executable-file is run. What will happen if the data-file already
exists?

There were some errors in the code you posted. It was necessary to add:
  #include <stdlib.h>
  #include <unistd.h>
to the header file references. This is because exit and write are
defined in these header files. In addition, the line near the end that
said:
        Close(fd);
should read
        close(fd);
Once these changes were made, inputrecords could be built and run.

The first time run (no myfile present), it created the file and wrote
the records to the file. The second time run (myfile present), it
destroyed the original contents of the file and wrote the records
entered to the file.

d)      Suppose instead that 1 is changed to 0 in the #if-statement prior
to compilation. What effect, if any, does this have as far as the
data-file is concerned? (Consider both the case in which the data-file
does not exist and the case in which it does.)

The first time run (myfile not present), it creates the file and wrote
the contents to the file. The second time run (myfile present), it
appended the records entered to the end of the file.

e)      Which C-function is accessing the standard input stream? Which is
accessing the standard output stream? Which is accessing the standard
error stream?

Standard input is accessed by scanf.
Standard output is accessed by printf.
Standard error is accessed by perror.
For completeness, fd (myfile) is accessed by creat (or open), write, and close.

f)       Can you provide for me an example of a session in which you have
checked the size of the data-file, input a number of records and
finally re-checked its size?

See the following for such a run.
 ls -l myfile
-rw-r--r--  1 maniac  staff  72 Mar 23 21:19 myfile
maniac% ./inputrecords 
Enter first record: Doris Day 456
Enter next record: maniac% ls -l myfile 
-rw-r--r--  1 maniac  staff  96 Mar 23 21:23 myfile

Before this run, there were three records present and after there were four.

g)      Are all records of the same size? 
Yes - each record is 24 bytes long (10 bytes for each name, 4 for the number).

h)      What gets stored when a record that you enter includes a very
long first name?
maniac% ./inputrecords
Enter first record: averylongfirstnamethatiswaytoolong jones 234
myfile: No space left on device
maniac%

It appears that the size of the record (rlen) was overwritten by the
very long first name entered on the line of input. The program
attempted to write a huge number of characters to the file and was
prevented by the operating system. This is generally called a "buffer
overflow" which should be prevented by more careful programming.

i)       Can you provide for me a listing of a program outputrecords.c that
you have written? It should output the records stored in the data-file
one line at a time.

Certainly. The output of "outputrecords" is below, followed by the
source of the program used to produce it.

maniac% ./outputrecords
Sally Smith 123
Jerry Jones 234
[when myfile had two records]

/* outputrecords.c
 */

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "myheader.h"

int main()
{
        int fd;
        int c;
        struct myrecord r; 
        int rlen = sizeof(r);

        if ( (fd = open(MY_FILE, O_RDONLY , 0)) == -1 ) {
                perror( MY_FILE );
                exit(1);
        }

        while ( (c = read(fd, &r, rlen)) == rlen) {
	  printf("%s %s %d\n", r.fstname, r.surname, r.id);
        }

        close(fd);
        return 0;
}
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy