Skip to main content

Posts

Showing posts from May, 2017

Files in C

Files Devices represented as files If file can be physical device, not fixed in size or behavior Streams are associated with files May support file position indicator [0,length] Binary or not Can be closed, opened, flushed Can be  buffered, unbuffered, line buffered Unbuffered - i/o immediate Fully buffered - i/o accumulated into block, then passed Line buffered - block size depends on \n Each file opened has a file descriptor (fd) fd describes state of file Opened, closed, position etc FILE --- a struct defined in stdio.h Functions -fopen (const char* path, const char* mode); -fscanf(stdin,....) == scanf -fprintf(stdout,....) == printf -fread -fwrite -fclose -fgets -feof(stream), does not happen until beyond stream When program begins, special files -stdin, stdout, stderr are opened If file position supported, position == 0 Print, scan adjusts position in stream Get position == ftell, change position == fseek Setvbuf

C pointers,structs,unions

Pointers,Structs,Unions char* p -address to single char value / first in array char* argv[] -array of type char* with unknown char** argv - address to the first element of an array of type char* int** data 1.  Pointer to pointer to single int value 2.  Array of addresses that point to single int 3.  Address that points to one array of int values 4.  Array of addresses that point to arrays of int values Sizeof(void*) returns size of address Include limits.h for supporting max and min values with TYPE_MAX/MIN Const avoids value being modified, would be illegal if attempting to change so Enums - maps to int, associates name with value EXAMPLE: enum month_name{ Jan,Feb,Mar.......,month_undef }; Structs Definition: Struct date{       //date is the tag enum day_name day; Int   day_num; Enum month_name month; Int                year; } Big_Day { //declaration Mon,7,Jan,1980  //initialization }; Function arguments/re

C Introductory threading notes

Threads Compiling threads- <pthread.h> clang -pthread x.c Thread functions 1. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,  void *(*start_routine) (void*), void* arg); EXAMPLE : pthread_create (&threads[i], NULL, thread_function, NULL); Starts a new thread while calling, starts execution by invoking “ start_routine() ” Arg is passed as sole argument of start_routine Terminates via 1.  pthread_exit 2.  Returns from start_routine, equivalent to calling pthread_exit 3.  pthread_cancel 4.  Threads call exit(), or main returned, this calls termination of all threads in process If attr  is NULL, thread is created with default attributes Else, it points to a structure whose contents are used at thread creation time to determine attributes to new thread Successful call stores ID of new thread in buffer pointed to by thread,  used to refer to thread in subsequent calls to other pthreads functions Successful call retur