Part 1
Write a program where a child is created to execute command that tells you the date and time in
Unix. Use execl(...). Note: you need to specify the full path of the file name that gives you date and time information. Announce the successful forking of child process by displaying its PID.
Part 2
Write a program where a child is created to execute a command that shows all files (including hidden files) in a directory with information such as permissions, owner, size, and when last modified. Use execvp(...). Announce the successful forking of child process by displaying its PID.
Part 3
[Step 1] Process_P1.c:
Create two files namely, destination1.txt and destination2.txt with read, write and execute permissions.
[Step 2] Process_P2.c: Copy the contents of source.txt into destination1.txt and destination2.txt as per the following procedure:
1. Read the next 50 characters from source.txt, and among those characters write chars 5 and 8 only to destination1.txt
2. Then the next 100 characters are read from source.txt and written in destination2.txt.
C program
Need Process_P2 only
Working code implemented in C and appropriate comments provided for better understanding.
Here I am attaching code for all files:
Process_P1.c:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void)
{
int fd_1, fd_2;
errno = 0;
// Opens and creates "destination1.txt" with
read-write permissions
fd_1 = open("destination1.txt", O_CREAT | O_RDWR,
S_IRWXU);
if (fd_1 == -1) {
printf("Failed to open with error
[%s]\n", strerror(errno));
}
else {
printf("Destination 1:
Opened\n");
}
close(fd_1);
// Opens and creates "destination2.txt" with
read-write permissions
fd_2 = open("destination2.txt", O_CREAT | O_RDWR,
S_IRWXU);
if (fd_2 == -1) {
printf("\n Failed to open with
error [%s]\n", strerror(errno));
}
else {
printf("Destination 2:
Opened\n");
}
close(fd_2);
return 0;
}
Process_P2.c:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void)
{
int src, rd, acs, destination1, destination2;
src = open("source.txt", O_RDONLY);
destination1 = open("destination1.txt", O_CREAT |
O_RDWR, S_IRWXU);
destination2 = open("destination2.txt", O_CREAT |
O_RDWR, S_IRWXU);
if (src == -1 && destination1 == -1
&& destination2 == -1)
{
printf("Error: [%s]\n",
strerror(errno));
}
else{
// Array of index size 101
allows it to read through 100 characters
char buffer[101];
int n = 101;
while ( (rd = read(src, buffer, n)) > 0){
if (n == 101) {
for (int i = 0; i < sizeof(buffer);
i++){
// Checks first 100
characters. If a '1' is found, it will be replaced with an 'A' in
destination1.txt
if (buffer[i] == '1')
buffer[i]
= 'A';
}
write(destination1, buffer, rd);
n = 51;
}
else{
for (int i = 0; i < sizeof(buffer);
i++){
if (buffer[i] == '2')
buffer[i]
= 'B';
}
write(destination2, buffer, rd);
n = 101;
}
}
}
close(src);
close(destination1);
close(destination2);
return 0;
}
Parent_Process.c:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void)
{
int pid_1, pid_2;
// Child 1 created
pid_1 = fork();
if(pid_1 == 0) {
// Child 1 Process
printf("Child 1\n");
char *const argv[] = {"Process_P1",
NULL};
execv(argv[0], argv);
printf("EXECV Failed! Did you
compile Prcs_P1.c yet? \n");
}
else {
// 1 second wait time before
processing second child
sleep(1);
pid_2 = fork();
if (pid_2 == 0) {
// Child 2
Process
printf("Child
2\n");
char *const
argv[] = {"Process_P2", NULL};
execv(argv[0], argv);
printf("EXECV
Failed! Did you compile Process_P2.c yet? \n");
}
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.