Processes and Threads (Different System Resource Management Methods)
Difference • Process: Basic unit of resource allocation, composed of one or more threads. • Thread: Basic unit scheduled by the dispatcher, representing a task. • Each process has its independent memory space, and a process can have multiple threads. Process switching has high overhead. • Multiple threads share memory, and thread switching has low overhead. • A process crash doesn’t affect other processes. • A thread crash affects the entire process it belongs to.
Pros and Cons of Multiprocessing and Multithreading • Memory usage, data sharing, synchronization, CPU utilization, creation/destruction, and switching speed, reliability, programming and debugging comparisons.
Choosing Between Processes and Threads • Choose threads for frequent creation/destruction. • Choose threads for tasks requiring significant computation and frequent switching. • Choose processes for safety and stability. • Choose threads for speed.
Interprocess and Interthread Communication (Synchronization) Methods • Interprocess: Named pipes, unnamed pipes (parent-child processes), message queues, semaphores, shared memory, sockets.
Process State Transitions • Ready, Blocked, Running.
Parent Process and Child Process • After calling fork() in the parent process, a child process is created with identical code, data, and user stack. • Typically, the parent process waits for the child process to finish.
Context Switching • Process context switching involves saving the values of all CPU registers, stack content, and execution position for later restoration. • Interrupt context involves saving the current process’s execution state when an interrupt occurs.
C/C++
new and malloc • malloc and free are C/C++ library functions, requiring the stdlib.h header. • new and delete are C++ keywords, no header needed, compiler support required. • new allocates memory without specifying size; malloc requires size. • Results: new returns a typed pointer; malloc returns a void pointer.
Memory Allocation for 1GB (malloc 1.2GB) • Possible; malloc allocates virtual address space, not physical memory.
extern “C” Purpose • Allows C++ code to call C code, ensuring the compiler compiles according to C language conventions.
Functions Leading to Memory Overflow and Improvement • strcat and strcpy can lead to overflow. • Improvement: Use strncat, strncpy, or memcpy with specified limits.
Static Usage • static for local variables keeps them in the static area; they persist beyond function calls. • static for global variables limits their scope to the current file. • static for functions restricts their visibility to the current file.
const Usage • const makes data read-only.
Volatile Purpose and Usage • Prevents compiler optimization, forces reading from memory instead of cached values. • Usage: Hardware registers, shared variables in multithreading, variables accessed by interrupt handlers.
const Constants vs. #define Macros • #define max 1 replaces during preprocessing, no memory allocation, no type safety. • const int max = 1; determines value during compilation, allocates memory, type-safe.
Variable Scope and Lifetime • Global variables: File scope, available externally, lasts until program termination. • Local variables: Function scope, created on entry, destroyed on exit.
sizeof and strlen • sizeof() is an operator, returns the size in bytes during compile time. • strlen() is a function, returns the length of a string during runtime, requires <string.h> header.
intmain(){
char*p ="hello";// p is a pointer to a string literal "hello"char arr1[]="hello";// arr1 is an array of characters initialized with the string "hello"char arr2[]={
'h','e','l','l','o'};// arr2 is an array of characters initialized individuallyprintf("%d\n",sizeof(p));// Result is 4 on 32-bit systems and 8 on 64-bit systems// The size of a pointer variable depends on the system's architectureprintf("%d\n",sizeof(arr1));// Result is 6 because it includes the null terminator '\0'printf("%d\n",sizeof(arr2));// Result is 5 because it's a character array, not a string, and doesn't include '\0'printf("%d\n",strlen(p));// Result is 5 because strlen calculates the length of the string, excluding the null terminatorprintf("%d\n",strlen(arr1));// Result is 5 because strlen calculates the length of the string, excluding the null terminatorp