Unix

60
S.No Program List Page No 1 Unix command 2 Shell Programming 2.1 Shell Script for addition, subtraction, multiplication and division of two numbers 2.2 Shell Script to Calculate the simple interest 2.3 Shell Script to Calculate the area of rectangle parameter of rectangle, area of circle and circumference of circle. 2.4 Ramesh basic salary is input through the keyboard his da is 40% of salary and hra is 20% of basic salary W.A.P to calculate the given salary 2.5 Shell Script to enter the marks of five subjects and calculate the percentage of five subjects. 2.6 Shell Script to find the greatest no. between two no. 2.7 In a company an employee is paid as under (a) If his basic pay less than 1500 then hra is 10% of basic pay ,da is 90% of basic pay (b) If his basic pay equal or above 1500 then hra is 500 and da is 98% of basic pay If the employee salary is input through keyboard, write a program to find its gross salary. 2.8 Shell Script to check the given number is even or odd 2.9 Shell Script to check the given year is leap year or not: 2.10 If cost and selling price of an item is input through the keyboard check whether the seller has made profit or encored loss. All determine how much profit or loss encored. 2.11 Shell Script to find the greatest no. among three number (using multiple if) 2.12 The marks obtained by student are input through keyboard in five subject the student get the division as: % above or equal to 60 – first division % 50 -59 -second division % 40 -49 - third division % less than 40 -failed 2.13 Shell Script for the addition subtraction multiplication division of two number through case 2.14 Shell Script a program for command who list and cal through case 2.15 Shell Script to print 1 to 10 numbers 1

Transcript of Unix

Page 1: Unix

S.No Program List Page No1 Unix command2 Shell Programming

2.1 Shell Script for addition, subtraction, multiplication and division of two numbers

2.2 Shell Script to Calculate the simple interest

2.3 Shell Script to Calculate the area of rectangle parameter of rectangle, area of circle and circumference of circle.

2.4 Ramesh basic salary is input through the keyboard his da is 40% of salary and hra is 20% of basic salary W.A.P to calculate the given salary

2.5 Shell Script to enter the marks of five subjects and calculate the percentage of five subjects.

2.6 Shell Script to find the greatest no. between two no.

2.7 In a company an employee is paid as under (a) If his basic pay less than 1500 then hra is 10% of basic pay ,da is 90% of basic pay(b) If his basic pay equal or above 1500 then hra is 500 and da is 98% of basic pay If the employee salary is input through keyboard, write a program to find its gross salary.

2.8 Shell Script to check the given number is even or odd

2.9 Shell Script to check the given year is leap year or not:

2.10 If cost and selling price of an item is input through the keyboard check whether the seller has made profit or encored loss. All determine how much profit or loss encored.

2.11 Shell Script to find the greatest no. among three number (using multiple if)

2.12 The marks obtained by student are input through keyboard in five subject the student get the division as:% above or equal to 60 – first division % 50 -59 -second division% 40 -49 - third division% less than 40 -failed

2.13 Shell Script for the addition subtraction multiplication division of two number through case

2.14 Shell Script a program for command who list and cal through case

2.15 Shell Script to print 1 to 10 numbers

2.16 Shell Script to calculate simple interest for three times

2.17 Shell Script to find the factorial of a given number

2.18 Shell Script to print the table of any number

2.19 Shell Script to find the factorial between given range

2.20 Shell Script to check whether the given number is prime or not

2.21 Shell Script to print the Fibonacci series

2.22 Shell Script to find the (i) Sum of digit (ii) Reverse of a digit

2.23 Shell Script to convert the temperature given Fahrenheit into Centigrade and temperature given in centigrade into Fahrenheit.

2.24 Shell Script to convert the distance given in Km into meter feet and inches

2.25 Shell Script to find the power of any number

2.26 Shell Script to find whether two files are equal or not

2.27 Shell Script to find whether a file is a file or directory

1

Page 2: Unix

3 System Programming3.1 Use the fork() create the Process3.2 Check the Child Process is created or not3.3 Use the fork to create the child process3.4 Create the parent child process Relationship3.5 CALLING OF OUTSIDE PROCEDURE OF FILE BY USE OF

