0% found this document useful (0 votes)
6 views12 pages

Lab - 7 by Anees

This document outlines the structure and requirements for Lab #07 of the CSE-302L Systems Programming Lab at the University of Engineering and Technology. It includes a declaration of academic integrity, assessment rubrics for various criteria, and detailed programming tasks involving file copying, monitoring files, and implementing delays using the select function in C. The document is submitted by a student and includes code examples for each task.

Uploaded by

mranees5323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Lab - 7 by Anees

This document outlines the structure and requirements for Lab #07 of the CSE-302L Systems Programming Lab at the University of Engineering and Technology. It includes a declaration of academic integrity, assessment rubrics for various criteria, and detailed programming tasks involving file copying, monitoring files, and implementing delays using the select function in C. The document is submitted by a student and includes code examples for each task.

Uploaded by

mranees5323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MULTIPLE FILE

COPYING

LAB # 07

Fall 2024

CSE-302L
Systems Programming Lab

Submitted by: Anees Ur Rehman


Registration No.: 22PWCSE2168
Class Section: B

“On my honor, as student of University of Engineering and Technology, I have neither given
nor received unauthorized assistance on this academic work.”

Student Signature: ______________

Submitted to:
Dr. Madiha Sher
Friday, November 29, 2024

Department of Computer Systems Engineering


University of Engineering and Technology, Peshawar
CSE 302L: SYSTEMS PROGRAMMING LAB

LAB ASSESSMENT RUBRICS

Criteria & Point Outstanding Acceptable Considerable Below Score


Assigned 2 1.5 1 Expectations
0.5
Attendance and Attended in Attended in Attended late but Attended late not
Attentiveness in Lab proper Time and proper Time but attentive in Lab attentive in Lab
PLO08 attentive in Lab not attentive in
Lab

Capability of writing Right attempt/ no Right attempt/ Right attempt/


Program/Algorithm/Drawing errors and well no errors but not minor errors and Wrong attempt
Flow Chart formatted well formatted not well
PLO1, PLO2, PLO3, PLO5 formatted

Result or Output/ 100% target has 75% target has 50% target has None of the
Completion of target in Lab been completed been completed been completed outputs are
PLO9 and well and well but not well correct.
formatted. formatted. formatted.

Overall, Knowledge Demonstrates Demonstrates Has partial idea Has poor idea
PLO10, excellent good knowledge about the Lab and about the Lab and
knowledge of lab of lab procedure procedure
followed followed

Attention to Lab Report Submission of Lab Submission of Late Submission Late Submission
PLO4, Report in Proper Lab Report in with proper very poor
Time i.e., in next proper time but documentation. documentation
day of lab, with not with proper
proper documentation.
documentation.

Instructor:

Name: _________________________ Signature: ________________________


Multiple File Copying
Tasks:
Task 1: Write a program that copies two files sequentially in a single process.
Code in C:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int readWrite(const char *src, const char *dest) {


// Code to read from src and write to dest
// Return 0 on success, negative on failure
FILE *sourceFile = fopen(src, "rb");
FILE *destFile = fopen(dest, "wb");
if (sourceFile == NULL || destFile == NULL) {
perror("File open error");
return -1;
}

char buffer[1024];
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer),
sourceFile)) > 0) {
fwrite(buffer, 1, bytes, destFile);
}

fclose(sourceFile);
fclose(destFile);

return 0;
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
fprintf(stderr, "Usage: %s FILE_1 FILE_2
[FILE_N...]\n", argv[0]);
return 1;
}

if (argc % 2 == 0)
{
perror("Argument must be even in count.\n");
return 1;
}

// int readWriteReturnValue;
for (int i = 1, readWriteReturnValue; i < argc; i += 2)
{
readWriteReturnValue = readWrite(argv[i], argv[i +
1]);
if (readWriteReturnValue < 0)
{
perror("Something went wrong while reading or
writing a file.\n");
return 1;
}
printf("Copied from SRC: %s to DIST: %s.\n", argv[i],
argv[i + 1]);
}

printf("Sequential copy completed.\n");

return 0;
}
Output:

Task 2: Write a program that monitors two files using 'select'.


Code in C:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>

int readWrite(const char *src, const char *dest) {


// Code to read from src and write to dest
// Return 0 on success, negative on failure
FILE *sourceFile = fopen(src, "rb");
FILE *destFile = fopen(dest, "wb");
if (sourceFile == NULL || destFile == NULL) {
perror("File open error");
return -1;
}

char buffer[1024];
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer),
sourceFile)) > 0) {
fwrite(buffer, 1, bytes, destFile);
}

fclose(sourceFile);
fclose(destFile);

return 0;
}

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


