Question

Part 1 Write a program where a child is created to execute command that tells you...

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

Homework Answers

Answer #1

Working code implemented in C and appropriate comments provided for better understanding.

Here I am attaching code for all files:

  • Process_P1.c
  • Process_P2.c
  • Parent_Process.c

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;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write Linux command lines to do the following: 1. Create a directory with name “yourName_ID” 2....
Write Linux command lines to do the following: 1. Create a directory with name “yourName_ID” 2. Create a file with name “yourLastName.txt” inside the directory created in step.1 3. Write inside the created file exactly the following content (you may use editors such as vim, pico, ….): Linux Lab Lab. ToDo#1 Summer Semester 2019/2020# 4. Display the first 2 lines of the file. 5. Change the permission of the file to read write and execute for owner, groups and others....
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
You must write a function that works like a small editor. It should open an existing...
You must write a function that works like a small editor. It should open an existing file (let us say this is File1.txt. This file must exist before the function is run), read it line by line. Then write it to another file (let us say this is File2.txt. This file need not exist since Python will create it). However, if a certain line exists in the first file, it should be replaced by another line supplied by you. You...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT