Вы находитесь на странице: 1из 57

Q1 ASSEMBLERS Pass 1 #include<stdio.h> #include<conio.h> #include<string.

h> void main() { FILE *fp1,*fp2,*fp3,*fp4; char label[10],opcode[10],operand[10],symbol[10],code[10]; char mnemonic[5],character,add[10],objectcode[10]; int flag,flag1,locctr,location,loc; clrscr(); fp1=fopen("out.dat","r"); fp2=fopen("p2out.dat","w"); fp3=fopen("optab.dat","r"); fp4=fopen("symtab.dat","r"); if(fp1==NULL) { printf("The file cannot be open"); } if(fp2==NULL) { printf("The file cannot be open"); } if(fp3==NULL) { printf("The file cannot be open"); }

if(fp4==NULL) { printf("The file cannot be open"); } fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand); } while(strcmp(opcode,"END")!=0) { flag=0; fscanf(fp3,"%s%s",code,mnemonic); while(strcmp(code,"END")!=0) { if((strcmp(opcode,code)==0)&&(strcmp(mnemonic,"*"))!=0) { flag=1; break; } fscanf(fp3,"%s%s",code,mnemonic); } if(flag==1) { flag1=0; rewind(fp4); while(!feof(fp4))

{ fscanf(fp4,"%s%d",symbol,&loc); if(strcmp(symbol,operand)==0) { flag=1; break; } } if(flag1==1) { itoa(loc,add,10); strcpy(objectcode,strcat(mnemonic,add)); } } else if(strcmp(opcode,"BYTE")==0 || strcmp(opcode,"WORD")==0) { if((operand[0]=='C')||(operand[0]=='X')) { character=operand[2]; itoa(character,add,16); strcpy(objectcode,add); } else { itoa(atoi(operand),add,10); strcpy(objectcode,add); } }

strcpy(objectcode,"\0"); fprintf(fp2,"%s\t%s\t%s\t%s\n",label,opcode,operand,locctr,objectcode); fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand); } fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand,locctr); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); }

INPUT FILE: Out.dat ** 2000 2003 2006 2009 2012 2015 2018 ** ** ** ** ALPHA FIVE CHARZ BYTE RESW WORD START LDA STA LDCH STCH 1 5 CZ 2000 FIVE ALPHA CHARZ C1

2019 2020

C1 **

RESB END

1 **

Optab.dat START * LDA STA LDCH STCH END 03 0F 53 57 *

Symtab.dat ALPHA 2012 FIVE 2015

CHARZ 2018 C1 2019

OUTPUT FILE: P2out.dat ** ** ** ** ** START LDA STA LDCH STCH 1 5 C'Z' 1 ** 2000 FIVE ALPHA CHARZ C1 2012 2015 2018 2019 2020 5 5a 2003 2006 2009 2000 0F2012 532018 572019 032015

ALPHA RESW FIVE WORD

CHARZ BYTE C1 ** RESB END

Pass 2 #include<stdio.h> #include<conio.h> #include<string.h> void main() { char opcode[10],mne[3],operand[10],label[10],code[10]; int loc,start,len; FILE *fp1,*fp2,*fp3,*fp4; clrscr(); fp1=fopen("input.dat","r"); fp2=fopen("sym.dat","w"); fp3=fopen("out.dat","w"); fp4=fopen("optab.dat","r"); fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) { start=atoi(operand); loc=start; fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); } else loc=0; while(strcmp(opcode,"END")!=0) { fprintf(fp3,"%d\t",loc); if(strcmp(label,"**")!=0)

fprintf(fp2,"%s\t%d\n",label,loc); fscanf(fp4,"%s%s",code,mne); while(strcmp(code,"END")!=0) { if(strcmp(opcode,code)==0) { loc+=3; break; } fscanf(fp4,"%s%s",code,mne); } if(strcmp(opcode,"WORD")==0) loc+=3; else if(strcmp(opcode,"RESW")==0) loc+=(3*(atoi(operand))); else if(strcmp(opcode,"RESB")==0) loc+=(atoi(operand)); else if(strcmp(opcode,"BYTE")==0) ++loc; fprintf(fp3,"%s\t%s\t%s\n,label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); } fprintf(fp3,"%d\t%s\t%s\t%s\n",loc,label,opcode,operand); len=loc-start; printf("Length of the program is %d",len); fclose(fp1); fclose(fp2); fclose(fp3);

fclose(fp4); getch(); }

