Q5.Pass the file descriptor across process using execl?
Q6.Pass the file descriptor across process using execv?
Q7.Pass the file descriptor across process using execp?
Q8.Pass the file descriptor across process using execvp?
Q9. From 1st process open a file and write some data into it.Then read the same data from another process after passing file descriptor as an argument using 4 program execl,execv,execp,execvp?
int main() { int fd; char sfd; char *ch; ch = (char*)malloc(sizeof(char)*40); if(!ch) { perror("malloc"); exit(EXIT_FAILURE); } printf("Enter some string to write in file\n"); fgets(ch,30,stdin); printf("%s:Begin :File %d:pid\n",__FILE__,getpid()); fd = open("myfile",O_RDWR); write(fd,ch,30); sprintf(&sfd,"%d",fd); execl("./hii",&sfd,NULL); printf("%s:End %d:pid\n",__FILE__,getpid()); } ---------------------------------------------------------------------- other process where it will use the fd of above program and read the content of that file which is written by the fd used in process1
int main(int argc ,char *argv[]) { int fd,i,ret; char c; printf("%s:Begin :File %d:pid\n",__FILE__,getpid()); fd = atoi(argv[0]); //printf("Hello!!!Never lose your Hope,Tomorrow will be new Sunshine :) \n"); printf("%d:argv[0]\n",fd); printf("fd value is = %d\n",fd); lseek(fd,0,SEEK_SET); for(i=0;i<10;i++) { ret =read(fd,&c,1); printf("No of acutal charchter read is ret =%d\n",ret); printf("string value is: %c \n",c); } close(fd); printf("%s:End %d:pid\n",__FILE__,getpid()); } Please check
Q.9 using execv 1. Here i get a file descriptor and write some data into a file and send this FD to other program in a array that will used to read the data from the same file. int main() { int fd; char sfd; //char ch[40] = {"Hello my Name is Himanshu"}; char *ch; ch = (char*)malloc(sizeof(char)*40); if(!ch) { perror("malloc"); exit(EXIT_FAILURE); } printf("Enter some string to write in file\n"); fgets(ch,30,stdin); printf("%s:Begin :File %d:pid\n",__FILE__,getpid()); fd = open("myfile",O_RDWR); write(fd,ch,30); sprintf(&sfd,"%d",fd); char *c[] = {&sfd,"Hello","Himanshu",NULL}; //an array in which i insilized the FD and all arguments execv("./hiiexecv",c); printf("%s:End %d:pid\n",__FILE__,getpid()); } ============================================================== 2.Here i am using the FD after converting it into integer and read the same data from same file in which i have write the data int main(int argc ,char *argv[]) { int fd,i; char c; printf("%s:Begin :File %d:pid\n",__FILE__,getpid()); fd = atoi(argv[0]); printf("Hello!!!Never lose your Hope,Tomorrow will be new Sunshine :) \n"); printf("%s:argv[0]\n",argv[0]); printf("%s:argv[1]\n",argv[1]); printf("%s:argv[2]\n",argv[2]); printf("%s:argv[3]\n",argv[3]); lseek(fd,0,SEEK_SET); int ret; for(i=0;i<20;i++) { ret = read(fd,&c,1); printf("The actual number of character read is %d \n",ret); printf(" The string is %c\n",c); } printf("%s:End %d:pid\n",__FILE__,getpid()); } if any possible correction make me inform .. Thanks