Queue optimizations between two queues - c

I'll try to explain my problem best I can. I'm having trouble with the final part of a college assignment and got really stuck.
The scenario is this: we have to build a simulation of an airport, in which a thread represents a flight (either departure or arrival), and have to optimize the arrivals and departures to minimize the time that it takes flights to land or depart.
So, onto the problem. I have all structures working, threads being created in the right moment, shared memory fully functional, message queue created and working, etc.
But now, I'm struggling with actually managing the flights. I created two linked lists, one for the arrivals and one for the departures. Each node of the linked list has a pointer to a space in the shared memory. Each of those spaces has the information relative to a flight (eta and fuel for arrivals, desired takeoff for departures). The arrivals linked list is sorted by ETA, and the takeoff linked list is sorted by desired takeoff. These linked lists are supposed to be a queue.
Problem is, I have no clue on how to manage them. There can be two departures or two arrivals at the same time, but there can't be both arrivals and departures at the same time.
I'm thinking of using semaphores, but I'm not sure if that's a good approach. I'd greatly appreciate any pointers in the right direction.
Thanks in advance!
EDIT:
We have a couple of specifications, but I thought this post would become too big as I was just looking for general headlines. In short, we have to minimize the number of times we need to order an arrival to hold (wait in the air), and "alternate between arrivals and departures to improve airport efficiency"

The specification is short. I understand this is for the purpose of keeping question small. But with this limited specification, I can only do some guess work here.
This problem seems to me similar to Old bridge problem.
An old bridge has only one lane and can only hold at most 3 cars at a
time without risking collapse. Create a solution that controls traffic
so that at any given time, there are at most 3 cars on the bridge, and
all of them are going the same direction. A car calls ArriveBridge
when it arrives at the bridge and wants to go in the specified
direction (0 or 1); ArriveBridge should not return until the car is
allowed to get on the bridge. A car calls ExitBridge when it gets off
the bridge, potentially allowing other cars to get on. Don’t worry
about starving cars trying to go in one direction; just make sure cars
are always on the bridge when they can be.
If you think so, you can find the solution to this problem here:
With some modifications you may be able to use it for your air traffic problem.
https://codeistry.wordpress.com/2018/04/18/old-bridge/

Related

When to use an array vs database

