5
1
What does the term block mean wrt computer science? I have seen it being used in multiple occassions but never understood what it means. Like blocking I/O? Googling doesn't seem to help me much.
5
1
What does the term block mean wrt computer science? I have seen it being used in multiple occassions but never understood what it means. Like blocking I/O? Googling doesn't seem to help me much.
9
Blocking I/O means that the program essentially asks, "Get me this data, I'll wait until its ready". This is in contrast to non-blocking I/O. There are two flavors: "Get me this data, I'm going to go do something else. Interrupt me when the data is ready.", and "Get me this data, I'm going to go do something else. I'll ask you later if you have it yet."
It's important because if a program blocks while waiting for data, it can't do things like respond to mouse clicks or repaint the screen. This is what's happening when you see a program labeled as "Not Responding" in the task manager.
It should also be mentioned that "blocking I/O" causes the program to give up it's turn for the CPU until the I/O is ready. The OS then wakes the process by delivering the requested I/O or its status. So by implementing blocking I/O programs can co-operate better on multitasking OSes such as Windows, Linux, etc. This is different than "spin-locking" which is basically a loop polling a system call or something else indicating I/O status waiting for the I/O. – LawrenceC – 2011-03-16T18:51:46.307
0
First meaning of block is easy in computer science: a block of source code in a computer language. For C/C++, perl, etc. the curly bracket { a block of code; } delimits a block of code. Python use indentation for blocks.
The second meaning is used in the context of the operating system (as usually called the kernel) that is seen as the supreme manager who manages resources. When different processes or threads ask for (through system calls) resources (CPU, disk, network, memory, ...) and the resource may not be available due to multiple competing threads or processes are asking for the same thing. The kernel will put some process or threads into suspension (wait queue) and only allowing some processes or threads working on the resources (execution state). The suspended processes are in blocking state or are blocked.
Blocking can be achieve through either hardware or software mechanisms by the operating system. The running process can also ask the operating system to suspend their execution by the sleep command as in Shell and perl.
Please edit if my interpretation is wrong.
0
In summary it means that the process hangs (waits) until the I/O completes. This Wikipedia article covers it in passing, focussing on non-blocking I/O:
(blocking I/O would) block the progress of a program while the communication is in progress, leaving system resources idle. When a program makes many I/O operations, this means that the processor can spend almost all of its time idle waiting for I/O operations to complete
Of course, non-blocking I/O doesn't help much if the next step is to do something with that data.
0
Blocking is the simpler, move obvious way:
bytes = socket.read(); // Thread "blocks" or waits if there's nothing to read
doSomethingWith(bytes);
You can't do a two-liner with non-blocking, since it involves setting up objects for each connection, which are then fed by a single "multiplexor" that constantly looks for the next thing that comes in. Imagine how the two approaches are different with a thousand or a million connections. You choose one or the other because of factors like:
For example, you might have a problem with blocking I/O holding several thousand connections open, one with each thread, because your system doesn't like having several thousand threads. But it turns out to be fairly complex issue. Read about how the perception has flip-flopped with Java, including the linked slide show (PDF).
0
In addition to indicating a form of synchronous signalling, in a programming context 'block' can also mean a section of code grouped together.
1What's the context? Programming, networks or something else? – ChrisF – 2010-10-16T18:58:43.553
Networks and file I/O – Alfred – 2010-10-16T19:01:19.143