Pages

Wednesday, February 5, 2014

Question no#04:
Using an IPC means inter process communication to the question share one person to another person is called Pipe
Pipes<stdio.h>
Int pipe(int fd[2]);
fd: file descriptor.
popen(): A  person has been started  in program a new process started data from that location
FILE* popen(char*command, char*type);
returns a pointer to a NULL.
Pclose(): The function is used  by popen()
Int  pclose(FILE*stream_to_close);
Shared Memory<sys/shm.h>
This memory one person created and accesses other person
Shmget(): To allows in a process shared memory.
Int shmget(key_t key,size_t size,int shmflg);
Shmget(): Share memory modifiy & permission
Int shmctt(int shmid,int cmd,struct shmid_ds*buf);
Shmat(): Memory using segments.
Void*shmat(int shmid,const void*shmaddr,int shmflg);
Shmdt(): For detach memory segments.
Shmdt(const void *shmaddr):
Print(“Bytes written: % s\n”,message);
 return0;
}



Lab 2. Questions 3,4 and 5

Tiara Barias
Lab 2
February 5, 2014

# 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.

length of transmission= 36,000m
speed of transmission= 1 Gbps
propagation= 3.0*10^8 m/s which equals 299,792,458 m/s
Propagation Delay formula,also represented by (D) formula as shown in the video notes: length/ speed of light also represented by (C)

D= 36,000/299.792,458 m/s
  =0.12 m/s
transmission delay formula: M/R + D
M= bits
R= rate
rate= 1 Gbps= 10^9=84 ms
Transmission delay: 10*2^20*8/10^9=84 ms
Latency formula= transmission delay + propagation delay
Latency= 0.12+84= 84.12 ms

Part two of same question:
length of transmission= 2m
speed of transmission= 10bps
propagation speed of light= 3.0*10^8 m/s which equals 299,792,458 m/s
Propagation Delay formula,also represented by (D) formula as shown in the video notes: length/ speed of light also represented by (C)

D=2/299,792,458 ms
  =6.67*10^6 ms
transmission delay= 10*2^20*8/10= 8.4*10^9 ms
latency= 6.67*10^-6+8.4*10^9= 8.4*10^9ms


#4) We will start working on a group project that implements “Interprocess communication using pipes”. Pipes is one of the ways to implement IPC. Write a blog that explains the functions needed to implement IPC using pipes and also those needed for communication using shared memory.


popen() : this begins another to send and receive data
pclose() : this ends the file stream that was opened
fread() : this is used to read data from the file opened by the popen
fwrite() : this is used to write data to the file
shmget() :  this allows us to accesses memory that is shared
shmctl() : this alters characteristics and permissions of a shared memory
shmat() :  this connects memory portions
shmdt() : this breaks memory portions



#5) fread and fwrite are C functions that can read bytes (instead of lines, or strings, char etc) from a file. Describe these functions and also write short programs that demonstrate them. 

fread: is used to read data from the file opened
fwrite: is used to write data to a file.
basically they do opposite jobs.

fread function: 
size_p fread(void *ptr, size_p size, size_p nmemb, FILE *stream);


fwrite function:

size_p fwrite(const void *ptr, size_p size, size_p nmemb, FILE *stream);












lab 2

3a
Transmission Delay:
m/r= 10mb/1gbps
10mb= 83886080bits
1gbps= 1000000000bits
m/r= 0.083886 ms

Propagation delay:
speed of light(C)= 299792458
2/3(c)= 199861639
36000/199861639= 0.000180126


Latency= 0.083886 + 0.000180126= 0.08406613

3b
Transmission Delay:
m/r= 10mb/10bps
10mb= 83886080bits
10bps= 10 bits
m/r= 838860

Propagation delay:
speed of light(C)= 299792458
2/3(c)= 199861639
2/199861639= 0.000000001000692


Latency= 0.000000001000692 + 838860= 838860.000000001


4

open- opens the function and makes the connection between a file and file descriptor.
connect- connects a function by attempting to connect to a socket
write- tries to write nbyte from the buffer pointed to the by buf to the file associated with the open file descriptor.
close- closes a file descriptor, that way it doesn't go back to any file and may be recycled.
read- tries to read to count bytes from file descriptor fd into the buffer starting at buf.
pipe- makes a pipe, a unidirectional daa channel that can be used for interprocess communication.







5 fwrite

#include <stdio.h>

struct clientData {
    int acctNum;
    char lastName[ 15 ];
    char firstName[ 10 ];
    double balance;
};


