COEN146L – COEN 146: Computer Networks (Solution)

$ 24.99
Category:

Description

Lab 2: Multithreading in C and File transfer

Objectives
1. To develop multithreading in C
2. To write C programs for copying files

Guidelines
For all COEN 146L you need to develop a good knowledge of C in a Linux development environment. You are highly encouraged to use command line tools in developing, compiling, and running your programs.

Please pay attention to your coding style and good programming practices, if your program is not worth documenting, it will not worth running.

Skills in multithread programming is required for developing client-server applications, as a way to create parallelism. For example, a server spawns a separate thread for every client connection to look after the specific client requests independently. A thread is a single sequence stream within in a process, and it is often referred to as a lightweight process. Threads operate faster than processes during their creation and termination, context switching, and communication. Threads are not independent like processes and they share with other threads their code and data, open file descriptors. Threads maintain their own program counters, registers, and stack.

Recall from Lab 1 the following library function are used for setting up threads.
#include <pthread.h> int pthread_create(pthread_t *thread, pthread_attr_t *attr,
void *(*start_routine) (void *arg), void *arg);
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: o 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

o System calls: open, read, write
 int open (const char* Path, int flags [, int mode ]);
COEN 146 – Lab 2 1/2
 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>
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”
Step 1. Write your C program to copy files (binary and text) using functions, compile, debug, run, and test
Step 2. Write your C program to copy files (binary and text) using system calls, compile, debug, run, and test
Step 3.

#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;
Step 4. 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.

Requirements to complete the lab

Be sure to retain copies (machine and/or printed) of your source code. You will want these for study purposes and to resolve any grading questions (should they arise)
COEN 146 – Lab 2 2/2

Reviews

There are no reviews yet.

Be the first to review “COEN146L – COEN 146: Computer Networks (Solution)”

Your email address will not be published. Required fields are marked *