You are on page 1of 31

slip No:1_Q.

2
typeline
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
void typeline(char *p1,char*fname)
{
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)
{
printf("\nContents of FILE=%s\n",fname);
while(read(handle,&ch,1))
{
printf("%c",ch);
}
close(handle);
}
else
{
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);
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);
switch(n)
{
case 1:
if(fork()==0)
{
execlp(tok1,tok1,NULL);
}
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;
}
}
}
Slip No:2_Q.2
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h> //opendir(),readdir(),closedir()
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>// open,read(),close() : file related function
void list(char *p1,char*dname)
{
DIR *dir;
struct dirent *entry;
struct stat info;
int cnt=0;
dir=opendir(dname);
if(dir==NULL)
{
printf("\n Directory %s Not Found....",dname);
}
else
{
if(strcmp(p1,"F")==0)
{
while((entry=readdir(dir))!=NULL)
{
stat(entry->d_name,&info);
if(info.st_mode & S_IFREG)
printf("%s\n",entry->d_name);
}
}
else if(strcmp(p1,"N")==0)
{
cnt=0;
while((entry=readdir(dir))!=NULL)
{
cnt++;
}
printf("\nTotal no. of entries in directory '%s' = %d",dname,cnt);
}
else if(strcmp(p1,"I")==0)
{
while((entry=readdir(dir))!=NULL)
{
stat(entry->d_name,&info);
if(info.st_mode & S_IFREG)
{
printf("File name =%s\t",entry->d_name);
printf("Inode=%d\n",info.st_ino);
}
}
}
}
}
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);
switch(n)
{
case 1:
if(fork()==0)
{
execlp(tok1,tok1,NULL);
}
break;
case 2 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,NULL);
}
break;
case 3:
if(strcmp(tok1,"list")==0)
{
list(tok2,tok3);
}
else
{
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,NULL);
}
}
break;
case 4 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,tok4,NULL);
}
break;
}
}
}
Slip No:3_Q.2
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>// open,read(),close() : file related function
void count(char *p1,char*fname)
{
int handle,ccnt=0,wcnt=0,lcnt=0;
char ch;
handle=open(fname,O_RDONLY);
if(handle==-1)
{
printf("\n File %s Not Found....",fname);
}
else
{
while(read(handle,&ch,1))
{
if(ch==' '||ch=='\t')
wcnt++;
else if(ch=='\n')
{
lcnt++;
wcnt++;
}
ccnt++;
}
close(handle);
if(strcmp(p1,"C")==0)
{
printf("\nTotal No. of character in file = %d",ccnt);
}
else if(strcmp(p1,"W")==0)
{
printf("\nTotal No. of words in file = %d",wcnt);
}
else if(strcmp(p1,"L")==0)
{
printf("\nTotal No. of Lines in file = %d",lcnt);
}
else
{
printf("\nInvalid Option......\n");
}
}
}
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);
switch(n)
{
case 1:
if(fork()==0)
{
execlp(tok1,tok1,NULL);
break;
case 2 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,NULL);
}
break;
case 3:
if(strcmp(tok1,"count")==0)
{
count(tok2,tok3);
}
else
{
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,NULL);
}
}
break;
case 4 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,tok4,NULL);
}
break;
}
}
}
Slip No:4_Q.2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>// open,read(),close() : file related function
void search(char *p1,char *pat,char*fname)
{
int handle,i=0,cnt=0;
char ch,data[100],*ptr;
handle=open(fname,O_RDONLY);
if(handle==-1)
{
printf("\n File %s Not Found....",fname);
}
else
{
if(strcmp(p1,"F")==0)
{
i=0;
while(read(handle,&ch,1))
{
data[i]=ch;
i++;
if(ch=='\n')
{
data[i]='\0';
if(strstr(data,pat)!=NULL)
{
printf("\nFIRST OCCURANCE OF PATTERN IN LINE IS GIVEN BELOW\n");
puts(data);
break;
}
i=0;
}
}
close(handle);
}
else if(strcmp(p1,"C")==0)
{
cnt=0;
i=0;
while(read(handle,&ch,1))
{
data[i]=ch;
i++;
if(ch=='\n')
{
data[i]='\0';
ptr=data;
while((ptr=strstr(ptr,pat))!=NULL)
{
cnt++;
ptr++;
}
i=0;
}
}
printf("\nNo of occurances of '%s' = %d",pat,cnt);
close(handle);
}
else if(strcmp(p1,"A")==0)
{
printf("\nDispaying All Occurances of '%s'\n",pat);
i=0;
while(read(handle,&ch,1))
{
data[i]=ch;
i++;
if(ch=='\n')
{
data[i]='\0';
if((ptr=strstr(data,pat))!=NULL)
{
puts(data);
}
i=0;
}
}
}
}
}
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);
switch(n)
{
case 1:
if(fork()==0)
{
execlp(tok1,tok1,NULL);
}
wait(0);
break;
case 2 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,NULL);
}
wait(0);
break;
case 3:
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,NULL);
}
wait(0);
break;
case 4 :
if(strcmp(tok1,"search")==0)
{
search(tok2,tok3,tok4);
}
else
{
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,tok4,NULL);
}
wait(0);
}
break;}
}
}

