You are on page 1of 18

1) Displaying lines echo 'The lines between' $2 'and' $3 'from' $1 echo if [ $2 -lt $3 ] then n1=`expr $2 + 1` n2=`expr $3 - 1` sed -n "$n1,$n2

p" $1 elif [ $2 -gt $3 ] then n3=`expr $2 - 1` n4=`expr $3 + 1` sed -n "$n4,$n3 p" $1 else echo fi 2) deleting lines containing specified word echo 'enter a word to be deleted' read word echo for fname in $* do echo 'lines in' $fname 'after deleting the word' $word ':' sed "/$word/d" $fname done 3) read,write and execute permission files for i in * do if [ -r $i -a -w $i -a -x $i ] then echo $i fi done 4)list of files(no.of lines) and directories for fname in $* do if [ -f $fname ] then echo $fname 'is a file' echo 'no.of lines in' $fname ':' wc -l $fname

elif [ -d $fname ] then echo $fname 'is a directory' else echo 'Does not exist' fi done 5)counting word occurrences if [ $# -eq 0 ] then echo "no arguments" else tr " " "\n" < $1 > temp shift for i in $* do tr " " "\n" < $i > temp1 y=`wc -l < temp` j=1 while [ $j -le $y ] do x=`head -n $j temp | tail -1` c=`grep -c "$x" temp1` echo $x $c j=`expr $j + 1` done done fi 6)list of all directory files in the directory echo 'enter a directory name' read dname echo 'The list of directory files in the directory' $dname 'are' cd $dname ls -l | grep '^d' 7)Factorial echo 'enter a number' read n i=1 fact=1 while [ $i -le $n ] do fact=`expr $fact \* $i` i=`expr $i + 1`

done echo 'The factorial of' $n 'is' $fact 8)no.of lines do not contain vowels echo 'enter a file name' read fn awk '$0 !~/[aeiou]/ { c=c+1 } END { print("The no.of lines that do not contain vowels:",c) }' $fn 9) no.of lines,words,characters
echo "enter a file name" read fn awk '{ w=w+NF c=c+length($0) } END { print("No.of lines:",NR) print("No.of words:",w) print("No.of characters:",c) }' $fn

10)copying a file
#include<stdio.h> #include<fcntl.h> #define MAX_SIZE 1000 main() { int fd1,fd2,r1,w1; char buffer[MAX_SIZE]; char sourceName[100],destName[100]; printf("enter the source file\n"); scanf("%s",sourceName); printf("enter a new file name"); scanf("%s",destName); fd1=open(sourceName,O_RDONLY); r1=read(fd1,buffer,MAX_SIZE); fd2=open(destName,O_CREAT|O_RDWR,0600); w1=write(fd2,buffer,r1); } #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *from, *to; char ch; if(argc!=3) { printf("Usage: copy <source> <destination>\n"); exit(1); }

/* open source file */ if((from = fopen(argv[1], "rb"))==NULL) { printf("Cannot open source file.\n"); exit(1); } /* open destination file */ if((to = fopen(argv[2], "wb"))==NULL) { printf("Cannot open destination file.\n"); exit(1); } /* copy the file */ while(!feof(from)) { ch = fgetc(from); if(ferror(from)) { printf("Error reading source file.\n"); exit(1); } if(!feof(from)) fputc(ch, to); if(ferror(to)) { printf("Error writing destination file.\n"); exit(1); } } if(fclose(from)==EOF) { printf("Error closing source file.\n"); exit(1); } if(fclose(to)==EOF) { printf("Error closing destination file.\n"); exit(1); } return 0; }

11a) cat command


#include<fcntl.h> #define MAX_SIZE 500 main() { int fd1,n; char buf[MAX_SIZE],fname[20]; printf("enter a file name\n"); scanf("%s",fname); fd1=open(fname,O_RDONLY); if(fd1==-1) printf("the file does not exist"); else { printf("The contents of file %s are:\n",fname); n=read(fd1,buf,MAX_SIZE);

} }

write(1,buf,n);

