Pages

Wednesday, February 5, 2014

Christopher Francis - Lab #2 (Questions 3-5)

Christopher Francis
CS 461

                                                                                Lab #2

3a) 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?
D = 36,000 / 299,792,458 = .12ms
M = 10mb = 83,886,080b
R = 1 Gbps = 1,000,000,000bps
L = M/R + D
L = (83,886,080) / (1 * 10^9) + 0.00012
   = .08412ms
   

3b) Repeat this question for a 2m long link with 10bps transmission rate.  Assume that the propagation speed is equal to the speed of light.

D = 2 / 299,732,458 = .00000667ms
M = 83,886,080b
R = 10bps
L = (83,886,080 / 10) + .00000667
   = 8,388,608ms


4) We will start working on a group project that implement “Interprocess communication using pipes”.  Pipes are 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.

Pipes: A two-way data stream interfaced through standard input and output and is read character by character. Pipes are normally used when two processes want to run in parallel, with data moving from one process to the other in a single direction. Pipes are usually read and written in C.
The functions that Pipes implements are: int pipe, popen(), pclose(), fwrite() & fread() and they are accessible once the library header <stdio.h> is used.

int pipe: This function creates the actual pipe. After the pipe is created the function puts the file descriptors for the reading and writing of the end of the pipe into two int fildses, fildes[0] and fildes[1]. fildes[0] is known as the child and fildes[1] is known as the parent. If an error occurs a return value of -1 will be used, if successful 0 is returned.
popen(): This function is used as a way to make a subprocess and creates a direct link between this new subprocess and the corresponding link connecting to the pipe. This is done by creating a shell command. This function takes two arguments. The first argument is a pointer to a string containing name of the file to be opened while the second argument is the mode in which the file is to be opened.
pclose(): This function is used to close a stream created by popen(). It waits for the created child process to get terminated and then returns its status value.
fread() and fwrite(): Will be described in question #5

Shared Memory: Shared memory is an alternate method in implementing IPC. Like other methods, shared memory is a type of IPC where the two processes share same memory chunk and use it for IPC. With shared memory, one process writes into that memory while the other reads it. Shared memory uses the header library <sys/shm.h> and uses the functions shmget(), shmctl(), shmat() & shmdt().

shmget(): This function allows processes to access the created shared memory.
shmctl(): This function performs the control operation specified by cmd on the shared memory segment whose identifier is given in shmid.
shmat(): This function attaches the shared memory segment identified by shmid to the address space of the calling process.
shmdt(): This function detaches the shared memory segment located at the address specified by shmaddr from the address space of the calling process.

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(): The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr. The function fread takes four parameters: a memory address, the number of bytes to read per block, the number of blocks to read and the file variable.
It uses the code: size_t fread(void *ptr, size_t size, size_t count, FILE *stream);

fwrite(): The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.
It uses the code: size_t fwrite(void *ptr, size_t size, size_t count, FILE *stream);

Sample Code:
#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;
}


Source: Thegeekstuff

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.