Pages

Wednesday, February 5, 2014

Lab 2


Question #3

(A)   36000 m long
        1Gbps speed
        10MB size

        D = 36000/299792458
           = 0.00012 ms
        M= 10MB
           =83886080 bits
        R=1Gbps = 1000000000 bps

        L= (M/R)+D
          = (83886080/1000000000)+0.00012
          =0.084+0.00012
          =0.08412 ms

(B)    2 m long
         10 bps speed

        D= 2/299792458
           =0.00000667 ms
        M=83886080 bits
        R=10 bps
        L=(8388608/10)+0.00000667
          =8388608+0.00000667
          =8388608





Question #4

Pipes:

Pipes are implemented in file system.Pipes are basically files with only two file offsets: one for reading and another for writing.writing to a file and reading from a pipe is strictly in a queue manner,therefore pipes are called FIFOs.
There are two types of pipes: anonymous pipes and named pipes.

There are several functions to implement a pipe:
1. a pipe must be created by the server this is done using the Createnamedpipe function(). the pipe name is a string with format : \\.\pipe\[pathname]pipename. the "." stands for the local machine name.

2. After a pipe has been created,the creating process(sever) must wait for clients to connect to the pipe.
the Connectnamedpipe() function connects to the named pipe by calling createfile(). after sucessfully  connecting,the server can read or write into the pipe.

3. Readfile() allows the clients to read from the pipe.
4.Writefile() allows the clients to write to the pipe.

Reading or writing from the pipe is usually done through a special thread created by the server after each connection with a new client.This way the server can create a new instance of the pipe and wait for another client to connect,while the threads are called client threads. Each instance of the pipe will have its own pipe handle.which has to be passed to the client thread.
 5. After a pipe has been created at the server side, a client process can connect to the pipe by calling the createfile() function.

Shared Memory:

Two or several processes can map a segment or their virtual space into an identical segment of physical memory. shared memory is the most effecient way of IPC,but it may require synchronization.

System calls:
1. shmget(key,size,flags);
    int sid;
    long key;
    int size;
    int flags;

this function creates a shared memory segment.

2. shmdt(sid,addr,flags);
    char *p;
    void addr;

this function attaches the shared memory segment,that is, it gets the shared memory segment address.

3. shmdt(p);

this funtion detaches the shared memory segment without destroying it.

4. shmctl(sid,cmd,sbuf);
    int cmd;

this function allows you to control the share memory segment.


Question 5.

Fread()

The fread function is a subroutine that copies the number of data specified by the numberofitems parameter from the input stream into an array beginning at the location pointed to by the pointer parameter. each data item has the form *pointer. The fread subroutine stops copying bytes it an end of file (eof) or error condition is encountered while reading from the input specified by the numberofitems parameter have been copied. This subroutine leaves the file pointer of the stream parameter,if defined, pointing to the byte following the last byte read. the fread subroutine does not change the contents of the stream parameter.

example:

#include <studio.h>
#include<string.h>

int main()
{
File *f;
char t[]=" this is a networking class";
char buffer[20];
f=fopen(file.txt", "x+');
fseek(f, set,0);
fread(buffer, length(t)+1, 1,f);
printf("%s\n",buffer);
fclose(f);

return(0);
}

Fwrite()

The fwrite subroutine writes items from the array pointed to by the pointer parameter to the stream pointed to by the stream parameter. each item's size is specified by the size parameter. the fwrite subroutine writes the number of items specified by the numberofitems parameter. the file position indicator for the stream is advanced by the number of bytes successfully written. if an error occurs, the resulting value of the file position indicator for the stream is intermediate. the fwrite subroutine appends items to the output from the array pointed to by the pointer parameter. the fwrite subroutine appends as many items as specified in the numberofitems parameter. the fwrite subroutine stops writing bytes if an error condition is encountered on the stream, or when the number of items of data specified by the numberofitems parameter have been written. the fwrite subroutine does not change the contents of the array pointed to by the pointer parameter.

examples:

#include <studio.h>
#include<string.h>

int main()
{
File *f;
char t[]=" this is a networking class";
char buffer[20];
f=fopen(file.txt", "x+');
fwrite(t, length(t) + 1,1, f);
fseek(f, set,0);
printf("%s\n",buffer);
fclose(f);

return(0);
}
                                                                                     

1 comment:

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