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

For the problems below, report your results and answers, along with all commands

ID: 655247 • Letter: F

Question

For the problems below, report your results and answers, along with all commands used to achieve the required tasks. Number all answers according to this document (1., 2., 3., etc

1. Explain with the maximum level of details what the following command does;

    $ sed 's/w*ing/FOUND/ig' mobydick.txt

2. Write a bash script that uses regular expressions to list all quoted text from mobydick.txt (e.g., "From my twenty-fifth year I date my life.", "Call me Ishmael.", etc).

3. Write a C program that recursively traverses all folders in your Unix environment and adds your ITU student id in front of every .c, .pdf and .doc files it finds. For example, a file named mycode.c will become XXXXXXmycode.c, where XXXXXX is your student id).

4. Write a C program that launches and manages 3 parallel processes (tip: use fork()), each to monitor one metric from your Unix machine. Namely:

(a) the most intense running process by CPU usage;

            (b) the most intense running process by memory usage; and

(c) the longest running process by time.

Each child process should report to a centralized log file (maintained by the main program) at a 20 second interval. Each log record should contain the metric being observed, the name of the process, its PID and user. Terminate the program if the log file reaches 5kbytes. Note that, since all 3 processes are writing to the same log file, you must avoid racing conditions that may occur if 2 processes try to write to the log file at the same time

Explanation / Answer

1.

command for sed directory for data searching .

2.

#!/bin/bash

sed -e 'N;s/.*/[&]/' << EOF   # Here Document

line1

line2

EOF

# OUTPUT:

# [line1

# line2]

echo

awk '{ $0=$1 " " $2; if (/line.1/) {print}}' << EOF

line 1

line 2

EOF

# OUTPUT:

# line

# 1

# Thanks, S.C.

exit 0

3.

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <dirent.h>

#include <errno.h>

#include <string.h>

void listdir(char *argv[])

{

DIR *mydirhandle;

struct dirent *mydirent;

struct stat statinfo;

int n = 1;

while(argv[n] != NULL)

{

    if((mydirhandle = opendir(argv[n])) == NULL)

    {

      perror("opendir");

      exit(1);

    }

    printf("%s/ ", argv[n]);

    while((mydirent = readdir(mydirhandle)) != NULL)

    {

      if((strcmp(mydirent->d_name, ".") == 0) || (strcmp(mydirent->d_name, "..") == 0))

      {

        continue;

      }

      else          

      {

        printf(" %s ", mydirent->d_name);

         if(mydirent->d_type == DT_DIR)

        {  

  

            listdir(mydirent->d_name);

        }

      }   

}                      

    n++;

    closedir(mydirhandle);

}

}

int main(int argc, char *argv[])

{

if(argc < 2)

{

    printf("usage: %s <directory> ", argv[0]);

    return 0;

}

listdir(argv);

return 0;

}

4.

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    printf("--beginning of program ");

    int counter = 3;
    pid_t pid = fork();

    if (pid == 0)
    {
  
        int i = 0;
        for (; i < 5; ++i)
        {
            printf("child process: counter=%d ", ++counter);
        }
    }
    else if (pid > 0)
    {
  
        int j = 0;
        for (; j < 5; ++j)
        {
            printf("parent process: counter=%d ", ++counter);
        }
    }
    else
    {

        printf("fork() failed! ");
        return 1;
    }

if (pid == 1)
    {
  
        int i = 1;
        for (; i < 5; ++i)
        {
            printf("child process: counter=%d ", ++counter);
        }
    }
    else if (pid > 0)
    {
        int j = 0;
        for (; j < 5; ++j)
        {
            printf("parent process: counter=%d ", ++counter);
        }
    }
    else
    {
        printf("fork() failed! ");
        return 1;
    }

if (pid == 2)
    {
        // child process
        int i = 2;
        for (; i < 5; ++i)
        {
            printf("child process: counter=%d ", ++counter);
        }
    }
    else if (pid > 2)
    {
        int j = 0;
        for (; j < 5; ++j)
        {
            printf("parent process: counter=%d ", ++counter);
        }
    }
    else
    {
        printf("fork() failed! ");
        return 1;
    }

    printf("--end of program-- ");

    return 0;
}