How locking actually works [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got one question a very simple one.
while implementing a simple locking mechanism using global variable.I can't find out how to prevent the access of global variable from 2 processes/thread.
My algorithm (take 2 process)
Process p1 check if variable g is set then do not modify the code.
If not set then set it then modify code.
Same for process 2 .
while executing I got fair result but is it correct.
My doubt in some architecture if 2 instructions are not atomic then how to avoid accessing the global variable at a time.
please give me some basic idea.

Use atomic methods to manipulate the global variable.
Atomic operations are 'built-in' to gcc. While they are not generally 'portable', most compilers offer atomic operations.
For GCC, you might implement something like this:
if(__sync_bool_compare_and_swap(&g, g, g+1))
/* Changed by you. */;
else
/* Another process changed it before you could. Perhaps try again. */

Related

(#ifdef) vs. (#define <boolean>) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am trying to figure out which practice is the best between :
#define TEST
//#define TEST commented if not used or simply deleted
#define TEST 1
#define TEST 0 //if not used
For readability, I prefer to define a "Boolean" and check it in a if but I guess it is not efficient since it doesn't use #ifdef and it needs to be checked everytime in a if
While you can't use an #ifdef for your second case, you absolutely can use an #if so there would be no additional runtime cost for TEST 0. This btw. also means you can have multiple different values for TEST, maybe corresponding to different log levels or things like that
(Posted my comment as an answer, as suggested by Bathsheba)
Many moons ago some compilers (one for Solaris if I recall correctly) would not correctly compile code that used the first snippet.
But the C standard (now) requires them to and as such there is no benefit at all in using the second way which could now be regarded as idiosyncratic.
Of course if you need TEST to be a numerical value such as in a condition check as opposed to merely using #ifdef then you'd need to use the second way.

Is it a good practice to initialize a dynamically allocated structure with all 0 using memset(), if yes what are the benefits? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In a client-server system where its possible that server may populate only some of the IE and rest has to be treated as default value (0) at client side.
For such a system, is it a good idea to do memset(.., 0, ...) of dynamically allocating memory for the received message before copying the contents of message?
There is no need to initialise memory which immediately gets overwritten after initialisation.
Also unused memory does not need to be initialised. ("unused" here means, that it will never be read.)
All other memory needs to be initialised. Whether this would be done by using memset() and writing 0s to it depends on the specific context and use-case.

What is the advantage of using pointers in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why do we use pointers in C Programming?
In general pointers are able to access the address where the int/float/char etc... is stored.
Are there any other uses?
It depends on what you try to achieve:
you can change the value of a variable inside a function
you can pass a struct to a function without having to copy all its fields - think of a function that receives a struct.
you can point to a specific variable/struct and point to it from other structs
and many other advantages (advantages is purpose dependant and it depends on whats your program is doing).
Pointers are quite basic C and there is a lot of material online you should get yourself familiar with them and the advantages will pop up themselves.
The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters. They can also be used to optimize a program to run faster or use less memory that it would otherwise. A few tasks these days, such as programming microcontrollers, still need this.

How big should a structure be before you should start using a pointer to it, rather than making a copy? [C] [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is a slightly subjective answer, but how large do you think a structure should be before you start using pointers to it in other structures or function calls rather than the structure by-value?
Depends on the compiler and architecture.
C and C++ define the size of the types they use, and how functions are written, but they don't define how they are implemented.
This is means the standard itself doesn't define how the structurs are passed, just that they are in essence copied.
The compiler might decide to do something else entirely, like not copying the struct at all if there's a default copy constructor , and the variable isn't used.
But after saying all that, most common-sense compiler implementations would store the struct in a register if it fits. So depends on the architecture, make sure the structure fits in a register.

how to store data in non volatile memory [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a beginner in the embedded field. I have done a few engineering project using ARM and PIC micro controller. Now I'm facing a problem how to store some details(like name and rate) in the non volatile memory or external memory? Can any one help me to solve this? Fom where I should get examples of this storing? Now am using keil u vision 4 for programming but printf is not working in it? Earlier I'm using keil u vision 3 printf was working on that?
Various controller families have such things as EEPROM, or modifiable Flash. But they are accessed in a completely controller-dependent way.
To use them, you just issue a command (or rather set of commands) to store data X to address Y in this memory area. Later on, you get them back.
How this is done should be obtainable from the manual and/or application notes from the manufacturer of the chip.
Also be aware of the timing. AFAIR, a PIC needs 4 ms to store a single byte, so it might be helpful to set up (or use) a framework which automatically stores data byte for byte and advances upon receipt of an interrupt. So the work is done in the background.

Resources