{
if (argc != 3)
{
fprintf(stderr, "Usage: %s FILE_1 FILE_2\n", argv[0]);
return 1;
}

// 1. Opening the two files.


int file1 = open(argv[1], O_RDWR);
if (file1 == -1)
{
fprintf(stderr, "Something went wrong while opening
the file: %s due to %s\n", argv[1], strerror(errno));
return 1;
}

int file2 = open(argv[2], O_RDWR);


if (file2 == -1)
{
fprintf(stderr, "Something went wrong while opening
the file: %s due to %s\n", argv[2], strerror(errno));
return 1;
}

// 2. Collecting arguments for select function.


int maxFd = file1 >= file2 ? file1 : file2;
fd_set readset;
FD_ZERO(&readset);

FD_SET(file1, &readset);
FD_SET(file2, &readset);

// calling the function select.


int readyFile = select(maxFd + 1, &readset, NULL, NULL,
NULL);

// Checking for ready file(S):


if (FD_ISSET(file1, &readset))
printf("The file: %s with FD = %d is ready.\n",
argv[1], file1);

if (FD_ISSET(file2, &readset))
printf("The file: %s with FD = %d is ready.\n",
argv[2], file2);

// Closing the files:


if (close(file1) == -1)
{
fprintf(stderr, "Something went wrong while closing
the file: %s due to %s\n", argv[1], strerror(errno));
return 1;
}

if (close(file2) == -1)
{
fprintf(stderr, "Something went wrong while closing
the file: %s due to %s\n", argv[2], strerror(errno));
return 1;
}

return 0;
}
Output:
Task 3 : Write a program that monitors N files using 'select'
Code in C:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <time.h>

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


{
if (argc < 2)
{
fprintf(stderr, "Usage: %s FILE_1 [FILE_N...]\n",
argv[0]);
return 1;
}

int length = argc - 1;


int fds[length];

for (int i = 0; i < length; i++)


{
fds[i] = open(argv[i + 1], O_RDWR);
if (fds[i] == -1)
{
fprintf(stderr, "Something went wrong while
opening the file: %s due to %s\n", argv[i + 1],
strerror(errno));
return 1;
}
}
fd_set readSet;
FD_ZERO(&readSet);

int max = findMax(fds, &length);

for (int i = 0; i < length; i++)


FD_SET(fds[i], &readSet);

int ret = select(max + 1, &readSet, NULL, NULL, NULL);

for (int i = 0; i < length; i++)


if (FD_ISSET(fds[i], &readSet))
printf("The file: %s with FD = %d is ready.\n",
argv[i + 1], fds[i]);

for (int i = 0; i < length; i++)


if (close(fds[i]) == -1)
{
fprintf(stderr, "Something went wrong while
closing the file: %s due to %s\n", argv[i + 1],
strerror(errno));
return 1;
}

return 0;
}
Output:

Task 4: Write a program that creates two child processes, both child read from the
same source and writes to two separate destination, make sure you use select to
monitor if the source is ready to be read from.
Code in C:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/wait.h>

int findMax(int arr[], int *length)


{
int max = arr[0];
for (int i = 1; i < *length; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}

int readWrite(const char *src, const char *dest) {


// Code to read from src and write to dest
// Return 0 on success, negative on failure
FILE *sourceFile = fopen(src, "rb");
FILE *destFile = fopen(dest, "wb");
if (sourceFile == NULL || destFile == NULL) {
perror("File open error");
return -1;
}

char buffer[1024];
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), sourceFile)) >
0) {
fwrite(buffer, 1, bytes, destFile);
}

fclose(sourceFile);
fclose(destFile);

return 0;
}

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


{
if (argc <= 3)
{
fprintf(stderr, "Usage:\n%s READ_FROM_FILE_1 WRITE_TO_FILE_1
WRITE_TO_FILE_2 [WRITE_TO_FILE_N...]\n", argv[0]);
return 1;
}

int readFd = open(argv[1], O_RDONLY);


if (readFd == -1)
{
fprintf(stderr, "Cannot open source file %s: %s\n", argv[1],
strerror(errno));
return 1;
}

for (int i = 2; i < argc; i++)


{
pid_t pid = fork();
if (pid == -1)
{
perror("Failed to create child process.\n");
return 1;
}
else if (pid == 0)
{ // Child
int writeFd = open(argv[i], O_WRONLY | O_TRUNC |
O_CREAT, S_IRWXU | S_IRGRP | S_IROTH);
if (writeFd == -1)
{
fprintf(stderr, "Cannot open destination file %s:
%s\n", argv[i], strerror(errno));
return 1;
}

fd_set readSet;
FD_ZERO(&readSet);
FD_SET(readFd, &readSet);

// Child process has to select on the readFd again


because file descriptors are shared.
if (select(readFd + 1, &readSet, NULL, NULL, NULL) > 0)
{
if (FD_ISSET(readFd, &readSet))
if (readWriteOnly(&readFd, &writeFd) == -1)
{
fprintf(stderr, "Error reading/writing
files.\n");
return 1;
}
}
else
{
perror("Error while monitoring files.\n");
return 1;
}

if (close(writeFd) == -1)
{
fprintf(stderr, "Something went wrong while closing
the file: %s due to %s\n", argv[i], strerror(errno));
return 1;
}
printf("Process -> %d, %d.\n", i, writeFd);
return 0; // Child process exits after writing.
}
}

// Parent process
for (int i = 2; i < argc; i++)
wait(NULL); // Wait for all children to finish.

// Close the source file descriptor in the parent as well.


if (close(readFd) == -1)
{
fprintf(stderr, "Something went wrong while closing the
file: %s due to %s\n", argv[1], strerror(errno));
return 1;
};

printf("Reading from file: %s and writing to other files


completed.\n", argv[1]);
return 0;
}

Output:

Task 5: Write a function that creates a delay of N seconds using select function.
Pass N as an argument to the function
Code in C:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <time.h>

int doDelay(int sec, int microSec)


{
struct timeval t;
t.tv_sec = sec;
t.tv_usec = microSec;

int ret = select(1, NULL, NULL, NULL, &t);


return 0;
}

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


{
if (argc != 3)
{
fprintf(stderr, "Usage: %s <seconds> <micro_seconds>.\n",
argv[0]);
return 1;
}

printf("Starting program...\n");

int ret = doDelay(atoi(argv[1]), atoi(argv[2]));

printf("Continuing after the delay of sec: %d:%d.\n",


atoi(argv[1]), atoi(argv[2]));
return 0;
}

Output:

You might also like