简体   繁体   中英

Linux & C: How to set file reading priority in multi-process program?

This is for an assignment I'm working on, and NO I'm not looking for you to just GIVE me the answer. I just need someone to point me in the right direction, maybe with a line or two of sample code.

I need to figure out how to set the priority of a file read operation from within my program. To the point:

  • server process receives a message and spawns a child to handle it
  • child tries to open the filename from the message and starts loading the file contents into the message queue
  • there may be several children running at the same time, and the initial message contains a priority so some messages may get more device access

The only way I can think to do this (right now, anyways) would be to increment a counter every time I create a message, and to do something like sched_yield after the counter reaches a given value for that process' assigned priority. That's most likely a horrible, horrible approach, but it's all I can think of at the moment. The assignment is more about the message queues than anything else, but we still have to have data transfer priority.

Any help/guidance is appreciated :)

Have the pool of child processes share a semaphore. Once a child acquires the semaphore it can read a predefined number of bytes from the resource and return it to the client. The number of bytes read can be related to the priority of the request. Once the process has read the predefined number of bytes release the semaphore.

Until recently, there was no IO prioritization in Linux. Now there is ionice . But I doubt you are meant to use it in your assignment.

Are you sure your assignment is talking about files and not system V message queues?

Read the man pages for:

   msgctl(2),    msgget(2),    msgrcv(2),    msgsnd(2),   capabilities(7),
   mq_overview(7), svipc(7)

Although I think you can use a file as a key to create a message queue, so that multiple processes have a way to rendezvous via the message queue, a Sys V message queue itself is not a file.

Just wondering because you mention "message queues" specifically, and talk about "priorities", which might conceivably map to the msgtyp field of eg. msgsnd and msgrcv, though it's hard to tell with what information you've given what the assignment really is about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM