473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fork and fopen example

Hello,

I've got two questions from the code below::

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <assert.h>
  4.  
  5. int main(void){
  6.         int status;                             
  7.         int ch;
  8.         FILE *f1 = fopen("input.txt", "r");
  9.  
  10.         assert(f1 != NULL);
  11.         if(fork()){                            
  12.                 printf("ProgA_Father: father's PID: %d\n", getpid());
  13.                 printf("ProgA_Father: file: %d\n", f1);
  14.                 ch=getc(f1);
  15.                 printf("ProgA_Father: data from file: %c\n", ch);
  16.                 wait(&status);         
  17.                 printf("ProgA_Father: child's exiting code: %d\n", status);
  18.                 ch=getc(f1);
  19.                 printf("ProgA_Father: data from file: %c\n", ch);
  20.                 system("ps -al");      
  21.         }else{
  22.                 printf("ProgA_Child: child proccess created; PID: %d\n", getpid());
  23.                 printf("ProgA_Child: file: %d\n", f1);
  24.                 ch=getc(f1);
  25.                 printf("ProgA_Child: data from file: %c\n", ch);
  26.                 execl("b", "1", "2", "3", NULL);               
  27.  }
  28.         printf("\nProgA: all done.\n");
  29.         fclose(f1);
  30.         return 0;
  31. }
  32.  
  33. //-----------------------------------------------
  34.  
  35.  
  36. #include <stdio.h>
  37.  
  38. int main(int argc, char* argv[]){
  39.         int i;  
  40.         printf("ProgB: print arguments: \n");
  41.         for(i=0; i<argc; printf("%s\n", argv[i++]));
  42.                 printf("ProgB: all done!\n");
  43.         return 0;
  44. }
  45.  
  46.  
here are my questions:
1. Why does 'execl("b", "1", "2", "3", NULL);' string terminates child process? any functions placed below it were not completed.

2. Why does child proccess read faulty data from text file? f.e. files contains '123', so father reads '1' and '2', and child reads '`'.

thanks in advance.
Oct 15 '07 #1
2 7776
Hi

I can answer one of your two questions. You raised a question why execl() ends the child process.

All exec() family of function wil work like that. Whenver you call some other other program through execl() actually the called program will be replaced by the calling program. For example

Program A:
execl("program b");
printf("Program A after execl\n");
Program B:
printf("program b called and executed\n");

In the above example after calling execl() "Program A after exec" statement will never get printed.

This is because "A's" program memory will now be replaced by "B's" program. Now B will be running in the same memory area where "A" was residing.

If you are getting "Program A after execl" then it means that your execl() system call failed.

I hope I have given the correct information. If you find anything wrong please let me know. Whatever I studied I explained that.

And regarding your 2nd question I did not try that. May be after sometime I may reply for that question.

Thanks & Regards
Sathish Kumar

Hello,

I've got two questions from the code below::

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <assert.h>
  4.  
  5. int main(void){
  6.         int status;                             
  7.         int ch;
  8.         FILE *f1 = fopen("input.txt", "r");
  9.  
  10.         assert(f1 != NULL);
  11.         if(fork()){                            
  12.                 printf("ProgA_Father: father's PID: %d\n", getpid());
  13.                 printf("ProgA_Father: file: %d\n", f1);
  14.                 ch=getc(f1);
  15.                 printf("ProgA_Father: data from file: %c\n", ch);
  16.                 wait(&status);         
  17.                 printf("ProgA_Father: child's exiting code: %d\n", status);
  18.                 ch=getc(f1);
  19.                 printf("ProgA_Father: data from file: %c\n", ch);
  20.                 system("ps -al");      
  21.         }else{
  22.                 printf("ProgA_Child: child proccess created; PID: %d\n", getpid());
  23.                 printf("ProgA_Child: file: %d\n", f1);
  24.                 ch=getc(f1);
  25.                 printf("ProgA_Child: data from file: %c\n", ch);
  26.                 execl("b", "1", "2", "3", NULL);               
  27.  }
  28.         printf("\nProgA: all done.\n");
  29.         fclose(f1);
  30.         return 0;
  31. }
  32.  
  33. //-----------------------------------------------
  34.  
  35.  
  36. #include <stdio.h>
  37.  
  38. int main(int argc, char* argv[]){
  39.         int i;  
  40.         printf("ProgB: print arguments: \n");
  41.         for(i=0; i<argc; printf("%s\n", argv[i++]));
  42.                 printf("ProgB: all done!\n");
  43.         return 0;
  44. }
  45.  
  46.  
here are my questions:
1. Why does 'execl("b", "1", "2", "3", NULL);' string terminates child process? any functions placed below it were not completed.

2. Why does child proccess read faulty data from text file? f.e. files contains '123', so father reads '1' and '2', and child reads '`'.

thanks in advance.
Oct 15 '07 #2
I did some research on my own and I noticed that everything works fine with printf("blah blah", getc(f1)); before calling fork(). I wonder why it is so.
Oct 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: CwK | last post by:
How to use fork() system function to fork multi child process at the same time ? For example: Run a program to fork 5 child process at the same time and the parent must wait until all child...
1
by: Gvs | last post by:
Hi, I'm just playing around with forking processes and was wondering how can you keep a child alive as long as the parent is alive. To give an example here is my code. ...
16
by: mishra | last post by:
Hi, i thied the following code.. # include<stdio.h> int main() { int a; printf("Hello..."); a=fork(); printf("hi\n"); return(0);
27
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork();...
11
by: ramu | last post by:
Hi All, We know that a c function never returns more than one value. Then how come the fork() function returns two values? How it is implemented? Regards
3
by: bite me if you can... | last post by:
The man page of fork() says that the file descriptor are shared by parent process & childern processes, so any operation of file descriptor will affect them both. But why the behavior of FILE...
3
by: blackrosezy | last post by:
This question always in my mind,actually what is fork all about? When to use it? Any example? Thanks.
1
by: Falcolas | last post by:
Hi all, This may be more of a Linux question, but it relates to how Python forks... Today, I implemented a pretty simple listener script using os.fork. The script runs fine, and performs as...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.