I'm a student and starting to relearn again the basics of programming.
The problem I stated above starts when I have read some Facebook posts that most of the programmers use arrays in their application and arrays are useful. And I started to realize that I never use arrays in my program.
I read some books but they only show the syntax of array and didn't discuss on when to apply them in creating real world applications. I tried to research this on the Internet but I cannot find any. Do you guys have circumstance when you use arrays. Can you please share it to me so I can have an idea.
Also, to clear my doubts can you please explain to me why arrays are good to store information because database can also store information. When is the right time for me to use database and arrays?
I hope to get a clear answer because I have one remaining semester before the internship and I want to clear my head on this. I do not include any specific programming language because I know most of the programming language have arrays.
I hope to get an answer that can I can easily understand.
When is the right time for me to use database and arrays?
I can see how databases and arrays may seem like competing solutions to the same problem, but you're comparing apples and oranges. Arrays are a way to represent structured data in memory. Databases are a tool to store data on disk until you need to retrieve it.
The question you pose is kind of like asking: "When is the right time to use an integer to hold a value, vs a piece of paper?" One of them is a structural representation in memory; the other is a storage tool.
Do you guys have circumstance when you use arrays
In most applications, databases and arrays work together. Applications often retrieve data from a database, and hold it in an array for easy processing. Here is a simple example:
Google allows you to receive an alert when something of interest is mentioned on the news. Let's call it the event. Many people can be interested in the event, so Google needs to keep a list of people to alert. How? Probably in a database.
When the event occurs, what does Google do? Well it needs to:
Retrieve the list of interested users from the DB and place it in an array
Loop through the array and send a notification to each user.
In this example, arrays work really well because users form a collection of similarly shaped data structures that needs to be put through a similar process. That's exactly what arrays are for!
Some other common uses of arrays
A bank wants to send invoice and payment due reminders at the end of the day. So it retrieves the users with past due payments from the DB, and loops through the users' array sending notifications.
An IT admin panel wants to check whether all critical websites in a list are still online. So it loops through the array of domains, pings each one and records the results in a log
An educational program wants to perform statistical functions on student test results. So it puts the results in an array to easily perform operations such as average, sum, standardDev...
Arrays are also awesome at keeping things in a predictable order. You can be certain that as you loop forward through an array, you encounter values in the order you put them in. If you're trying to simulate a checkout line at the store, the customers in a queue are a perfect candidate to represent in an array because:
They are similarly shaped data: each customer has a name, cart contents, wait time, and position in line
They will be put through a similar process: each customer needs methods for enter queue, request checkout, approve payment, reject payment, exit queue
Their order should be consistent: When your program executes next(), you should expect that the next customer in line will be the one at the register, not some customer from the back of the line.
Trying to store the checkout queue in a database doesn't make sense because we want to actively work with the queue while we run our simulation, so we need data in memory. The database can hold a historical record of all customers and their checkout outcomes, perhaps for another program to retrieve and use in another way (maybe build customized statistical reports)
There are two different points. Let's me try to explain the simple way:
Array: container objects to keep a fixed number of values. The array is stored in your memory. So it depends on your requirements but when you need a fixed and fast one, just use array.
Database: when you have a relational data or you would like to store it in somewhere and not really worry about the size of the objects. You can store 10, 100, 1000 records to you DB. It's also flexible and you can select/query/update the data flexible. Simple way to use is: have a relational data, large amount and would like to flexible it, use database.
Hope this help.
There are a number of ways to store data when you have multiple instances of the same type of data. (For example, say you want to keep information on all the people in your city. There would be some sort of object to hold the information on each person, and you might want to have a data structure that holds the information on every person.)
Java has two main ways to store multiple instances of data in memory: arrays and Collections.
Databases are something different. The difference between a database and an array or collection, as I see it, are:
databases are persistent, i.e. the data will stay around after your program has finished running;
databases can be shared between programs, often programs running in all different parts of the world;
databases can be extremely large, much, much larger than could fit in your computer's memory.
Arrays and collections, however, are intended only for use by one program as it runs. Your program may want to keep track of some information in order to do its calculations. But the data will be in your computer's memory, and therefore other programs on other computers won't be able to access it. And when your program is done running, the data is gone. However, since the data is in memory, it's much faster to use it than data in a database, which is stored on some sort of external device. (This is really an overgeneralization, and doesn't consider things like virtual memory and caching. But it's good enough for someone learning the basics.)
The Java run time gives you three basic kinds of collections: sets, lists, and maps. A set is an unordered collection of unique elements; you use that when the data doesn't belong in any particular order, and the main operations you want are to see if something is in the set, or return all the data in the set without caring about the order. A list is ordered, though; the data has a particular order, and provides operations like "get the Nth element" for some number N, and adding to the ends of the list or inserting in a particular place in the list. A map is unordered like a set, but it also attaches keys to the data, so that you can look for data by giving the key. (Again, this is an overgeneralization. Some sets do have order, like SortedSet. And I haven't even gotten into queues, trees, multisets, etc., some of which are in third-party libraries.)
Java provides a List type for ordered lists, but there are several ways to implement it. One is ArrayList. Like all lists, it provides the capability to get the Nth item in the list. But an ArrayList provides this capability faster; under the hood, it's able to go directly to the Nth item. Some other list implementations don't do that--they have to go through the first, second, etc., items, until they get to the Nth.
An array is similar to an ArrayList, but it has a different syntax. For an array x, you can get the Nth element by referring to x[n], while for an ArrayList you'd say x.get(n). As far as functionality goes, the biggest difference is that for an array, you have to know how big it is before you create it, while an ArrayList can grow. So you'd want to use an ArrayList if you don't know beforehand how big your list will be. If you do know, then an array is a little more efficient, I think. Although you can probably get by mostly with just ArrayList, arrays are still fundamental structures in every computer language. The implementation of ArrayList depends on arrays under the hood, for instance.
Think of an array as a book, and database as library. You can't share the book with others at the same time, but you can share a library. You can't put the entire library in one book, but you can checkout 1 book at a time.

Database design to track multi-step process

I am working on a database to handle the process flow of a multi-step process.
Each specific process includes several actions.
Each action within a specific process uses some common files and some unique files. The actual actions will be performed by a person with specific skills. For instance, the first action may be to take one (of the common to each process) files and create a new file. The second action will be to take the common file and the new file and create a third file; etc.
The problem is that each action is just unique enough that I think I need separate tables for each type of action.
So my issue is how to “track” them within the process.
Imagine a car wash facility as the overall idea.
Each car would be the same as a specific project
Each car may have a different process assigned to it.
One process is to run it through the tunnel and hand dry; a two step flow.
Another is to run it through the tunnel, hand dry and vacuum; a three step flow.
A third is to run it through the tunnel, hand dry and detail; a different three step flow.
Now imagine that the car could be teleported between steps. (I know; if I could teleport the car, I certainly wouldn’t be asking this question on stackoverflow.)
Also that the cars could sit in a big parking lot between actions.
And finally, that workers that were equipped and allowed to do a specific action; could check to see or be informed if there were any cars awaiting (their) action and could teleport them from the parking lot (checking them out), do their work and teleport them back to the parking lot (checking them back in.)
(I left out some stuff that would be obvious; such as checking in or out - updates the car’s project status; including checking in and out time constraints; there is an inherent order (cannot dry before washing); some actions could occur in parallel (such as vacuuming while the car is in the tunnel), etc. This car wash analogy isn’t perfect, but seems to be pretty good.)
What I am trying to figure out is how best to set up the database to handle this.
And while realizing that the initial idea is that it is only for three specific processes and only car washes, I NEED to generalize this to handle more than just these three different processes and more or less than just two or three steps and to handle the addition of (say) oil changes which require completely different inputs but would still be just steps in a process.
I am sure this has been done to death before, so simply pointing me to an example is great. (I read the question and answer to an example of "BoxItems" flow, but that was missing the basics.)
Where I am hung up at is my mindset of a process needing to handle varying individual ordered steps.
One process needing action one, then two, then three
and another needing action one, then four and five in parallel, then action two.
Obviously, I cannot have a process table that has varying number of fields for differing records. Each of these varying fields being a foreign key to a table containing unique actions. (And besides; the process table would be more high-level information such as owner, percent completed, etc.)
I have gone down the path of a table (flow for lack of a better name) that defines each process flow; one specific sequence of actions per record. But that seems to just be moving the conundrum from the process table to flow table.
Any thoughts would be greatly appreciated.
thank you,
chuck

Implementing 'thread-safe" linked list

I am writing an applicaation where more then one link list is shared among threads. Operations on the linked list is as usual:searching,inserting,deleting,modifying node contents.
I came across ann implementation to keep the entire procedure of link list operation "thread-safe". http://www.cs.cf.ac.uk/Dave/C/node31.html#SECTION003100000000000000000
But was wondering if I caan do it as follows:
lock(mutex)
link list operation
unlock(mutex)
I.e I associate a mutex with each link list and use it as above whenever I start an operation
Would be gratefull for views
It is possible to do it this way, but you sacrifice liveness as the linked list can now only be touched by one thread at a time - this may lead to the list becoming a bottleneck in your program.
Think about the interface of the linked list (what methods may be called by the threads) and how you can keep the list safe, but also allow as many threads as possible to use it at once.
For example, if you're using the list as a queue, one thread could be enqueueing items at the tail of the list, while another thread dequeues an item.
There are lots of challenges in creating thread safe utilities, but you should try to be as surgical as possible to make sure you aren't sacrificing the performance that you're trying to gain by parallelising your software in the first place! Have fun!
Depends. If your threads will mainly search through the lists without modifying them, it may be worthwhile to implement a reader/writer-lock. There's no reason to prevent other threads from reading the information as long as none of them modifies it. If however, the most common operations involve modifying the lists or the information in them, there may not be much to gain, so a simple lock/do operation/unlock scheme should work just as well.

