Lab
2
Question 3:
Consider a transmission link that is 36000m
long and uses 1Gbps transmission speed. How long will it take to transmit a 10MB file along
this link? Repeat this question for a 2m long link with 10bps transmission
rate. Assume that the propagation speed is equal to the speed of light.
1 Gbps is 10^9 bits per second or 1000000000 bps, this is more
than 100,000 times larger than the 10 MB file being considered (122070.3125), so the file is transferred at 12 milliseconds
(file size*8 / transmission speed). The
transmission itself will be dependent on the length of the wire and must be
added to the transfer time. Transmission time = light speed / distance, not
taking minor things into account like friction losses and electromagnetic
interference, etc.. The transmission time is very small: about 12 milliseconds
(1.2008*10-4). Thus the total time (add transmission time to
transfer time) takes barely more than the transfer time (about 12.8 milliseconds.)
For the shorter link at lower transmission rate, the
distance is almost insignificant as the transmission time is about 6.67*10-9
(6.67 nanoseconds.) The transfer time however us huge: 819.2 seconds or 13
minutes 39.2 seconds. This makes the total time just a hair above the transfer
time, but this can be ignored (819.200000006671.)
These two examples show that because transmission time is
dependent on the speed of light, long distances affect total file transfer times
minimally. It is the transfer rate (bandwidth) which has the greater impact.
Also, when the transfer rate is very small, even over short distances, the
amount of data will most significantly impact the total transfer time.
Question 4
Interprocess Communication (IPC) using
Pipes.
Modern computing often requires multiple applications
(threads) to operate simultaneously. This is accomplished either through
elaborate algorithms or by the use of multiple processors which allow true
simultaneous operations. When these parallel processes must share data and/or
resources, this is Interprocess Communication (IPC). The exchange or sharing of data across
threads can be accomplished though various methods which are categorized by the
functions they perform:
- · message passing
- · remote procedure calls (RPC)
- · shared memory
- · synchronization
This communication often involves programs using the output
of another program as their input which is passed to yet another program, and
these programs or applications can even be linked over networks.
The word pipe is short for pipeline which is a concept of
passing information from application to application or program to program such
that the data can be shared, exchanged and used efficiently. When an
application or program is using inputs that exceed the rate at which they
process information, a buffer and cache can be used to control the flow of data
so no information is lost. When the maximum processing memory is used, the
input is stopped until the cache is cleared and more data can be accepted.
When considering the flow of operations, two types of pipes
can be implemented (alone or together): shell pipes or named pipes.
The pipe command functions are included in the <stdio.h>
header file library in c programming language.
popen and pclose
Two commands which illustrate the use of pipes are popen and pclose.
popen connects one
program or application to another, allowing one to pass information as input to
the other.
pclose shuts down
the process, much like closing a file that was opened for writing and needs to be
closed (see: Question 5, below.)
fread and fwrite
When there is a need to pass more information into a
program, multiple instances of fread
and fwrite can be used (see: Question
5, below.)
unistd.h
Some pipe functions exist within the <unistd.h> header
file which contains POSIX libraries and allows access to Unix commands such as:
dup, dup2, fcntl, and F_DUPFD.
These are used when one file descriptor may be unknown
and/or there is a need to create a duplicate file descriptor for use in a pipe.
This is also how a parent and child process can be assigned
or created, with the pipe linking the two. The child process can be a different
program from the parent process.
mknod and mkfifo
Unlike the above commands which use shell pipes (unnamed), mknod and mkfifo are Unix/Linux functions which allow the creation of named
pipes.
The header files required for these functions are:
<sys/types.h> and <sys/stat.h> in addition to <unistd.h>.
The named pipes are FIFO (first-in-first-out) processes
which are different from unnamed pipes which are file descriptors. They are
actual files which must be opened with open()
and close() functions.
Because of the flexibility and power of the functions above,
pipes can be used to efficiently control many important applications such as
data retrieval or client/server processes.
Shared Memory
Shared memory differs from pipes in that many programs or
applications can access the memory as long as they have the permission to. This
is a very efficient way to access memory and it is more easily in a user control
than pipes.
shmget()
shmget() provides access to shared memory by providing a
shared memory segment ID on a successful call.
int shmget(key_t key, size_t size, int shmflg);
shmctl()
shmctl() from a controlling user/owner that already has
permission to access shared memory allows another application to get the same
access
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmat() and shmdt()
shmat() and shmdt() are used to allow access to shared
memory once an ID has been received, they function like open and close,
allowing access to memory and then removing it.
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
Question 5
fread and fwrite commands in c programming
language.
fread and fwrite are used in file handling
operations which are very important to any operating system (OS) processes.
File handling is the set of functions or commands which
allows a user to copy, move, delete, read, write, truncate, append,
concatenate, find or replace parts or the entirety of a text contained within a
file.
fread requires
four parameters: a pointer to the stream
from which the elements will be read, the size of what will be read in bytes,
the number of elements to be read, a pointer to the stream from which the
elements will be read (input).
fwrite also
requires four parameters: a pointer to the string or stream that will be
written, the size of what will be read in bytes, the number of elements to be
read, a pointer to the stream to which the elements will be read (output).
The following program illustrates the use of fread and fwrite with other file handling commands which usually accompany
their use. The program is heavily commented for ease in reading.
#include <stdio.h> // the base
library with I/O functions
#include <string.h> // required
for using string operations
#include <stdlib.h> // not
necessary unless calling from the standard library
#define SIZE 1
#define STRLN 25 // these two lines
set parameters to be used in verifying information later
int main() {
FILE *fp = NULL; // creates a pointer allowing the use of file I/O
functions
char msg[] = "This is a sample of text."; // this is the text
string that will be read/written
char holder[30]; // this is a buffer string
fp = fopen("rw_test.dat", "w+"); // the pointer
above will be set to the file location on creation
// if this file does not exist, it will be created, the 2nd parameter is
the mode, which is set for read/write
//‘a’ :
Open to append (write at end of file). Any new information will be added at the
end of the file.
//‘a+’ :
Open to read and append (writing at end of file). Creates the file if it does
not exist.
// The initial position for reading is at the
beginning. New information is always added to the end.
//‘r’ :
Open text file to read. The initial file position for reading is at the start
of the file.
//‘r+’ :
Open to read and write. The initial file position for reading and writing is at
the start of the file.
//‘w’ :
This will create a new file or erase an existing file!!.
// The initial file position for writing is at
the start of the file.
//‘w+’ :
Open to read and write. This will create a new file or erase an existing
file!!.
// The initial file position for reading and
writing is at the start of the file.
printf("\n File \"rw_test.dat\" opened successfully
through fopen()\n"); // simply
outputs a message after the file is opened
fwrite(msg, strlen(msg) + 1, 1, fp); // this writes the message to the
file
//
Note: when the message is done being written, fp no longer points to the
beginning of the file
fseek(fp, SEEK_SET, 0); // this resets the location of the pointer, so
any read operations start there
fread(holder, strlen(msg)+1, SIZE, fp); // this assigns to the string
buffer the contents of the file
int
s = strlen(holder); // creates a new variable used below, also: debug line 1
printf("\nThere
are : [%i] characters in the buffer.\n",s); // debug line 2 - prints the
number of characters read
if(SIZE*STRLN != s) { printf("\n
ERROR! fread() failed\n"); return 1; } // debug line 3 - checks to make sure the
program works
printf("\n Some text was successfully read through
fread()\n");
printf("\n The contents read from the file are:
[%s]\n",holder); // the %s indicated a string will be used in this printf
output
fclose(fp); //when fopen() is
used fclose() must be used
//stops
problems with closing a program while not closing opened files
printf("\n File stream closed with fclose()\n"); // just to
verify the program is done and files closed
return 0;
}
The result of running the program above
is displayed below:
nice formatting!
ReplyDelete