Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network WSN's Operation System The software components in WSNs include sensor operating systems and middleware as shown in figure below. The purpose of these is to ease the application development by providing network access and allowing support for heterogeneous platforms with hardware abstraction. Fig : Software architecture of a WSN node An operating system (OS) in a WSN is a thin software layer that logically resides between the node’s hardware and the application and provides basic programming abstractions to application developers. Its main task is included: 1. Enable applications to interact with hardware resources. 2. Schedule tasks. 3. Arbitrate between application program and other services that try to seize resources. 4. Memory management, power management and File management. 5. Networking. 6. Enable users to develop, debug, and execute their own programs. Page 1 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network Modern sensor operating systems differ from conventional PC operating systems in a number of ways. These differences stem from the unique hardware and energy constraints typically encountered in sensor networking. The following points describe the general principles behind sensor operating systems. I. Small memory footprint: Due to the resource constraints, an operating system should be as small as possible to leave space for applications. II. Real-time operation: Sensor networks are inherently coupled with the real world, which sets timing constraints to the operation. In addition, network protocols are often time-sensitive. Both of these require real-time support to make right actions at the right time. III. Energy management: As sensor nodes may operate on batteries, an OS should operate efficiently to reduce its energy overhead by e.g. shutting down unused peripherals. IV. Memory management: The scarce data memory should be efficiently divided among applications. V. Hardware abstraction: In order to provide coherent interfaces for applications, an OS needs to abstract heterogeneous node platforms. This way, a developer can write applications using a single Application Programming Interface (API). VI. Concurrency: WSNs have high degree of concurrency. Sensing is not limited to one application but can involve multiple physical sensors. Also, a node must be able to forward and process (e.g. via aggregation) several concurrent data flows. Thus, basic services that allow concurrency are essential in an OS. 1. WSN's OS Implementation Approaches Sensor operating systems have two basic implementation approaches: I. Event-driven. II. Preemptive multithreading. Both approaches allow real-time responses but differ in how they are programmed. Page 2 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network In an event-driven OS, processes are often referred to as tasks. A task is activated when an event occurs and is run to completion, as shown in Figure 4.2. An event might indicate e.g. the reception of data from a transceiver, the expiration of a timer, or finished sensing. The main benefit of the approach is extremely low overhead. However, this approach is not suitable for long running computations as they block execution of other events. A programmer can work around this issue by manually slicing the computation to little pieces but this increases the complexity of the software. Preemptive multithreading allows a thread (or process) to run constantly, while an OS shares execution time between active threads. A thread may decide to wait some specific event in which case the execution halts until the event occurs. The approach can be combined with priority scheduling where each thread is assigned with a priority. This way, low priority background computation does not interfere with timing critical code assigned with high priority. As a drawback, the preemptive multithreading requires more memory and has higher computational overhead than event-driven OS as the context of each thread must be stored and switched when the execution of a thread changes. Also, sharing data structures and resources requires mutual exclusions to avoid data corruption. Page 3 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network 2. WSN's Operating System Functional Aspects 1. Data Types In wireless sensor networks (WSNs), communication between the different subsystems is vital. These subsystems communicate with each other for various reasons such as to exchange data, delegate functionalities, and signaling. Interactions take place through well formulated protocols and data types that are supported by the OS. Complex data structures have strong expression power but consume resources, while simple data types are resource efficient but have limited expression capability. Almost all of the existing operating systems in WSNs support the native data types of the C programming language and some of the complex data types such as struct [Structure] and enum [Enumerated]. 2. Scheduling Task scheduling is one of the basic functions of an OS. How efficiently tasks can be organized, prioritized, and executed determines the efficiency of the OS. Regardless of how tasks are executed, a scheduler can be either a nonpreemptive or preemptive scheduler. In strictly nonpreemptive scheduling, a task is executed to the end and will not be interrupted by another task. On the contrary, in strictly preemptive scheduling, the scheduler decides how time is shared between tasks and allows a task of higher priority to interrupt a task of lower priority Broadly speaking, there are two scheduling mechanisms: queuingbased and round robin scheduling. Page 4 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network A. In a queuing-based scheduling, tasks originating from the various subsystems are temporarily stored in a queue and executed serially according to a predefined rule. Some operating systems enable tasks to specify priority levels so that they can be given precedence. Queuing-based scheduling can be further classified into first-in-first-out (FIFO) and sorted queue. In a FIFO scheme, tasks are processed according to their arrival time: a task that arrives first will be executed first as soon as the processor is free. A nonpreemptive OS will execute the task to the end before another task is admitted for execution. In a preemptive OS, however, a task of higher priority may interrupt a task of low priority. In a sorted queue scheme, tasks in a queue are sorted according to some criteria. One way is to sort tasks according to their estimated execution duration. This approach prevents long-duration tasks from blocking short-duration tasks. The approach is also known as the shortest job first (SJF) rule. B. Round-robin scheduling is a time-sharing scheduling technique in which several tasks can be processed concurrently. The scheduler defines a time frame by dividing time into slots and tasks will be given slots in a multiplexed manner. This way, all the tasks advance toward their completion. 3. Stacks A stack is a data structure that is used to temporarily store data objects in memory by piling one upon another. The objects are accessed on a last-in first-out (LIFO) basis. The processor subsystem uses stacks to store system state information when it begins executing subroutines. This way it “remembers” where to return after the subroutine is completed. Subroutines can also call other subroutines by storing the state of the current subroutine on top of the previous state information Page 5 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network in the stack. When the subroutine is completed, the processor pulls the first address it finds on the top of the stack and jumps to that location. In a multithreaded OS, each thread requires its own stack to manage state information. This is one of the reasons why multithreaded operating systems are expensive in WSNs. Interrupt Process using stack 4. System Calls The OS provides a number of basic functions which enable the separation of concern, namely, the need to decouple the concern of accessing hardware resources and additional low-level services from the implementation details of the access mechanisms. Users invoke these operations whenever they wish to access a hardware resource such as a sensor, watchdog timer, or the radio without the need to concern themselves how the hardware is accessed. 5. Memory Allocation Page 6 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network Memory can be allocated to a program either statically or dynamically. A static memory allocation is a frugal way of using the memory, but it can only be used if the program’s memory requirement is known in advance. The memory allocation takes place when the program starts –as part of the execution operation – and is never freed. Since the program’s memory requirement is precisely known at the time of compilation, memory is used efficiently. On the other hand, static memory allocation does not allow runtime adaptation. Dynamic memory allocation is used when the size and duration of the required memory are not known at the time of the program’s compilation. This includes programs that use dynamic data structures whose memory requirement cannot be determined when the program starts. Such programs often use memory on a transient basis. They allocate some memory, use it for a while, but then reach a point where they no longer need that particular piece. Because memory is not inexhaustible, memory that is no longer used can be released or assigned to a different owner. Dynamic memory allocation enables flexibility in programming but produces a considerable management overhead. As a strategy to increase the memory capacity of a node, most architectures use EEPROM or flash memory to store program code. Consequently, it is possible to deploy relatively complex applications and communication protocols. However, reading and writing to flash memory is costly with respect to energy consumption. 3. WSN's Operating System NonFunctional Aspects 1. System Overhead An operating system executes program code and, therefore, requires its own share of resources. How much resource it consumes depends on its size and the type of services it provides to the higher-level services and applications. The resources consumed by the operating system are the system’s overhead. Page 7 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network Presently available wireless sensor nodes have resources that are measured in terms of kilobytes and a few megahertz. These resources have to be shared by programs carrying out sensing, data aggregation, self-organization, network management, and communication. The operating system’s overhead should be understood in view of these tasks. 2. Dynamic Programming Once a WSN is deployed, it may be necessary to reprogram some part of the application or the operating system for the following reasons: A. complete knowledge of the deployment setting may not be known at the time of deployment and, as a result, the network may not perform optimally; B. Both the application requirements and the properties of the physical environment in which the networks operate can change over time; and C. it may be necessary to detect and fix bugs while the network is still operating. It is necessary to adopt operating system in WSN that provides dynamic reprogramming support. Apparently, if there is no clear Dynamic programming can be supported in principle, but its practical implementation depends on several factors. First, the operating system should be able to receive the Software update piece by piece and assemble and store it temporarily in the active memory. Second, the OS should make sure that this is indeed an updated version. Page 8 Date: Friday, March 25, 2016 Wireless Sensor Network University of Babylon Mehdi Ebady Manaa College of IT 2rd class – Department of Network Third, it should be able to remove the piece of software that should be updated and install and configure the new version. All these consume resources and may cause their own bugs. Page 9 Date: Friday, March 25, 2016