osslip20
*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 +n filename
:- To print first n 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;
}
}
}
/* OUTPUT
- [shalmali@localhost SHELL]$ ./a.out
- MYSHELL $]typeline -2 a.txt
- Displaying Last 2 lines of file
- we are in tybcs.
- we are programmer.
- MYSHELL $]typeline 2 a.txt
- Displaying First 2 lines of file
- India is my country.
- I love my india.
- MYSHELL $]typeline a a.txt
- Contents of FILE=a.txt
- India is my country.
- I love my india.
- we are computer students from india.
- we are in tybcs.
- we are programmer.
- MYSHELL $]^C
- [shalmali@localhost SHELL]$
- */
/*Write a C program to simulate Non-preemptive Shortest Job First (SJF) –
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 10
typedef struct PROCESS
{
char name[20] ;
int at,bt,ct,tat,wt,tbt ;
}PROCESS ;
PROCESS p[MAX] ;
int time,processCount;
float avgtat,avgwt,totaltat,totalwt ;
void sort()
{
PROCESS p1 ;
int i,j;
for(i=0;i<processCount;i++)
{
for(j=i+1;j<processCount;j++)
{
if(p[j].at < p[i].at)
{
p1 = p[i] ;
p[i] = p[j] ;
p[j] = p1 ;
}
}
}
}
void readProcess()
{
int i ;
printf("\nEnter the number of processes: ") ;
scanf("%d",&processCount) ;
for(i=0;i<processCount;i++)
{
printf("\nEnter the process name: ") ;
scanf("%s",p[i].name) ;
printf("Enter the CPU Burst time: ") ;
scanf("%d",&p[i].bt) ;
printf("Enter the Arrival time: ") ;
scanf("%d",&p[i].at) ;
p[i].tbt = p[i].bt ;//extra line from fcfs
}
sort() ;
}
int getProcess()
{
int i , min = 999 , p1 ;
for(i=0;i<processCount;i++)
{
if(p[i].at <= time && p[i].tbt!=0)
{
if(p[i].bt < min)
{
min = p[i].bt ;
p1 = i ;
}
}
}
return p1 ;
}
void scheduleProcess()
{
int i,count = 0 ;
printf("\n\n GanttChart:\n") ;
printf("________________________________________________\n") ;
printf("|%d ",time) ;
while(1)
{
i = getProcess() ;
p[i].wt = time - p[i].at ;//current time-arrival time
time = time + p[i].bt ;
p[i].tbt = 0 ;
p[i].ct = time ;//extra line from fcfs
p[i].tat = p[i].ct - p[i].at ; //finishtime-arrival time
//p[i].tat=p[i].bt+p[i].wt; //working time+waiting time
totaltat+=p[i].tat ;
totalwt+=p[i].wt ;
count++ ;
printf("%s ",p[i].name) ;
printf("%d|%d ",time,time) ;
if(count==processCount)
break ;
}
printf("\n________________________________________________\n") ;
avgtat = totaltat / processCount ;
avgwt = totalwt / processCount ;
}
void display()
{
int i;
printf("\n--------------------------------------------------------------------------------\n") ;
printf("Process ArrivalTime BurstTime CPUTime TurnAroundtime WaitTime\n");
printf("--------------------------------------------------------------------------------\n");
for(i=0 ; i<processCount ; i++)
printf("%s\t %d\t\t%d\t %d\t %d\t\t %d\n",p[i].name,p[i].at,p[i].bt,p[i].ct,p[i].tat,p[i].wt) ;
printf("--------------------------------------------------------------------------------") ;
printf("\n\nTotal Turn Around Time: %f",totaltat) ;
printf("\nTotal Wait Time: %f",totalwt) ;
printf("\n\nAverage Turn Around Time: %f",avgtat) ;
printf("\nAverage Wait Time: %f\n",avgwt) ;
}
int main()
{
//clrscr();
readProcess();
scheduleProcess();
//getch();
display();
//getch();
return 0;
}