Skip to content

Commit 0e4c4d9

Browse files
authored
Merge pull request #3753 from sivaprasath2004/sivaprasath-closes-issue-3715
[Feature]: Add C Language Course
2 parents b1a657d + 550af1e commit 0e4c4d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+4706
-1
lines changed

courses/C/_category_.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "C Language",
3+
"position": 11,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn C Language."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
---
2+
id: lesson-1
3+
title: "Advanced File Handling in C"
4+
sidebar_label: Advanced File Handling
5+
sidebar_position: 1
6+
description: "Learn Advanced File Handling in C"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
11+
Advanced file handling techniques are essential for optimizing performance, ensuring data integrity, and working efficiently with large files. Here’s a detailed overview of advanced file handling topics in C:
12+
13+
#### 1. File Locking Mechanisms
14+
15+
File locking is used to control access to files when multiple processes or threads are involved. It helps in preventing data corruption or inconsistency.
16+
17+
- **Types of Locks**:
18+
- **Exclusive Lock**: Prevents other processes from reading or writing the file.
19+
- **Shared Lock**: Allows other processes to read the file but not write to it.
20+
21+
- **Using `flock()`**:
22+
- **Description**: A POSIX function for file locking.
23+
- **Example**:
24+
```c
25+
#include <stdio.h>
26+
#include <stdlib.h>
27+
#include <unistd.h>
28+
#include <fcntl.h>
29+
#include <sys/file.h>
30+
31+
int main() {
32+
int fd = open("file.txt", O_RDWR | O_CREAT, 0666);
33+
if (fd < 0) {
34+
perror("open");
35+
exit(EXIT_FAILURE);
36+
}
37+
38+
// Acquire an exclusive lock
39+
if (flock(fd, LOCK_EX) < 0) {
40+
perror("flock");
41+
close(fd);
42+
exit(EXIT_FAILURE);
43+
}
44+
45+
printf("File locked. Press Enter to release lock...\n");
46+
getchar(); // Wait for user input
47+
48+
// Release the lock
49+
if (flock(fd, LOCK_UN) < 0) {
50+
perror("flock");
51+
close(fd);
52+
exit(EXIT_FAILURE);
53+
}
54+
55+
close(fd);
56+
printf("File unlocked.\n");
57+
return 0;
58+
}
59+
```
60+
61+
- **Using `fcntl()`**:
62+
- **Description**: Provides more control over file locking.
63+
- **Example**:
64+
```c
65+
#include <stdio.h>
66+
#include <stdlib.h>
67+
#include <fcntl.h>
68+
#include <unistd.h>
69+
70+
int main() {
71+
int fd = open("file.txt", O_RDWR | O_CREAT, 0666);
72+
if (fd < 0) {
73+
perror("open");
74+
exit(EXIT_FAILURE);
75+
}
76+
77+
struct flock lock;
78+
lock.l_type = F_WRLCK; // Write lock
79+
lock.l_whence = SEEK_SET;
80+
lock.l_start = 0;
81+
lock.l_len = 0; // Lock the whole file
82+
83+
if (fcntl(fd, F_SETLKW, &lock) < 0) {
84+
perror("fcntl");
85+
close(fd);
86+
exit(EXIT_FAILURE);
87+
}
88+
89+
printf("File locked. Press Enter to release lock...\n");
90+
getchar(); // Wait for user input
91+
92+
lock.l_type = F_UNLCK; // Unlock
93+
if (fcntl(fd, F_SETLK, &lock) < 0) {
94+
perror("fcntl");
95+
close(fd);
96+
exit(EXIT_FAILURE);
97+
}
98+
99+
close(fd);
100+
printf("File unlocked.\n");
101+
return 0;
102+
}
103+
```
104+
105+
#### 2. File I/O Performance Optimization
106+
107+
To optimize file I/O performance, you can use techniques such as buffering, efficient file access, and reducing system calls.
108+
109+
- **Buffered I/O**:
110+
- **Description**: Reduces the number of I/O operations by using a buffer.
111+
- **Example**:
112+
```c
113+
#include <stdio.h>
114+
115+
int main() {
116+
FILE *file = fopen("largefile.txt", "r");
117+
if (!file) {
118+
perror("fopen");
119+
return 1;
120+
}
121+
122+
char buffer[1024];
123+
size_t bytesRead;
124+
125+
while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0) {
126+
// Process data in buffer
127+
}
128+
129+
fclose(file);
130+
return 0;
131+
}
132+
```
133+
134+
- **Efficient File Access**:
135+
- **Description**: Minimize the number of file open/close operations and use appropriate file modes.
136+
137+
- **Reducing System Calls**:
138+
- **Description**: Combine operations where possible to reduce the number of system calls.
139+
140+
#### 3. Handling Large Files
141+
142+
When working with large files, consider techniques for efficient reading, writing, and memory management.
143+
144+
- **Reading Large Files**:
145+
- **Description**: Use buffered reading to handle large files in chunks.
146+
- **Example**:
147+
```c
148+
#include <stdio.h>
149+
150+
int main() {
151+
FILE *file = fopen("largefile.txt", "r");
152+
if (!file) {
153+
perror("fopen");
154+
return 1;
155+
}
156+
157+
char buffer[1024];
158+
size_t bytesRead;
159+
160+
while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0) {
161+
// Process data
162+
}
163+
164+
fclose(file);
165+
return 0;
166+
}
167+
```
168+
169+
- **Writing Large Files**:
170+
- **Description**: Write data in chunks to avoid memory overflow.
171+
- **Example**:
172+
```c
173+
#include <stdio.h>
174+
175+
int main() {
176+
FILE *file = fopen("largefile.txt", "w");
177+
if (!file) {
178+
perror("fopen");
179+
return 1;
180+
}
181+
182+
char buffer[1024];
183+
for (int i = 0; i < sizeof(buffer); i++) {
184+
buffer[i] = 'A'; // Example data
185+
}
186+
187+
fwrite(buffer, 1, sizeof(buffer), file);
188+
fclose(file);
189+
return 0;
190+
}
191+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Advanced File Handling",
3+
"position": 3,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn Advanced File Handling in C Language."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
id: lesson-2
3+
title: "Working with Memory-Mapped Files"
4+
sidebar_label: Working with Memory-Mapped Files
5+
sidebar_position: 2
6+
description: "Learn Working with Memory-Mapped Files in C"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
Memory-mapped files allow you to map a file into memory and access it as if it were an array. This can be more efficient than traditional file I/O.
11+
12+
- **Using `mmap()`**:
13+
- **Description**: Maps a file or a portion of a file into memory.
14+
- **Example**:
15+
```c
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <sys/mman.h>
19+
#include <fcntl.h>
20+
#include <unistd.h>
21+
#include <string.h>
22+
23+
int main() {
24+
int fd = open("file.txt", O_RDWR | O_CREAT, 0666);
25+
if (fd < 0) {
26+
perror("open");
27+
exit(EXIT_FAILURE);
28+
}
29+
30+
// Resize file to 100 bytes
31+
ftruncate(fd, 100);
32+
33+
// Map file to memory
34+
char *mapped = mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
35+
if (mapped == MAP_FAILED) {
36+
perror("mmap");
37+
close(fd);
38+
exit(EXIT_FAILURE);
39+
}
40+
41+
// Write data to the mapped memory
42+
strcpy(mapped, "Hello, memory-mapped file!");
43+
44+
// Unmap and close
45+
munmap(mapped, 100);
46+
close(fd);
47+
return 0;
48+
}
49+
```
50+
51+
- **Advantages**:
52+
- **Efficiency**: Direct access to file data without additional I/O calls.
53+
- **Simplicity**: Treats file data like an array.
54+
55+
- **Considerations**:
56+
- **File Size**: Be cautious with very large files as mapping the entire file may not be feasible.
57+
- **Concurrency**: Ensure proper synchronization if multiple processes or threads are accessing the memory-mapped file.
58+
59+
### Summary
60+
61+
- **File Locking Mechanisms**: Use `flock()` or `fcntl()` to manage file access in concurrent environments.
62+
- **File I/O Performance Optimization**: Use buffering and efficient access patterns to enhance performance.
63+
- **Handling Large Files**: Read/write in chunks and manage memory efficiently.
64+
- **Memory-Mapped Files**: Map files into memory for efficient access and manipulation.

0 commit comments

Comments
 (0)