Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0

    Unhappy Processes and EOF


    Hey guys, I have two processes, and one process puts information into a file, and another one takes them out.

    The one that puts them in can have a sleep flag put in which puts information every say 1 sec.

    the one that reads the information, should read for new input and if there isn't any, is supposed to incremet an integer every time, until there is new item to be read.

    I tried to do it as follows:

    if pid = 0{
    ofile = fopen("...", "r");
    while (i < N){ // N is the total num of things to be written on file

    if(feof(ofile)){
    c = c+1;
    }
    else{
    fscanf(....);
    i = i+1;
    }
    }

    else{
    for(j = 1; j = N; j++){
    ... puts information in per line
    wait(s);
    }

    It seems as though i go into a infinate loop whenever i do this for some reason. I can't seem to figure it out. Please help with anything you can think of !!!
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    in the future, enclose your code in [code] tags [ / code ] (w/o spaces). several things i noticed:
    * if pid = 0 - this is always true, use ==
    * for(j = 1; j = N; j++) - should this be j <= N ?
    * instead of i = i + 1, just do i++

    note: you also may want to look into FIFO's/Named Pipes, they are usually used for IPC(interproccess communication)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    sorry those were little errors... your right... on my program i have it so that
    pid == 0
    j <= N
    and i++ i'll change.

    Also one more thing to note...

    wait(s).. i meant wait (pid);

    But, Now i got a lot of things working... Now my program works when i make the sleep flag = 0. Since there is no wait, the process that takes things out gets everything without running into feof.

    But when I put sleep as 1, it seems to run into infinate loop in the if(feof(ofile)) line, and increment c forever. any suggestion will do! please help!!!
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    could you post the actual code plz? not the entire mess just the relevant parts of both processes.

    please use the [code] tags as well.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Another complication is that the writes to the file are buffered, so when you are reading from the file there's probably nothing there yet. One solution would be to close the file every time you're done with it and reopen it when you need to read from it or write to it again, . Another solution would be to flush the buffer after every write but I don't how the read file will react to having the eof status changing from under it.

    It would be better to follow infamous' advice to use another IPC method. opens for buffered I/O. Actually a FIFO, AKA "named pipe", works almost exactly like a file that one process writes to and the other reads from. All without having to worry about flushing buffers.

    BTW, I assume that you are using Linux/UNIX, which has FIFOs, shared memory, UNIX sockets, and other IPC mechanisms. As I understand, Windows NT/2000/XP has named pipes.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <time.h>
    
    
    main()
    {
      char *CONSOWN[N], buf[100],*temp;
      
    
      ifp = fopen("SHAREFILE", "w+");  
      
      printf(">>HOW MANY? ");
      scanf("%d",&N);
      
      printf("\n>>SLEEP ");
      scanf("%d", &slp);
      
      pid = fork();
      
      if(pid == 0){   //chld process(consumer)
        i = 0;
        count = 0;
        ofp = fopen("SHAREFILE","r");
        fseek(ofp, 0, SEEK_SET);
        
          
        while(i < N){
    	
          if(feof(ofp)){
    	count++;
    	 }
           
          else{
    	fscanf(ofp, "%d %d", &num1, &num2);
    	num3 = num1 + num2;
    	CONSOWN[i] = (char*)malloc(100);
    	sprintf(CONSOWN[i], "%d + %d = %d", num1, num2, num3);
    	i++;
          }
        }
    
        for(j = 0; j<N; j++){
        printf("%s\n", CONSOWN[j]);
        }
    
        printf("This is the final count value: %d\n", count);
        fclose(ofp);
        exit(1);
    }
      
      else{
        for (x = 1; x <= N; x++){ 
          n1 = rand() % 99 + 1;
          n2 = rand() % 99 + 1;
          
          fprintf(ifp, "%d %d\n", n1, n2);
          sleep(slp); 
        }
        fflush(0);
        fclose(ifp);
        wait(pid);
        exit(1);
        
      } 
      
    }
    Last edited by iamnamja; September 28th, 2003 at 11:13 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    20
    Rep Power
    0
    Originally posted by infamous41md
    in the future, enclose your code in [code] tags [ / code ] (w/o spaces).
    It makes any code much easier to read.
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    you know i asked you twice to use the code tags and i really don't think that's asking too much of a person... it's fvcking annoying to try and match up 3 lines of parentheses all lined up. and honestly i m not even going to try. once again, USE THE CODE TAGS. THE WAY TO USE THEM IS LIKE THIS:

    [ code ] //here goes the code [ /code ]

    you have to TAKE OUT THE spaces in the above tags to make them work.
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    infamous I'm sorry that I've irritated you. I have fixed the above code to make it more readable. I hope this helps
  18. #10
  19. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    ok thanks for changing that, makes our life much easier :)
    it appears that your test for feof() is always returning true, thus i is never incremented so the loop condition remains true forever. using files for IPC you're making your life more complicated imho. using a FIFO is just as easy to read/write to, and letting the other process know your finished is even easier.
    also, the bigger problem is that there is no synchronization on the file access. you have two processes, one reading and one writing both to the same file. this is not possible w/o synchronization, i.e. : mutex, critical section, semaphore, etc..
    i believe that is the root of your problem. when you do stuff like that the results are undefined(or un* whatever it is for you picky ones ;) ). you need to either:
    a) use a fifo/named pipe/socket(if that floats ur boat)
    b) properly synchronize the file access

    if you would like some sample code of using fifo's i'll post it for you, although google prolly has many examples as well.
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    Thanks for the help~ But one problem that I have is that i'm not supposed to/ allowed to use fifo or synchronization. This is a project for a school, and I got stuck with this small part. Actually i'm not even sure if i'm allowed to post things like this =) But that's besides the point.

    The professor mentioned that writing small things as such should be almost instantaneous so it shouldn't cause much of synchronization process. Also, because we're learning about all that now... so i'm sure that'll be the next project...

    But I don't understand why it gets caught in that infinate loop. It should check the file every time to see if its at the end of file isn't it?

    I know it works, when I put the wait time to 0, since all the write is instaneous, and its finished before the other process gets to finish it.

    But it seemed, as you have mentioned once it goes into checking if its at the end of file, it never gets out. If you have any other suggestions, please feel free to suggest them to me.

    But once agian, thaks for all your help sofar! I really apreciate it!
  22. #12
  23. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    im sorry to say, and someone else correct please if im wrong, but your teacher is incorrect. Statements like this:
    writing small things as such should be almost instantaneous so it shouldn't cause much of synchronization process.
    are the essence of bad programming imho. "oh the user wont want to type more than 512 characters, just use gets()"... honestly i have no idea why it is returning EOF, as i said, when you do stuff like that the results are undefined, but it's 3am here and im pretty coded-out for the day, so maybe someone will have some better insights for you tomorrow.

    edit: also, even assuming that it will work when the sleep time is 0 is a bad assumption. there is no guarantee whatsoever as to the order processes will run. its possible(although unlikely) that the CPU runs the child first, then switches to the parent but for only a instant, and then back to the child. there is absolutely NO guarantees for this kinda stuff which is why synchronization is necessary.
    Last edited by infamous41md; September 29th, 2003 at 01:58 AM.
  24. #13
  25. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Once EOF is set it remains set until clearerr is called. I have tinkered with a 'tail' program in the past and been caught by that.

    And I also agree with infamous41md that if your teacher is making those sorts of statements, he/she has no idea what he/she is talking about.
    Last edited by mitakeet; September 29th, 2003 at 02:49 AM.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    9
    Rep Power
    0
    You guys are probably right about the whole bad coding thing... But I don't think he had much of a choice.. He just wanted to first get us familiar with processes and using fork.. Since we've already learned that... As I've said, i'm sure the next project, will include semaphores and such.

    I was thinking.. Is there a big difference between fopen and open? I was thinking that maybe if I use open to write to the file, it might give a diferent result.. I guess I would have to try it out.. =) Well~ If you guys think of any other suggestions, please help! =)
  28. #15
  29. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Did your teacher specify using buffered I/O or would he allow low-level I/O?

    Instead of fopen and fread/fwrite, use open and read/write. It's faster than fread/fwrite and it reduces the effects of buffering, but you still have to worry about disk caching (data is written to and read from disks in blocks according to sector-size. So again you would need to flush the output buffer with either a flush or by closing it -- I'm not familiar with a similar command to refresh the input cache for reading.

    Hopefully, your instructor is using this assignment is an exercise to show you how miserable you can make your life by not synchronizing shared resources between processes and so to motivate you to learn those synchronization techniques.

    Is file locking allowed? So that the writer would lock the file when he's updating it and unlock it when he's done and then the reader would check and wait until it's unlocked before reading it? I haven't played with it yet, but I understand that abnormally terminated processes leaving files locked can be a problem.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo