[Solved] COEN146L-Lab 2 Multithreading in C and File transfer

$25

File Name: COEN146L-Lab_2_Multithreading_in_C_and_File_transfer.zip
File Size: 489.84 KB

SKU: [Solved] COEN146L-Lab 2 Multithreading in C and File transfer Category: Tag:
5/5 - (1 vote)
Multithreading in C Copying files

Problem: Write a C program to copy binary/ text files simultaneously.

Analysis:

  • Input: pass file names as arguments to main(), so your main function needs to be defined as follows:

int main(int argc, char * argv[])

Your files: src.dat and dest.dat files.

  • File reading can be accomplished by using either:
    • Functions: fopen, fwrite, and fread for binary files or fprintf and fscanf for text files
      • FILE *fopen(const char *filename, const char *mode)
      • fwrite( ptr, int size, int n, FILE *fp ); or fprintf() (for text files)
      • fread( ptr, int size, int n, FILE *fp ); or fscanf() (for text files)
      • fclose(ptr);

e.g.

FILE *fp;

fp = fopen(src.dat,r);

fp = fopen(dest.dat,w);

fwrite(&buf,sizeof(buf),1,fp);

fread(&buf,sizeof(buf),1,fp);

fclose(fp);

OR

  • System calls: open, read, write
    • int open (const char* Path, int flags [, int mode ]);
    • size_t read (int fd, void* buf, size_t cnt);
    • size_t write (int fd, void* buf, size_t cnt);

e.g.:

int fd = open(foo.txt, O_RDWR);

int nw = write(fd, buf, strlen(buf));

int nr = read(fd, buf, 40);

close (fd);

You need to include the following libraries:

  • #include<sys/types.h>
  • #include<sys/stat.h>
  • #include <fcntl.h>

You may create files of random data with different sizes/ bytes. You may use cat and head commands ( i.e. $cat /dev/random | head -c <bytecount>). /dev/random are special files that serve as pseudorandom number generator. cat is used to display the content and head is used to display the specified number of lines. So the result of cat is sent to the upstream end of PIPE and head receives these results and redirects the content of the specified bytes to a file.

$cat /dev/random | head -c 100000 > src1.dat creates a file with 100KB

$cat /dev/random | head -c 1000000 > src2.dat creates a file with 1MB

Check the size of the files with the command ls -la

  • Write your C program to copy files (binary and text) using functions, compile, debug, run, and test
  • Write your C program to copy files (binary and text) using system calls, compile, debug, run, and test
  • Calculate the time taken to copy files for both step 1 and 2. You may useclock()function (in h library). Call the clock function at the beginning and end of the code to measure the time, subtract the values, and then divide by CLOCKS_PER_SEC (the number of clock ticks per second) to get processor time. You may use the following code snippet:

#include <time.h> clock_t start, end; double cpu_time_used; start = clock(); /* Time consuming process. */ end = clock(); cpu_time_used = ((double) (end start)) / CLOCKS_PER_SEC;

  • Write your C program to copy multiple files (say 10 files) by creating threads, each of which will be responsible of copying one file. Use good style practices, compile, debug, run, and test for large sizes of matrices.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] COEN146L-Lab 2 Multithreading in C and File transfer
$25