Question

In: Computer Science

Write a C program that creates a toy scheduler for the child processes in part A....

Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time of each ”process” scheduled. The first toy scheduling algorithm to write this program for is First Come First Serve. After implementing FCFS, write a toy scheduler for Round Robin scheduling.

Here is code for part A:

prog.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/wait.h>

/**
 * This consists of a loop that echoes
 * the PID and the niceness of the
 * current process once per second.
 */
void proccessBody()
{
    for(;;)
    {
        printf("PID: %d; Niceness: %d;\n", getpid(), getpriority(PRIO_PROCESS, 0));
        sleep(1); // waiting for one second
    }
}

/**
 * This creates the n dummy processes.
 */
int main()
{
    // creating the processes
    const int n = 5;
    for(int i = 0; i < n; ++i)
    {
        if(fork() == 0)
        {
            // inside child process
            proccessBody(); // calling the process body
            exit(EXIT_SUCCESS); // child exiting successfully
        }
    }

    // waiting for the child processes to end
    while(wait(NULL) > 0);

    return 0;
}

Solutions

Expert Solution

void findTurnAroundTime( int processes[], int n,  

                  int bt[], int wt[], int tat[])

{

    for (int i = 0; i < n ; i++)

        tat[i] = bt[i] + wt[i];

}

   

void findavgTime( int processes[], int n, int bt[])

{

    int wt[n], tat[n], total_wt = 0, total_tat = 0;

   

    

    findWaitingTime(processes, n, bt, wt);

   

    findTurnAroundTime(processes, n, bt, wt, tat);

   

      

    printf("Processes   Burst time   Waiting time   Turn around time\n");

   

    

  

    for (int i=0; i<n; i++)

    {

        total_wt = total_wt + wt[i];

        total_tat = total_tat + tat[i];

        printf("   %d ",(i+1));

        printf("       %d ", bt[i] );

        printf("       %d",wt[i] );

        printf("       %d\n",tat[i] );

    }

    int s=(float)total_wt / (float)n;

    int t=(float)total_tat / (float)n;

    printf("Average waiting time = %d",s);

    printf("\n");

    printf("Average turn around time = %d ",t);

}

   

int main()

{

    //process id's

    int processes[] = { 1, 2, 3};

    int n = sizeof processes / sizeof processes[0];

   

    

    int burst_time[] = {10, 5, 8};

   

    findavgTime(processes, n, burst_time);

    return 0;

}


Related Solutions

Write a program in C or C++ that spawns three child processes. The first child sends...
Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.
Write a C program that outputs a formatted header line and creates 10 children processes each...
Write a C program that outputs a formatted header line and creates 10 children processes each of which displays a line of the output as shown below. Notice the values of Child no and x, they have to be the same as shown here while the processes PIDs values are based on the what your system assigns to these processes. Child    PID        PPID      X 0           13654    13653    5 1           13655    13653    10 2           13656    13653    15 3           13657    13653    20...
Write a C++ program to display toy name
Write a C++ program to display toy name
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
You will write a C program to fork a child process. The child process will print...
You will write a C program to fork a child process. The child process will print the current time/date. The parent will wait for the child to complete. Here is how you can programmatically call the Linux date command from a C program. execlp("/bin/date","date",NULL); Your C file should be named myfork.c The binary will be named myfork.
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Write a C program that creates a database of plant height samples. In this scenario a...
Write a C program that creates a database of plant height samples. In this scenario a researcher enters a name of a group/type of plants, and heights for each plant in the group/type Use input will be stored in a database file, which should be a file called database.csv and should be in the following formatPlant1, number_of_samples, sample1, sample2, sample3 ...Plant2, number_of_samples, sample1, sample2, sample3 ...Plant3, number_of_samples, sample1, sample2, sample3 ...Lines starts with the name of a plant type, then...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT