-1

I try to find out a way to read from a txt file while I amin different processes. For example, I am in process A and I read the first 10 records of the file ( lets say that there are 100 records). In process B I want to read the next ten records. The problem is that only in process A I take the right records and when I am in process B i take only 0. Can someone help? Thanks in advance!

a_user
  • 207
  • 3
  • 13
  • 1
    Your processes will have to communicate with each other about their progress. This can be done via various flavors of shared state (e.g. shared memory, which would require synchronization) or via IPC (inter-process communication). That's about as specific as I could get given an unspecific question. – DevSolar Feb 12 '14 at 12:59
  • You are missing a lot of detail. What is a "record"? (Is it a fixed number of bytes, or determined by a specific delimeter, or something else?) How are you reading them? (Do you use fread, read, or something else?) Are process A and B related (are they in the same process group? Is one the parent of the other? Are they even running on the same machine?) Without details, it will be difficult to diagnose your problem. – William Pursell Feb 12 '14 at 13:29

2 Answers2

0

If you dig deeper especially in linux environment, you would find out that threads are lighter than processes when it is about achieving something which needs multiple strands of execution. I would do this in following manner:

  1. Create a process that has 10 threads. Each thread will read 10 records from the TXT file. ( I will need to use pthread_create() instead of fork() ). I will also create a mutex which each thread will lock while it reads the file.
  2. I will create Th1 (thread_1) using the above call, lock the mutex, open the file, read is using read() call, the buffer will hold all the 100 records, filter out which 10 I need at that point of time, unlock the mutex when it is done, end it using pthread_join()
  3. Repeat the step 2 for 10 times in total so that I have all the records.
0

This can be done using Inter-process Communication which mainly involves shared-memory, semaphores or message-queues. For a basic knowledge regarding this you can read my blog

One another way of doing it is by passing the file-descriptors between the processes. One process opens the file, reads 10 records, then passes it to the second process. This does the same thing and sends it to the first one. And the entire process repeats until the end of file is reached.

The passing of file-descriptors is mainly done using UNIX Domain Sockets and you can find the code related to this in this answer

Hope this helps.

Community
  • 1
  • 1
nitish712
  • 19,504
  • 5
  • 26
  • 34