int main()
{
    FILE *cfPtr;
    struct clientData client = { 0, "", "", 0.0 };

    if ( ( cfPtr = fopen( "credit.dat", "r+" ) ) == NULL )
        printf( "File could not be opened.\n" );
    else {
        printf( "Enter account number "
               " ( 1 to 100, 0 to end input )\n? " );
        scanf( "%d", &client.acctNum );

        while ( client.acctNum != 0 ) {
            printf( "Enter lastname, firstname, balance\n? " );
            fscanf( stdin, "%s%s%lf", client.lastName, client.firstName, &client.balance );
            fseek( cfPtr, ( client.acctNum - 1 ) * sizeof( struct clientData ), SEEK_SET );
            fwrite( &client, sizeof( struct clientData ), 1, cfPtr );
            printf( "Enter account number\n? " );
            scanf( "%d", &client.acctNum );
        }

        fclose( cfPtr );
    }

    return 0;
}






fread

#include <stdio.h>

struct clientData {
    int acctNum;
    char lastName[ 15 ];
    char firstName[ 10 ];
    double balance;
};


int main()
{
    FILE *cfPtr;
    struct clientData client = { 0, "", "", 0.0 };

    if ( ( cfPtr = fopen( "credit.dat", "r" ) ) == NULL )
        printf( "File could not be opened.\n" );
    else {
        printf( "%-6s%-16s%-11s%10s\n", "Acct", "Last Name", "First Name", "Balance" );

        while ( !feof( cfPtr ) ) {
            fread( &client, sizeof( struct clientData ), 1, cfPtr );

            if ( client.acctNum != 0 )
                printf( "%-6d%-16s%-11s%10.2f\n",
                      client.acctNum, client.lastName,
                      client.firstName, client.balance );
        }

        fclose( cfPtr );
    }

    return 0;
}







lab #2


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? Assume that the propagation speed is equal to the speed of light.

latency = m/r +d

transmission delay is m/r
m= 10 mb
r= 1 gps
Next i converted m to bits which is 83886080 b
and also r to bits per second which is 1,000,000,000 bps

these values are then divided to equal 0.08388608
i then mutliply by 1,000 to convert this which is 83.88608 ms

propagation delay =d
36000/speed of light
36000/299792458 =  0.000120083
i then converted this to get 0.120083074ms

latency = m/r +d
83.88608 + 0.120083074 = 84.00616307 ms
---------
2m long link and 10bps transmission rate
  =  (10 * 1048576) * 8 / 10
          = 8388608s
         = 8388608000ms
propagation delay= 2/c(speed of light)

2/299792458 = 0.0000000066728 seconds 
 then converted is 6.6728x10^-6

l=m/r +d
8388608000 + 6.6728*10^-6
-------------------------------------------------------------------------------
4) We will start working on a group project that implements "Interprocess communication using pipes". Pipes is one of the ways to implement IPC. Write a blog that explains the functions needed to implement IPC using pipes and also those needed for communication using shared memory.

pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication
write() is used to attempt to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor
open() is able to open a function and establishes the connection between a file and a file descriptor
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
connect() can connect a function by trying to connect to socket.
close() closes a file descriptor, so that it no longer refers to any file and may be reused.
----------
shared memory is one method of interprocess communication whereby 2 or more processes share a single chunk of memory to cummunicate.
shmget() provides access to shared memory by providing a shared memory segment ID on a successful call.
shmctl() from a controlling user/owner that already has permission to access shared memory allows another application to get the same access
shmat() used to allow access to shared memory once an ID has been received.

---------------------------------------------
5)
#include <stdio.h>
#include <string.h>

int main()
{
   FILE *fp;
   char h[] = "khem";
   char buffer[20];

   // Open file for both reading and writing 
   fp = fopen("test.txt", "w+");

   // Write data to the file 
   fwrite(h, strlen(h) + 1, 1, fp);

   // Seek to the beginning of the file 
   fseek(fp, SEEK_SET, 0);

   // Read and display data 
   fread(buffer, strlen(h)+1, 1, fp);
   printf("%s\n", buffer);
   fclose(fp);
   
   return(0);
}

















lab 2 answers

3a.

Since there are 10485760 bytes in 10 megabytes and i did 1,000000000 for the gigabyte and when you divide them it will take .01048576 seconds for it to transfer a 10 mb file with a 1gb speed.
.001274  seconds with the length of 36000 and the speed of light at 299792458 m/s. Since the latency is both of them added up.001274+.01048576 = .01175976