Q3(a) 17(a) SIGNALS

#include<sys/types.h> #include<signal.h> #include<stdio.h> #define MAXLINE 20 static void sig_int(int); int main() { char buf[MAXLINE]; pid_t pid; int status; if(signal(SIGINT,sig_int)==SIG_ERR) perror("Signal error"); printf("%%"); while(fgets(buf,MAXLINE,stdin)!="") { buf[strlen(buf)-1]=0; if((pid=fork())<0) printf("Fork error"); else if(pid==0) { execlp(buf,buf,(char *)0);

printf("\nCould not execute %s",buf); exit(127); } printf("%%"); } exit(0); } void sig_int(int signo) { printf("\nInterrupt\n%%"); }

[student@localhost lij]$ cc sign.c [student@localhost lij]$ ./a.out %pwd %/home/student/lij %cal April 2012 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 %sdfg Could not execute sdfg [1]+ Stopped ./a.out

Q3 (b) FILE EXIST OR NOT if [ $# -gt 0 ]; then FILE=$1 if [ -f $FILE ]; then echo "file $FILE exists" else echo "file $FILE does not exists" fi else echo "not enough arguments" fi OUTPUT [student@localhost lij]$ sh exfile.sh dd file dd exists [student@localhost lij]$ sh exfile.sh da file da does not exists [student@localhost lij]$

Q4(a) MESSAGE QUEUE [student@localhost lij]$ vi msgque.c #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<sys/msg.h> struct msgbuf

{ int type; char mtext[100]; }m1; int main() { int mqid,m,n; pid_t childes; mqid=msgget(IPC_PRIVATE,600/IPC_CREAT); if(mqid==-1) { printf("\nError during message queue creation"); exit(1); } else { printf("\nMessage queue id:%d",mqid); printf("\nEnter the message:\n"); n=read(0,m1.mtext,100); m=msgsnd(mqid,&m1.mtext,100,0); iif(n==-1) { printf("\nError during message send"); exit(1); } else {

write(1,"Message send successfully",30); if(childes==fork()==0) { m=msgrcv(mqid,&m1.mtext,100,0,0); if(n==-1) { printf("\nError during message receiving"); exit(1); } else { write(1,"\nMessage received successfully",33); write(1,"\nReceived message ",22); write(1,&m1.mtext,100); msgctl(mqid,IPC_RMID,NULL); } exit(0); } } } sleep(3); exit(0); }

[student@localhost lij]$ cc msgque.c [student@localhost lij]$ ./a.out

Message queue id:163841 Enter the message: Hai Message send successfully Message received successfully Received message Hai

Q4 (b) .REVERSING OF NUMBER echo "Program to reverse the digit" echo "enter the numbers" read n rev=0 sd=0 while [ $n -gt 0 ] do sd=`expr $n % 10` rev=`expr $rev \* 10 + $sd` n=`expr $n / 10` done echo "The reverse digit is: $rev"

OUTPUT [student@localhost lij]$ sh rev.sh Program to reverse the digit enter the numbers 1234 The reverse digit is: 4321

Q8 NAMED PIPE

FIFO

student@localhost lij]$ vi fifo.c #include<stdio.h> main() { int burst_time[20],no_process,i,j; float avg_wait_time=0; int tot_wait_time=0,wait_time=0; char process_name[10][20]; printf("\nEnter the no:of process:"); scanf("%d",&no_process); printf("\nEnter the process:\n"); for(i=0;i<no_process;i++) { scanf("%s",&process_name[i]); } printf("\nEnter the burst time for each process\n"); for(j=0;j<no_process;j++) { scanf("%d",&burst_time[j]); } printf("\nGrant chart\n"); for(i=0;i<=no_process;i++) {