Slip No:17_Q.2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct process_info
{
char pname[10];
int AT,BT,TBT,CT,TAT,WT;
};
struct process_info p[10];
struct gantt_chart
{
int ST,ET;
char pname[10];
};
struct gantt_chart g[100];
int n,gcnt=0,totalTAT,totalWT;
void accept()
{
int i;
for(i=0;i<n;i++)
{
printf("\nEnter process: ");
scanf("%s",p[i].pname);
printf("\nEnter Arrival Time:");
scanf("%d",&p[i].AT);
printf("\nEnter Burst Time:");
scanf("%d",&p[i].BT);
p[i].TBT=p[i].BT;
}
printf("\n");
}
void sort_AT()
{
int i,j;
struct process_info t;
for(i=n-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(p[j].AT>p[j+1].AT)
{
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
}

void fcfs()
{
int time=0,i;
for(i=0;i<n;)
{
if(p[i].AT<=time && p[i].TBT!=0)
{
g[gcnt].ST=time;
strcpy(g[gcnt].pname,p[i].pname);
g[gcnt].ET=time+p[i].TBT;
gcnt++;
time=time+p[i].TBT;
p[i].TBT=0;
i++;
}
else
{
g[gcnt].ST=time;
strcpy(g[gcnt].pname,"CPU IDLE");
g[gcnt].ET=time+1;
gcnt++;
time++;
}
}
}
void calculate()
{
int ct=0,i,j;
for(i=0;i<n;i++)
{
for(j=0;j<gcnt;j++)
{
if(strcmp(p[i].pname,g[j].pname)==0)
{
ct=g[j].ET;
}
}
p[i].CT=ct;
}
for(i=0;i<n;i++)
{
p[i].TAT=p[i].CT-p[i].AT;
p[i].WT=p[i].TAT-p[i].BT;
totalTAT=totalTAT+p[i].TAT;
totalWT=totalWT+p[i].WT;
}
}
void display()

{
int i=0;
printf("\nInformation of System Info\n");
printf("\nPname\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t
%d",p[i].pname,p[i].AT,p[i].BT,p[i].CT,p[i].TAT,p[i].WT);
}
printf("\n\nGantt Chart=");
for(i=0;i<gcnt;i++)
{
printf("|%d|%s|%d|\t",g[i].ST,g[i].pname,g[i].ET);
}
printf("\nTotal TAT=%d",totalTAT);
printf("\nTotal WT=%d",totalWT);
printf("\nAverage TAT=%f",(float)(totalTAT/n));
printf("\nAverage WT=%f",(float)(totalWT/n));
}
void acceptSystemBT()
{
int i;
for(i=0;i<n;i++)
{
p[i].BT=(rand()%10)+1;
p[i].TBT=p[i].BT;
}
}
int main()
{
int i;
printf("How many processes you have: ");
scanf("%d",&n);
printf("\nExecuting user Given Burst Time For FCFS.....\n");
accept();
printf("\nPname\tAT\tBT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d",p[i].pname,p[i].AT,p[i].BT);
}
printf("\n");
sort_AT();
fcfs();
calculate();
display();
printf("\n");
printf("\nExecuting System Given Burst Time For FCFS.....\n");
acceptSystemBT();
printf("\nPname\tAT\tBT");

for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d",p[i].pname,p[i].AT,p[i].BT);
}
printf("\n");
sort_AT();
fcfs();
calculate();
display();
printf("\n");
}
Slip No:21_Q.1
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid = fork();
if (pid > 0)
{
printf("I am Parent process\n");
printf("ID : %d\n\n", getpid());
}
else if(pid == 0)
{
printf("I am Child process\n");
printf("ID: %d\n\n", getpid());
}
else
{
printf("Failed to create child process");
}
return 0;
}

Slip No:22 _Q.1


#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("\n I am child process,id=%d\n",getpid());
nice(0);
printf("\n Priority:%d,id=%d\n",nice(-7),getpid());
}
else
{
printf("\n I am parent process,id=%d\n",getpid());
nice(1);
printf("\n Priority:%d,id=%d\n",nice(15),getpid());
}
return 0;
}
Slip No: 23_ Q.1
#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int=pid;
pid=getpid();
printf("Current Process ID is:%d\n",pid);
printf("\n[Forking Child Process...]\n");
pid=fork();
if(pid<0)
{
printf("\n Process can not be created");
}
else
{
if(pid==0)
{
printf("\nChild Process is sleeping...");
sleep(5);
printf("\nOrphan Child's Parent ID:%d",getppid());
}
else
{
printf("\nParent Process Completed...");
}
}
return 0;
}

You might also like