1

I am working on the dining philosophers problem, and I am attempting to create a way to keep track of the shared recourse (the forks). So my thinking is to create an array of semaphores, and that way when i have to fork off i can keep track of the philosophers number and his fork for eating by utilizing the index in the array which would ideally contain the recourse.

So my question is, is this possible? Everything i have attempted has resulted in an error such as:

char ** sems[5];
sems[0] = struct sembuf a[1] = {{0, 1, 0}};
sems[1] = struct sembuf b[1]
sems[2] = struct sembuf c[1].... and so forth

however this is obviously the incorrect way to do this. can someone help point me in the right direction? .

itsMe
  • 51
  • 7
  • `char**` isn't going to be able to store an arbitrary `struct`. – tadman Mar 17 '21 at 01:08
  • how do you do that? can you declare a struct as an array? like array struct smes[5]? – itsMe Mar 17 '21 at 01:11
  • `struct x[N]`, sure. – tadman Mar 17 '21 at 01:13
  • okay so then how can i modify those semaphores independantly? like how could i use the semop command per each semaphore? semop(semID, sems[3], 1); gives me an error – itsMe Mar 17 '21 at 02:10
  • You're going to need to give a more complete example here. It sounds like you're really unfamiliar with C, so I would strongly recommend getting a good [C reference book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to work from before embarking on something this technically challenging. – tadman Mar 17 '21 at 02:19
  • I most certainly agree, however I just need to overcome this section of the semaphores, i need to declare them as such: struct sembuf free[5] = {foo }, and then i need the decrement semaphores which i would use like struct sembuf inUse[5] = { bar } I just need to know what to put in the foo bar values, and after which how to correctly use the semop command on the appropriate index in each array. – itsMe Mar 17 '21 at 02:30
  • 1
    It sounds to me like you're trying to take a shortcut here and really over-extend yourself. It would help to go back to the fundamentals and learn how to declare and use both arrays and arrays of structs in C before going further. – tadman Mar 17 '21 at 02:32

1 Answers1

2

Yes, is possible, cf Arrays of semaphore and mutual assignment in C

There are two types of semaphores in linux : SystemV and POSIX semaphores (https://www.tutorialspoint.com/inter_process_communication/inter_process_communication_system_v_posix.htm)

SystemV (sem.h)

Kernel-inbuilt semaphore arrays exist in distros that use SysVinit as init system (SystemV), see https://serverfault.com/questions/312982/what-are-the-semaphore-arrays-on-linux

There is an older semaphore library from SystemV (sem.h) in that a function GETALL exists that writes all semaphores to an array

semctl(semid, 2, GETALL, outarray); in How semaphore operations are done for parent & child processes?

POSIX (semaphores.h)

Semaphores are maintained in the kernel (Ring 0), they are stored in /dev filesystem, see https://man7.org/linux/man-pages/man7/sem_overview.7.html and https://unix.stackexchange.com/questions/275650/where-is-a-named-semaphore-stored

You can get an overview over all semaphores using sem_overview() (named semaphores) and shm_overview() (unnamed semaphores)

ralf htp
  • 9,149
  • 4
  • 22
  • 34