printf("%s\t",process_name[i]); } printf("\n%d\t",wait_time); for(j=0;j<=no_process-1;j++) { wait_time=wait_time+burst_time[j]; printf("%d\t",wait_time); if(j<no_process-1) tot_wait_time=wait_time+tot_wait_time; } printf("\nTotal waiting time=%d",tot_wait_time); avg_wait_time=(float)tot_wait_time/no_process; printf("\nAverage waiting time=%f",avg_wait_time); }

OUTPUT

[student@localhost lij]$ cc fifo.c [student@localhost lij]$ ./a.out

Enter the no:of process:3 Enter the process: p1 p2 p3

Enter the burst time for each process 2 3 5

Grant chart p1 0 p2 2 5 p3 10

Total waiting time=7 Average waiting time=2.333333

Q8 UNNAMED PIPE #include <stdio.h> #define READ 0 #define WRITE 1 char *phrase = "Stuff this in your pipe and smoke it"; main () { int fd[2], bytesRead; char message [100]; pipe ( fd ); if ( fork ( ) == 0 ) { close (fd[READ]); write (fd[WRITE], phrase, strlen ( phrase) +1); close (fd[WRITE]); printf("Child: Wrote '%s' to pipe!\n", phrase);

} else { close (fd[WRITE]); bytesRead = read ( fd[READ], message, 100); printf ( "Parent: Read %d bytes from pipe: %s\n", bytesRead, message); close ( fd[READ]); } }

OUTPUT [student@localhost lij]$ cc unnamepipe.c [student@localhost lij]$ ./a.out Child: Wrote 'Stuff this in your pipe and smoke it' to pipe! Parent: Read 37 bytes from pipe: Stuff this in your pipe and smoke it

Q10 SHAERED MEMORY

#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> #define shmsize 30 int main() { int shmid; char *s,c;

shmid=shmget(51532,shmsize,IPC_CREAT|0666); if(shmid<0) { perror("Shared memory creation error"); exit(1); } s=shmat(shmid,NULL,0); printf("\nEnter the message to send:"); while((c=getc(stdin))!=EOF) *s++=c; *s='\0'; } OUTPUT

[student@localhost lij]$ cc sm1.c [student@localhost lij]$ ./a.out

Enter the message to send: Hello

Q11(b). PALINDROME clear echo "Enter a string" read a b=`expr $a | wc -c` b=`expr $b - 1` echo "number of letter= $b" while test $b -gt 0

do e=`expr $a | cut -c $b` d=$d$e b=`expr $b - 1` done echo "the reversed string is:$d" if test $a = $d then echo "It is a palindrome" else echo "It is not a palindrome" fi OUTPUT [student@localhost lij]$ sh pali.sh echo "Enter a string" Enter a string noon number of letter= 4 the reversed string is:noon It is a palindrome

Q12 SIMPLE TEXT EDITOR #include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> void newf(); void view(); void saveas();