11b)mv command
#include<fcntl.h> #include<sys/stat.h> main(int argc,char *argv[]) { open(argv[1],O_RDONLY); creat(argv[2],S_IWUSR); rename(argv[1],argv[2]); unlink(argv[1]); }

12) list of directory files in directory


#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<dirent.h> main() { struct stat buf; struct dirent *direntp; DIR *dirp; char dirName[40],name[40]; printf("enter directory name\n"); scanf("%s",&dirName); chdir(dirName); dirp=opendir(dirName); while((direntp=readdir(dirp))!=NULL) { stat(direntp->d_name,&buf); if(S_ISDIR(buf.st_mode)) printf("%s\n",direntp->d_name); } }

14)filename,inode number in a given directory


#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<dirent.h> main() { struct stat buf; struct dirent *direntp; DIR *dirp; char dirName[40],name[40]; printf("enter directory name\n"); scanf("%s",&dirName); chdir(dirName); dirp=opendir(dirName);

while((direntp=readdir(dirp))!=NULL) { stat(direntp->d_name,&buf); printf("%d %s",buf.st_ino,direntp->d_name); printf("\n"); } }

15) redirect standard output to a file


#include<stdio.h> #include<fcntl.h> main(int argc,char **argv) { int fd; if(argc!=3) { printf("please provide necessary arguments"); exit(1); } fd=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC); if(fd==-1) { printf("failed to open a file"); exit(1); } dup2(fd,1); execlp(argv[1],argv[1],NULL); }

16)printing parent and child


#include<stdio.h> #include<fcntl.h> main() { pid_t pid; pid=fork(); if(pid==-1) printf("failed to fork"); else if(pid==0) printf("child process:%d\n",getpid()); else printf("parent process:%d",getpid());

17)(i) zombie process


#include<stdio.h> main()

int pid; pid=fork(); if(pid!=0) { printf("parent process:%d\n",getpid()); while(1) sleep(100); } else { printf("child id:%d\n",getpid()); exit(1); }

17)(ii) zombie process

#include<stdio.h> main() { int pid; pid=fork(); if(0==pid) { printf("child process %d \n",getpid()); _exit(0); } else { wait(0); sleep(30); printf("parent process \n"); } }

17)(iii)zombie process
#include<sys/wait.h> #include<stdlib.h> #include<unistd.h> int main(void) { pid_t pids[10]; int i; for(i=9;i>=0;--i) { pids[i]=fork(); if(pids[i]==0) { sleep(i+1); _exit(0); } } for(i=9;i>=0;i--) waitpid(pids[i],NULL,0); return 0; }

18)i) Orphan process

#include<stdio.h> main() { int pid; pid=fork(); if(pid==0) { printf("I am the child process with PID:%d and my parent id is: %d\n\n",getpid(),getppid()); sleep(15); printf("I am the child process with PID:%d and my parent id: %d\n",getpid(),getppid()); } else { printf("I am the parent process with PID:%d\n \n",getpid()); printf("My child's PID is:%d\n\n",pid); sleep(1); } printf("PID %d terminates \n",getpid()); /*Both processes execute this*/}

18) ii)Orphan process

#include<stdio.h> main() { int pid; pid=fork(); if(0==pid) { printf("%d\n",getpid()); sleep(5); printf("%d\n",getpid()); } else { printf("%d\n",getpid()); printf("bye from parent\n"); exit(0); } }

19)executing two commands concurrently using pipe

#include<stdio.h> main(int argc,char **argv) { int pid,fd[2],p; if(argc!=3) { printf("insufficient arguments"); exit(1); } p=pipe(fd); if(p==-1)

} pid=fork(); if(pid==-1) { printf("fork failed"); exit(1); } if(pid==0) { close(fd[0]); dup2(fd[1],1); close(fd[1]); execlp(argv[1],argv[1],NULL); } else { close(fd[1]); dup2(fd[0],0); close(fd[0]); execlp(argv[2],argv[2],NULL); } }

printf("pipe creation failed"); exit(1);

20a)named pipe

#include<stdio.h> #include<fcntl.h> main() { char *msg="hai"; int fd,pfd; pfd=mkfifo("pipe1",0666); if(pfd==-1) { printf("error creating the named pipe"); exit(1); } fd=open("pipe1",O_WRONLY); if(fd==-1) { printf("failed to open a pipe"); exit(1); } write(fd,msg,4); unlink("pipe1"); }

20b)named pipe

#include<stdio.h> #include<fcntl.h> #define MSGSIZE 60 main() { char msg[MSGSIZE]; int fd,n;

fd=open("pipe1",O_RDONLY); if(fd==-1) { printf("failed to open pipe"); exit(1); } for(;;) { while((n=read(fd,msg,MSGSIZE))>0) printf("message: %s\n",msg); }}

21)parent writes child reads through pipe


#include<stdio.h> main(){ int p[2],pid;char *buf; buf=(char *) malloc(10); pipe(p); pid=fork(); if(pid>0){printf("parent is writing on pipe\n"); write(p[1],"hello",5); } else { read(p[0],buf,5); printf("child is reading %s \n",buf);} close(p[0]);close(p[1]); }

22)messagequeue(sender)
#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> struct msgbuf{ long mtype; char mtext[40]; }; main() { int msqid,len,ret; struct msgbuf msgsend={0,"\0"}; msqid=msgget((key_t)7,IPC_CREAT|0666); if(-1==msqid) { perror("msgget:"); exit(1); } printf("enter msgtype:\n"); scanf("%d",&msgsend.mtype); printf("enter msgtext:\n"); scanf("%s",msgsend.mtext); len=strlen(msgsend.mtext); ret=msgsnd(msqid,&msgsend,len,0); if(-1==ret) { perror("msgsnd:"); exit(1); } else

printf("msgsent:\n");

23)message queue(receiver)

#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> struct msgbuf{ long mtype; char mtext[40]; }; main() { int msqid,len,type,ret; struct msgbuf msgread={0,"\0"}; msqid=msgget((key_t)7,IPC_CREAT|0666); if(-1==msqid) { perror("msgget:"); exit(1); } printf("enter the msg type:\n"); scanf("%d",&type); len=sizeof(msgread.mtext); ret=msgrcv(msqid,&msgread,len,type,0); printf("ret=%d\n",ret); if(-1==ret) { perror("msgrcv:"); exit(1); } else printf("message type=%d message text=%s\n",msgread.mtype,msgread.mtext); }

26)suspending and resuming using signals


#include<stdio.h> #include<signal.h> void fun(int k) { printf("signal is caught %d\n",k); } void bun(int p) { printf("i am in bun %d\n",p); } main() { signal(SIGINT,SIG_IGN);

sleep(5); signal(SIGINT,fun); sleep(5); signal(SIGINT,bun); sleep(5); signal(SIGINT,SIG_DFL); for( ; ; );

27)producer and consumer using semaphores

#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<signal.h> void abc() { printf("bye bye from child\n"); exit(0); } void xyz() { printf("bye bye from parent\n"); exit(0); } main() { int semid; int pid; struct sembuf sop; semid=semget((key_t)29,1,IPC_CREAT|0666); if(-1==semid) { perror("semget:"); exit(1); } semctl(semid,0,SETVAL,6); printf("semid=%d\n",semid); pid=fork(); if(0==pid) { sop.sem_num=0; sop.sem_op=-1; sop.sem_flg=0; signal(SIGALRM,abc); alarm(6); while(1) { semop(semid,&sop,1); printf("child%d\n",semctl(semid,0,GETVAL,0)); sleep(1); } } else { sleep(7);

sop.sem_num=0; sop.sem_op=1; sop.sem_flg=0; signal(SIGALRM,xyz); alarm(6); while(1) { semop(semid,&sop,1); printf("parent%d\n",semctl(semid,0,GETVAL,0)); sleep(1);}}}

28)a)unix domain sockets(interaction between server and client)(server)


#include #include #include #include <sys/types.h> <sys/socket.h> <sys/un.h> <stdio.h> 3 "mysocket" /* no. of strings */ /* addr to connect */

#define NSTRS #define ADDRESS

/* * Strings we send to the client. */ char *strs[NSTRS] = { "This is the first string from the server.\n", "This is the second string from the server.\n", "This is the third string from the server.\n" }; main() { char c; FILE *fp; int fromlen; register int i, s, ns, len; struct sockaddr_un saun, fsaun; /* * Get a socket to work with. This socket will * be in the UNIX domain, and will be a * stream socket. */ if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { perror("server: socket"); exit(1); } /* * Create the address we will be binding to. */ saun.sun_family = AF_UNIX; strcpy(saun.sun_path, ADDRESS); /* * Try to bind the address to the socket. We * unlink the name first so that the bind won't * fail.

* * The third argument indicates the "length" of * the structure, not just the length of the * socket name. */ unlink(ADDRESS); len = sizeof(saun.sun_family) + strlen(saun.sun_path); if (bind(s, &saun, len) < 0) { perror("server: bind"); exit(1); } /* * Listen on the socket. */ if (listen(s, 5) < 0) { perror("server: listen"); exit(1); } /* * Accept connections. When we accept one, ns * will be connected to the client. fsaun will * contain the address of the client. */ if ((ns = accept(s, &fsaun, &fromlen)) < 0) { perror("server: accept"); exit(1); } /* * We'll use stdio for reading the socket. */ fp = fdopen(ns, "r"); /* * First we send some strings to the client. */ for (i = 0; i < NSTRS; i++) send(ns, strs[i], strlen(strs[i]), 0); /* * Then we read some strings from the client and * print them out. */ for (i = 0; i < NSTRS; i++) { while ((c = fgetc(fp)) != EOF) { putchar(c); if (c == '\n') break; } }

/* * We can simply use close() to terminate the

* connection, since we're done with both sides. */ close(s); } exit(0);

28b)unix domain sockets(interaction between server and client) Client.c


#include #include #include #include <sys/types.h> <sys/socket.h> <sys/un.h> <stdio.h> 3 "mysocket" /* no. of strings */ /* addr to connect */

#define NSTRS #define ADDRESS

/* * Strings we send to the server. */ char *strs[NSTRS] = { "This is the first string from the client.\n", "This is the second string from the client.\n", "This is the third string from the client.\n" }; main() { char c; FILE *fp; register int i, s, len; struct sockaddr_un saun; /* * Get a socket to work with. This socket will * be in the UNIX domain, and will be a * stream socket. */ if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { perror("client: socket"); exit(1); } /* * Create the address we will be connecting to. */ saun.sun_family = AF_UNIX; strcpy(saun.sun_path, ADDRESS); /* * * * * * * Try to connect to the address. For this to succeed, the server must already have bound this address, and must have issued a listen() request. The third argument indicates the "length" of

* the structure, not just the length of the * socket name. */ len = sizeof(saun.sun_family) + strlen(saun.sun_path); if (connect(s, &saun, len) < 0) { perror("client: connect"); exit(1); } /* * We'll use stdio for reading * the socket. */ fp = fdopen(s, "r"); /* * First we read some strings from the server * and print them out. */ for (i = 0; i < NSTRS; i++) { while ((c = fgetc(fp)) != EOF) { putchar(c); if (c == '\n') break; } }

/* * Now we send some strings to the server. */ for (i = 0; i < NSTRS; i++) send(s, strs[i], strlen(strs[i]), 0); /* * We can simply use close() to terminate the * connection, since we're done with both sides. */ close(s); exit(0); }

30a)shared memory(client)
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #define SHMSZ 27 main() { char c; int shmid; key_t key; char *shm, *s;

key = 5678; if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } printf("shmid=%d\n",shmid); /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now put some things into the memory for the * other process to read. */ s = shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; /* * Finally, we wait until the other process * changes the first character of our memory * to '*', indicating that it has read what * we put there. */ while (*shm != '*') sleep(1); exit(0); }

30b)shared memory(server)
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #define SHMSZ 27 main() { int shmid; key_t key; char *shm, *s;

/* * We need to get the segment named * "5678", created by the server. */ key = 5678; /* * Locate the segment.

*/ if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now read what the server put in the memory. */ for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); /* * Finally, change the first character of the * segment to '*', indicating we have read * the segment. */ *shm = '*'; } exit(0);

You might also like