Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN THE UNIX OS BUT USING C, NOT C++. Please don\'t use answers already on chegg.

ID: 3735326 • Letter: I

Question

IN THE UNIX OS BUT USING C, NOT C++. Please don't use answers already on chegg. Thank you in advance

This assignment covers the creation of processes using C and communicating between processes using unnamed pipes. You are to write a program that creates one child process. .On the command line, when you execute your program, I will specify the path of a single directory as the single argument to your program. each file in that directory. Your main program will spawn a child process. Your main program will examine this directory. It will need to get the status record for Your main program will open a new report file for writing. .Your main program will send file status entries, one at a time to the child process. At the end of the set of data, send the child process a status record with the inode value set to 0 so the child knows when the end of the records has beer reached. OR have the child wait for a few 5 second periods. If it receives no new buffer data after a few cycles, then time-out and complete the child tasks, exit. o o The child process will collect statistics on the files sent to it by the main program: o o o o how many regular files are there? how many directories are there? What is the total filesize in bytes? What is the total file storage allocation in blocks? .The child process will write these statistics to the report file that was opened by main. The main program will wait for the child to complete and then echo the report file's

Explanation / Answer

unnamed pipe :-

we can construct unnamed pipes with the help of pipe system call.

pipe system call :- pipe system call returns two integer file descriptors, filedes[0] and filedes[1] .
The file descriptors reference two data streams. Historically, pipes were unidirectional, and data flowed in one direction only. If two-way communication was needed, two pipes were opened: one for reading and another for writing. This is still true in Linux today. However, in some versions of UNIX (such as Solaris) the file descriptors returned by pipe are full duplex (bidirectional) and are both opened for reading/writing .

In a full duplex communication if the process writes to filedes[0] , then filedes[1] is used for reading. otherwise, the process writes to filedes[1] , and filedes[0] is used for reading. In a half duplex setting (such as in Linux) filedes[1] is always used for writing, and filedes[0] is always used for reading an attempt to write to fildes[0] or read from filedes[1] will produce an error is called bad file descriptor.

Returned value :- If successful, pipe() returns 0.
If unsuccessful, pipe() returns -1 and sets errno (error value).

ERROR CODES :-

ENFILE     Opening the pipe would exceed the number of files that the system can have open simultaneously.

EMFILE     Opening the pipe would exceed the limit on the number of file descriptors the process can have open. This limit is given by OPEN_MAX, defined in the limits.h header file.
ENOMEM     Opening the pipe requires more space than is available.


//this program uses 2 pipe system calls .

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#define MAX 4096

    char command[MAX];
    char choice1[] = "display";
    char choice2[] = "chars";
    char choice3[] = "lines";
    char choice4[] = "words";
    char choice5[] = "find";
    char choice6[] = "exit";
    char filename[50] = "";
    char line[255];
    char x;
    int ch, c;
    int count = 0;
    int words = 0;
    int l = 0;
    FILE *fp;
    FILE *in_file;
    FILE *pFile;
    FILE *wordcount;

int main(void){

    int pid;    //allows parent and child to identify themselves
    int p[2];   //Pipe 'p'
    int q[2];   //Pipe 'q'
   
    //create 2 pipes
    if (pipe(p) < 0 || pipe(q) < 0){
    puts("Cannot create pipe");
    exit(0);
    } // end if pipe fails
   
    pid = fork();
   
    if (pid > 0){ //in parent process
    close (p[0]);
    close (q[1]);
   
    menu(q[0], p[1]);
   
    close (p[1]);
    close(q[0]);
    exit(0);  
    } //end if in parent
   
    else{ //in child
    close (p[1]);
    close (q[0]);

    execution(p[0], q[1]);
   
    close (p[0]);
    close (q[1]);
    exit(0);
    } //end if in child
} // end main program


int menu (int argc, char *argv[]){

    printf("------------------------------------------ ");
    printf("Please choose from the following commands: ");
    printf("display ");
    printf("chars ");
    printf("lines ");
    printf("words ");
    printf("find ");
    printf("exit ");
    printf("------------------------------------------ ");
   
    printf("Please enter a command: ");
    scanf("%s" "%s", command, filename);
   
    printf(" The command is: %s %s ", command, filename);

    return(0);
}

int execution (int argc, char *argv[]){

if(strcmp(choice1, command) == 0){
   
       printf("Executing command: %s %s ", command, filename);
       printf("================= ");
       printf(" File contents are: %s ", filename);
      
       fp = fopen(filename, "r");
       if((fp == NULL))
       {     
       printf("No file was selected! ");
       exit(1);     
       }
       while(!feof(fp))
        {
        if(fgets(line, 255, fp))
        printf("%s", line);             
        }
        printf(" End of file: ");
        printf("================ ");
        printf("The command was executed ");
        printf("The file was displayed ");
       
        fclose(fp);  
        }
    else if(strcmp(choice2, command) == 0){
              
        printf("Executing command: %s %s ", command, filename);
        in_file = fopen(filename, "r");
                if(in_file == NULL){
                     printf("Cannot open %s ", filename);
                     exit(8);
                }
                while(1){
                     ch = fgetc(in_file);
                     if(ch == EOF)
                       break;
                     ++count;
                }
                printf(" Number of characters in %s is %d ", filename, count);
                fclose(in_file);
         printf("The command was executed ");
         printf("The file was displayed ");
              
               }
    else if(strcmp(choice3, command) == 0){
         printf("Executing command: %s ", command);
         pFile = fopen(filename, "r");
         if(pFile == NULL)perror("Error openning file");
         else{
              do{
                 c = fgetc(pFile);
                 if(c == ' ') l++;
              }
              while(c != EOF);
           printf("File contains %d lines ", l);
           fclose(pFile);
                }
               }
               return 0;
}