void modify(); void main() { int op; clrscr(); do { printf("\nMENU\n"); printf("1.New\n"); printf("2.Open\n"); printf("3.Save As\n"); printf("4.Modify\n"); printf("5.Exit\n"); printf("Enter u'r choice: "); scanf("%d",&op); switch(op) { case 1: newf(); break; case 2: view(); break; case 3: saveas(); break; case 4: modify();

break; case 5: exit(0); default: printf("\nWrong choice!!!"); break; } }while(op!=5); } void newf() { FILE *f; char fname[20],c; printf("\nEnter the Filename: "); scanf("%s",fname); printf("\nType the content and CTRL+Z to terminate:\n"); f=fopen(fname,"w"); rewind(f); while((c=getchar())!=EOF) { putc(c,f); } fclose(f); } void view() { FILE *f; char fname[20],c;

printf("\nEnter the name of the file:"); scanf("%s",&fname); if(searchpath(fname)) { f=fopen(fname,"r"); while((c=getc(f))!=EOF) { printf("%c",c); } } else printf("\nThe file %s does not exist",fname); fclose(f); } void saveas() { FILE *f1,*f2; char c,sou[20],des[20]; printf("\nEnter the Source file name: "); scanf("%s",sou); if(searchpath(sou)) { printf("Enter the Destination file name: "); scanf("%s",des); f1=fopen(sou,"r"); f2=fopen(des,"w"); while((c=getc(f1))!=EOF) {

putc(c,f2); } fclose(f1); fclose(f2); } else printf("\nFile does not exist"); getch(); } void modify() { int ch1; FILE *f1; char c,*word,*sent,fname[20]; printf("Enter the filename to be modified: "); scanf("%s",fname); if(searchpath(fname)) { printf("\n1.Character"); printf("\n2.Word"); printf("\n3.Sentence"); printf("\nEnter U'r choice: "); scanf("%d",&ch1); if(ch1==1) { f1=fopen(fname,"a+"); fseek(f1, 0L, SEEK_END); printf("Enter the character and CTRL+Z to exit:\n ");

while((c=getchar())!=EOF) { putc(c,f1); } } else if(ch1==2) { printf("Enter the word: "); scanf("%s",word); f1=fopen(fname,"a+"); fseek(f1, 0L, SEEK_END); fputs(word,f1); } else { printf("Enter the sentence and CTRL+Z to exit: "); f1=fopen(fname,"a+"); fseek(f1, 0L, SEEK_END); while((c=getchar())!=EOF) { putc(c,f1); } } } else printf("\nFilename does not exist"); fcloseall();

}
Q13(a) Print parent id processid groupid #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> int main() { pid_t pid,ppid; gid_t gid; if((pid=getpid())<0) { perror("Unable to get pid"); } else { printf("The process id is:%d",pid); } if((ppid=getppid())<0) { perror("Unable to get ppid"); } else {printf("The process id is %d",ppid); } if((gid=getgid())<0) { perror("Unable to get group id"); } else { printf("The groupid is %d",gid); } return 0; } Output [student@localhost btr]$ cc parent.c [student@localhost btr]$ ./a.out The process id is:6166 The process id is 5412 The groupid is 100 14(d)occurences of a word ----------------------------------

TOTAL=0; echo Enter the word read y for WORD in `cat $1 ` do if [ "$WORD" = "$y" ]; then TOTAL=` expr $TOTAL + 1 ` ; fi ; done ; echo "THE TOTAL COUNT IS : $TOTAL "

[student@localhost lij]$ cat> ps hai welcome hai wish

[9]+ Stopped

cat > ps

[student@localhost lij]$ sh sa.sh ps Enter the word hai THE TOTAL COUNT IS : 2

14(b)count no. of lines in multiple files -------------------------------------for file in $* do if [ ! -f $file ] then echo "$file not a file" exit 1 fi

echo "Characters : $(cat $file | wc -c)" echo "Words: $(cat $file | wc -w)" done

OUTPUT

[student@localhost lij]$ sh ccn.sh [student@localhost lij]$ cat > ss hai hai hai word hai hello

[3]+ Stopped

cat > ss

[student@localhost lij]$ cat > ss1 ss os php

[4]+ Stopped

cat > ss1

[student@localhost lij]$ sh ccn.sh ss ss1 Characters : 27 Words: 6 Characters : 10 Words: 3

14(c) count lines containing a word -----------------------------------echo Enter the word to be searched

read word echo Enter the file to be used read filename echo "The no. of lines containing the word $word are" grep -c "$word" $filename

OUTPUT [student@localhost lij]$ cat > hh hai hai hello hello

[2]+ Stopped

cat > hh

[student@localhost lij]$ sh cl.sh Enter the word to be searched hai Enter the file to be used hh The no. of lines containing the word hai are 2 [student@localhost lij]$