EXECL ( ) SYSTEM CALL4 Socket Programming

4.1 Socket programming in sever side4.2 Socket programming in client side5 Inter Process Communication

5.1 Create the message queues5.2 Try to generate message queues if its already generated it will

produces error 5.3 Setting Permission in message queues5.4 Removing message Queues5.5 Program of piping5.6 To check max no of pipes that can be opened concurrently5.7 Program for pipe-in pipe out5.8 Creating semaphore5.9 To find max no of semaphores set that can be created5.10 Program to delete semaphores5.11 Program for synchronization

2

Page 3: Unix

(2.1) The addition, subtraction, multiplication and division of two numbers:

tput clearecho “Enter the first number:”read n1echo “Enter the second number:”read n2k = `expr $n1 + $n2`echo “The addition of numbers is “ $kk = `expr $n1 - $n2`echo “ The subtraction of numbers is:” $kk = `expr $n1 \* $n2`echo “The multiplication of numbers is “ $kk = `expr $n1 + $n2`echo “The division of numbers is “ $k

Output:

Enter the first number20Enter the second number2The addition of two numbers: 22The subtraction of two numbers: 18The multiplication of two numbers: 40The division of two numbers: 10

3

Page 4: Unix

(2.2) Calculate the simple interest

tput clearecho “Enter the Principal”read pecho “Enter the rate:”read recho “Enter the time”read tsi = `expr $p \* $r \* $t / 100`echo “the simple interest is :” $si

Output:

Enter the principal40Enter the rate5Enter the time2The simple interest is : 4

4

Page 5: Unix

(2.3) Calculate the area of rectangle parameter of rectangle, area of circle and circumference of circle.

tput clearecho “ Enter the breadth:”read becho “ Enter the length:”read lecho “Enter the radius:”read ra1 = `expr $l \* $b`echo “ The area of rectangle is :” $a1p=`expr 2\* $l + 2\* $b `echo “ The parameter of rectangle is :”$pa=`expr 22\* $r \*$r/7`echo “the area of circle is :”$ac= `expr 2\* 22\* $r/7`echo “ The circumference of circle is”$c

Output:

Enter the breadth:2Enter the length:3Enter the radius:4The area of rectangle is :6The parameter of rectangle is :10The area of circle is :30The circumference of circle is:25

5

Page 6: Unix

(2.4) Ramesh basic salary is input through the keyboard his da is 40% of salary and hra is 20% of basic salary SHELL SCRIPT to calculate the given salary

tput clearecho “Enter the basic salary”read bsda= ‘expr 40\*$bs/100’hra= ‘expr 20\*$bs/100’gs=’expr $da + $hra + $bs’echo “The gross salary is:” $gs

Output:

Enter the basic salary: 50The gross salary is: 80

6

Page 7: Unix

(2.5) SHELL SCRIPT ato enter the marks of five subject and calculate the percentage of five subject .

Tput clear Echo “Enter the marks in English:”Raed aEcho “Entr the marks in hindi”read bEcho “Enter the marks in science:”read cEcho “Enter the marks in maths:”read dEcho “Enter the marks in history:”Read et = ‘expr $a+$b +$c + $d + $e’p = ‘expr $t \*100/500’echo “the percentage is:” $p

Output:

Enter the marks in english:50Enter the marks in hindi:50Enter the marks in science:50Enter the marks in maths:50Enter the marks in history:50Percentage is :50

7

Page 8: Unix

(1.6) SHELL SCRIPT to find the greatest no. between two no.

tput clearecho ”Enter the first no.”raed aecho “Enter the second no.”read bif test $a –gt $bthen echo “first no. is greater:”elseecho “second no. is greater:”fi

Output:

Enter the first no. 8Enter the second no.5First no. is greater:

8

Page 9: Unix

(2.7) In a company an employee is paid as under

(a) If his basic pay less than 1500 then hra is 10% of basic pay ,da is 90% of basic pay(b) If his basic pay equal or above 1500 then hra is 500 and da is 98% of basic pay If the employee salary is input through keyboard ,write a program to find its gross salary.

