/MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler...../
//THIS IS SERVER PROCESS....FURTHER CHILD WILLBE HYZACKED BY PARENT
/*An implementation showing hw one process can be hyjacked by another using execl system call*/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>//file related operations
#include<unistd.h>//machine bbased statements
#include<string.h>//for strlen...strcpy...etc....only to printf a string ..it is not required
int main()
{
int ret,fd[2],arr[2];
char rd[4],wr[4];
ret=pipe(fd);
if(ret==0)
printf("client pipe created\n");
ret=fork();
if(ret==0)
{
printf("in child process\n");
sprintf(rd,"%d",fd[0]);//int value fd[0] ..i.e.read for pipe is converted into char type ..so char shold be a 4 byte array in which.....int value can be placed
sprintf(wr,"%d",fd[1]);//int value fd[1]...i.e.write for pipe is converted into char type ...so char should be 4 byte array..in which int value is placed..further on..this char type array will be argument of execl......bcz execl...which is used to replace the pcb of one process to pcb of another process...always takes char type arguments
printf("wr=%s rd=%s\n",wr,rd);//arrays r printed with %s
ret=execl("./client1","client1",rd,wr,NULL);//1st arg is path in which that compiled file is placed whose process is to be replaced....2nd arg is name of that file...3rd arg may be read or write char type argument....any no. of args may be send frm this process to another process..NULL means no arg...
printf("ret=%d in execl\n",ret);//these statements will not be printed..bcz pcb got replaced
printf("done\n");
}
else
{
sleep(4);//i had made parent to wait untill child executes
printf("in parent\n");//normally..parent & child r executed..together...on executing time seperate pc is also provided to them....
int main(int argc,char* argv[])//char type arg send by execl placed by this process will come to this main....as arguments
{
printf("argc=%d\n",argc);//argc is of int type ..so no. of arguments comes into it
printf("hi client1\n");
int fd,wfd=0;
printf("argv[1]=%s\n",argv[1]);//arg[0] is name of this file...arg[1] will be 1st arg send by process by which this process is replaced
printf("argv[2]=%s\n",argv[2]);//arg[1] will be 2nd arg..of char type..send by another process
//main also picks the args from command line..
sscanf(argv[1],"%d",&fd);//sscanf will convert char type value into int
//in which type is to be converted..always use address of that variable..either in sprintf..or in sscanf
printf("fd=%d wfd=%d\n",fd,wfd);
sscanf(argv[2],"%d",&wfd);
printf("fd=%d wfd=%d\n",fd,wfd);//print int type descriters...now we write into these descripters or read from these descriptors....& communicate to another process...whose child is to be replaced
return 0;
}
////////way that i find...only to use pipe is.....execl...pass its args...& communicate b/w two...so replacement of process may be very beneficial.......//////////