Q14(a) COUNT NO:OF WORDS,CHAR,LINES file=$1 v=0 if [ $# -ne 1 ] then echo "$0 fileName"

exit 1 fi if [ ! -f $file ] then echo "$file not a file" exit 2 fi echo "lines: $(cat $file | wc -l)" echo "character: $(cat $file | wc -c)" echo "Words: $(cat $file | wc -w)" OUTPUT [student@localhost lij]$ cat > jj hai hello fine

[1]+ Stopped

cat > jj

[student@localhost lij]$ sh wcl.sh jj lines: 3 character: 15 Words: 3

Q16 (b) FILE SYS RELATED CALLS [student@localhost lij]$ vi filesys.c #include<sys/types.h> #include<fcntl.h> #include<unistd.h> main() {

int ch,fd,n,fdl; char fname[20],buff[20],fnamel[20]; printf("\nMENU\n1.creat\n2.delete\n3.Read\n4.Copy\n5.Exit\n"); ab: printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the file to be created:"); scanf("%s",fname); if(!access(fname,F_OK)) { printf("\nFile already created"); exit(1); } fd: creat(fname,0777); if(fd!=1) printf("\nSuccessfully created"); goto ab; case 2: printf("\nThe name of the file to be deleted:"); scanf("%s",fname); remove(fname); printf("\nSuccessfully deleted"); goto ab; case 3: printf("\nEnter the name of file to read:");

scanf("%s",fname); fd=open(fname,0); if(fd==-1) { perror("open error"); exit(1); } while((n=read(fd,buff,1))>0) write(1,buff,n); close(fd); goto ab; case 4: printf("\nEnter the name of the file to copy:"); scanf("%s",fname); printf("\nEnter the file to which it is coppied:"); scanf("%s",fnamel); fd=open(fname,0); fdl=creat(fnamel,0777); while((n=read(fd,buff,1))>0) write(fdl,buff,n); close(fd); close(fdl); printf("\nSuccessfully coppied"); goto ab; case 5: break; } }

[student@localhost lij]$ cc filesys.c [student@localhost lij]$ ./a.out

MENU 1.creat 2.delete 3.Read 4.Copy 5.Exit

Enter your choice:1

Enter the file to be created:aa

Successfully created Enter your choice:5 [student@localhost lij]$ cat > bb Lij

[1]+ Stopped

cat > bb

[student@localhost lij]$ ./a.out

MENU 1.creat 2.delete 3.Read 4.Copy 5.Exit

Enter your choice:3

Enter the name of file to read:bb Lij

Enter your choice:1

Enter the file to be created:cc

Successfully created Enter your choice:4

Enter the name of the file to copy:bb

Enter the file to which it is coppied:cc

Successfully coppied Enter your choice:2

The name of the file to be deleted:bb

Successfully deleted Enter your choice:5

Q17 (b) .SIGNAL() & KILL() #include <stdio.h> #include <signal.h> void sighup(); void sigint(); void sigquit(); main() { int pid; if ((pid = fork()) < 0) { perror("fork"); exit(1); }

if (pid == 0) { signal(SIGHUP,sighup); signal(SIGINT,sigint); signal(SIGQUIT, sigquit); for(;;); } else { printf("\nPARENT: sending SIGHUP\n\n"); kill(pid,SIGHUP); sleep(3); printf("\nPARENT: sending SIGINT\n\n");

kill(pid,SIGINT); sleep(3); printf("\nPARENT: sending SIGQUIT\n\n"); kill(pid,SIGQUIT); sleep(3); } } void sighup() { signal(SIGHUP,sighup); printf("CHILD: I have received a SIGHUP\n"); } void sigint() { signal(SIGINT,sigint); printf("CHILD: I have received a SIGINT\n"); }

void sigquit() { printf("My DADDY has Killed me!!!\n"); exit(0); } OUTPUT [student@localhost lij]$ cc signalkill.c [student@localhost lij]$ ./a.out PARENT: sending SIGHUP CHILD: I have received a SIGHUP PARENT: sending SIGINT CHILD: I have received a SIGINT PARENT: sending SIGQUIT My DADDY has Killed me!!

Q17(a)SIGNALS #include<sys/types.h> #include<signal.h> #include<stdio.h> #define MAXLINE 20 static void sig_int(int); int main() { char buf[MAXLINE]; pid_t pid; int status; if(signal(SIGINT,sig_int)==SIG_ERR) perror("Signal error"); printf("%%"); while(fgets(buf,MAXLINE,stdin)!="") { buf[strlen(buf)-1]=0; if((pid=fork())<0) printf("Fork error"); else if(pid==0) { execlp(buf,buf,(char *)0); printf("\nCould not execute %s",buf); exit(127); } printf("%%"); } exit(0); }

