Sunday 6 April 2014

Example Programs on OS Concepts

myHeader.h
#include<stdio.h>
#include<stdlib.h>
#include<sys/resource.h>
#include<utime.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<signal.h>
#include<time.h>
#include<sys/resource.h>
#include<unistd.h>
#include<utime.h>
#include<fcntl.h>
#include<sys/timeb.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<limits.h>
#include<dirent.h>
#include<errno.h>
#include<stdio.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/time.h>
#include<sys/timeb.h>
stat to find size of our file
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdio.h>
main(int argc,char **argv)
{
struct stat buf;
if(stat(argv[1],&buf)==-1)
{
perror("stat");
return;
}
printf("size=%u\n",(unsigned int)buf.st_size);
printf("mode=%u\n",(unsigned int)buf.st_size);
}
file name:madan
welcome madan reddy
hyderabad
always truth wins
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
size=48
mode=48
program to find resource limits
#include"myHeader.h"
main()
{
struct rlimit v,new;
getrlimit(RLIMIT_STACK,&v);
new=v;
new.rlim_cur=2000;
setrlimit(RLIMIT_STACK,&new);
getrlimit(RLIMIT_STACK,&new);
printf("new resource current limit address=%lu \n new resource maximum limit=%lu\n",new.rlim_cur,new.rlim_max);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex2.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
new resource current limit address=2000 
 new resource maximum limit=18446744073709551615
fork system call for dual processes
#include"myHeader.h"
main()
{
fork(); fork(); fork();
printf("pid:%d ppid:%d \n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex3.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
pid:3351 ppid:2817 
pid:3353 ppid:3351 
pid:3356 ppid:3353 
pid:3352 ppid:3351 
pid:3354 ppid:1144 
pid:3357 ppid:1144 
pid:3355 ppid:1144 
pid:3358 ppid:1144
pipe for read and write operations
#include"myHeader.h"
char ch='a';
main()
{
int p[2];
pipe(p);
if(fork()==0)
{
printf("%d ",getpid());
read(p[0],&ch,1);
printf("%c\n ",ch);
//ch++;
}
else
{
printf("%d ",getppid());
for(ch='a';ch<='z';ch++)
write(p[1],&ch,1);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex4.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
2817 3378 a
parent & child relation program
#include"myHeader.h"
int main()
{
if(fork())
{
if(fork())
{
if(fork())
printf("parent id :%d\n",getpid());
else
printf("child3 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child2 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child1 id: %d parent id: %d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
child1 id: 3514 parent id: 3513
parent id :3513
child2 id: 3515 parent id:3513
child3 id: 3516 parent id:1144
program with 3forks(3child,3parents)
#include"myHeader.h"
int main()
{
if(fork())
{
printf("child1 id: %d parent1 id: %d\n",getpid(),getppid());
if(fork())
{
printf("child2 id: %d parent2 id: %d\n",getpid(),getppid());
if(fork())
printf("child3 id: %d parent3 id: %d\n",getpid(),getppid());
else
printf("child3 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child2 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child1 id: %d parent id: %d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex7.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
child1 id: 3551 parent1 id: 2817
child1 id: 3552 parent id: 3551
child2 id: 3551 parent2 id: 2817
child2 id: 3553 parent id:3551
child3 id: 3551 parent3 id: 2817
child3 id: 3554 parent id:3551
program to execute multiple processes through command line arguments
#include"myHeader.h"
main(int argc,char **argv)
{
int i;
for(i=1;i<argc;i++)
{
system(argv[i]);
}
printf("task completed...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex9.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out cal date madan
     April 2014       
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           
                      
Fri Apr  4 07:12:10 IST 2014
sh: 1: madan: not found
task completed...
Program for vfork
#include"myHeader.h"
int data=0;
main(int argc,char **argv)
{
int ret;
ret=vfork();
if(ret==0)
{
printf("in child process:data=%d\n",data);
data++;
printf("in child process:data(inc)=%d\n",data);
}
else
{
printf("in parent process:data=%d\n",data);
data++;
printf("in parent process:data(inc)=%d\n",data);
}
exit(0);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ vi ex10.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex10.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in child process:data=0
in child process:data(inc)=1
in parent process:data=1
in parent process:data(inc)=2
program to print long listing of files using execlp
#include"myHeader.h"
main(int argc,char **argv)
{
execlp("ls","ls","-li","-t",NULL);
}
output:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
total 2884
4325487 -rwxr-xr-x 1 madan madan    8560 Apr  4 07:17 a.out
4325511 -rw-r--r-- 1 madan madan      87 Apr  4 07:17 ex11.c
4325510 -rw-r--r-- 1 madan madan     319 Apr  4 07:17 ex10.c
4325509 -rw-r--r-- 1 madan madan     131 Apr  4 07:14 ex9.c
4325508 -rw-r--r-- 1 madan madan     234 Apr  4 07:10 ex8.c
4325507 -rw-r--r-- 1 madan madan     453 Apr  4 07:04 ex7.c
4325505 -rw-r--r-- 1 madan madan     306 Apr  4 07:02 ex6.c
4325493 -rw-r--r-- 1 madan madan     281 Apr  4 06:56 ex5.c
4325506 -rw-r--r-- 1 madan madan     613 Apr  4 06:55 myHeader.h
4325496 -rw-r--r-- 1 madan madan     230 Apr  4 06:51 ex4.c
4325495 -rw-r--r-- 1 madan madan     104 Apr  4 06:49 ex3.c
4325494 -rw-r--r-- 1 madan madan     322 Apr  4 06:49 ex2.c
4325488 -rw-r--r-- 1 madan madan 2886512 Apr  4 06:44 myHeader.h.gch
4325490 -rw-r--r-- 1 madan madan      48 Apr  4 06:43 madan
4325491 -rw-r--r-- 1 madan madan     277 Apr  4 06:43 ex1.c
fork system call example
#include"myHeader.h"
main()
{
if(fork())
{
printf("in parent process...\n");
printf("process id:%d\n",getpid());
}
else
{
printf("in child process...\n");
printf("child process id:%d\n",getpid());
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex12.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent process...
process id:3891
in child process...
child process id:3892
example program for parrellel processing
#include"myHeader.h"
int pid[3];
main()
{
int status;
if(pid[2]==fork())
{
printf("in parent2..\n");
wait(-1,&status,NULL);
if(pid[1]==fork())
{
printf("in parent1..\n");
if(pid[0]==fork())
printf("in parent0..\n");
else
{
sleep(1);
printf("in child0 process...\n");
}
}
else
{
sleep(10);
printf("in child1 process...\n");
}
}
else
{
sleep(5);
printf("in child2 process...\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex13.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent2..
in parent1..
in parent0..
in child0 process...
in child2 process...
madan@madan-Lenovo-G570:~/madan/osconcepts$ in child1 process...
parent process waits for child process
#include"myHeader.h"
main()
{
if(fork())
{
printf("in parent...\n");
sleep(2);
printf("parent is terminating...\n");
}
else
{
printf("in child...\n");
sleep(7);
while(1);
printf("child is terminating...\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex14.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent...
in child...
parent is terminating...
Program for process synchronisation using waitpid
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/resource.h>
int p[2];
pipe(p);
int t;
char str1[100];
void child1()
{
printf("child1 id:%d parent id:%d\n",getpid(),getppid());
char str1[100]=("child1 is terminating first\n");
printf("in child process1:\n");
srand(getpid());
t1=rand()%10+1;
printf("in child process1, time delay:%d\n",t);
sleep(t1);
write(p[1],t1,100);
printf("child1 process is exiting..\n");
}
void child2()
{
printf("child2 id:%d parent id:%d\n",getpid(),getppid());
char str1[100]=("child2 is terminating first\n");
printf("in child process2:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process2, time delay:%d\n",t);
sleep(t);
write(p[1],str1,100);
printf("child2 process is exiting..\n");
}
void child3()
{
printf("child3 id:%d parent id:%d\n",getpid(),getppid());
char str1[100]=("child3 is terminating first\n");
printf("in child process3:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process3, time delay:%d\n",t);
sleep(t);
write(p[1],str1,100);
printf("child3 process is exiting..\n");
}

void parent()
{
int status;
printf(" in parent process...\n");
wait(&status);
status=status>>8;
read(p[0],str1,20);
printf("%s ",str1);
printf("parent process is exiting...\n");
}

main()
{
if(fork())
{
if(fork())
{
if(fork())
parent();
else
child1();
}
else
child2();
}
else
child3();
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex15.c
ex15.c:7:1: warning: data definition has no type or storage class [enabled by default]
 pipe(p);
 ^
ex15.c:7:1: warning: parameter names (without types) in function declaration [enabled by default]
ex15.c: In function �child1�:
ex15.c:19:1: warning: passing argument 2 of �write� makes pointer from integer without a cast [enabled by default]
 write(p[1],t1,100);
 ^
In file included from myHeader.h:10:0:
/usr/include/unistd.h:366:16: note: expected �const void *� but argument is of type �int�
 extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur;
                ^
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
 in parent process...
child2 id:4256 parent id:4254
in child process2:
in child process2, time delay:2
child3 id:4255 parent id:4254
in child process3:
in child process3, time delay:1
child1 id:4257 parent id:4254
in child process1:
in child process1, time delay:0
child3 is terminating first
child3 process is exiting..
child2 is terminating first
child2 process is exiting..
child1 process is exiting..


 parent process is exiting...
Program to calculate arithmetic expressions in child processes
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/resource.h>
int t;
int a=20,b=10,sum,sub,mul;
int child1()
{
printf("child1 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process1:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process1, time delay:%d\n",t);
sleep(t);
sum=a+b;
return sum;
printf("child1 process is exiting..\n");
}
int child2()
{
printf("child2 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process2:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process2, time delay:%d\n",t);
sleep(t);
sub=a-b;
return sub;
printf("child2 process is exiting..\n");
}
int child3()
{
printf("child3 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process3:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process3, time delay:%d\n",t);
sleep(t);
mul=a*b;
return mul;
printf("child3 process is exiting..\n");
}

int parent()
{
int status;
printf(" in parent process...\n");
wait(&status);
status=status>>8;
printf("first termionating is:%d\n",status);
printf("parent process is exiting...\n");
}

main()
{
if(fork())
{
if(fork())
{
if(fork())
parent();
else
printf("multiplication=%d\n",child3());
}
else
printf("substraction=%d\n",child2());
}
else
printf("addition=%d\n",child1());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex16.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
 in parent process...
child3 id:4407 parent id:4404
in child process3:
in child process3, time delay:9
child2 id:4406 parent id:4404
in child process2:
child1 id:4405 parent id:4404
in child process1:
in child process1, time delay:1
in child process2, time delay:4
addition of two numbers=30
first termionating is:27
parent process is exiting...
madan@madan-Lenovo-G570:~/madan/osconcepts$ substraction=10
multiplication=200
example for pause system call
#include"myHeader.h"
main()
{
int a=10;
if(fork())
{
printf("%d ",a);
pause();
}
else
{
printf("%d ",a++);
printf("%d ",a);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex17.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
10 11
program to find size of pipe
#include"myHeader.h"
main(int argc,char **argv)
{
int cnt=0;
int p[2];
pipe(p);
fcntl(p[1],F_SETFL,O_NONBLOCK);
while(1)
{
if(write(p[1],"a",1)==1)
cnt++;
else
break;
}
printf("count=%d\n",cnt);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
count=65536
program to print character by character from file named madan
#include"myHeader.h"
main(int argc,char **argv)
{
char ch;
int fd;
fd=open(argv[1],O_RDONLY);
while(read(fd,&ch,1)==1)
{
printf("ch= %c ",ch);
write(1,&ch,1);
}
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
welcome madan reddych= w ch= e ch= l ch= c ch= o ch= m ch= e ch=   ch= m ch= a ch= d ch= a ch= n ch=   ch= r ch= e ch= d ch= d ch= y ch= 

hyderabad ch= h ch= y ch= d ch= e ch= r ch= a ch= b ch= a ch= d ch= 

always truth wins ch= a ch= l ch= w ch= a ch= y ch= s ch=   ch= t ch= r ch= u ch= t ch= h ch=   ch= w ch= i ch= n ch= s ch=
program to compare two files using stat
#include"myHeader.h"
main(int argc,char **argv)
{
struct stat buf;
struct stat buf1;
if(stat(argv[1],&buf)==-1)
{
perror("stat");
exit(0);
}
printf("%o..\n",&buf.st_mode);
printf("%u..\n",&buf.st_size);
if(stat(argv[2],&buf1)==-1)
{
perror("stat");
exit(0);
}
printf("%o..\n",&buf1.st_mode);
printf("%u..\n",&buf1.st_size);

if((&buf.st_mode)==(&buf1.st_mode))
printf("two files are same...\n");
else
printf("two files are not same...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ vi ex20.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex20.c
ex20.c: In function �main�:
ex20.c:11:1: warning: format �%o� expects argument of type �unsigned int�, but argument 2 has type �__mode_t *� [-Wformat=]
 printf("%o..\n",&buf.st_mode);
 ^
ex20.c:12:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �__off_t *� [-Wformat=]
 printf("%u..\n",&buf.st_size);
 ^
ex20.c:18:1: warning: format �%o� expects argument of type �unsigned int�, but argument 2 has type �__mode_t *� [-Wformat=]
 printf("%o..\n",&buf1.st_mode);
 ^
ex20.c:19:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �__off_t *� [-Wformat=]
 printf("%u..\n",&buf1.st_size);
 ^
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan madan
25763044350..
2949400832..
25763044570..
2949400976..
two files are not same...

program for time stamp updation using utime
#include"myHeader.h"
main(int argc,char **argv)
{
struct utimbuf v;

v.actime=v.modtime=time(0);
if((utime(argv[1],&v)==-1))
{
perror("utime");
exit(0);
}
printf("access time:%d modification time:%d\n",v.actime,v.modtime);
printf("time stamp updated..\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex21.c
ex21.c: In function �main�:
ex21.c:12:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �__time_t� [-Wformat=]
 printf("access time:%d modification time:%d\n",v.actime,v.modtime);
 ^
ex21.c:12:1: warning: format �%d� expects argument of type �int�, but argument 3 has type �__time_t� [-Wformat=]
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
access time:1396578347 modification time:1396578347
time stamp updated..
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
utime: Bad address

program to update modification details of two files
#include"myHeader.h"
main(int argc,char **argv)
{
struct utimbuf v1;
if(utime(argv[1],&v1)==-1)
{
perror("utime");
exit(0);
}
v1.actime=v1.modtime=time(0);
struct utimbuf v2;
if(utime(argv[2],&v2)==-1)
{
perror("utime");
exit(0);
}
v2.actime=v2.modtime=time(0);
if(v1.modtime==v2.modtime)
printf("two time stamps have same mod time...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cat madan
welcome madan reddy
hyderabad
always truth wins
madan@madan-Lenovo-G570:~/madan/osconcepts$ cat mohan
welcome htderabad
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex22.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan mohan
two time stamps have same mod time...
program for parent process waits for the status of child process
#include"myHeader.h"
int data=10;
main()
{
int status;
if(fork()==0)
{
printf("data in child process:%d\n",data);
sleep(5);
printf("child exiting...\n");
}
else
{
printf("data in parent process:%d\n",data);
wait(&status);
printf("parent exiting....\n");
//sleep(2);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex23.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
data in parent process:10
data in child process:10
child exiting...
parent exiting....
Example program for sleep
#include"myHeader.h"
int pid[3];
main()
{
int status;
if(pid[2]==fork())
{
printf("in parent2..\n");
wait(-1,&status,NULL);
if(pid[1]==fork())
{
printf("in parent1..\n");
if(pid[0]==fork())
printf("in parent0..\n");
else
{
sleep(1);
printf("in child0 process(1sec)...\n");
}
}
else
{
sleep(10);
printf("in child1 process(10sec)...\n");
}
}
else
{
sleep(5);
printf("in child2 process(5sec)...\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex24.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent2..
in parent1..
in parent0..
in child0 process(1sec)...
in child2 process(5sec)...
madan@madan-Lenovo-G570:~/madan/osconcepts$ in child1 process(10sec)...
Example program for wait(&status)
#include"myHeader.h"
main()
{
int i,status;
if(fork()>0)
{
printf("parent id:%d\n",getppid());
for(i=6;i<10;i++)
{
printf("i=%d\n",i);
sleep(1);
}
wait(&status);
}
else if(fork()==0)
{
printf("child id:%d\n",getpid());
for(i=10;i<25;i++)
{
printf("i=%d\n",i);
sleep(1);
}
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex25.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent id:2817
i=6
child id:4930
i=10
i=7
i=11
i=8
i=12
i=9
i=13
i=14
madan@madan-Lenovo-G570:~/madan/osconcepts$ i=15
i=16
i=17
i=18
i=19
i=20
i=21
i=22
i=23
i=24
Example program for fork system call
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
if(fork())
printf("parent=%d bash=%d\n",getpid(),getppid());
else
if(fork())
printf("child4==%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child3=%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child2=%d parent=%d\n",getpid(),getppid());
else
printf("child1=%d parent=%d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex26.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent=4954 bash=2817
madan@madan-Lenovo-G570:~/madan/osconcepts$ child4==4955 parent=1144
child3=4956 parent=4955
child2=4957 parent=1144
child1=4958 parent=4957
Program to find size of pipe
#include"myHeader.h"
int i=0;
main()
{
int count=0;
int fp[2];
pipe(fp);
while(1)
{
printf("count=%d\n",count);
count++;
write(fp[1],&count,1);
}
}
OutPut:

count=65535
count=65536
Example program on preprocessor directives
#include<stdio.h>
#include<stdlib.h>
#define a 2+5
//#define a 5-2
main()
{
int i;
i=a*a*a;
printf("result:%d\n",i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex30.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
result:27
program to view differences between process id, parent process id, group id
#include"myHeader.h"
main()
{
printf("child:%d parent %d groupid:%d uid:%d\n",getpid(),getppid(),getgid(),getuid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex31.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
child:5444 parent 2817 groupid:1000 uid:1000
Program to view relationship among process id's
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
if(fork())
{
//system("date");
        printf("parent=%d bash=%d\n",getpid(),getppid());

        if(fork())
        {
//      system("cal");
        printf("parent=%d bash=%d\n",getpid(),getppid());
        if(fork())
        {
        printf("parent=%d bash=%d\n",getpid(),getppid());
        if(fork())
        printf("parent=%d bash=%d\n",getpid(),getppid());
                else
                printf("in process3 child3=%d parent= %d...\n",getpid(),getppid());
        }
        else
        printf("in process2 child2= %d parent=%d...\n",getpid(),getppid());
        }
else
printf("in process1 child1= %d parent=%d...\n",getpid(),getppid());
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex32.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent=5464 bash=2817
parent=5464 bash=2817
in process1 child1= 5466 parent=5464...
parent=5464 bash=2817
parent=5464 bash=2817
in process2 child2= 5467 parent=5464...
in process3 child3=5468 parent= 1144...
Examle program to refer process id's
#include"myHeader.h"
main()
{
int i;
printf("process:%d\n",getpid());
for(i=1;i<=3;i++)
{
fork();
printf("process id:%d\n",getpid());
}
printf("exiting ......\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex33.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
process:5492
process id:5492
process id:5492
process id:5494
process id:5492
exiting ......
process id:5494
exiting ......
process id:5495
exiting ......
process id:5496
exiting ......
process id:5493
process id:5493
process id:5493
exiting ......
madan@madan-Lenovo-G570:~/madan/osconcepts$ process id:5497
process id:5498
process id:5497
exiting ......
exiting ......
process id:5499
exiting ......
program to print file descriptor value
#include"myHeader.h"
main(int argc,char **argv)
{
int ret,i;
char readbuf[30];
ret=open(argv[1],O_RDONLY);
if(argc!=2)
printf("enter correct arguments\n");
if(ret==-1)
{
perror("open");
return;
}
printf("file description:%d\n",ret);
while((read(ret,readbuf,30)))
printf("%s",readbuf);
//readbuf[20]='\0';
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex34.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
file description:5
welcome madan reddy
hyderabad
always truth wins
y
hyderabad
Program to read a file
#include"myHeader.h"
main(int argc,char **argv)
{
char ch;
int fd;
fd=open(argv[1],O_RDONLY);
if(fd==-1)
{
perror("open");
return;
}
while(read(fd,&ch,1)==1)
{
write(1,&ch,1);
//printf("%c",ch);
}
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
welcome madan reddy
hyderabad
always truth wins
program to view semaphore operation
#include"myHeader.h"
main(int argc,char **argv)
{
int id;
struct sembuf v;
id=semget(1,3,IPC_CREAT|0600);
printf("id=%d in process %d\n",id,getpid());
printf("about to pend on semaphore\n");
v.sem_num=0;
v.sem_op=0;
v.sem_flg=0;
semop(id,&v,1);
printf("after semop on critical region\n");
printf("process exiting...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex36.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
id=0 in process 5741
about to pend on semaphore
after semop on critical region
process exiting...
program to lock a file
#include"myHeader.h"
#include<sys/stat.h>
#include<sys/ipc.h>
main()
{
struct flock lockbuf;
int fd;
fd=open("datafile",O_RDWR);
getchar();
printf("enter to lock file...\n");
lockbuf.l_type=F_WRLCK;
lockbuf.l_whence=0;
lockbuf.l_start=0;
lockbuf.l_len=0;
if(fcntl(fd,F_SETLKW,&lockbuf)<0)
perror("fcntl");
printf("LOCKED");
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out

enter to lock file...
fcntl: Bad file descriptor
LOCKED
Alarm program by using sigaction
#include"myHeader.h"
void isr(int n)
{
printf("ALARM.....\n");
alarm(2);
}
main()
{
struct sigaction v;
printf("%d\n",getpid());
alarm(5);
v.sa_handler=isr;
v.sa_flags=SA_RESETHAND;
v.sa_flags=0;
sigemptyset(&v.sa_mask);
sigaction(SIGALRM,&v,NULL);
printf("ALARM is set...\n");
printf("in infinite loop....\n");
while(1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
5876
ALARM is set...
in infinite loop....

ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....

ALARM.....
ALARM.....
^C
Program for sigalrm
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
void isr(int n)
{
static int i=0;
printf("in ISR......\n");
if(++i==3)
exit(0);
alarm(2);
}
main()
{
printf("proces:%d\n",getpid());
//signal(SIGINT,SIG_DFL);
alarm(3);
signal(SIGALRM,isr);
printf("alarm is set....\n");
//printf("in infinite loop...\n");

while(1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex40.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
proces:5897
alarm is set....
in ISR......
in ISR......
in ISR......
srand to provide random time delays
#include<stdio.h>
#include<stdlib.h>


void child1(void)
{
int t;
printf("in child1\n");
srand(getpid());
t=rand()%10+1;
printf("child1 delay for %d seconds\n",t);
sleep(t);
printf("child1 exiting....\n");
exit(0);
}
void child2(void)
{
int t;
printf("in child2\n");
srand(getpid());
t=rand()%10+1;
printf("child2 delay for %d seconds\n",t);
sleep(t);
printf("child2 exiting....\n");
exit(0);
}
void parent(void)
{
int status;
printf("parent waiting...\n");
while(1){
wait(&status);
status>>=8;
if(status==2)
break;
}
printf("child exiting...\n");
printf("parent exiting....\n");
exit(0);
}
main()
{
if(fork())
{
 if(fork())
 parent();
 else
 child2();
}
else
child1();
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in child1
parent waiting...
child1 delay for 2 seconds
in child2
child2 delay for 3 seconds
child1 exiting....
child2 exiting....
program to calculate arithmatic operation in child process and print result in parent process
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int child(int a,int b)
{
int sum;
sum=a+b;
sleep(5);
return(sum);
//parent(sum);
}
int parent(int sum)
{
int status;
wait(&status);
status>>=8;
//printf("sum=%d\n",sum);
//printf("sum=%d\n",sum);
}
main(int argc,char **argv)
{
int a,b,sum=0;
if(argc!=3)
printf("enter correct no of arguments\n");
a=atoi(argv[1]);
b=atoi(argv[2]);
if(fork())
printf("sum in parent process is=%d\n",parent(sum));
//printf("sum in parent process=%d\n",parent(sum));
else
printf("sum in child process=%d\n",child(a,b));
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex43.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out 10 20
sum in child process=30
sum in parent process is=24
Program for system call
#include<stdio.h>
#include<sys/resource.h>a
#include"myHeader.h"
main()
{
printf("system=%d parent=%d\n",getpid(),getppid());
printf("p2 id=%d \tp2 parent=%d\n",getpid(),getppid());
system("date");
system("cal");
system("./system");
printf("p2=%d parent=%d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex44.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
system=6169 parent=2817
p2 id=6169  p2 parent=2817
Fri Apr  4 09:10:48 IST 2014
     April 2014       
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           
                      
sh: 1: ./system: not found
p2=6169 parent=2817