Q.10) Write the program to simulate Non-preemptive Priority 
scheduling.	
The arrival time and first CPU-burst and priority for different n
number
of processes should be input to the algorithm. Assume the fixed 
IO
waiting time (2 units). The next CPU-burst should be generated
randomly. The output should give Gantt chart, turnaround time and
waiting time for each process. Also find the average waiting time
and
turnaround time.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct process_info
{
    char pname[20];
    int at, bt, ct, bt1, p;
    struct process_info *next;
} NODE;
int n;
NODE *first, *last;
void accept_info()
{
    NODE *p;
    int i;
    printf("Enter no.of process:");
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        p = (NODE *)malloc(sizeof(NODE));
        printf("Enter process name:");
        scanf("%s", p->pname);
        printf("Enter arrival time:");
        scanf("%d", &p->at);
        printf("Enter first CPU burst time:");
        scanf("%d", &p->bt);
        printf("Enter priority:");
        scanf("%d", &p->p);
        p->bt1 = p->bt;
        p->next = NULL;
        if (first == NULL)
            first = p;
        else
            last->next = p;
        last = p;
    }
}
void print_output()
{
    NODE *p;
    float avg_tat = 0, avg_wt = 0;
    printf("pname\tat\tbt\tp\ttct\ttat\twt\n");
    p = first;
    while (p != NULL)
    {
        int tat = p->ct - p->at;
        int wt = tat - p->bt;
        avg_tat += tat;
        avg_wt += wt;
        printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n",
               p->pname, p->at, p->bt, p->p, p->ct, tat, wt);
        p = p->next;
    }
    printf("Avg TAT=%f\tAvg WT=%f\n",
           avg_tat / n, avg_wt / n);
}
void print_input()
{
    NODE *p;
    p = first;
    printf("pname\tat\tbt\tp\n");
    while (p != NULL)
    {
        printf("%s\t%d\t%d\t%d\n",
               p->pname, p->at, p->bt1, p->p);
        p = p->next;
    }
}
void sort()
{
    NODE *p, *q;
    int t;
    char name[20];
    p = first;
    while (p->next != NULL)
    {
        q = p->next;
        while (q != NULL)
        {
            if (p->at > q->at)
            {
                strcpy(name, p->pname);
                strcpy(p->pname, q->pname);
                strcpy(q->pname, name);
                t = p->at;
                p->at = q->at;
                q->at = t;
                t = p->bt;
                p->bt = q->bt;
                q->bt = t;
                t = p->ct;
                p->ct = q->ct;
                q->ct = t;
                t = p->bt1;
                p->bt1 = q->bt1;
                q->bt1 = t;
                t = p->p;
                p->p = q->p;
                q->p = t;
            }
            q = q->next;
        }
        p = p->next;
    }
}
int time;
NODE *get_p()
{
    NODE *p, *min_p = NULL;
    int min = 9999;
    p = first;
    while (p != NULL)
    {
        if (p->at <= time && p->bt1 != 0 &&
            p->p < min)
        {
            min = p->p;
            min_p = p;
        }
        p = p->next;
    }
    return min_p;
}
struct gantt_chart
{
    int start;
    char pname[30];
    int end;
} s[100], s1[100];
int k;
void pnp()
{
    int prev = 0, n1 = 0;
    NODE *p;
    while (n1 != n)
    {
        p = get_p();
        if (p == NULL)
        {
            time++;
            s[k].start = prev;
            strcpy(s[k].pname, "*");
            s[k].end = time;
            prev = time;
            k++;
        }
        else
        {
            time += p->bt1;
            s[k].start = prev;
            strcpy(s[k].pname, p->pname);
            s[k].end = time;
            prev = time;
            k++;
            p->ct = time;
            p->bt1 = 0;
            n1++;
        }
        print_input();
        sort();
    }
}
void print_gantt_chart()
{
    int i, j, m;
    s1[0] = s[0];
    for (i = 1, j = 0; i < k; i++)
    {
        if (strcmp(s[i].pname, s1[j].pname) == 0)
            s1[j].end = s[i].end;
        else
            s1[++j] = s[i];
    }
    printf(" %d ", s1[0].start);
    for (i = 0; i <= j; i++)
    {
        m = (s1[i].end - s1[i].start);
        for (k = 0; k < m / 2; k++)
            printf("-");
        printf("%s", s1[i].pname);
        for (k = 0; k < (m + 1) / 2; k++)
            printf("-");
        printf("%d", s1[i].end);
    }
}
int main()
{
    accept_info();
    sort();
    pnp();
    print_gantt_chart();
    printf("\n");
    print_output();
    return 0;
}
*************************OUTPUT********************
cs10@cs10-Lenovo-C20-30:~$ cd Documents/
cs10@cs10-Lenovo-C20-30:~/Documents$ cd kadam 
cs10@cs10-Lenovo-C20-30:~/Documents/kadam$ gcc osprogram10.c 
cs10@cs10-Lenovo-C20-30:~/Documents/kadam$ ./a.out
Enter no.of process:5
Enter process name:sjf
Enter arrival time:3
Enter first CPU burst time:4
Enter priority:2
Enter process name:4
Enter arrival time:9
Enter first CPU burst time:7
Enter priority: 
3
Enter process name:fcfs
Enter arrival time:3
Enter first CPU burst time:2
Enter priority:4
Enter process name:sjf
Enter arrival time:4
Enter first CPU burst time:3
Enter priority:2
Enter process name:fcfs
Enter arrival time:4
Enter first CPU burst time:6
Enter priority:3
pname at bt p
sjf 3 4 2
fcfs 3 2 4
sjf 4 3 2
fcfs 4 6 3
4 9 7 3
pname at bt p
sjf 3 4 2
fcfs 3 2 4
sjf 4 3 2
fcfs 4 6 3
4 9 7 3
pname at bt p
sjf 3 4 2
fcfs 3 2 4
sjf 4 3 2
fcfs 4 6 3
4 9 7 3
pname at bt p
sjf 3 0 2
fcfs 3 2 4
sjf 4 3 2
fcfs 4 6 3
4 9 7 3
pname at bt p
sjf 3 0 2
fcfs 3 2 4
sjf 4 0 2
fcfs 4 6 3
4 9 7 3
pname at bt p
sjf 3 0 2
fcfs 3 2 4
sjf 4 0 2
fcfs 4 0 3
4 9 7 3
pname at bt p
sjf 3 0 2
fcfs 3 2 4
sjf 4 0 2
fcfs 4 0 3
4 9 0 3
pname at bt p
sjf 3 0 2
fcfs 3 0 4
sjf 4 0 2
fcfs 4 0 3
4 9 0 3
 0 -*--3---sjf----10---fcfs---16---4----23-fcfs-25
pname at bt p tct tat wt
sjf 3 4 2 7 4 0
fcfs 3 2 4 25 22 20
sjf 4 3 2 10 6 3
fcfs 4 6 3 16 12 6
4 9 7 3 23 14 7
Avg TAT=11.600000 Avg WT=7.200000
 
by

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}