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 a C program that creates (or spawns) a child process to execute the Unix's command...
Write a C program that creates (or spawns) a child process to execute the Unix's command (echo) with a string as the command-line argument; while the program itself (the parent process) will execute another Unix command less with a filename as the command-line argument. If you aren't sure how the echo and less commands are meant to behave, experiment with them by running them directly from the command line first.
drwxr-xr-x 10 mst staff 320 Sep 18 14:58 Sites drwxr-xr-x 2 mst staff 64 May 28...
drwxr-xr-x 10 mst staff 320 Sep 18 14:58 Sites drwxr-xr-x 2 mst staff 64 May 28 2017 VirtualBox VMs -rw-r--r-- 1 mst staff 633 Dec 11 2019 balance.pl drwxr-xr-x 4 mst staff 128 Oct 11 2014 bin -rw-r--r-- 1 mst staff 473 Dec 13 2019 bitshift.py For this problem, we are mainly concerned with the set of access permissions, which appears at the beginning of the line for each file or directory. Directories have a "d" at the beginning of...
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...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
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...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
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,...