Echo “Enter the basic pay:”read bs if test $bs –lt 1500then hra =‘expr $bs /10’da = ‘expr $bs\*9/10’gs = ‘expr $bs + $hra +$da’echo “the gross salary is :” $gselseda = ‘expr $bs\* 98/100’gs = ‘expr $bs +500 + $da’echo “the gross salary is:” $gsfi

Output:

Enter the basic salary :1600The gross salary is :3668

9

Page 10: Unix

(2.8) SHELL SCRIPT to check the given number is even or odd .

tput clearecho “Enter the no.”read numif test ‘expr $num%2’ –eq 0thenecho “the given no. is even:”elseecho “the given no. is odd”fi

Output:

Enter the given no.:8The given no. is even:

10

Page 11: Unix

(2.9) SHELL SCRIPT to check the given year is leap year or not:

tput clearecho “Enter the year”raed yif test ‘expr $y%4’ –eq 0thenecho “the given year is a leap year”else echo “the given year is not leap year:”fi

Output:

Enter the year2004The given year is a leap year:

11

Page 12: Unix

(2.10) If cost and selling price of an item is input through the keyboard

check whether the seller has made profit or encored loss. All determine how much profit or loss encored.

tput clear echo "Enter the cost price:"read cecho "Enter the selling price:"read s if test $s -gt $cthen p = 'expr $s -$c'echo "the seller has made profit:"echo "profit = " $pelsel = 'expr $c - $s'echo "the seller has made loss:"echo "loss = " $lfi

Output:

Enter the cost price:400Enter the selling price:450the seller has made profit profit =50

12

Page 13: Unix

(2.11) SHELL SCRIPT to find the greatest no. among three number (using multiple if) :

Echo “Enter the three no.:”read a b cif test $a –gt $bthen if test $a – gt $c then echo “the greatest no. is :” $a fifiif test $b – gt $athen if test $b –gt $c then echo “the greatest no. is ” $b fifiif test $c – gt $athen if test $c – gt $b then echo “the greatest no. is ” $c fifi

Output:

Enter the three number 10 20 15The greatest no.is 20

13

Page 14: Unix

(2.12) The marks obtained by student are input through keyboard in five subject the student get the division as:% above or equal to 60 – first division % 50 -59 -second division% 40 -49 - third division% less than 40 -failed

tput clear echo “Enter the marks in English:”read aecho “Enter the marks in maths:”read becho “Enter the marks in hindi:”read cecho “Enter the marks in science:”read decho “Enter the marks history:”raed eper = ‘expr \($a + $b + $c + $d + $e)/500’echo “the percentage is :” $perif test $per – ge 60then echo “first division:”fiif test $per – ge 50 –a $per – lt 60thenecho “second division”fiif test $per –ge 40 –a $per –lt50thenecho “third division:’fiif test $per – lt 40thenecho “failed”fi

Output:Enter the marks in English: 80Enter the marks in maths:80Enter the marks in hindi: 80Enter the marks in science:80Enter the marks in history:80The percentage is :80

14

Page 15: Unix

First division:

(2.13) SHELL SCRIPT for the addition subtraction multiplication division of two number through case:

tput clearecho “Enter the first no. :”read aecho “Enter the second no.”read becho “1. addition”echo “2. subtraction”echo “3 multiplication”echo “4. division”echo “Enter the your choice:”read chcase $ch in

1) c=’expr $a + $b’ echo “ The addition of two number is “ $c ;;2) d=’expr $a - $b”

echo “the subtraction of two number is “ $d;;

3) e=’expr $a\* $b’echo “ The multiplication of two number “ $e;;

4) f=’expr $a/$b’echo “the division of two number is “$f;;

*) Echo “ You have a wrong choice”;;

esac exit 0

Output:Enter the first no: 2Enter the second no:21. addition2. subtraction3 multiplication4. divisionEnter you choice 1The addition of two number is 4

15

Page 16: Unix

(2.14) SHELL SCRIPT a program for command who list and cal through case

tput clearecho “enter the command”read commandcase $command inwho) echo “ Who is running” who ;;List) echo “running list” ls ;;Cal) echo “running cal” cal ;;*) echo “ bad command” ;; esac exit 0

Output:

Enter the command whoRoot consol Aug 1 07:51 (:0)

16

Page 17: Unix

(2.15) SHELL SCRIPT to print 1 to 10 numbers

tput cleari=1while [$i –ie 10]do echo $i i= `expr $i + 1`done

output:12345678910

17

Page 18: Unix

(2.16) SHELL SCRIPT to calculate simple interest for three times

tput cleari=1while [ $i –le 3] do echo “enter the time” read t echo “enter the rate” read r echo “enter the principle” read p si = `expr $p \* $r \* $t /100` echo “the simple interest is “$si i=`expr $i +1done

Output:Enter the time 2Enter the rate 5Enter the Principle 40The simple interest is : 4

Enter the time 3Enter the rate 5Enter the Principle 50The simple interest is : 7

Enter the time 4Enter the rate 7Enter the Principle 70The simple interest is : 19

18

Page 19: Unix

(2.17) SHELL SCRIPT to find the factorial of a given number

tput clearecho “enter the number”read np=1while [ $n –gt 1] do p= ‘expr $p \* $n` n=`expr $n -1`doneecho “the factorial is :” $p

Output:

Enter the number 5 The factorial is :120

19

Page 20: Unix

(2.18) SHELL SCRIPT to print the table of any number

tput clearecho “enter the any number”read ni=nwhile [$i –le 10] do echo “$n \* $i = ` expr $n \* $i`” i= `expr $i +1`done

Output:

Enter the any number :2 2 *1 = 2 2 *2 = 4 2 *3 = 6 2 *4 = 8 2 *5 = 10 2 *6 = 12 2 *7 = 14 2 *8 = 16 2 *9 = 18 2 *10 = 20

20

Page 21: Unix

(2.19) SHELL SCRIPT to find the factorial between given range

tput clearecho “ enter the lower limit”read necho “enter the upper limit”read mq= `expr $n`while [$q –le $m] do p=1 n=`expr $q` while [$n –ne 1] do p=`expr $p \* $n` n=`expr $q +1` done q=`expr $q +1` d=`expr $q -1` echo “the factorial of number $d is “ $pdone

output:

enter the lower limit :2enter the upper limit:5the factorial of number 2 is : 2the factorial of number 3 is : 6the factorial of number 4 is : 24the factorial of number 5 is : 120

21

Page 22: Unix

(2.20) SHELL SCRIPT to check whether the given number is prime or not

tput clearecho “ enter the any number”read np=2flag =0while [$p –lt $n] do if test `expr $n % $p` -eq 0 then flag break elif test `expr $n % $p` -ne 0 then flag =0 fi p=`expr $p +1`done

if test `expr $flag` -eq 0 then echo “ the number is prime” elif test `expr $flag` -ne 0 then echo “the number is not prime”fi

Output:

Enter the any number :7The number is prime

22

Page 23: Unix

(2.21) SHELL SCRIPT to print the Fibonacci series

tput clearecho "Enter the limit:"real la=0b=1i=2echo $aecho $bwhile [ $i –le $l]do fab =` expr $a + $b ` echo $faba = $bb = $fabi=` expr $i + 1 `done

Output:Enter the limit :70112358

23

Page 24: Unix

(2.22) SHELL SCRIPT to find the (i) Sum of digit (ii) Reverse of a digit

tput clearecho “enter the digit =”read nwhile [ $n –gt 0]do r=`expr $n % 10` s=`expr $s + $r` n= `expr $n /10`doneecho “the sum of digit is =“ $swhile [$n –gt 0]do r=`expr $n % 10` s=`expr $s\*10 + $r` n= `expr $n /10`doneecho “reverse of digit is =“ $s

Output:

enter the number = 231sum of digit =6reverse of digit is =132

24

Page 25: Unix

(2.23) SHELL SCRIPT to convert the temperature given Fahrenheit into Centigrade and temperature given in centigrade into Fahrenheit.

tput clear echo “enter the temperature in Fahrenheit “read fc= `expr \($f -32\) \* 5/9`echo “the temperature in centigrade is “ $c

echo “Enter the temperature in centigrade”read dg=`expr \(9 \* $d/5\) +32`echo “the temperature in Fahrenheit is “ $g

Output:

enter the temperature in Fahrenheit 98the temperature in centigrade is 36Enter the temperature in centigrade 36the temperature in Fahrenheit is 98

25

Page 26: Unix

(2.24) SHELL SCRIPT to convert the distance given in Km into meter feet and inches

tput clearecho “ enter the distance in Km”read kecho “ converting in meter”m=`expr $k \* 1000`echo “ the distance in meter is “ $mecho “converting in feet”f=`expr $k \* 3330`echo “the distance in feet is “ $fecho “ Converting in inches”i=`expr $k \* 39960`echo “ the distance in inches “ $i

Output:Enter the distance in Km 10converting in meterthe distance in meter is 10000converting in feetthe distance in feet is 33300Converting in inchesthe distance in inches 399600

26

Page 27: Unix

(2.25) SHELL SCRIPT to find the power of any number

tput clearecho “enter the number”read aecho “enter the power”read bp=ai=1while [$i –lt $b]do p=`expr $p \* $a` i=`expr $i +1`doneecho “the power of number is “$p

Output:Enter the number 2Enter the power 2The power of number is 4

27

Page 28: Unix

(2.26) Shell Script to find whether two files are equal or not.

echo "Enter the files "read var1read var2if [ -f $ var1 ] –a [ -f $var2 ]then echo "Files Exist"fiif cmp $var1 $var2then echo " Files are Equal" else echo " Files are not Equal" fi

Output:Enter the filesFile1File2Files are equal

28

Page 29: Unix

(2.27) SHELL SCRIPT to find whether a file is a file or directory.

echo "Enter the file or directory "read var1if [ -f $ var1 ] then echo "This is a File "else echo " This is a Directory" fi

Output:Enter the file or directoryXyzThis is a directory

29

Page 30: Unix

(3.1) use the fork create the processes

#include <stdio.h>main(){

fork(); printf(“ Process is %d”,getpid();

}

Output:

Process is 5546

30

Page 31: Unix

(3.2) create the child process

# include<stdio.h>

Main(){ int pid; pid = fock(); printf(“ \n process id %d”, getpid() );

if ( pid == 0) { printf (“ Child Process”); }}

Output:

Process id 5432Child process

31

Page 32: Unix

(3.3) check the process is created or not

# include < stdio.h>

main(){Int pid;pid = fork();if (pid <0) { printf(“\n pid = %d “,pid); printf(“Process id = %d”,getpid()); printf(“\n fork() failed”); }else { printf(“\n pid = %d “,pid); printf(“Process id = %d”,getpid()); printf(“\n fork() Successfully”); }

}

Output:Pid = 4532Process id = 4568Fork() successfully

32

Page 33: Unix

(3.4) Create the Parent Child Process Relationship

#include <stdio.h>main(){int pidpid = fork();if(pid == 0){ Printf (“\n I am child, value of pid = %d Process id = %d”,pid,getpid()); Printf(“\n child parent id = %d, process id = %d”,pid,getpid());}

else{ Printf (“\n I am Parent, value of pid = %d Process id = %d”,pid,getpid()); Printf(“\n parent parent id = %d, process id = %d”,pid,getpid());

}}

Output:

I am parent value of pid = 5678 Process id = 5643Parent parent id = 3456 process id = 4589

33

Page 34: Unix

(3.5) Calling the Outside procedure of file using by execl() System call

ex1.c#include <stdio.h>main(){

printf(“Before execute my id is %d”,getpid());printf(“my process is %d \n”,getpid());printf(“Execution start \n”);execl(“/usr/quest/ex2”,”ex2”,(char *)0);printf(“this will not print”);

}

Exe2.c

#include<stdio.h>