Design scalable highly updated ressource on a DHT in key/value pair

I would like to design a resource that users would continuously update and read, this resource need not to be always update but must scale well, I mean that the nodes responsible for the resource and his replicas should not be overloaded.
The main problem is that I cannot see how it is possible! I could offload these nodes by adding read cache and update them at a slow pace, but for the writing I have no idea how to scale because values must be at known keys to be recovered by the users and so I can't share the load on the DHT...
Thx a lot for your ideas!
After some though,
one possible idea would be to make something approximative, each time someone want to post a feed it also post the feed with a key equal to a timestamp representing the second (like 25/03/2011 at 5 min and 1 sec) at which he posted the feed, so that the load is automatically shared between the node and that the responsible node quickly change.
When someone want to know the last feeds posted he just check the timestamp of the actual second and the 2-3 past second and can thus recover the last feeds distributing the load between the nodes responsible for the different seconds...
That should make the job I think :).
Hope my own answer will help someone later ;)

stadium problem: Provide algorithm to solve the problem

In a small stadium there are several thousand people in the stands. Devise a distributed algorithm
enabling the audience to count itself. Do not assume any particular geometry of the stadium except, if you want, that it is bowl shaped. Explicitly state your assumptions, then present your algorithm and analysis
I was assuming the members to be a linked-list and appending the counter and free(ptr)..I may be wrong...Kindly provide some useful insights
Thanks in adv...
Assuming everyone can talk to his/her neighbor (possibly over many empty seats) and that fans of team A are willing to speak to fans of team B, the following could work:
Everyone grabs his/her nearest neighbor, who is not already grabbed by someone else, to form groups of at most two people. Now everyone remembers the size of the group they are in (can be 1 or 2). Now a leader of each group is chosen in a way that he is able to communicate to a member of another group. The leaders of each group try to join their group with one other and each member of the two (now joined) groups remembers the sum of the members of each group (this can be done by broadcasting the new value to be added to the group). This process continues until there is only one group left. Upon termination of this process everyone knows the number of people in the stadium.
Hope this helps.
In a small stadium there are several
thousand people in the stands. Devise
a distributed algorithm enabling the
audience to count itself.
Feynman answer (see round manhole question): Have everyone shout "Several thousand!"
Here is another algorithm:
Let everyone count the others and himself.
Then, at the sound of a horn, each counter shout his count.
Keep the most shouted.
With this algorithm you can cope with errors.
For every column, a leader is chosen with the rule "the person in the row closest to the field is the leader" (these seats are usually filled). The leader initiates a count of the people in that column in the following manner:
1. Shake hand with the person directly sitting behind, and ask, "you?"
2. If no one is behind the person, the response should be 1, or else do step 1 with person behind, and the answer is one more than the answer from the person behind.
3. The leader immediately writes this number on a board, and holds it up
4. Among these leaders, the youngest person should start collecting these boards, and add them. If she meets a person younger than her collecting boards, the count till then is handed over to the other person. If the same age, the taller person would take over.
Everyone drops their dacks, and the "output" is available in the local paper the next day ala "N people arrested at world's first mass-streaker incident". (i.e. the application asks the system to do the work).
Each person picks one other person to fight, and first asks how many people they've knocked out. Winner finds another opponent, adds the defeated opponent's count. Last man (or woman) standing has the answer.
Everyone stands up, plucks a hair, then hands it to someone nearby with at least as many hairs before sitting down again. Standing people continue to seek others to give hairs too. The last person counts the hairs.
You invite people to grab a bag of minties from the canteen, then hand them around until they can't find anyone who hasn't had one, then drop the bag at a central point. Multiply bags * number-of-minties-per-bag - number-of-minties-left-in-bags.

Resources