3b.

With a 10bps rate and not a gigabyte rate the change is large with a 10 megabyte file still which is 10485760 bytes it will take 1048576 seconds which is around 291 hours.  with a 2m link and the speed still the same it becomes .000000006712819. The latency becomes 1048576 seconds.


4.
Piping is a process where the output of one process is made the input of another.  First you must create a pipe and then open the pipe like this FILE *popen(char *command, char *type) This is how you create a pipe int pipe(int fd[2]). Since shared memory is basically data transfer and transferring data is easier if the output and the input go hand in hand.  Therefore pipes are important for shared memory.

Credits to http://www.cs.cf.ac.uk/Dave/C/node23.html

5.

The functions fread/fwrite are used for reading/writing data from/to the file opened by fopen function. These functions accept three arguments. The first argument is a pointer to buffer used for reading/writing the data. The data read/written is in the form of ‘nmemb’ elements each ‘size’ bytes long. In case of success, fread/fwrite return the number of bytes actually read/written from/to the stream opened by fopen function. In case of failure, a lesser number of byes (then requested to read/write) is returned.

ex.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

#include<stdio.h>
#include<string.h>

#define SIZE 1
#define NUMELEM 5

int main(void)
{
    FILE* fd = NULL;
    char buff[100];
    memset(buff,0,sizeof(buff));

    fd = fopen("test.txt","rw+");

    if(NULL == fd)
    {
        printf("\n fopen() Error!!!\n");
        return 1;
    }

    printf("\n File opened successfully through fopen()\n");

    if(SIZE*NUMELEM != fread(buff,SIZE,NUMELEM,fd))
    {
        printf("\n fread() failed\n");
        return 1;
    }

    printf("\n Some bytes successfully read through fread()\n");

    printf("\n The bytes read are [%s]\n",buff);

    if(0 != fseek(fd,11,SEEK_CUR))
    {
        printf("\n fseek() failed\n");
        return 1;
    }

    printf("\n fseek() successful\n");

    if(SIZE*NUMELEM != fwrite(buff,SIZE,strlen(buff),fd))
    {
        printf("\n fwrite() failed\n");
        return 1;
    }

    printf("\n fwrite() successful, data written to text file\n");

    fclose(fd);

    printf("\n File stream closed through fclose()\n");

    return 0;
}
Credits to http://www.thegeekstuff.com/2012/07/c-file-handling/

Lab Assignment 2

3.)
  A)   length = 36,000
      Speed of signals = 10^9

Transmission Delay = M(bits)/R(bps)
10 MB = 83886080 bits / 1GB = 10^9 = 0.083 m/s



 Propagation Delay = D(seconds) so, formula for D = length / speed of signals.   36,000/10^9 =0.000036 m/s


Latency = M(bits)/R(bps)+D

10 MB = 83886080 bits / 1GB = 10^9 = 0.083 m/s , therefore 0.083+0.000036 = 0.084 m/s


B)
Transmission Delay = 10^6/10 =100000 m/s

Propagation Delay
 Length=                  2
                         ________ =  0.2 m/s

Speed of signal=     10 bps

Latency = 100000+0.2 = 100000.2 m/s

4.) IPC is a set of methods for the exchange of data among multiple threads.
the pipe() function provides a means of passing data between two programs and also allows to read and write the data.
#include<unistd.h>
int pipe(int file_descriptor[2]);
Return 0; // if an error Return -1;

Pipe Processing-
The process of passing data between two programs can be done with the help of popen() and pclose() functions.
#include<stdio.h>
FILE *popen(const char *command ,  
const char *open-mode);
int pclose(FILE *stream_to_close);
The popen function allows a program to declare another program as a new process and either writes the data to it or to read from it. To perform bi-directional communication you have to use two pipes.

By using pclose(), we can close the filestream associated with popen() after the process started by it has been finished. The pclose() will return the exit code of the process, which is to be closed.
Shared Memory Segment is a method of IPC that where two or more processes share a single chunk of memory to communicate.
#include<sys/shm.h>
void *shmat(int shm_id,
const void *shm_addr,int shmflg);

The first parameter is the shm_id,
identifier returned by the shmget.
The second parameter is the address at which the segment is to be attached to the current process. This will always be NULL pointer so that system can choose the address at which the memory is available.

