Skip to content

system of collaborating processes where a parent process manages child processes to handle requests for specific lines from a text file

Notifications You must be signed in to change notification settings

annegog/OS-project1-K22

Repository files navigation

Anna Gogoula
OS project1 2021
—----------------------------------------------------------------------
gcc -g -Wall producer_parent.c -o producer_parent -lpthread
gcc -g -Wall consumer_child.c -o consumer_child  -lpthread
./producer_parent

PRODUCER (producer_parent.c):
------
- Initially, we create and initialize 3 semaphores that will be needed later. The first semaphore is initialized to 1, and the other 2 to 0, allowing us 
to block both the child and the parent processes (this will be needed later).
- We perform some checks on the semaphores, initialize the arguments we receive, and determine the number of lines in the file we want to use.
- Next, we create shared memory, perform some checks, and pass necessary values to the shared memory, including the lines of the file, the number of children
created by the parent, the number of requests each child should make, and a counter to keep track of how many children have finished their requests.
- We use the semaphore to create the children without interference, and then the critical section begins. Until the "running" variable is not equal to 0, 
there is a condition that indicates all child requests have been completed, so we do not want to find another line. In this section, the parent receives the 
child's request for a specific line from the file, searches for that line in the file, and stores it in "some_text" in shared memory (so the child can access it). 
The process of handling child requests is described in more detail below with the children's processes.
- Subsequently, the parent waits for the children to finish their requests, performs checks, closes the semaphores, and unlinks them.

CONSUMERS (consumer_child.c):
------
- We open the names of the semaphores to ensure shared semaphores exist. Necessary checks are performed to confirm that the semaphores have been opened.
- We declare the shared memory and enter the critical section.
- For each request (up to N), a random number within the range of the number of lines in the file is selected. The timer starts counting, 
and when the line is returned by the parent, it is printed. The timer stops, and the time taken for each request is added to a variable. 
This allows the calculation of the average time taken by each child upon completion.
- The counter keeping track of children is incremented, and a check is performed to determine if the current child is the last one. 
Finally, checks are made regarding shared memory, and the semaphores are closed.

Description of the Critical Section:
------
-The parent enters the while loop, and the child process is blocked (the semaphore 3 is waited) to allow the child to execute and make its request.
- The child process decrements semaphore 3 to ensure that multiple requests do not interfere with each other. The child selects a random line number, starts the timer, 
and decrements semaphore 1 to allow the parent to search for the requested line. It also decrements semaphore 2 to allow the parent to find and return the line.
- The parent searches for the line, stores it, and increments semaphore 2. The child prints the requested line from the shared memory, stops the timer, 
and increments semaphore 1, unblocking the child and allowing more requests. The time difference for each child request is recorded.
- This continues until all children have finished their requests.
- Once the last child finishes, the parent unblocks (increments semaphore 3), setting "running" to 0, and the while loop stops.

Note: 
The code does not include a Makefile, and it may not run correctly in many cases because sometimes the child count variable is zeroed out,
indicating that not all children have finished.
- This issue may be related to some children exiting with a time of -nan.

--------------------------------------------------------------------------------------------------------------------------------------

PRODUCER (producer_parent.c): 
    Αρχικά δημιουργούμε και αρχικοποιούμε 3 σημαφόρους, που θα χρειαστουν στην συνέχεια. Ο πρώτος σημαφόρος αρχικοποιήται στο 1 
και οι 2 άλλοι στο 0 ώστε να μπορούμε να μπλοκάρουμε το παιδί και τον πατέρα (θα χρειαστεί στην συνέχεια). Γίνονται κάποιοι 
έλεγχοι για τους σημαφόρους, αρχικοποιούμε τα ορίσματα που δεχόμαστε και βρίσκουμε πόσες σειρές έχει το αρχείο που θέλουμε.
    Μετά δημιουργείται η διαμεριζόμενη μνήμη, γίνονται κάποιοι έλεγχοι και περνούν στην μνήμη απαραίτητες τιμές. 
Οι γραμμές του αρχείου, ο αριθμός των παιδιών που έχει δημιουργήσει ο “πατέρας”, τον αριθμό των δοσοληψιών που πρέπει να κάνει 
κάθε παιδί και έναν μετρητή για να γνωρίζουμε πόσα παιδιά έχουν τελειώσει τις δοσοληψίες τους.
	Χρησιμοποιούμε τον σημαφόρο ώστε να γίνει η δημιουργία των παιδιών, χωρίς να υπάρχουν παρεμβάσεις και μετά ξεκινάει 
