#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...