Lab - 7 by Anees
Lab - 7 by Anees
COPYING
LAB # 07
Fall 2024
CSE-302L
Systems Programming Lab
“On my honor, as student of University of Engineering and Technology, I have neither given
nor received unauthorized assistance on this academic work.”
Submitted to:
Dr. Madiha Sher
Friday, November 29, 2024
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:
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]);
}
return 0;
}
Output:
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;
}
FD_SET(file1, &readset);
FD_SET(file2, &readset);
if (FD_ISSET(file2, &readset))
printf("The file: %s with FD = %d is ready.\n",
argv[2], file2);
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>
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>
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;
}
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(readFd, &readSet);
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.
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>
printf("Starting program...\n");
Output: