Выбрать главу

12

no overtime pay

enter working hours for employee number 2

21

no overtime pay

enter working hours for employee number 3

33

no overtime pay

enter working hours for employee number 4

45

overtime pay = Rs. 60

enter working hours for employee number 5

50

overtime pay = Rs. 120

85. Write a program to generate all combinations of digits 1, 2 and 3 to form different numbers using for loops.

$vi prg85

clear

for i in 1 2 3

do

for j in 1 2 3

do

for k in 1 2 3

do

echo $i $j $k

done

done

done

Sample Run

$sh prg85

1 1 1

1 1 2

1 1 3

1 2 1

1 2 2

1 2 3

1 3 1

1 3 2

1 3 3

2 1 1

2 1 2

2 1 3

2 2 1

2 2 2

2 2 3

2 3 1

2 3 2

2 3 3

3 1 1

3 1 2

3 1 3

3 2 1

3 2 2

3 2 3

3 3 1

3 3 2

3 3 3

86. Write a program to check whether a given number is an Armstrong number or not, An Armstrong number is one in which the sum of cube of each of the digits equals that number.

$vi prg86

clear

echo Enter a Number

read n

m=$n

s=0

while [ $n -gt 0 ]

do

q=‘expr $n / 10’

r=‘expr $n - \( $q \* 10 \)’

s=‘expr $s + \( $r \* $r \* $r \)’

n=$q

done

if [ $s -eq $m ]

then

echo The Number Is Armstrong

else

echo The Number Is Not Armstrong

fi

$sh prg86

Enter a Number

153

The Number Is Armstrong

$sh prg86

Enter a Number

152

The Number Is Not Armstrong

87. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153=(1*1*1)+(5*5*5)+(3*3*3)

$vi prg87

clear

i=1

echo Armstrong numbers are

while [ $i -le 500 ]

do

a=‘echo $i % 10|bc’

b=‘echo $i % 100|bc’

b=‘echo \( $b - $a \) / 10|bc’

c=‘echo $i / 100|bc’

sum=‘echo \( $a \* $a \* $a \) + \( $b \* $b \*

$b \) + \($c \* $c \* $c \)|bc’

if [ $sum -eq $i ]

then

echo $i

fi

i=‘expr $i + 1’

done

Sample Run

$sh prg87

Armstrong numbers are

1

153

370

371

407

88.  Write a program for swapping of two numbers without using any third variable.

$vi prg88

clear

echo enter numbers a and b

read a

read b

b=‘expr $a - $b’

a=‘expr $a - $b’

b=‘expr $a + $b’

echo After Swapping

echo a = $a

echo b = $b

Sample Run

$sh prg88

enter numbers a and b

12

3

After Swapping

a = 3

b = 12

$sh prg88

enter numbers a and b

21

23

After Swapping

a = 23

b = 21

123

89. Program to get pid of the process.

$vi prg89.c

#include<stdio.h>

#include<sys/types.h>

int main()

{

int pid;

pid=getpid();

printf(“The process id of the process is %d\n”,pid);

return 0;

}

Compile

$cc -o prg89 prg89.c

Run

$./prg89 Output is

The process id of the process is 4884

90. Program to get pid of the parent process.

$vi prg90.c

#include<stdio.h>

#include<sys/types.h>

int main()

{

int ppid;

ppid=getppid();

printf(“The process id of the parent process

is %d\n”,ppid);

return 0;

}

Compile

$cc -o prg90 prg90.c

Run

$./prg90

Output is

The process id of the parent process is 4904

Parent and Child Process

Any running program is called a process. From the process we can create another process. There is a parent-child relationship between these two processes. The way to achieve this is by using a function called fork(). This function splits the running process into two processes at the point where fork is called. The first is known as parent and the new process created is known as child. Both the processes have same copy of the code after the point where fork() is called.

91. Program to show how fork() divide the process into two parts.

$vi prg91.c

#include<stdio.h>

#include<sys/types.h>

int main()

{

printf(“Hello\n”);

fork(); #fork system call is used to create child

printf(“World\n”);

return 0;

}

Compile

$cc -o prg91 prg91.c

Run

$./prg91 Output is

Hello World World

92. Program to show the existence of both child and parent processes.

$vi prg92.c

#include<stdio.h>

#include<sys/types.h>

int main()

{

int pid;

pid=fork(); #pid=pid of child (fork()

returns pid of child process)

if(pid==0)

{

#This part gets executed in child

printf(“I am child. The value of

variable pid is %d\n”, pid);

printf(“I am child and my process

id is %d\n”, getpid());

printf(“I am child and my parent process

id is %d\n”, getppid());

}

else

{

#This part gets executed in parent

printf(“I am parent. The value of pid

is %d\n”, pid);

printf(“I am parent and my process

id is %d\n”, getpid());

printf(“I am parent and my parent process

id is %d\n”, getppid());

}

return 0;

}

Compile

$cc -o prg92 prg92.c

Run

$./prg92 Output is

I am child. The value of variable pid is 0

I am child and my process id is 4985

I am child and my parent process id is 4984

I am parent. The value of pid is 4985

I am Parent and my process id is 4984

I am Parent and my parent process id is 4822

Zombie and Orphans

When we fork a new child process and the parent and the child continue to execute, there are two possibilities – either the child process ends first or the parent process ends first.

If child terminates earlier than the parent then the parent process is known as Zombie.

If parent terminates earlier than the child then the child process is known as Orphan.

93. Program to show the orphan process.

#include<stdio.h>

#include<sys/types.h> int main()

{

int pid;

pid=fork();

if(pid==0)

{

printf(“I am child and my pid is %d\n”,getpid());

printf(“I am child and my ppid is %d\n”,getppid());

sleep(10);

printf(“\nI am child and my pid is %d”\n,getpid());

printf(“I am child and my ppid is %d\n”,getppid());