Jump to content

MikeD

Member
  • Posts

    216
  • Joined

  • Last visited

Everything posted by MikeD

  1. Yes, that will work. But , again, be careful with the locks. You don't want to be in the middle of adding something to the list and then interrupt everything and traverse it.
  2. HA! That was basically my third Operating Systems graded group homework (except we wanted to increment and print a counter)! First of all, you can do that with just one function that is the same for all threads. Each thread can have an identifier and some other information like which item does it insert. Then, you have to choose a synchronization mechanism to control the access to the code that is actually going to do something. We had semaphores in that particular one. Finally you need to check if it is your turn to do something and, if it is, do it. Now, the software engineer's job comes in figuring out how to order these last steps without causing deadlocks, starvation or any other undesirable behavior.
  3. i==0 is comparison, not assignment. int A cannot be declared without assigning a positive value to i first. Otherwise that results in undefined behavior. And an array must have a concrete size, you can't just say that it's size is 'i' and expect it to grow as you put more numbers or increment 'i'. You can't scanf("%s", ...) into a char. You need a char array, either "char* choice = malloc(sizeof(char) * <size of string>) ;" or "char choice[<size of string>+1]; choice[<size of string>] = '\0';". I recommend the former to avoid security (among other) issues. "while (choice != exit)" should be "while(strcmp(choice, "exit") != 0)". Also, you are missing an opening bracket there. There is an extra i++ inside the "while(A<=0)" block. And from there it is generally not finished. (A wild "if you finished press exit" when the user has already exited?! And some random brackets). This is the code oriented review. But like @Ciccioo said, you need to perform some revisions at higher levels.
  4. This seems like the philosopher's dinner problem. I assume the 3 producers means there are 3 threads...? Or are there "m" producers? Either way, each producer is a thread. There is some information missing but here's what I would do: I would have a list of materials, a list for the outputs and an array of tools. Each tool must have a lock that represents possession by a producer. The lists of materials and outputs would have a single lock so that there is only one thread accessing any at any given time (either removing or adding). (To simplify you could keep a pointer to the end of the list so that you don't have to iterate over every item every time.) Allocation and creation of outputs by each thread need not be protected, only addition to the shared list. Let me know if this helps or if there is something that you did not understand. EDIT: fixed spacing. If it's still weird, there is some problem with my browser, maybe?
  5. Why would you want to move backwards? I have been reading Arch Linux documentation pages and now I regret having installed Windows and Ubuntu with legacy BIOS and MBR.
  6. All I can say is that I believe you are right about the wire connected to ground, since, as it is, there are two grounds connected to the chip and no power. And the orange one seems to be connected to nothing! Where to plug it beats me.
  7. That is pseudo-code, right? Because there are a lot of errors in there (apart from the obvious "number in question" bit and the part at the end). EDIT: did not see the last reply before posting How exactly are they hard coded?
  8. If the program you used to burn the ISO to USB had an option to create a persistent file, then yes.
  9. Why not just set up shared folders on VirtualBox? You specify a folder of the host in the machine's settings, mount it on the guest and just drop files there.
  10. Ls should be the same gedit, unless you want to create your own way of displaying the files. To determine the path to pass to ls and to implement cd I would keep a shell variable with the complete path and when the command is cd, verify if the destination folder exists and update the var.
  11. http://linux.about.com/library/cmd/blcmdl3_execv.htm
  12. The way the actual shell does it: fork a new process and replace the child's process image with gedit's image, passing the rest of the arguments.
  13. Or (again, small scale) you could just make a class that implements serializable that holds the data you want to maintain and just write the object onto a file with an ObjectOutputStream and read it up next time.
  14. This made me remember an assignment for our Natural Language course earlier this semester. Here's an excerpt: sed -r -n 's/[^0-9]*([0-9]+[.[0-9]+]?)[^0-9]*/\1\n/pg' $INPUT | sed -r '/^$/d' > $OUTPUT1sed -r "s/[[:alpha:]\'-]{6,}|[^[:alpha:]]/\n/g" $INPUT | sed -r '/^$/d' | sort | uniq -c > $OUTPUT5 Useful when you are working with natural language processing systems. Or, in this case, if you just want to find the numbers and big words in a text!
  15. Careful with that. If P(x) is false P(x) => Q(x) is true. But that does not mean Q(x) is true. P => Q P(x) is false Then Q(x) is true Is not a valid argument.
  16. I haven't checked out the app (you'll see why in a second) but there are 2 thing that are kind of bugging me! 1st: Does the app really need to target android 4.0 and up? As of January 8, according to Google (https://developer.android.com/about/dashboards/index.html), there are still 22.6% of users with android < 4.0 (including me!). If supporting higher versions is strictly necessary, fine, but if not you'd increase the reach of your app. Other Google apps still target lower versions. 2nd: The app id is com.myApp.linustechtips. While it is not something of extreme importance it gives the feeling that the app just came out of an android tutorial! Still, Google has this to say about the package name (from https://developer.android.com/guide/topics/manifest/manifest-element.html): I know these observations are not very helpful for the app itself, but it's what I can see!
  17. Someone is developing an unofficial Android app. You can find a thread in the Off Topic section (here). I haven't tried it, though.
  18. On 11: The only thing you know is that anything belonging to D has the property that it makes P false. You can't say anything for things that are not in D (some may make P true, others false) On 12: You have P(x) is false as a premise, you can't have P(x) is true as a conclusion.
  19. Well, in the quest to answer your question I learned a little bit more myself! In my experience, the device file names do not change much. My 2 sata disks have had the same file names since I first set up the system (/dev/sda and /dev/sdb) as far as I know (I don't check it every time). However I never changed/switched them. On the other hand, external USB HDDs and pen drives tend to share the same name (sdc) if I plug one, unplug, then plug the other. This is expected, since the names are given in the order the drives are registered, but the files are removed after unplugging. So, in theory, you should not trust the device file names. Enter, udev. udev creates symbolic links with fixed names to identify the drives. Since each different device has a different id, this is the best method to unambiguously identify them. You can find the links in /dev/disk/by-*. You have 3 methods of identification: by-id, by-label and by-uuid. According to an answer in http://askubuntu.com/questions/39760/how-can-i-control-hdd-spin-down-time you can configure hdparm using these links. More reading material: http://www.reactivated.net/writing_udev_rules.html
  20. Since the FileReader class accepts a string and automatically opens the file I was trying to pass the string filename to the scanner... <_< Anyways... As far as I know there is no immediate way of getting a HashMap from a Scanner. But I did manage to do this: public static void main(String[] args) { String filename = "pedidos.txt"; Scanner leitor = null; HashMap<String, Integer> lista = new HashMap<String, Integer>(); try { leitor = new Scanner (new File (filename)).useDelimiter(" : |\n"); while (leitor.hasNext()){ String nome = leitor.next(); Integer contagem = leitor.nextInt(); System.out.println (nome + " -> " + contagem); if(lista.containsKey(nome)) lista.put(nome, lista.get(nome) + contagem); else lista.put(nome, contagem); } } catch (FileNotFoundException e) { System.out.println ("Ficheiro desconhecido"); } finally { if(leitor != null) leitor.close(); } System.out.println("---"); for(Map.Entry<String, Integer> m : lista.entrySet()) System.out.println(m.getKey() + " -> " + m.getValue()); System.out.println("---"); }
  21. [bem sei. Também já tive de estar de pé 30+ horas na altura de entregas de projectos. Mas agora devia ser época de exames, não? Eu estou a (tentar) estudar para 2 para a semana.] With the Scanner? When I was using the scanner the only thing I could print was the name of the file, which was weird since I was printing the content of the first read line. That's why I switched to the BufferedReader. Here is what I have (again, I am using Java 7): import java.io.*;import java.util.*;public class MenuCounter { public static void main(String[] args) { String filename = "pedidos.txt"; HashMap<String, Integer> lista = new HashMap<String, Integer>(); try(BufferedReader pedidos = new BufferedReader(new FileReader(filename))) { String linha; while((linha = pedidos.readLine()) != null) { String[] splits = linha.split(" : "); System.out.println(splits[0] + " " + splits[1]); } } catch(IOException ioe) { ioe.printStackTrace(); } }}
  22. Projectos a esta hora?! (Assumindo que estão em Portugal) First of all, at least with Java 7 in linux, I can't read the contents of the file with the Scanner, so I recommend (and always use) BufferedReader. BufferedReader pedidos = new BufferedReader(new FileReader(fileName)) If you are having trouble with the names or arguments of any method in any java class you can always check out Oracle's online documentation. http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html is what you need in this case, specifically containsKey(key), get(key), and put(key, value). Since you have already split the line into array splits you will need something like if(lista.containsKey(splits[0])) lista.put(splits[0], lista.get(splits[0]) + splits[1]); else lista.put(splits[0], splits[1]);
  23. Global variables are initialized by the compiler and they exist in a certain section of the executable (simplification). Hence, you can only initialize the variables whose lengths are known. If what you are trying to do was possible, with which size would the matrix be initialized? 10? And then when length changed, what would happen? So, the only way to initialize such matrix is during runtime, in the heap or in the stack. Since the stack is not designed to hold big objects and the matrix is supposed to be global, it is not a good idea to allocate it there. Heap method: You declare the array as int **grid; and then, after knowing the size you want, you have two options: malloc or new. Since you are using C++, let us take advantage of new: grid = new int*[length]; foreach i < length grid = new int[length]; And now you can initialize and access the matrix with grid[x][y]. When allocating in the heap do not forget to delete (or free, if you used malloc) the memory to prevent memory leaks. My version (I use linux, so I eliminated the getch at the end): #include <iostream>using namespace std; int difficulty;int length = 10;int **grid;int main() { cout<<"Choose a difficulty."<<endl; cout<<"1. Easy [10x10]"<<endl; cout<<"2. Medium [15x15]"<<endl; cout<<"3. Hard [20x20]"<<endl; cin>> difficulty; switch (difficulty) { case 1: length = 10; break; case 2: length = 15; break; case 3: length = 20; break; default: length = 10; break; } cout<<length<<endl; grid = new int*[length]; for(int i = 0; i < length; i++) grid[i] = new int[length]; grid[0][length-1] = 5; for(int i = 0; i < length; i++) delete grid[i]; delete grid; return 0;}
  24. Chrome: Right click > Inspect Element > Delete node Done!
  25. Added bonus: insults If set, sudo will insult users when they enter an incorrect password. This flag is off by default.
×