main(){printf(“Before execution my id is %d”,getpid());printf(“my process is %d \n”,getppid();printf(“ Execution Start”);}

34

Page 35: Unix

(4.1) Socket programming in sever side

#include <sys/types.h>#include<sys/socket.h>#include<sys/un.h>#include<stdio.h>#define nstrs 3#define address “my socket”

Char * str[nstrs]={ “this is first string from server \n”, “this is second string from server \n”, “this is third string from server \n”};

main(){

char c;file *fp;int fromlen;register int I,s,ns,len;struct socadd_un saun, fsan;if((( s = socket( AF_UNIX,SOCK_STREAM,0 ))<0); { perror ( “Server : Socket”); exit(1) }

Saun.sun.family = AF_UNIX;strcpy (saun.sun.path, Address);unlike(address);len = sizeof(sann.sun.family)+strlen(sann.sun.path);if (binds(s,&saun.len)<0) { perror ( “Server : Bind”); exit(1) }

If (listen(s,5)<0) { perror ( “Server : listen”); exit(1) }

35

Page 36: Unix

If (( ns = accepts (s,&fsaun,&fromlen))<0) { perror ( “Server : accept”); exit(1) }

p = fdopen(ns, “r”);

for (i = 0; i < nstrs ; i++) send ( ns,strs[i],strlen(strrs[i],0));

for( I = 0;i<nstrs;i++) { While (( c=fgetc(fp)) ! = EOF) { putchar(c); if ( c == ‘\n’) break; }close(s);exit(0);

}

}

36

Page 37: Unix

(4.2) Socket programming in client side

#include <sys/types.h>#include<sys/socket.h>#include<sys/un.h>#include<stdio.h>#define nstrs 3#define address “my socket”

Char * str[nstrs]={ “this is first string from client \n”, “this is second string from client \n”, “this is third string from client \n”};

main(){

char c;file *fp;int fromlen;register int I,s,ns,len;struct socadd_un saun, fsan;if((( s = socket( AF_UNIX,SOCK_STREAM,0 ))<0); { perror ( “client : Socket”); exit(1) }

Saun.sun.family = AF_UNIX;strcpy (saun.sun.path, Address);unlike(address);len = sizeof(sann.sun.family)+strlen(sann.sun.path);if (binds(s,&saun.len)<0) { perror ( “client : Bind”); exit(1) }

If (listen(s,5)<0) { perror ( “client : listen”); exit(1) }

37

Page 38: Unix

If (( ns = accepts (s,&fsaun,&fromlen))<0) { perror ( “client : accept”); exit(1) }

p = fdopen(ns, “r”);

for (i = 0; i < nstrs ; i++) send ( ns,strs[i],strlen(strrs[i],0));

for( I = 0;i<nstrs;i++) { While (( c=fgetc(fp)) ! = EOF) { putchar(c); if ( c == ‘\n’) break; }close(s);exit(0);

}

}

38

Page 39: Unix

(5.1) Create the message queues

#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>

main(){

int msg_id; msg_id = msgget((key_t)10,IPC_CREAT); printf(“\n message queue created id is %d”,msg_id); }

Output:

Message queue created id is 22

39

Page 40: Unix

(5.2) try to generate message queue if it is already generate its will produces error

#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>

main(){

int msg_id; key_t key = 10; msg_id = msgget((key,IPC_CREAT)| IPC_EXCL); if (msg_id < 0) { perror(“error”) }

else printf(“\n message queue created id is %d”,msg_id); }

Output:

Message queue created id is 21

40

Page 41: Unix

(5.3) setting permission is message queue

#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>

main(){

int msg_id; key_t key = 10; msg_id = msgget((key,IPC_CREAT)| 0644); if (msg_id < 0) { perror(“msgget fail”) }

else printf(“\n message queue created successfully with my permission); }

Output:

Message queue created successfully with permission

41

Page 42: Unix

(5.4) Removing message queues

#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>

main(){ int I,msgid; key tkey =100; for (i=0;i<10;i++) { msgid = msgget(key,IPC_CREATE | 0666) if (msgid <0) { perror(“msgget() failed “); exit(1); } printf(“ msgid = %d”,msgid); if(msgget(msgid,IPC_RMID,0)<0) { printf(“\n msgget() failed”); exit(1); } }}

Output:

msgid= 170345msgid= 170546msgid= 170367msgid= 170345msgid= 170429msgid= 174589msgid= 171258msgid= 171548msgid= 171598

42

Page 43: Unix

msgid= 170456

(5.5) Program of Piping

#include<stdio.h>

main(){ int p[2]; pipe(p); printf(“\n p[0] =%d \np[1] = %d\n”,p[0],p[1]);}

Output:

p[0] = 3p[1]= 4

43

Page 44: Unix

(5.6) Program for pipe-in pipe-out

# include<stdio.h># define msg_size 16main((){ char *msg1 = “hello 1”; char *msg2 = hello 2”; char *msg3 = “hello3”; char in_buf[msg_size]; int p[2],j;

pipe(p);write(p[1],msg1,msg_size);write(p[1],msg2,msg_size);write(p[1],msg3,msg_size);

for(j=0;j<3;j++){ read(p[0].in_buf,msg_size);printf(“\n in_buffer = %d\n”,in_buf);}

exit(0)

}

Output:

In_buffer = -1073465789In_buffer = -1001359874In_buffer = -1023589471

44

Page 45: Unix

(5.7) to check max no of pipes that can be opened concurrently

# include<stdio.h>

main(){

int p[2];int ret_val,I;while(1){ ret_val = pipe(p); if(ret_val == -1){ printf(“\n max no of pipes that can be opened concurrently = %d”,i); break;}i++;printf(“\n p[0] =%d \n p[1] = %d \n”,p[0],p[1]);}}

Output:

p[0] = 1009p[1] = 1010

p[0] = 1011p[1] = 1012

p[0] = 1013p[1] = 1014

p[0] = 1015p[1] = 1016

p[0] = 1017p[1] = 1018

Max no of pipes that can be opened concurrently= 134514171

45

Page 46: Unix

(5.8) creating semaphore

#include<sys/types.h>#include<sys/ipc.h>#include<stdio.h>

main(){ int sem_id,key,nsem;key=(key_t)0x20;nsem=0sem_id=semget(key,nsem,IPC_CREAT |0666);printf(“\n Semaphore created with id = %d \n”,sem_id);}

Output:

Semaphore created with id = -1

46

Page 47: Unix

(5.8) to find max no of semaphore set that can be created

# include<sys/types.h># include<sys/ipc.h># include<stdio.h>

main(){ int semid,nsemset,key,nsem,flag; nsem = 1; flag = 0666| IPC_CREAT; for(nsemset =0;; nsemset++) { key = (key_t)nsemset; semid=semget(nsemset,nsem,flag); if(semid>0) printf(“\n create semaphore with id %d”,semid); else

{ printf(“max no of semaphore sets =%d”,nsemset); exit(0); } }}

Output:

Max no of semaphore sets = 0

47

Page 48: Unix

(5.10) program to delete semaphores

#include<sys/types.h>#include<sys/ipc.h>#include<stdio.h>

main(){ int semid,key,flag,nsem,i;key = (key_t)0x30;flag= IPC_CREAT |0666;for(i=0;;i++){

nsem = i+1; semid=semget(key,nsem,flag); if(semid>0) printf(“semaphore create with id = %d”,semid); else { printf(“\n max semaphores in one set are %d”i); exit(0); }}}

Output:

Semaphore created with id = 32769Max semaphores in one set are :1

48

Page 49: Unix

(5.11) Program for synchronization

#include<stdio.h>

main(){

int i,pid;printf(“\n Ready to fork”);pid=fork();if(pid==0) { printf(“child starts”,i); for(i=0;i<10;i++) {

printf(“\n i=%d”,i); printf(“\n child ends”); }else { wait(0); printf(“\n parent process”); }

}}

49

Page 50: Unix

Output:

Ready to forkChild startsi=0Child endsi=1Child endsi=2Child endsi=3Child ends

i=4Child endsi=5Child endsi=6Child endsi=7Child endsi=8Child endsi=9Child endsParent process

50