void sig_int(int signo) { printf("\nInterrupt\n%%"); } [student@localhost lij]$ cc sign.c [student@localhost lij]$ ./a.out %pwd %/home/student/lij %cal April 2012 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 %sdfg Could not execute sdfg [1]+ Stopped Q18 (a) SIGALRM #include<stdio.h> #include<signal.h> volatile int breakflag=5; void handle(int sig) { printf("Hello\n"); --breakflag; alarm(1); } ./a.out

int main() { signal(SIGALRM,handle); alarm(1); while(breakflag) { sleep(1); } printf("done\n"); return 0; } OUTPUT [student@localhost lij]$ cc sig.c [student@localhost lij]$ ./a.out Hello Hello Hello Hello Hello Done

18(b)Two way comunication #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main() { int pfds[2],plfds[2]; char buf[10]; int i; pipe(pfds); pipe(plfds); if(fork()==0) { close(pfds[0]); close(plfds[1]); write(pfds[1],"test",5); read(plfds[0],buf,5); write(1,buf,5); } else { close(pfds[1]); close(plfds[0]); read(pfds[0],buf,5); for(i=0;i<4;i++) buf[i]=toupper(buf[i]); write(plfds[1],buf,5);

} } OUTPUT "childs.c" 29L, 381C written [student@localhost ni]$ cc childs.c [student@localhost ni]$ ./a.out [student@localhost ni]$ TEST [student@localhost ni]$

19(a)MSG QUE 19(b) TRANS

echo -n "Enter filename" read filename if [ ! -f $filename ]; then echo "Filename $filename doesn't not exist" exit 1 fi tr '[A-Z]' '[a-z]' < $filename

OUTPUT [student@localhost geet]$ cat> gg SFDGhjkjhj [2]+ Stopped cat > gg

[student@localhost geet]$ sh trans.sh Enter filenamegg sfdghjkjhj


.

Q20(a) PRODUCER CONSUMER PROBLEM FIFO : PRODUCER CONSUMER VI TICK.C #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #define FIFO_NAME "XYZ" main() { char s[300]; int num,fd; mknod(FIFO_NAME, S_IFIFO | 0666, 0); printf("Waiting for writers......\n"); fd=open(FIFO_NAME, O_RDONLY); printf("got a writer : \n"); do { if((num= read(fd, s, 300))== -1) perror("read"); else { s[num]='\0'; printf("tick : read %d bytes : \" %s\" \n", num,s); } }while(num>0); } [student@localhost vp]$ cc tick.c [student@localhost vp]$ ./a.out Waiting for writers...... got a writer : tick : read 2 bytes : " ds" tick : read 3 bytes : " jhf" tick : read 2 bytes : " hh" tick : read 4 bytes : " fghg" tick : read 5 bytes : " hello" VI SPEAK.C #include<stdio.h> #include<stdlib.h> #include<errno.h>

#include<string.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #define FIFO_NAME "XYZ" main() { char s[300]; int num,fd; mknod(FIFO_NAME, S_IFIFO | 0666, 0); printf("Waiting for readers......\n"); fd=open(FIFO_NAME, O_WRONLY); printf("got a reader-type some stuff\n"); while(gets(s), !feof(stdin)) { if((num= write(fd, s, strlen(s)))==-1) perror("write"); else printf("speak:wrote %d bytes\n", num); } } [student@localhost vp]$ cc speak.c [student@localhost vp]$ ./a.out Waiting for readers...... got a reader-type some stuff ds speak:wrote 2 bytes jhf speak:wrote 3 bytes hh speak:wrote 2 bytes fghg speak:wrote 4 bytes

20(b)LS COMMAND -----------#include<stdio.h> #include<string.h> #include<unistd.h> #include<dirent.h>

int main() { DIR *dirp; struct dirent *dp; char dname[10]; printf("Enter the directory name"); scanf("%s",dname); dirp=opendir(dname);

if(dirp==NULL) printf("error"); else for(dp=readdir(dirp);dp!=NULL;dp=readdir(dirp)) printf("%s\n",dp->dname); }

OUTPUT

[student@localhost lij]$ mkdir aps [student@localhost lij]$ cd aps [student@localhost aps]$ cat > ap hai hello [1]+ Stopped cat > ap