The third parameter is the flag value. There are two possible flag values they are:
1.) SHM_RND-controls the address at which the segment is attached.
2.) SHM_RDONLY-makes the attachment read-only.
 


Source - http://developeriq.in/articles/2012/may/30/interprocess-communicationipc-programs-in-c-in-ubu/



5.) #include <stdio.h>

fread(void *buffer, size_t size, size_t num,FILE *fp);
{
.
.
.
return num;
}

 fwrite(void *buffer, size_t size, size_t num, FILE *fp);
{
.
.
.
 return num;
}

fread- reads a block memory or file and Fwrtites does the complete opposite of Fread.Fwrite creates a block into the file.

fp(stands for file pointer) is where you should write your file. Num is return because it is how many elements that were read and written. In Fread if
the number returned is  zero its either 1: no files has been read.
                               2: an error occured.

In Fwrite if the number of values written is less than num then an there is an error.

Lab #2

3) (b-bits, B-bytes)
Transmission Delay: (M/R)
M/R= 10mb(83886080 b)/1gbps(1000000000 b)
M/R= 0.083886 s


Propagation delay: (D)
speed of light
(C)= 299792458 m/s
2/3(c)= 199861639 m/s


length/((⅔)C): 36000m/199861639m/s = 0.000180126 s


Latency= (M/R)+D
0.083886 s + 0.000180126 s= 0.08406613 s

-----------------------------------------------------------------

Transmission Delay: (M/R)
M/R= 10mb(83886080 b)/10bps(10 b)
M/R= 838860 s


Propagation delay: (D)
speed of light(C)= 299792458 m/s
2/3(c)= 199861639 m/s
2m/199861639 m/s= 0.000000001000692 s


Latency= (M/R)+D
0.000000001000692 s + 838860 s = 838860.000000001 s

------------------------------------------------------------------


4)
Pipe is one type of interprocess communication that connects one or more clients to the server, through one or more processes. This enables communication or the transfer of data between server and client.



Pipe Functions:
  • pipe() - creates a pipe, a unidirectional data channel that can be used for interprocess communication
  • open() - opens a function and establishes the connection between a file and a file descriptor
  • connect() - connects a function by trying to connect to a socket.
  • write() - attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor
  • read() - attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
  • close() - closes a file descriptor, so that it no longer refers to any file and may be reused.

Shared memory is all about data transfer between programs. One side of the program will basically be about creating memory, and the other is all about reading it. This varies as compared to pipes, because pipes don’t have all the functionality that shared memory has. Shared memory gives the user more control and freedom over what is allocated and when.



Shared memory functions:


shmget() - returns the identifier of the shared memory segment associated with the value of the argument key.
shmat() - attaches the shared memory segment identified by shmid to the address space of the calling process. 
shmdt() - attaches the shared memory segment identified by shmid to the address space of the calling process
shmctl() - performs the control operation specified by cmd on the shared memory segment whose identifier is given in shmid.




5)


/*
*
fwrite example code
*
*/

  • The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects that were written.


  • To read the file, use fread.


#include <stdio.h> /* including standard library */
struct CD_COLLECTION {
 char album_name[42];
 int year;
 char artist[42];
};
int main (void)
{
 FILE *stream = fopen ("collection.bin","wb");
 struct CD_COLLECTION collection[5];
 unsigned short i = 0;

 for (i=0; i < 5 ; i++) {
 
 sprintf(collection[i].album_name, "Interpret %i", i);
  collection
[i].year=2012;
 
 sprintf(collection[i].artist,"Mr Unrockable %i", i);
  
}

 fwrite (collection, sizeof(struct CD_COLLECTION), 5, stream);
 fclose (stream);
 return 0;
}
/*
* fread example code
*
*/

  • fread reads num number of objects from a stream (where each object is size bytes) and places them into the array pointed to by buffer. The return value of the function is the number of read data

  • To generate the binary file, you have to use fwrite.


#include <stdio.h> /* including standard library */
struct CD_COLLECTION {
 char album_name[42];
 int year;
 char artist[42];
};
int main (void)
{
 FILE *stream = fopen ("collection.bin","rb");
 struct CD_COLLECTION collection[5];
 unsigned short i = 0;

 fread (collection, sizeof(struct CD_COLLECTION), 5, stream);
 for (i=0; i<5 ; i++) {
 printf("Album Name: %s\nYear %i\nArtist: %s\n\n",collection[i].album_name, collection[i].year, collection[i].artist);
 
}

 fclose (stream);
 return 0;
}