το critical section. Εως ότου το running δεν είναι 0, Υπάρχει μία συνθήκη που όταν ισχύει σημαίνει ότι έχουν γίνει όλες 
οι δοσοληψίες των παιδιών επομένως δεν θέλουμε να βρούμε άλλη γραμμή. Εκεί ο πατέρας δέχεται το αίτημα του παιδιού για μία 
δοσοληψία (μια συγκεκριμένη σειρά του αρχείου), ψάχνει στο αρχείο την σειρά αυτη, την αποθηκεύει στο some_text του shared memory 
(ώστε να μπορεί το παιδί να έχει πρόσβαση). (Περιγράφεται πιο κάτω πως γίνεται η διαδικασία μαζί με των παιδιών).
	Στη συνέχεια ο πατέρας περιμένει τα παιδιά να τελειώσουν τις δοσοληψίες τους. Γίνονται έλεγχοι, κλείνουν οι σημαφόροι 
και γίνεται unlink τους.

CONSUMERS (consumer_child.c):
	Ανοίγουν τα ονόματα των σημαφόρων, ώστε να υπάρχουν κοινοί σημαφόροι. Κάνουμε απαραίτητους ελέγχους να ξέρουμε αν έχουν 
ανοίξει οι σημαφόροι.
    Δηλώνεται η κοινή διαμοιραζόμενη μνήμη και ξεκινά το critical section.
	Για κάθε δοσοληψία (εως Ν)  επιλέγεται ένας τυχαίος αριθμός μέχρι τον αριθμό των γραμμών του αχρείου, ξεκινάει να 
μετράει ο χρόνος και όταν επιστραφεί απο το πατέρα η γραμμή, αυτή εκτυπώνεται. Ο χρόνος σταματάει να μετράει και ο χρόνος καθεμίας 
δοσοληψίας προστίθεται σε μία μεταβλητή. Ώστε όταν τελειώσει το κάθε παιδί να υπολογίζεται ο μέσος χρόνος που έκανε.
	Στη συνέχεια ο μετρητής που υπολογίζει τα παιδιά  αυξάνεται και γίνεται έλεγχος αν το τρέχον παιδί είναι το τελευταίο. 
Τέλος γίνονται έλεγχοι για την διαμοιραζόμενη μνήμη και κλείνουν οι σημαφόροι.

Περιγραφή critical section:
→ Ο πατέρας μπαίνει στην while επομένως τον κάνουμε block (κάνουμε wait τον σημαφόρο 3) ώστε να τρέξει το παιδί και 
    να κάνει την αίτηση του. 
→ Το παιδί κάνει down τον σημαφόρο ώστε να μην μπλεχτουν και τρέχουν ταυτόχρονο και γίνονται κι άλλες αιτήσεις. 
    Το παιδί ζητάει μια τυχαία γραμμή, ο χρόνος ξεκινά να μετρά και κάνουμε up τον σημαφόρο (semaphore 3) ώστε να 
    μπορεί να ψάξει ο πατέρας την γραμμή που αιτήθηκε το παιδί. Επίσης κάνουμε down τον σημαφόρο 2 ώστε να μπορέσει 
    ο πατέρας να βρεί και να επιστρέψει την γραμμή.
→Ο πατέρας ψάχνει την γραμμή, την αποθηκεύει και κάνει up τον σημαφόρο 2.
→Το παιδί εκτυπώνει την γραμμή που ζήτησε από τον γονέα, ο χρόνος σταματάει να μετράει και ο σημαφόρος (1) γίνεται up, 
    άρα ξεμπλοκάρετε το παιδί και μπορούν να γίνουν και άλλες αιτήσεις. Μετράμε την διαφορά του χρόνου για κάθε δοσοληψία των παιδιών.
→ Κάθε φορά που τελειώνει ένα παιδί εκτυπώνεται ο μέσος χρόνος που έκανε.
Συνεχίζεται μέχρι να τελειώσουν όλα τα παιδιά.
→Μόλις τελειώσει και το τελευταίο παιδί, ξεμπλοκάρει ο πατέρας (up semaphore 3).
→Επομένως το running γίνεται 0 και σταματάει να εκτελείται η while.

—------------------------------------------------------------------------------------------------------------------------

* Δεν έχω κάνει Makefile
* Δεν τρέχει καλά σε πολλές περιπτώσεις, γιατί απ’ ότι είδα κάποιες φορές μηδενίζεται ο μετρητής που έχω για τα παιδιά 
άρα φαίνεται ότι δεν τελειώνουν όλα τα παιδιά. Ίσως φταίει ότι πολλά παιδιά κάνουν exit με χρόνο -nan. :/

About

system of collaborating processes where a parent process manages child processes to handle requests for specific lines from a text file

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages