-structures are user defined datatypes that can store multiple types of fixed data.
-eg: struct book
{
char name ;
float price ;
int pages ;
};
struct booka, bookb, bookc.
-here we are storing char, float and int in one combined struct called 'book'.
-then we instantiated the structure into three variables booka, bookb, bookc.
-struct type declaration does not tell the compiler to reserve any space in memory.
-all it does is define the 'form'.
-also structure type declaration is usually defined at the top of a source code file
before any other variable or function.
-when structure elements are stored, they are stored contiguosly in memory.
-structures can also be made into arrays.
-in the above example, we simply say:
struct book b[10] to store 10 elements in one go (b0 to b9).
389 -structures allow copying of elements in one go.
-eg: b2=b1 -copies name, price, pages of b1 into that of b2.
390 -structures can be nested.
-eg: struct book
{
char name;
float price;
}
struct author
{
char authorname;
struct book b; <<--structure book b in author.
}
-to get values: author a.b.name or author a.b.price;
-structures can also have pointers.
-eg: struct book *ptr;
ptr=&b1; initialized with addr of b1.
printf("\n %s %d, ptr->name, ptr->price);
397 -some uses of structures:
. in databases
. in changing size of cursor
. in sending output to a printer
. in finding memory size of a comp
. in drawing graphics on a screent and so on..
Nov 17
=====
409 -Input and output is not part of C language. By C here we mean the C compiler.
-instead they are part of library functions that are add-ons & called by C compiler.
-IO facilities for different OS are different.
-thus printf on Linux is different from printf on solaris/windows.
-IO functions can be of two types:
. console IO -io bet stdin(keybd), stdout(monitor, stderr(console)
. file IO -io to and from files.
409 -console = keyboard + monitor together (directly connected to a host)
-console io can be subdivided into:
. formatted io - user in control of format of output/input
. unformatted io - user not in control
-eg: formatted console io - printf(), scanf()
410 -eg: unformatt console io - getch(),getche(),getchar(),gets(), putch(), putchar(), puts()
-note: there is no unformatted console io functions for ints and floats (only char/strings)
419 -printf vs sprintf - printf prints on screen, sprintf stores to string
-eg: printf("\n %d", i) vs sprintf(astr, "%d" i), printf("\n%s",astr)
-scanf vs sscanf - read from stdin vs read from string
-eg: scanf("%s") vs sscanf("%s",a) where a="abcd"
419 -note: in formatted console io, 'enter' key has to be hit before the io takes place. If io is desired the moment something is entered/read, instead
of waiting for 'enter key', we use unformatted io functions.
-unformatted io functions examples:
.getch() - reads input without echoing on screen
.getche() - reads input and echoes on screen
.getchar()- reads input and echoes after you hit enter key
.fgetchar() like getchar. getchar is a macro, fgetchar is a func.
-more:
.putch() - output on screen //dos only?
.putchar()- output on screen //works on linux
-more:
.gets() - receives a string that has spaces (scanf cant do that)
.puts() - outputs a string to the screen.
430 -all data stored on disk is in binary form.
-the manner in which this binary data is stored on disk varies from OS to OS.
-but this doesnt matter to C programs as they simply have to call the
IO functions for that OS via the compiler designed for that OS.
432 -in io, strings are quoted in double quotes " " whereas chars in single ' '.
-once a file is opened using fopen, and directed to a pointer, we no longer need to address it by name, pointer is enough.
-eg: fp=fopen("afile.txt");
then fgetc(fp), fclose(fp) etc are used instead of fgetc("afile.txt") etc.
482 -commandline params can be passed to main as two params known as:
.argv - an array of pointers to strings
.argc - an int = the # of strings argv points
-eg: in this command:
filecopy filea fileb
-the program interprets it internally as:
main(int argc, char *argv[])
-argc=3
-argv[0]=base addr of the string "filecopy"
-argv[1]=base addr of the string "filea"
-argv[2]=base addr of the string "fileb"
520 -enum data type allows user defined data types to be created.
-eg: enum mar_status
{
single, married, divorced,widowed
};
enum mar_status person1, person2
-internally the compiler treats enumerators as integers.
-eg: in the above, single is stored as 0, married as 1 and so on.
-once declared, enums can be included in structs, like this:
-eg: struct person
{
char name[30];
int age;
enum mar_status status;
};
struct person pers;
strcpy( pers.name,"Superman");
pers.age=30;
pers.status=single;
-enum scope can be global or local. global if declared outside all functions,
local if declared inside a function.
-typedef: a shorthand for a variable type, usually in CAPS.
-eg: typdef unsigned long int UNLONGIN; //UNLONGIN=unsigned long int
-then UNLONGIN var1, var2;
-typedef is useful when dealing with structs or enums.
-eg: in the enum mar_status above, we could say this to define person1,2
typedef enum mar_status MSTAT;
-then MSTAT p1, p2;
-in short, typedef improves program readability.
-typecasting: defining the type of variable return;
-defined in brackets (,) before an operation.
-eg: int i=4, j=6;
div = (float) 4/6; //(float) is typecast, else int would be retrn
-pointers can even point to functions because functions have addresses in C.
-to do so, all we have to do is to mention the name of the function
-eg: i=display //i gets the value of function display.
-unions: unions are derived data types like structures.
-ie both unions and structs group a number of different variables together.
-the difference is in structure, the constituent variables are not necessarily
contiguous in memory.
-in unions the constituent variables are contiguous.
-eg: union a
{
int i;
char ch[2];
};
union a key;
-if we had done this using struct, it would take 6 bytes in mem.
-but with unions it takes only 4 bec int i and ch[] are made to occupy same
spot in memory.
-unions are useful in dealing with hardware.
620 -interaction with hardware means, interaction with the chip for that h/w.
621 -For different events there are different interrupts. As a reaction to the occurrence of an interrupt a table called Interrupt Vector Table (IVT) is looked up. IVT is present in memory. It is populated with addresses of different kernel routines during booting. Depending upon which interrupt has occurred the Microprocessor picks the address of the appropriate routine from IVT and transfers execution control to it. Once the control reaches the routine, the code in the routine interacts with the hardware. These routines are called
interrupt service routines (ISRs) and are part of the device drivers.
627 -as to why application programs cannot directly talk to hardware:
in a multiuser, multiprogramming OS, every device is shared amongst multiple applications running in memory. To avoid conflict between
different programs accessing the same device simultaneously the OS
does not permit an application program to directly access any of the devices. Instead it provides several API functions or system calls to carry out the interaction.
-Note: that the system calls dont change when a new device is added to the system.
-Linux Programing Discussion.
-C program -> system call -> device driver -> hardware.
652 -The programming architecture (better known as programming model) for each OS is different. Hence naturally the program that achieves the same task under different OS would have to be different.
-Gnome and KDE are just GUI shells.
-In multitasking os like linux, the microprocessor divides its execution time
equally among running processes in a round robin manner. Thus if we take a snapshot of the memory only one process would be found executing on the microprocessor.
-The scheduling of process on the cpu is done by scheduler.
-The scheduler stores the info a process (like cpu registers, contents of stack etc) into scheduler table and switches to next program.
This switching is called context switch.
-Preemptive scheduling: context switch happens as soon as the time slot allocated is over, whether the process is complete or not.
-1st linux program:
#include <stdio.h>
int main( )
{
printf ( "\n Parent Process ID = %d", getppid( ) ) ;
printf ( "\n Process ID = %d\n", getpid( ) ) ;
}
-In linux a process can spawn another process by calling fork() system call.
-system calls are also known as library functions.
-the parent and child process share the code.
661 -if some code needs to be run in the child, it is run using the exec() func.
662 -The ps command gets its info from the "process-table".
-Apart from other information the process table contains an entry of ‘exit code’ of the process. This integer value indicates the reason why the process was terminated. Even though the process comes to an end its entry would remain in the process table until such time that the parent of the terminated process queries the exit code. This act of
querying deletes the entry of the terminated process from the table
and returns the exit code to the parent that raised the query.
-If the child exits first (which is normal), its exit code would persist in the process table until the parent queries it. This is zombie process.
-If the parent exits first(not normal), the exit code is not queried and
persists. The child is called orphan and is adopted by init.
init queries the exit code and the child entry is removed from table.
-Knowing the exit code is important because that determines if the processes
terminated successfully or not.
670 -while the communication between programs and OS happens via system calls (aka
library functions like fork(), exec() ) etc, the reverse ie OS to
programs happens via signals.
-eg: when we hit ctrl+c, the kernel sends a terminate signal to the program.
684 -Signal Processing is the heart of programming under Linux.
Next we will look at Linux Programming...
No comments:
Post a Comment