Task Scheduling | DAA - IndianTechnoEra
Latest update Android YouTube

Task Scheduling | DAA

Admin

 

What is task scheduling?

Scheduling is the process by which you plan how you'll use your time. Doing it well can maximize your effectiveness and reduce your stress levels. Follow this six-step process to prepare your schedule: Identify the time you have available. Block in the essential tasks you must carry out to succeed in your job.


Code:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

// A structure to represent a job
typedef struct Job
{
    char id;    // Job Id
    int dead;   // Deadline of job
    int profit; // Profit if job is over before or on deadline
} Job;

// This function is used for sorting all jobs according to
// profit
int compare(const void *a, const void *b)
{
    Job *temp1 = (Job *)a;
    Job *temp2 = (Job *)b;
    return (temp2->profit - temp1->profit);
}

// Find minimum between two numbers.
int min(int num1, int num2)
{
    return (num1 > num2) ? num2 : num1;
}

// Returns minimum number of platforms required
void printJobScheduling(Job arr[], int n)
{
    // Sort all jobs according to decreasing order of profit
    qsort(arr, n, sizeof(Job), compare);
    //   sort(arr, arr+n, comparison);

    int result[n]; // To store result (Sequence of jobs)
    bool slot[n];  // To keep track of free time slots

    // Initialize all slots to be free
    for (int i = 0; i < n; i++)
        slot[i] = false;

    // Iterate through all given jobs
    for (int i = 0; i < n; i++)
    {
        // Find a free slot for this job (Note that we start
        // from the last possible slot)
        for (int j = min(n, arr[i].dead) - 1; j >= 0; j--)
        {
            // Free slot found
            if (slot[j] == false)
            {
                result[j] = i;  // Add this job to result
                slot[j] = true; // Make this slot occupied
                break;
            }
        }
    }

    // Print the result
    for (int i = 0; i < n; i++)
        if (slot[i])
            printf("%c ", arr[result[i]].id);
}

// Driver code
int main()
{

    Job arr[] = {{'A', 2, 100},
                 {'B', 1, 19},
                 {'C', 2, 27},
                 {'D', 1, 25},
                 {'E', 3, 15}};

    printf("\nAim: Task Scheduling in C");
    printf("\nUser Input: Pre input");
    printf("\n==============================================\n");

    int n = sizeof(arr) / sizeof(arr[0]);

    printf("\nYour scheduled Job sequence:\n");

    printJobScheduling(arr, n);
    return 0;
}

// This code is contributed by Aditya Kumar (adityakumar129)

Output:

إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.