I am currently writing a C program on an Industrial PC.
The operating system is Ubuntu. The PC is using flash file system.
I did some researches on flash file system. It turns out that flash file system has limited times of rewriting data on the same spot.
However, my program may need to fwrite files over and over to keep the config data.
My program needs to keep running on this PC over years, and it needs to record data every minutes.
I am wondering if I fwrite a file many times, is the system gonna rewrite data on the same memory spot? Is that going to cause the flash file system to be broken?
First thing to see is whether you are using a NOR flash file system or NAND flash file system. Because both of them differs in the number of erase cycles that can be done reliably .
Please see the link for differences between the two https://focus.ti.com/pdfs/omap/diskonchipvsnor.pdf
The problem also depends upon the amount of data you want to write. If its small you can write a custom filewrite function using putc instead of using the fwrite.
You can see one method here
https://bytes.com/topic/c/answers/876395-fwrite-efficiency-alternative
Hope it helps.
Related
I searched for info about this but didn't find anything.
The idea is:
If I code a program in C, or any other languages, what else do I need to do for it to get recognized in BIOS and started by it as a DOS program or just a prompt program?
I got this idea after I booted an flash drive with windows using the ISO and Rufus, which put some code in the flash drive for the BIOS to recognize it and run, so I would like to do the same with a program of mine, for example.
Thanks in advance!
An interesting, but rather challenging exercise!
The BIOS will fetch a specific zone from the boot device, called a master boot record. In a "normal" situation with an OS and one or more partitions, the MBR will need to figure out where to find the OS, load that into memory, and pass control to it. At that time the regular boot sequence starts and somewhat later the OS will be running and be able to interact with you. More detail on the initial activities can be found here
Now, for educational purposes, this is not strictly necessary. You could write an MBR that just reads in a fixed part of the disk (the BIOS has functions that will allow you to read raw sectors off a disk, a disk can be considered as just a bunch of sectors each containing 512 bytes of information) and starts that code. You can find an open source MBR here and basically in any open source OS.
That was the "easy" part, because now you probably want to do something interesting. Unless you want to interact with each part of the hardware yourself, you will have to rely on the services provided by the BIOS to interact with keyboard, screen and disk. The traditionally best source about BIOS services is Ralf Brown's interrupt list.
One specific consideration: your C compiler comes with a standard library, and that library will need a specific OS for many of its operations (eg, to perform output to the screen, it will ask the operating system to perform that output, and the OS will typically use the BIOS or some direct access to the hardware to perform that task). So, in going the route explained above, you will also need to figure out a way to replace these services by some that use the BIOS and nothing more - ie, more or less rewrite the standard library.
In short, to arrive at something usable, you will be writing the essential parts of an operating system...
Actually BIOS is going to be dead in the next two years (INTEL will not support any BIOSes after this date) so you may want to learn UEFI standard. UEFI from v2.4 allows to write and add custom UEFI applications. (BTW the "traditional" BIOS settings on the UEFI computers is often implemented as a custom UEFI App).
I have to read/write a single file multiple times in a day. say may be every second, I have to update the file.
how does this effect the memory(I am using Emmc flash).
since emmc having the defined write cycle over which it will corrupt, please suggest me a best way to handle this.
how about using the mmap and msync, is there any possibility to avoid writes..?
if I am using a mmap and writing frequently is it will also write in flash every time I write into the shared mapped memory.?
It could depend upon the computer, the file system, the mount options, the kernel (and version).
Perhaps (and actually probably) your file is sitting in the page cache and write(2)-ing it often does not update the flash memory every time.
You could use something else that a plain file (e.g. a database, or use sqlite) or write your own data daemon to avoid that. If you need that to stay in a file, perhaps consider writing your own FUSE
We cannot help more without actual code and much more details in your question.
I am new to embedded systems and have been trying to port over a MP3 conversion program to an ARM-based STM32L476G-DISCO development board. I'm also using the free System Workbench software based on Eclipse. I've been successful to the point that I have compiled the program and flashed it onto the board. It even runs right up to the point that the program asks for file input (.wav).
My question is how do I implement the file handling part? Previously when running the original windows console app I would just send in a command line argument like "'>C:\file.wav < C:\file.mp3".
The board comes with 128Mbit of flash memory utilizing QSPI for communications as well as internal flash. Do I need a file system to read/write a file into my program? I was thinking to start simple and just embed the file but I don't know how to call it in my code. I can program the memory manually via the programming software but again, all I know is the address of where I flashed the data.
If you have written the data to the internal flash, all you need is its address - it is no longer a "file" it can be treated as if you have read the data from the file into that location. Rather then separately programming the data from your application you could write a code generation tool that reads teh fike on teh development host and converts it to a C code data array thus:
static const uint8_t wav_file[] = { '\x00, `\x55` ...
...
... } ;
that you then compile and link to your application code.
This would then allow the linker to locate your data and avoid any issues of the application code and the data encroaching on each other. Moreover it gives toy data a symbolic start address and a size that can be determined by sizeof(wav_file).
You cannot however use this method if the data is stored in the external flash since that is not memory mapped. In that case the data will need to be read into RAM for processing.
If your audio library expects a file and you do not wish to modify it, then yes you need a file system. A file system is probably the simplest and safest means of managing the external flash in any case.
I wrote a program to find speed of read and write to a flash drive in c. I have a big text file that gets written to a flash drive. It outputs the time it took to write the file, and then reads the newly written file, and outputs the time it took to read it.
I know that a computer I run the program on will be running other things in the background while I run the c program, which will make the times inaccurate.
In order to make times more accurate, I want to make it so the computer will devote all resources to my c program while it runs. Is there such a way to make a c program run in real time this way?
I will test this program on linux, mac, and windows.
What you are asking is impossible. Think about it for a while: If the computer were to do nothing else than execute your program: who would do the memory management while your program is running? You would have to be your own operating system!
There is another flaw in your test: The file is most-likely not read from the flash drive anyway. It would come from the disk cache i.e. RAM on any modern operating system. To forego this, you would have to clear that cache, for example by ejecting and re-inserting the drive.
No. Neither (normal, consumer-used) Linux, nor MacOS, nor Windows supports real-time processing. (Note that existing benchmarking suites ask you nicely not to run other programs while they're doing their thing.)
With that said, all of these OSes prioritize running foreground processes, so the results you get aren't likely to be too far off unless you've got a lot of other stuff running at the same time. You should use multiple trials to get the most precise results.
The absolute best you can do in one of those OSes is to run your benchmark as a kernel process. That's a very difficult thing to do, however, and makes it absolutely impossible to get any sort of cross-platform compatibility.
Alternatively, you can run your benchmark on an actual real-time OS.
In order to accomplish your goal, you will most likely have to interface with the hardware directly, without the assistance of the operating system. It can be done if you are willing to write a kernel module that also acts as a driver for the flash drive. As a kernel module, there are ways to force your code to run to completion before yielding the processor.
This question already has answers here:
How to run a program without an operating system?
(4 answers)
Closed 5 years ago.
I just wonder, can we execute a program on a machine without an operating system?
Besides, I heard that the Linux kernel is written in C language and a kernel is run during booting, so I just wonder how a computer understand the language without first compiling?
From Wikipedia:
When a computer is first powered on, it does not have an operating system in ROM or RAM. The computer must initially execute a small program stored in ROM along with the bare minimum of data needed to access the nonvolatile devices from which the operating system programs and data are loaded into RAM.
The small program that starts this sequence of loading into RAM, is known as a bootstrap loader, bootstrap or boot loader. This small boot loader program's only job is to load other data and programs which are then executed from RAM.
The computer can understand the Linux kernel because it has already been compiled and stored (usually) on disk. The bootloader gives the computer enough functionality to load the precompiled kernel into memory.
You wouldn't need to load a whole operating system to run a program on a computer, you could write a bootloader to kick off a program you had compiled. You would not have access to any of the operating system calls that make life easier for programmers.
In short, yes.
Linux is still a software program, in machine code, that runs on a bare metal machine. While you can execute a software program without an operating system, your program will need to implement ALL the code that is used to talk to various pieces of hardware in a computer to various degrees - e.g. outputting data to a display, interpreting input from a keyboard / mouse / network card etc. (Some of the very low level stuff are implemented by the firmware in computer components, but the rest your program will have to implement). This makes it very time-consuming and difficult for you to write something that runs entirely without an operating system.
Yes, and it is done today for small microcontrollers with a few KB of memory.
The program is typically written in C and compiled on some other computer (that is called cross-compiling) and then loaded as binary data into the flash memory of the controller.
the linux kernel might be written in C. It is still compiled to machinecode. And it is this machine code which is executed during boot
You can also write software which is run during a boot. This way you can make your own custom OS, or make your own custom software which can run without an OS directly. Beware though, that an OS gives you a lot of functionality which you'll have to make yourself. Things like driver support, disk IO routines, networkstacks, multitasking and memory management you'll have to do yourself.
Finally: I don't think people don't like it that much if they have to reboot their machine in able to run your software. So I'd go with writing for an OS... it makes live easier on you and the user.
What is an operating system if not software running on a "bare" machine? Voodoo? XD
1st: Sure. You don't really need an operating system just to burn some cycles.
You might need some kind of OS support if you want to load or store files or data,
manage input or output, but this can also be done calling BIOS functions directly:
read key from keyboard, write to some screen or LED or serial interface.
Only when you want to run multiple programs, or deal with interrupts from outside,
conflicting ressources or such, then you will desperately need an OS.
2nd: The kernel is compiled to machine code, which is executed during boot. There's no
C involved when running a kernel. C only helps writing a kernel or any program
which should run, if in the kernel or "bare metal".
Just look at any games console prior to the 32 bit ones. Almost all of them lacked any boot code at all, and simply booted directly from the inserted cartridge.
theoretically, you can build bootloader by using hex editor on another machine.