[student@localhost aps]$ cd\ >

[student@localhost ~]$ cd lij [student@localhost lij]$ cc ls.c [student@localhost lij]$ ./a.out Enter the directory name aps ap .. .

Q11(a)MERGE SORT

#include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/wait.h> #include<stdlib.h> #include<stdio.h> #define MAXLINE 1000

struct mymesg

{ long mtype; int a[4]; }; void Merge(int a[],int mer[],int ct) { if(ct==0) { ct+=4; for(int i=1;i<=ct;i++) mer[i]=a[i]; } else { for(int i=1;i<=4;i++) { int j=1; while(a[i]>mer[j]&&j<=ct)j++; if(j>ct) mer[j]=a[i]; else { for(int k=ct;k>=j;k--) mer[k+1]=mer[k]; mer[j]=a[i]; } ct++;

} } } int main() { int n,pid,mpid,sum,b[17],mer[17],num=16; mpid=msgget(12,0666|IPC_CREAT); system("clear"); printf("Elements are... "); for(int i=1;i<=num;i++) { b[i]=rand()%150; printf("%d ",b[i]); } printf("

"); int i,ct=1,gmax;n=4;sum=0;gmax=4; for(i=1;i<=n;i++) { struct mymesg ptr; ptr.mtype=1; pid=fork(); if (pid>0) { int k=0;

printf("Group %d: ",i); for(int j=ct;j<=ct+gmax-1;j++) { ptr.a[++k]=b[j]; printf("%d ",ptr.a[k]); }

printf(" "); msgsnd(mpid,&ptr,MAXLINE,IPC_NOWAIT); waitpid(pid,NULL,0);

msgrcv(mpid,&ptr,MAXLINE,0,IPC_NOWAIT);

printf("Sorted Sub-Group %d: ",i); for(int j=1;j<=gmax;j++) printf("%d ",ptr.a[j]); printf("

");

Merge(ptr.a,mer,ct-1);

if(ct==num+1-gmax) break; ct+=gmax; continue;

} else { msgrcv(mpid,&ptr,MAXLINE,0,IPC_NOWAIT);

for(int j=1;j<=gmax;j++) { for(int k=1;k<=gmax-j;k++) { if(ptr.a[k]>ptr.a[k+1]) { int t=ptr.a[k+1]; ptr.a[k+1]=ptr.a[k]; ptr.a[k]=t; } } } ptr.mtype=2; msgsnd(mpid,&ptr,MAXLINE,IPC_NOWAIT); exit(0); } } printf("Merged Sorted Group.... "); for(int i=1;i<=num;i++) printf("%d ",mer[i]); printf("

"); return 0; }

BIGGEST NUMBER

#!/bin/bash echo Enter no of items read n echo Enter the numbers read -a ARRAY max=0 count=${#ARRAY[@]} for x in ${ARRAY[@]} do if [ $x -gt $max ] then max=$x fi done echo The biggest number is : $max

LINES REPEATED MORE THAN THRICE echo Enter filename read filename while read line do uc=$(echo $line | tr [a-z] [A-Z] | tr -d ' ') if [ $(grep -i "$uc" $filename | wc -l) -gt 3 ];then echo $uc fi done< $filename | sort | uniq

Quick sort #!/bin/bash printnumbers() { echo ${ARRAY[*]} } sortnumbers() { local array=( `echo "$@"` ) local -a l local -a g local -a e local x= if [ ${#array[@]} -lt 2 ]; then echo -n ${array[@]} else local pivot=${array[0]} for x in ${array[@]} do if [ $x -lt $pivot ] then l=( ${l[@]} $x ) elif [ $x -gt $pivot ] then g=( ${g[@]} $x ) else e=(${e[@]} $x) fi done

echo "`sortnumbers "${l[@]}"` ${e[@]} `sortnumbers "${g[@]}"`" fi } clear echo "Enter Numbers to be Sorted : " read -a ARRAY count=${#ARRAY[@]} echo "--------------------------------------------------------------" echo "Numbers Before Sort:" printnumbers echo "Numbers After Sort: " sortnumbers "${ARRAY[@]}" echo "--------------------------------------------------------------"

Вам также может понравиться