OneCompiler

osslip14

209

/*Write a C program to implement the shell which displays the command
prompt “myshell$”. It accepts the command, tokenize the command line and
execute it by creating the child process. Also implement the additional
command ‘typeline’ as
typeline -a filename :- To print all lines in the file.
*/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h> // open,read(),close() : file related function

void typeline(char p1,charfname)
{
int handle,n,lcnt;
char ch;

handle=open(fname,O_RDONLY);

if(handle==-1)
{
	printf("\n File %s Not Found....",fname);
}
else
{

	if(strcmp(p1,"a")==0)
	{
		/* Displaying All contents of file */
		printf("\nContents of FILE=%s\n",fname);
		while(read(handle,&ch,1))			//on failure read() function returns 0
		{
			printf("%c",ch);
		}
		close(handle);
	}
	else			//else means parameter is either +n or -n
	{
		n=atoi(p1);
		lcnt=0;
		if(n>0)
		{
			printf("Displaying First %d lines of file\n",n);
			while(read(handle,&ch,1))
			{
				printf("%c",ch);
				if(ch=='\n')
					lcnt++;
				if(lcnt==n)
					break;
			}
			close(handle);
		}
		else
		{
			n=-n;
			printf("Displaying Last %d lines of file\n",n);
                            while(read(handle,&ch,1))
                            {
                                   if(ch=='\n')
                                            lcnt++;
                            }

			n=lcnt-n;
			lcnt=0;
			lseek(handle,0,SEEK_SET);	//starting from start of file again
			while(read(handle,&ch,1))
                            {
                                     if(ch=='\n')
                                            lcnt++;
                                    if(lcnt==n)
                                            break;
                            }
			while(read(handle,&ch,1))
                            {
                                    printf("%c",ch);
                            }
			close(handle);
		}
	} 
}

}
int main()
{
char cmd[80],tok1[10],tok2[10],tok3[10],tok4[10];
int n;
while(1)
{
printf("\nMYSHELL $]");

            fgets(cmd,80,stdin);

            n=sscanf(cmd,"%s%s%s%s",tok1,tok2,tok3,tok4);   //n=no of tokens are formed from given command

            switch(n)
            {
                    case 1:

                            if(fork()==0)
                            {
                                    execlp(tok1,tok1,NULL); //paramenters- nameOfProcess,parametersOfProcess        
                            }
                            wait(0);
                            break;
                    case 2 :
                            if(fork()==0)
                            {
                                    execlp(tok1,tok1,tok2,NULL);
                            }
                            wait(0);
                            break;
                    case 3:
			if(strcmp(tok1,"typeline")==0)
			{
				typeline(tok2,tok3);
			}
			else
			{
                            	if(fork()==0)
                            	{
                                    execlp(tok1,tok1,tok2,tok3,NULL);
                            	}
                            	wait(0);
			}
                            break;
                    case 4 :
                            if(fork()==0)
                            {
                                    execlp(tok1,tok1,tok2,tok3,tok4,NULL);
                            }
                            wait(0);
                            break;

            }
    }

}

/*
MYSHELL $]typeline -a a.txt
Displaying Last 0 lines of file
I love India.
*/

/*
Write the program to simulate FCFS CPU-scheduling. The arrival time and
first CPU-burst of different jobs should be input to the system. Accept no. of
Processes, arrival time and burst time. 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>

#define MAX 100

typedef struct
{
int pid;
int burst_time;
int waiting_time;
int turnaround_time;
} Process;

void print_table(Process p[], int n);
void print_gantt_chart(Process p[], int n);

int main()
{
Process p[MAX];
int i, j, n;
int sum_waiting_time = 0, sum_turnaround_time;
printf("Enter total number of process: ");
scanf("%d", &n);
printf("Enter burst time for each process:\n");
for(i=0; i<n; i++) {
p[i].pid = i+1;
printf("P[%d] : ", i+1);
scanf("%d", &p[i].burst_time);
p[i].waiting_time = p[i].turnaround_time = 0;
}

// calculate waiting time and turnaround time
p[0].turnaround_time = p[0].burst_time;

for(i=1; i<n; i++) {
    p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
    p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
}

// calculate sum of waiting time and sum of turnaround time
for(i=0; i<n; i++) {
        sum_waiting_time += p[i].waiting_time;
        sum_turnaround_time += p[i].turnaround_time;
}

// print table
puts(""); // Empty line
print_table(p, n);
puts(""); // Empty Line
printf("Total Waiting Time      : %-2d\n", sum_waiting_time);
printf("Average Waiting Time    : %-2.2lf\n", (double)sum_waiting_time / (double) n);
printf("Total Turnaround Time   : %-2d\n", sum_turnaround_time);
printf("Average Turnaround Time : %-2.2lf\n", (double)sum_turnaround_time / (double) n);

// print Gantt chart
puts(""); // Empty line
puts("          GANTT CHART          ");
puts("          ***********          ");
print_gantt_chart(p, n);
return 0;

}

void print_table(Process p[], int n)
{
int i;

puts("+-----+------------+--------------+-----------------+");
puts("| PID | Burst Time | Waiting Time | Turnaround Time |");
puts("+-----+------------+--------------+-----------------+");

for(i=0; i<n; i++) {
    printf("| %2d  |     %2d     |      %2d      |        %2d       |\n"
           , p[i].pid, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time );
    puts("+-----+------------+--------------+-----------------+");
}

}

void print_gantt_chart(Process p[], int n)
{
int i, j;
// print top bar
printf(" ");
for(i=0; i<n; i++) {
for(j=0; j<p[i].burst_time; j++) printf("--");
printf(" ");
}
printf("\n|");

// printing process id in the middle
for(i=0; i<n; i++) {
    for(j=0; j<p[i].burst_time - 1; j++) printf(" ");
    printf("P%d", p[i].pid);
    for(j=0; j<p[i].burst_time - 1; j++) printf(" ");
    printf("|");
}
printf("\n ");
// printing bottom bar
for(i=0; i<n; i++) {
    for(j=0; j<p[i].burst_time; j++) printf("--");
    printf(" ");
}
printf("\n");

// printing the time line
printf("0");
for(i=0; i<n; i++) {
    for(j=0; j<p[i].burst_time; j++) printf("  ");
    if(p[i].turnaround_time > 9) printf("\b"); // backspace : remove 1 space
    printf("%d", p[i].turnaround_time);

}
printf("\n");

}